git repos / blockattack-game

blame: source/code/main.cpp

normal view · raw

89c4a3a6 sago007 2008-08-29 14:32 1
/*
c53e6443 sago007 2012-04-17 11:04 2
===========================================================================
c53e6443 sago007 2012-04-17 11:04 3
blockattack - Block Attack - Rise of the Blocks
bb13d559 Poul Sander 2016-04-13 15:42 4
Copyright (C) 2005-2016 Poul Sander
c53e6443 sago007 2012-04-17 11:04 5
c53e6443 sago007 2012-04-17 11:04 6
This program is free software: you can redistribute it and/or modify
c53e6443 sago007 2012-04-17 11:04 7
it under the terms of the GNU General Public License as published by
c53e6443 sago007 2012-04-17 11:04 8
the Free Software Foundation, either version 2 of the License, or
c53e6443 sago007 2012-04-17 11:04 9
(at your option) any later version.
c53e6443 sago007 2012-04-17 11:04 10
c53e6443 sago007 2012-04-17 11:04 11
This program is distributed in the hope that it will be useful,
c53e6443 sago007 2012-04-17 11:04 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
c53e6443 sago007 2012-04-17 11:04 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c53e6443 sago007 2012-04-17 11:04 14
GNU General Public License for more details.
c53e6443 sago007 2012-04-17 11:04 15
c53e6443 sago007 2012-04-17 11:04 16
You should have received a copy of the GNU General Public License
c53e6443 sago007 2012-04-17 11:04 17
along with this program.  If not, see http://www.gnu.org/licenses/
c53e6443 sago007 2012-04-17 11:04 18
c53e6443 sago007 2012-04-17 11:04 19
Source information and contacts persons can be found at
1029dfed Poul Sander 2019-03-13 17:22 20
https://blockattack.net
c53e6443 sago007 2012-04-17 11:04 21
===========================================================================
89c4a3a6 sago007 2008-08-29 14:32 22
*/
89c4a3a6 sago007 2008-08-29 14:32 23
215ca7b3 sago007 2009-03-06 16:37 24
#include "common.h"
26ddb424 sago007 2011-06-09 20:06 25
#include "global.hpp"
9b99cf7a sago007 2015-12-04 20:22 26
#include "scopeHelpers.hpp"
acc78225 sago007 2015-12-29 18:10 27
#include "icon.hpp"
9dc80982 sago007 2016-01-02 21:08 28
#include "os.hpp"
215ca7b3 sago007 2009-03-06 16:37 29
27ae0175 sago007 2009-01-30 06:02 30
#include <string.h>
27ae0175 sago007 2009-01-30 06:02 31
b6fd59ea sago007 2016-03-16 19:04 32
#include "version.h"
89c4a3a6 sago007 2008-08-29 14:32 33
2e57d791 sago007 2009-06-04 17:48 34
#define WITH_SDL 1
2e57d791 sago007 2009-06-04 17:48 35
c6bde3e3 sago007 2015-12-22 16:56 36
#include "sago/SagoSpriteHolder.hpp"
1a09fcd5 sago007 2018-03-25 13:32 37
#include "sago/SagoTextBox.hpp"
89c4a3a6 sago007 2008-08-29 14:32 38
#include <iostream>
89c4a3a6 sago007 2008-08-29 14:32 39
#include <stdlib.h>
89c4a3a6 sago007 2008-08-29 14:32 40
#include <time.h>           //Used for srand()
549ecfae sago007 2016-10-09 08:37 41
#include <sstream>          //Still used by std::to_string2
89c4a3a6 sago007 2008-08-29 14:32 42
#include <string>
89c4a3a6 sago007 2008-08-29 14:32 43
#include "SDL.h"            //The SDL libary, used for most things
89c4a3a6 sago007 2008-08-29 14:32 44
#include <SDL_mixer.h>      //Used for sound & music
89c4a3a6 sago007 2008-08-29 14:32 45
#include <SDL_image.h>      //To load PNG images!
41f02ab2 sago007 2009-05-10 21:40 46
#include <physfs.h>         //Abstract file system. To use containers
89c4a3a6 sago007 2008-08-29 14:32 47
#include <vector>
b7590374 sago007 2011-05-19 16:15 48
#include "MenuSystem.h"
055218be sago007 2015-08-22 18:44 49
#include "puzzlehandler.hpp"
ae5d6381 sago007 2015-12-29 13:26 50
#include "stageclearhandler.hpp"
f3a1637d sago007 2015-11-14 21:32 51
#include <memory>
c3b697c6 sago007 2016-02-07 17:30 52
#include "ScoresDisplay.hpp"
89c4a3a6 sago007 2008-08-29 14:32 53
89c4a3a6 sago007 2008-08-29 14:32 54
//if SHAREDIR is not used we look in current directory
89c4a3a6 sago007 2008-08-29 14:32 55
#ifndef SHAREDIR
89c4a3a6 sago007 2008-08-29 14:32 56
#define SHAREDIR "."
89c4a3a6 sago007 2008-08-29 14:32 57
#endif
89c4a3a6 sago007 2008-08-29 14:32 58
c9dd376f sago007 2012-01-20 17:13 59
#ifndef LOCALEDIR
c9dd376f sago007 2012-01-20 17:13 60
#define LOCALEDIR SHAREDIR"/locale"
c9dd376f sago007 2012-01-20 17:13 61
#endif
89c4a3a6 sago007 2008-08-29 14:32 62
c9dd376f sago007 2012-01-20 17:13 63
#ifndef PACKAGE
c9dd376f sago007 2012-01-20 17:13 64
#define PACKAGE "blockattack_roftb"
c9dd376f sago007 2012-01-20 17:13 65
#endif
89c4a3a6 sago007 2008-08-29 14:32 66
89c4a3a6 sago007 2008-08-29 14:32 67
#include "highscore.h"      //Stores highscores
89c4a3a6 sago007 2008-08-29 14:32 68
#include "ReadKeyboard.h"   //Reads text from keyboard
66e1a55c Poul Sander 2018-12-23 16:25 69
#include "stats.h"          //Saves general stats
76514d10 sago007 2016-06-05 20:25 70
#include "replayhandler.hpp"
89c4a3a6 sago007 2008-08-29 14:32 71
89c4a3a6 sago007 2008-08-29 14:32 72
#include "common.h"
826cf176 sago007 2016-03-12 10:53 73
#include "gamecontroller.h"
c8b43ea3 sago007 2016-02-02 18:20 74
#include <boost/program_options.hpp>
93f080d7 sago007 2016-06-08 20:28 75
#include <fstream>
c6c637cc sago007 2016-11-12 17:35 76
#include "levelselect.hpp"
89c4a3a6 sago007 2008-08-29 14:32 77
89c4a3a6 sago007 2008-08-29 14:32 78
/*******************************************************************************
ee077313 sago007 2016-02-09 18:39 79
* All variables and constant has been moved to mainVars.inc for the overview.  *
89c4a3a6 sago007 2008-08-29 14:32 80
*******************************************************************************/
ee077313 sago007 2016-02-09 18:39 81
#include "mainVars.inc"
89c4a3a6 sago007 2008-08-29 14:32 82
96eb7a16 sago007 2016-04-29 17:01 83
using std::string;
96eb7a16 sago007 2016-04-29 17:01 84
using std::cerr;
96eb7a16 sago007 2016-04-29 17:01 85
using std::cout;
96eb7a16 sago007 2016-04-29 17:01 86
using std::exception;
96eb7a16 sago007 2016-04-29 17:01 87
using std::vector;
90e6c797 sago007 2015-11-22 21:46 88
72383a9e sago007 2016-10-30 11:56 89
GlobalData globalData;
89c4a3a6 sago007 2008-08-29 14:32 90
1d2e2129 sago007 2015-12-23 15:41 91
static int InitImages(sago::SagoSpriteHolder& holder);
866417ca sago007 2009-05-10 21:35 92
8c45a7b1 sago007 2016-02-05 21:39 93
static void FsSearchParthMainAppend(std::vector<std::string>& paths) {
8c45a7b1 sago007 2016-02-05 21:39 94
	paths.push_back((string)SHAREDIR+"/blockattack.data");
8c45a7b1 sago007 2016-02-05 21:39 95
	paths.push_back((string)PHYSFS_getBaseDir()+"/blockattack.data");
8c45a7b1 sago007 2016-02-05 21:39 96
	paths.push_back((string)PHYSFS_getBaseDir()+"/data");
8c45a7b1 sago007 2016-02-05 21:39 97
}
8c45a7b1 sago007 2016-02-05 21:39 98
8c45a7b1 sago007 2016-02-05 21:39 99
static void PhysFsSetSearchPath(const vector<string>& paths, const string& savepath) {
8c45a7b1 sago007 2016-02-05 21:39 100
	for (const string& path : paths) {
8c45a7b1 sago007 2016-02-05 21:39 101
		PHYSFS_addToSearchPath(path.c_str(),1);
8c45a7b1 sago007 2016-02-05 21:39 102
	}
8c45a7b1 sago007 2016-02-05 21:39 103
	PHYSFS_addToSearchPath(savepath.c_str(), 1);
3647d72c sago007 2016-02-12 22:24 104
	PHYSFS_setWriteDir(savepath.c_str());
89c4a3a6 sago007 2008-08-29 14:32 105
}
89c4a3a6 sago007 2008-08-29 14:32 106
89c4a3a6 sago007 2008-08-29 14:32 107
cd724e02 sago007 2016-02-13 16:04 108
static void PhysFsCreateFolders() {
cd724e02 sago007 2016-02-13 16:04 109
	PHYSFS_mkdir("screenshots");
cd724e02 sago007 2016-02-13 16:04 110
	PHYSFS_mkdir("replays");
cd724e02 sago007 2016-02-13 16:04 111
	PHYSFS_mkdir("puzzles");
cd724e02 sago007 2016-02-13 16:04 112
}
cd724e02 sago007 2016-02-13 16:04 113
247fe34e sago007 2018-06-30 12:35 114
static void setGameOverFont(const sago::SagoDataHolder* holder, sago::SagoTextBox& field, const char* text) {
247fe34e sago007 2018-06-30 12:35 115
	field.SetHolder(holder);
247fe34e sago007 2018-06-30 12:35 116
	field.SetFont("freeserif");
247fe34e sago007 2018-06-30 12:35 117
	field.SetColor({0,0,255,255});
247fe34e sago007 2018-06-30 12:35 118
	field.SetFontSize(60);
247fe34e sago007 2018-06-30 12:35 119
	field.SetOutline(3, {192,192,255,255});
247fe34e sago007 2018-06-30 12:35 120
	field.SetMaxWidth(280);
247fe34e sago007 2018-06-30 12:35 121
	field.SetText(text);
247fe34e sago007 2018-06-30 12:35 122
}
247fe34e sago007 2018-06-30 12:35 123
89c4a3a6 sago007 2008-08-29 14:32 124
//Load all image files to memory
1d2e2129 sago007 2015-12-23 15:41 125
static int InitImages(sago::SagoSpriteHolder& holder) {
879f7e86 sago007 2016-09-18 11:49 126
	bricks[0] = holder.GetSprite("block_blue");
879f7e86 sago007 2016-09-18 11:49 127
	bricks[1] = holder.GetSprite("block_green");
879f7e86 sago007 2016-09-18 11:49 128
	bricks[2] = holder.GetSprite("block_purple");
879f7e86 sago007 2016-09-18 11:49 129
	bricks[3] = holder.GetSprite("block_red");
879f7e86 sago007 2016-09-18 11:49 130
	bricks[4] = holder.GetSprite("block_turkish");
879f7e86 sago007 2016-09-18 11:49 131
	bricks[5] = holder.GetSprite("block_yellow");
879f7e86 sago007 2016-09-18 11:49 132
	bricks[6] = holder.GetSprite("block_grey");
1d2e2129 sago007 2015-12-23 15:41 133
	bomb = holder.GetSprite("block_bomb");
1d2e2129 sago007 2015-12-23 15:41 134
	backgroundImage = holder.GetSprite("background");
72383a9e sago007 2016-10-30 11:56 135
	globalData.bHighScore = holder.GetSprite("b_highscore");
72383a9e sago007 2016-10-30 11:56 136
	globalData.bBack = holder.GetSprite("b_blank");
1d2e2129 sago007 2015-12-23 15:41 137
	bForward = holder.GetSprite("b_forward");
1d2e2129 sago007 2015-12-23 15:41 138
	blackLine = holder.GetSprite("black_line");
1d2e2129 sago007 2015-12-23 15:41 139
	stageBobble = holder.GetSprite("i_stage_clear_limit");
1d2e2129 sago007 2015-12-23 15:41 140
	crossover = holder.GetSprite("crossover");
1d2e2129 sago007 2015-12-23 15:41 141
	balls[0] = holder.GetSprite("ball_blue");
1d2e2129 sago007 2015-12-23 15:41 142
	balls[1] = holder.GetSprite("ball_green");
1d2e2129 sago007 2015-12-23 15:41 143
	balls[2] = holder.GetSprite("ball_purple");
1d2e2129 sago007 2015-12-23 15:41 144
	balls[3] = holder.GetSprite("ball_red");
1d2e2129 sago007 2015-12-23 15:41 145
	balls[4] = holder.GetSprite("ball_turkish");
1d2e2129 sago007 2015-12-23 15:41 146
	balls[5] = holder.GetSprite("ball_yellow");
1d2e2129 sago007 2015-12-23 15:41 147
	balls[6] = holder.GetSprite("ball_gray");
1d2e2129 sago007 2015-12-23 15:41 148
	cursor = holder.GetSprite("cursor");
742e0112 sago007 2016-09-18 11:14 149
	ready = holder.GetSprite("block_ready");
1d2e2129 sago007 2015-12-23 15:41 150
	explosion[0] = holder.GetSprite("explosion0");
9d259921 sago007 2015-12-23 18:46 151
	explosion[1] = holder.GetSprite("explosion1");
9d259921 sago007 2015-12-23 18:46 152
	explosion[2] = holder.GetSprite("explosion2");
9d259921 sago007 2015-12-23 18:46 153
	explosion[3] = holder.GetSprite("explosion3");
1d2e2129 sago007 2015-12-23 15:41 154
	counter[0] = holder.GetSprite("counter_1");
1d2e2129 sago007 2015-12-23 15:41 155
	counter[1] = holder.GetSprite("counter_2");
1d2e2129 sago007 2015-12-23 15:41 156
	counter[2] = holder.GetSprite("counter_3");
247fe34e sago007 2018-06-30 12:35 157
	setGameOverFont(&holder.GetDataHolder(),tbGameOver, _("GAME OVER"));
247fe34e sago007 2018-06-30 12:35 158
	setGameOverFont(&holder.GetDataHolder(),tbWinner, _("WINNER"));
247fe34e sago007 2018-06-30 12:35 159
	setGameOverFont(&holder.GetDataHolder(),tbDraw, _("DRAW"));
c1bd83fb sago007 2015-12-23 19:41 160
	iChainFrame = holder.GetSprite("chain_frame");
c6c637cc sago007 2016-11-12 17:35 161
	globalData.iLevelCheck = holder.GetSprite("i_level_check");
c6c637cc sago007 2016-11-12 17:35 162
	globalData.iLevelCheckBox = holder.GetSprite("i_level_check_box");
c6c637cc sago007 2016-11-12 17:35 163
	globalData.iLevelCheckBoxMarked = holder.GetSprite("i_level_check_box_marked");
c6c637cc sago007 2016-11-12 17:35 164
	globalData.iCheckBoxArea = holder.GetSprite("i_check_box_area");
1d2e2129 sago007 2015-12-23 15:41 165
	boardBackBack = holder.GetSprite("board_back_back");
1d2e2129 sago007 2015-12-23 15:41 166
	garbageTL = holder.GetSprite("garbage_tl");
1d2e2129 sago007 2015-12-23 15:41 167
	garbageT = holder.GetSprite("garbage_t");
1d2e2129 sago007 2015-12-23 15:41 168
	garbageTR = holder.GetSprite("garbage_tr");
1d2e2129 sago007 2015-12-23 15:41 169
	garbageR = holder.GetSprite("garbage_r");
1d2e2129 sago007 2015-12-23 15:41 170
	garbageBR = holder.GetSprite("garbage_br");
1d2e2129 sago007 2015-12-23 15:41 171
	garbageB = holder.GetSprite("garbage_b");
1d2e2129 sago007 2015-12-23 15:41 172
	garbageBL = holder.GetSprite("garbage_bl");
1d2e2129 sago007 2015-12-23 15:41 173
	garbageL = holder.GetSprite("garbage_l");
1d2e2129 sago007 2015-12-23 15:41 174
	garbageFill = holder.GetSprite("garbage_fill");
1d2e2129 sago007 2015-12-23 15:41 175
	garbageML = holder.GetSprite("garbage_ml");
1d2e2129 sago007 2015-12-23 15:41 176
	garbageM = holder.GetSprite("garbage_m");
1d2e2129 sago007 2015-12-23 15:41 177
	garbageMR = holder.GetSprite("garbage_mr");
1d2e2129 sago007 2015-12-23 15:41 178
	garbageGM = holder.GetSprite("garbage_gm");
1d2e2129 sago007 2015-12-23 15:41 179
	garbageGML = holder.GetSprite("garbage_gml");
1d2e2129 sago007 2015-12-23 15:41 180
	garbageGMR = holder.GetSprite("garbage_gmr");
1d2e2129 sago007 2015-12-23 15:41 181
	smiley[0] = holder.GetSprite("smileys0");
1d2e2129 sago007 2015-12-23 15:41 182
	smiley[1] = holder.GetSprite("smileys1");
1d2e2129 sago007 2015-12-23 15:41 183
	smiley[2] = holder.GetSprite("smileys2");
1d2e2129 sago007 2015-12-23 15:41 184
	smiley[3] = holder.GetSprite("smileys3");
1d2e2129 sago007 2015-12-23 15:41 185
	transCover = holder.GetSprite("trans_cover");
38fa9b68 sago007 2016-03-12 08:46 186
	bExit = holder.GetSprite("b_exit");
c7e50476 sago007 2015-12-25 09:44 187
	bSkip = holder.GetSprite("b_blank");
72383a9e sago007 2016-10-30 11:56 188
	globalData.bNext = holder.GetSprite("b_blank");
c7e50476 sago007 2015-12-25 09:44 189
	bRetry = holder.GetSprite("b_blank");
72383a9e sago007 2016-10-30 11:56 190
	globalData.mouse = holder.GetSprite("mouse");
7d1936bd sago007 2015-12-23 16:06 191
	backBoard = holder.GetSprite("back_board");
d2ba3cf5 sago007 2018-05-13 15:00 192
a11d40ec sago007 2018-03-31 18:04 193
	sagoTextSetBlueFont(player1name);
a11d40ec sago007 2018-03-31 18:04 194
	sagoTextSetBlueFont(player2name);
a11d40ec sago007 2018-03-31 18:04 195
	sagoTextSetBlueFont(player1time);
a11d40ec sago007 2018-03-31 18:04 196
	sagoTextSetBlueFont(player2time);
a11d40ec sago007 2018-03-31 18:04 197
	sagoTextSetBlueFont(player1score);
a11d40ec sago007 2018-03-31 18:04 198
	sagoTextSetBlueFont(player2score);
a11d40ec sago007 2018-03-31 18:04 199
	sagoTextSetBlueFont(player1chain);
a11d40ec sago007 2018-03-31 18:04 200
	sagoTextSetBlueFont(player2chain);
a11d40ec sago007 2018-03-31 18:04 201
	sagoTextSetBlueFont(player1speed);
a11d40ec sago007 2018-03-31 18:04 202
	sagoTextSetBlueFont(player2speed);
d2ba3cf5 sago007 2018-05-13 15:00 203
c53e6443 sago007 2012-04-17 11:04 204
89c4a3a6 sago007 2008-08-29 14:32 205
//Loads the sound if sound present
72383a9e sago007 2016-10-30 11:56 206
	if (!globalData.NoSound) {
c53e6443 sago007 2012-04-17 11:04 207
		//And here the music:
524f188c sago007 2017-04-22 09:01 208
		bgMusic = holder.GetDataHolder().getMusicHandler("bgmusic");
524f188c sago007 2017-04-22 09:01 209
		highbeatMusic = holder.GetDataHolder().getMusicHandler("highbeat");
c53e6443 sago007 2012-04-17 11:04 210
		//the music... we just hope it exists, else the user won't hear anything
c53e6443 sago007 2012-04-17 11:04 211
		//Same goes for the sounds
524f188c sago007 2017-04-22 09:01 212
		boing = holder.GetDataHolder().getSoundHandler("pop");
524f188c sago007 2017-04-22 09:01 213
		applause = holder.GetDataHolder().getSoundHandler("applause");
524f188c sago007 2017-04-22 09:01 214
		photoClick = holder.GetDataHolder().getSoundHandler("cameraclick");
524f188c sago007 2017-04-22 09:01 215
		globalData.typingChunk = holder.GetDataHolder().getSoundHandler("typing");
524f188c sago007 2017-04-22 09:01 216
		counterChunk = holder.GetDataHolder().getSoundHandler("counter");
524f188c sago007 2017-04-22 09:01 217
		counterFinalChunk = holder.GetDataHolder().getSoundHandler("counter_final");
a48ca120 sago007 2016-04-10 14:20 218
		const int soundVolume = 84;  //0-128
524f188c sago007 2017-04-22 09:01 219
		Mix_VolumeChunk(boing.get(), soundVolume);
524f188c sago007 2017-04-22 09:01 220
		Mix_VolumeChunk(applause.get(), soundVolume);
524f188c sago007 2017-04-22 09:01 221
		Mix_VolumeChunk(photoClick.get(), soundVolume);
524f188c sago007 2017-04-22 09:01 222
		Mix_VolumeChunk(globalData.typingChunk.get(), soundVolume);
524f188c sago007 2017-04-22 09:01 223
		Mix_VolumeChunk(counterChunk.get(), soundVolume);
524f188c sago007 2017-04-22 09:01 224
		Mix_VolumeChunk(counterFinalChunk.get(), soundVolume);
c53e6443 sago007 2012-04-17 11:04 225
	} //All sound has been loaded or not
c53e6443 sago007 2012-04-17 11:04 226
	return 0;
89c4a3a6 sago007 2008-08-29 14:32 227
} //InitImages()
89c4a3a6 sago007 2008-08-29 14:32 228
89c4a3a6 sago007 2008-08-29 14:32 229
/*Draws a image from on a given Surface. Takes source image, destination surface and coordinates*/
ce444d3b sago007 2016-04-02 18:59 230
void DrawIMG(const sago::SagoSprite& sprite, SDL_Renderer* target, int x, int y) {
1176ee18 sago007 2017-08-19 16:10 231
	sprite.Draw(target, SDL_GetTicks(),x,y);
89c4a3a6 sago007 2008-08-29 14:32 232
}
89c4a3a6 sago007 2008-08-29 14:32 233
ce444d3b sago007 2016-04-02 18:59 234
void DrawIMG_Bounded(const sago::SagoSprite& sprite, SDL_Renderer* target, int x, int y, int minx, int miny, int maxx, int maxy) {
c6bde3e3 sago007 2015-12-22 16:56 235
	SDL_Rect bounds;
c6bde3e3 sago007 2015-12-22 16:56 236
	bounds.x = minx;
c6bde3e3 sago007 2015-12-22 16:56 237
	bounds.y = miny;
c6bde3e3 sago007 2015-12-22 16:56 238
	bounds.w = maxx-minx;
c6bde3e3 sago007 2015-12-22 16:56 239
	bounds.h = maxy-miny;
7d1936bd sago007 2015-12-23 16:06 240
	sprite.DrawBounded(target, SDL_GetTicks(),x,y,bounds);
89c4a3a6 sago007 2008-08-29 14:32 241
}
89c4a3a6 sago007 2008-08-29 14:32 242
fee9551a sago007 2016-01-20 19:05 243
SDL_Window* sdlWindow;
fee9551a sago007 2016-01-20 19:05 244
e2252ee5 sago007 2017-04-20 19:02 245
sago::SagoDataHolder dataHolder;
1383de53 sago007 2016-02-21 12:18 246
ecef5838 sago007 2015-11-24 20:01 247
void ResetFullscreen() {
1383de53 sago007 2016-02-21 12:18 248
	Mix_HaltMusic();  //We need to reload all data in case the screen type changes. Music must be stopped before unload.
72383a9e sago007 2016-10-30 11:56 249
	if (globalData.bFullscreen) {
1383de53 sago007 2016-02-21 12:18 250
		SDL_SetWindowFullscreen(sdlWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
335635ec sago007 2016-02-06 13:51 251
	}
fee9551a sago007 2016-01-20 19:05 252
	else {
fee9551a sago007 2016-01-20 19:05 253
		SDL_SetWindowFullscreen(sdlWindow, 0);
fee9551a sago007 2016-01-20 19:05 254
	}
abb733e9 Poul Sander 2019-04-24 16:15 255
	SDL_RenderSetLogicalSize(globalData.screen, globalData.xsize, globalData.ysize);
e2252ee5 sago007 2017-04-20 19:02 256
	dataHolder.invalidateAll(globalData.screen);
e2252ee5 sago007 2017-04-20 19:02 257
	globalData.spriteHolder.reset(new sago::SagoSpriteHolder( dataHolder ) );
72383a9e sago007 2016-10-30 11:56 258
	InitImages(*(globalData.spriteHolder.get()) );
c53e6443 sago007 2012-04-17 11:04 259
	SDL_ShowCursor(SDL_DISABLE);
78d03b38 sago007 2008-11-13 19:56 260
}
78d03b38 sago007 2008-11-13 19:56 261
e17fcc86 sago007 2016-09-28 20:09 262
static bool logicalRenderer = false;
e17fcc86 sago007 2016-09-28 20:09 263
f1708e5e sago007 2016-09-28 17:09 264
void DrawBackground(SDL_Renderer* target) {
13476fc2 sago007 2016-09-28 18:34 265
	SDL_RenderClear(target);
abb733e9 Poul Sander 2019-04-24 16:15 266
	/*if (logicalRenderer) {
72383a9e sago007 2016-10-30 11:56 267
		globalData.xsize = 1024;
72383a9e sago007 2016-10-30 11:56 268
		globalData.ysize = 768;
e17fcc86 sago007 2016-09-28 20:09 269
	}
e17fcc86 sago007 2016-09-28 20:09 270
	else {
72383a9e sago007 2016-10-30 11:56 271
		SDL_GetWindowSize(sdlWindow, &globalData.xsize, &globalData.ysize);
abb733e9 Poul Sander 2019-04-24 16:15 272
	}*/
abb733e9 Poul Sander 2019-04-24 16:15 273
	//SDL_SetRenderDrawColor(target, 135, 206, 250, 255);
21624171 Poul Sander 2019-03-27 18:38 274
	SDL_RenderClear(target);
abb733e9 Poul Sander 2019-04-24 16:15 275
	//globalData.spriteHolder->GetSprite("background_sun").DrawScaled(globalData.screen, SDL_GetTicks(), globalData.xsize-320, -200, 600, 600);
abb733e9 Poul Sander 2019-04-24 16:15 276
	//globalData.spriteHolder->GetSprite("background_ground").DrawScaled(globalData.screen, SDL_GetTicks(), 0, globalData.ysize-200, globalData.xsize, 200);
abb733e9 Poul Sander 2019-04-24 16:15 277
	backgroundImage.DrawScaled(target, SDL_GetTicks(), 0, 0, globalData.xsize, globalData.ysize);
68313e95 sago007 2016-09-28 16:52 278
}
78d03b38 sago007 2008-11-13 19:56 279
6e6e44b7 sago007 2016-10-18 15:30 280
/**
6e6e44b7 sago007 2016-10-18 15:30 281
 * This function reads the mouse coordinates from a relevant event.
6e6e44b7 sago007 2016-10-18 15:30 282
 * Unlike SDL_GetMouseState this works even if SDL_RenderSetLogicalSize is used
6e6e44b7 sago007 2016-10-18 15:30 283
 * @param event
6e6e44b7 sago007 2016-10-18 15:30 284
 * @param mousex
6e6e44b7 sago007 2016-10-18 15:30 285
 * @param mousey
6e6e44b7 sago007 2016-10-18 15:30 286
 */
6e6e44b7 sago007 2016-10-18 15:30 287
void UpdateMouseCoordinates(const SDL_Event& event, int& mousex, int& mousey) {
092bdebe sago007 2017-03-19 14:39 288
	switch (event.type) {
092bdebe sago007 2017-03-19 14:39 289
	case SDL_MOUSEBUTTONDOWN:
092bdebe sago007 2017-03-19 14:39 290
	case SDL_MOUSEBUTTONUP:
092bdebe sago007 2017-03-19 14:39 291
		mousex = event.button.x;
092bdebe sago007 2017-03-19 14:39 292
		mousey = event.button.y;
092bdebe sago007 2017-03-19 14:39 293
		break;
092bdebe sago007 2017-03-19 14:39 294
	case SDL_MOUSEMOTION:
092bdebe sago007 2017-03-19 14:39 295
		mousex = event.motion.x;
092bdebe sago007 2017-03-19 14:39 296
		mousey = event.motion.y;
092bdebe sago007 2017-03-19 14:39 297
		break;
092bdebe sago007 2017-03-19 14:39 298
	default:
092bdebe sago007 2017-03-19 14:39 299
		break;
47fef8e8 sago007 2016-11-24 21:37 300
	}
6e6e44b7 sago007 2016-10-18 15:30 301
}
6e6e44b7 sago007 2016-10-18 15:30 302
d8c1f444 sago007 2016-01-31 11:36 303
static BallManager theBallManager;
89c4a3a6 sago007 2008-08-29 14:32 304
d8c1f444 sago007 2016-01-31 11:36 305
static ExplosionManager theExplosionManager;
89c4a3a6 sago007 2008-08-29 14:32 306
89c4a3a6 sago007 2008-08-29 14:32 307
89c4a3a6 sago007 2008-08-29 14:32 308
//Here comes the Block Game object
89c4a3a6 sago007 2008-08-29 14:32 309
#include "BlockGame.hpp"
9dc80982 sago007 2016-01-02 21:08 310
#include "os.hpp"
c3fc09e9 sago007 2016-01-22 18:31 311
#include "sago/SagoMiscSdl2.hpp"
c3b697c6 sago007 2016-02-07 17:30 312
#include "ScoresDisplay.hpp"
89c4a3a6 sago007 2008-08-29 14:32 313
8b1ab1cb sago007 2016-04-04 17:51 314
#include "BlockGameSdl.inc"
bbb1d879 sago007 2016-06-10 19:50 315
#include "sago/SagoMisc.hpp"
092bdebe sago007 2017-03-19 14:39 316
#include "ReplayPlayer.hpp"
6b1dc7a6 sago007 2010-11-08 21:03 317
6b1dc7a6 sago007 2010-11-08 21:03 318
6b1dc7a6 sago007 2010-11-08 21:03 319
89c4a3a6 sago007 2008-08-29 14:32 320
//writeScreenShot saves the screen as a bmp file, it uses the time to get a unique filename
ecef5838 sago007 2015-11-24 20:01 321
void writeScreenShot() {
72383a9e sago007 2016-10-30 11:56 322
	if (globalData.verboseLevel) {
161a010c sago007 2016-05-06 14:00 323
		cout << "Saving screenshot" << "\n";
acd79baf sago007 2015-11-22 19:37 324
	}
acd79baf sago007 2015-11-22 19:37 325
	int rightNow = (int)time(nullptr);
549ecfae sago007 2016-10-09 08:37 326
	string buf = getPathToSaveFiles() + "/screenshots/screenshot"+std::to_string(rightNow)+".bmp";
335635ec sago007 2016-02-06 13:51 327
	SDL_Surface* sreenshotSurface = SDL_CreateRGBSurface(0, 1024, 768, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
72383a9e sago007 2016-10-30 11:56 328
	SDL_RenderReadPixels(globalData.screen, NULL, SDL_PIXELFORMAT_ARGB8888, sreenshotSurface->pixels, sreenshotSurface->pitch);
fee9551a sago007 2016-01-20 19:05 329
	SDL_SaveBMP(sreenshotSurface, buf.c_str());
fee9551a sago007 2016-01-20 19:05 330
	SDL_FreeSurface(sreenshotSurface);
72383a9e sago007 2016-10-30 11:56 331
	if (!globalData.NoSound) {
72383a9e sago007 2016-10-30 11:56 332
		if (globalData.SoundEnabled) {
524f188c sago007 2017-04-22 09:01 333
			Mix_PlayChannel(1, photoClick.get(), 0);
acd79baf sago007 2015-11-22 19:37 334
		}
1d2e2129 sago007 2015-12-23 15:41 335
	}
89c4a3a6 sago007 2008-08-29 14:32 336
}
89c4a3a6 sago007 2008-08-29 14:32 337
89c4a3a6 sago007 2008-08-29 14:32 338
//Function to return the name of a key, to be displayed...
d82fedb9 Poul Sander 2018-06-20 16:48 339
string getKeyName(SDL_Keycode key);
89c4a3a6 sago007 2008-08-29 14:32 340
89c4a3a6 sago007 2008-08-29 14:32 341
58a2d7e1 sago007 2016-02-12 18:53 342
void RunGameState(sago::GameStateInterface& state ) {
c53e6443 sago007 2012-04-17 11:04 343
	bool done = false;     //We are done!
ecef5838 sago007 2015-11-24 20:01 344
	while (!done && !Config::getInstance()->isShuttingDown()) {
72383a9e sago007 2016-10-30 11:56 345
		state.Draw(globalData.screen);
58a2d7e1 sago007 2016-02-12 18:53 346
571b4d47 sago007 2015-12-05 16:02 347
		SDL_Delay(1);
c53e6443 sago007 2012-04-17 11:04 348
		SDL_Event event;
c53e6443 sago007 2012-04-17 11:04 349
fee9551a sago007 2016-01-20 19:05 350
		bool mustWriteScreenshot = false;
58a2d7e1 sago007 2016-02-12 18:53 351
ecef5838 sago007 2015-11-24 20:01 352
		while ( SDL_PollEvent(&event) ) {
20d77c6e sago007 2016-11-12 16:28 353
			UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey);
ecef5838 sago007 2015-11-24 20:01 354
			if ( event.type == SDL_QUIT ) {
c53e6443 sago007 2012-04-17 11:04 355
				Config::getInstance()->setShuttingDown(5);
c53e6443 sago007 2012-04-17 11:04 356
				done = true;
c53e6443 sago007 2012-04-17 11:04 357
			}
58a2d7e1 sago007 2016-02-12 18:53 358
c3b697c6 sago007 2016-02-07 17:30 359
			if ( event.key.keysym.sym == SDLK_F9 ) {
c3b697c6 sago007 2016-02-07 17:30 360
				mustWriteScreenshot = true;
c53e6443 sago007 2012-04-17 11:04 361
			}
58a2d7e1 sago007 2016-02-12 18:53 362
c3b697c6 sago007 2016-02-07 17:30 363
			bool processed = false;
c3b697c6 sago007 2016-02-07 17:30 364
			state.ProcessInput(event, processed);
58a2d7e1 sago007 2016-02-12 18:53 365
c53e6443 sago007 2012-04-17 11:04 366
		}
58a2d7e1 sago007 2016-02-12 18:53 367
c3b697c6 sago007 2016-02-07 17:30 368
		state.Update();
58a2d7e1 sago007 2016-02-12 18:53 369
20d77c6e sago007 2016-11-12 16:28 370
		globalData.mouse.Draw(globalData.screen, SDL_GetTicks(), globalData.mousex, globalData.mousey);
72383a9e sago007 2016-10-30 11:56 371
		SDL_RenderPresent(globalData.screen);
fee9551a sago007 2016-01-20 19:05 372
		if (mustWriteScreenshot) {
fee9551a sago007 2016-01-20 19:05 373
			writeScreenShot();
fee9551a sago007 2016-01-20 19:05 374
		}
6b1dc7a6 sago007 2010-11-08 21:03 375
c3b697c6 sago007 2016-02-07 17:30 376
		if (!state.IsActive()) {
c3b697c6 sago007 2016-02-07 17:30 377
			done = true;
c3b697c6 sago007 2016-02-07 17:30 378
		}
c3b697c6 sago007 2016-02-07 17:30 379
	}
c3b697c6 sago007 2016-02-07 17:30 380
}
6b1dc7a6 sago007 2010-11-08 21:03 381
c3b697c6 sago007 2016-02-07 17:30 382
void OpenScoresDisplay() {
c3b697c6 sago007 2016-02-07 17:30 383
	ScoresDisplay d;
c3b697c6 sago007 2016-02-07 17:30 384
	d.scoreX = buttonXsize*2;
c3b697c6 sago007 2016-02-07 17:30 385
	d.scoreY = 0;
c3b697c6 sago007 2016-02-07 17:30 386
	d.buttonXsize = buttonXsize;
c3b697c6 sago007 2016-02-07 17:30 387
	d.buttonYsize = buttonYsize;
c3b697c6 sago007 2016-02-07 17:30 388
	RunGameState(d);
81d9c25d sago007 2008-11-24 09:50 389
}
81d9c25d sago007 2008-11-24 09:50 390
4641beb0 sago007 2018-03-30 12:54 391
4641beb0 sago007 2018-03-30 12:54 392
static sago::SagoTextField* getSmallInt(size_t number) {
4641beb0 sago007 2018-03-30 12:54 393
	static std::vector<std::shared_ptr<sago::SagoTextField> > smallFontCache;
4641beb0 sago007 2018-03-30 12:54 394
	if (smallFontCache.size() < number+1) {
4641beb0 sago007 2018-03-30 12:54 395
		smallFontCache.resize(number+1);
4641beb0 sago007 2018-03-30 12:54 396
	}
4641beb0 sago007 2018-03-30 12:54 397
	if (!smallFontCache[number]) {
4641beb0 sago007 2018-03-30 12:54 398
		std::shared_ptr<sago::SagoTextField> newNumber = std::make_shared<sago::SagoTextField>();
4641beb0 sago007 2018-03-30 12:54 399
		newNumber->SetHolder(&globalData.spriteHolder->GetDataHolder());
4641beb0 sago007 2018-03-30 12:54 400
		newNumber->SetFont("freeserif");
4641beb0 sago007 2018-03-30 12:54 401
		newNumber->SetFontSize(16);
4641beb0 sago007 2018-03-30 12:54 402
		newNumber->SetColor({255,0,0,255});
0b8fb137 sago007 2018-03-31 19:53 403
		newNumber->SetText(std::to_string(number));
4641beb0 sago007 2018-03-30 12:54 404
		smallFontCache[number] = newNumber;
4641beb0 sago007 2018-03-30 12:54 405
	}
4641beb0 sago007 2018-03-30 12:54 406
	return smallFontCache[number].get();
4641beb0 sago007 2018-03-30 12:54 407
}
4641beb0 sago007 2018-03-30 12:54 408
89c4a3a6 sago007 2008-08-29 14:32 409
//Draws the balls and explosions
ecef5838 sago007 2015-11-24 20:01 410
static void DrawBalls() {
f5d6c281 sago007 2017-03-25 17:08 411
	for (size_t i = 0; i< theBallManager.ballArray.size(); i++) {
2e4b8a9f sago007 2017-03-25 16:23 412
		if (theBallManager.ballArray[i].inUse) {
72383a9e sago007 2016-10-30 11:56 413
			DrawIMG(balls[theBallManager.ballArray[i].getColor()],globalData.screen,theBallManager.ballArray[i].getX(),theBallManager.ballArray[i].getY());
c53e6443 sago007 2012-04-17 11:04 414
		} //if used
f5d6c281 sago007 2017-03-25 17:08 415
	}
f5d6c281 sago007 2017-03-25 17:08 416
	for (size_t i = 0; i< theExplosionManager.explosionArray.size(); i++) {
2e4b8a9f sago007 2017-03-25 16:23 417
		if (theExplosionManager.explosionArray[i].inUse) {
72383a9e sago007 2016-10-30 11:56 418
			DrawIMG(explosion[theExplosionManager.explosionArray[i].getFrame()],globalData.screen,theExplosionManager.explosionArray[i].getX(),theExplosionManager.explosionArray[i].getY());
c53e6443 sago007 2012-04-17 11:04 419
		}
c310f8a5 sago007 2017-03-25 16:05 420
	} //for
29724f4a sago007 2017-03-25 16:18 421
	for (size_t i = 0; i < globalData.theTextManager.textArray.size(); ++i) {
e2c3b704 sago007 2017-03-25 16:10 422
		if (globalData.theTextManager.textArray[i].inUse) {
c310f8a5 sago007 2017-03-25 16:05 423
			int x = globalData.theTextManager.textArray[i].getX()-12;
c310f8a5 sago007 2017-03-25 16:05 424
			int y = globalData.theTextManager.textArray[i].getY()-12;
72383a9e sago007 2016-10-30 11:56 425
			DrawIMG(iChainFrame,globalData.screen,x,y);
53203200 sago007 2015-12-25 13:30 426
3f3ff0b6 sago007 2018-03-30 12:59 427
			getSmallInt(globalData.theTextManager.textArray[i].getText())->Draw(globalData.screen, x+12, y+7,
d2ba3cf5 sago007 2018-05-13 15:00 428
			        sago::SagoTextField::Alignment::center);
c53e6443 sago007 2012-04-17 11:04 429
		}
c310f8a5 sago007 2017-03-25 16:05 430
	}
89c4a3a6 sago007 2008-08-29 14:32 431
}    //DrawBalls
89c4a3a6 sago007 2008-08-29 14:32 432
d2ba3cf5 sago007 2018-05-13 15:00 433
template <class T> void sagoTextSetHelpFont(T& field) {
9bbdb95d sago007 2018-03-26 11:39 434
	field.SetHolder(&globalData.spriteHolder->GetDataHolder());
9bbdb95d sago007 2018-03-26 11:39 435
	field.SetFont("freeserif");
9bbdb95d sago007 2018-03-26 11:39 436
	field.SetFontSize(30);
d8fe60bd sago007 2018-04-02 16:15 437
	field.SetOutline(1, {128,128,128,255});
9bbdb95d sago007 2018-03-26 11:39 438
}
9bbdb95d sago007 2018-03-26 11:39 439
d2ba3cf5 sago007 2018-05-13 15:00 440
void sagoTextSetHelpFont(sago::SagoTextField& gametypeNameField) {
9bbdb95d sago007 2018-03-26 11:39 441
	sagoTextSetHelpFont<sago::SagoTextField>(gametypeNameField);
9bbdb95d sago007 2018-03-26 11:39 442
}
89c4a3a6 sago007 2008-08-29 14:32 443
e7e189c7 sago007 2018-03-31 15:30 444
void sagoTextSetBlueFont(sago::SagoTextField& field) {
e7e189c7 sago007 2018-03-31 15:30 445
	field.SetHolder(&globalData.spriteHolder->GetDataHolder());
e7e189c7 sago007 2018-03-31 15:30 446
	field.SetFont("freeserif");
e7e189c7 sago007 2018-03-31 15:30 447
	field.SetFontSize(30);
e7e189c7 sago007 2018-03-31 15:30 448
	field.SetColor({0,0,255,255});
ae23c5f1 sago007 2018-03-31 18:14 449
	field.SetOutline(1, {128,128,255,255});
e7e189c7 sago007 2018-03-31 15:30 450
}
e7e189c7 sago007 2018-03-31 15:30 451
89c4a3a6 sago007 2008-08-29 14:32 452
//draws everything
ecef5838 sago007 2015-11-24 20:01 453
void DrawEverything(int xsize, int ysize,BlockGameSdl* theGame, BlockGameSdl* theGame2) {
c53e6443 sago007 2012-04-17 11:04 454
	SDL_ShowCursor(SDL_DISABLE);
72383a9e sago007 2016-10-30 11:56 455
	DrawBackground(globalData.screen);
ae0f6c7a sago007 2015-12-05 16:58 456
	theGame->DoPaintJob();
c699a9f9 sago007 2015-12-05 17:22 457
	theGame2->DoPaintJob();
c53e6443 sago007 2012-04-17 11:04 458
	string strHolder;
549ecfae sago007 2016-10-09 08:37 459
	strHolder = std::to_string(theGame->GetScore()+theGame->GetHandicap());
0b8fb137 sago007 2018-03-31 19:53 460
	player1score.SetText(strHolder);
a11d40ec sago007 2018-03-31 18:04 461
	player1score.Draw(globalData.screen, theGame->GetTopX()+310,theGame->GetTopY()+100);
ecef5838 sago007 2015-11-24 20:01 462
	if (theGame->GetAIenabled()) {
a11d40ec sago007 2018-03-31 18:04 463
		player1name.SetText(_("AI"));
acd79baf sago007 2015-11-22 19:37 464
	}
2c6a4bae sago007 2018-05-11 15:20 465
	else if (singlePuzzle) {
a11d40ec sago007 2018-03-31 18:04 466
		player1name.SetText(_("Playing field"));
acd79baf sago007 2015-11-22 19:37 467
	}
a7de0677 sago007 2016-04-29 16:54 468
	else {
0b8fb137 sago007 2018-03-31 19:53 469
		player1name.SetText(globalData.player1name);
acd79baf sago007 2015-11-22 19:37 470
	}
a11d40ec sago007 2018-03-31 18:04 471
	player1name.Draw(globalData.screen, theGame->GetTopX()+10,theGame->GetTopY()-34);
ecef5838 sago007 2015-11-24 20:01 472
	if (theGame->isTimeTrial()) {
c53e6443 sago007 2012-04-17 11:04 473
		int tid = (int)SDL_GetTicks()-theGame->GetGameStartedAt();
c53e6443 sago007 2012-04-17 11:04 474
		int minutes;
c53e6443 sago007 2012-04-17 11:04 475
		int seconds;
ecef5838 sago007 2015-11-24 20:01 476
		if (tid>=0) {
c53e6443 sago007 2012-04-17 11:04 477
			minutes = (2*60*1000-(abs((int)SDL_GetTicks()-(int)theGame->GetGameStartedAt())))/60/1000;
c53e6443 sago007 2012-04-17 11:04 478
			seconds = ((2*60*1000-(abs((int)SDL_GetTicks()-(int)theGame->GetGameStartedAt())))%(60*1000))/1000;
c53e6443 sago007 2012-04-17 11:04 479
		}
ecef5838 sago007 2015-11-24 20:01 480
		else {
c53e6443 sago007 2012-04-17 11:04 481
			minutes = ((abs((int)SDL_GetTicks()-(int)theGame->GetGameStartedAt())))/60/1000;
c53e6443 sago007 2012-04-17 11:04 482
			seconds = (((abs((int)SDL_GetTicks()-(int)theGame->GetGameStartedAt())))%(60*1000))/1000;
c53e6443 sago007 2012-04-17 11:04 483
		}
acd79baf sago007 2015-11-22 19:37 484
		if (theGame->isGameOver()) {
acd79baf sago007 2015-11-22 19:37 485
			minutes=0;
acd79baf sago007 2015-11-22 19:37 486
		}
acd79baf sago007 2015-11-22 19:37 487
		if (theGame->isGameOver()) {
acd79baf sago007 2015-11-22 19:37 488
			seconds=0;
acd79baf sago007 2015-11-22 19:37 489
		}
ecef5838 sago007 2015-11-24 20:01 490
		if (seconds>9) {
549ecfae sago007 2016-10-09 08:37 491
			strHolder = std::to_string(minutes)+":"+std::to_string(seconds);
acd79baf sago007 2015-11-22 19:37 492
		}
acd79baf sago007 2015-11-22 19:37 493
		else {
549ecfae sago007 2016-10-09 08:37 494
			strHolder = std::to_string(minutes)+":0"+std::to_string(seconds);
acd79baf sago007 2015-11-22 19:37 495
		}
0b8fb137 sago007 2018-03-31 19:53 496
		player1time.SetText(strHolder);
c53e6443 sago007 2012-04-17 11:04 497
	}
ecef5838 sago007 2015-11-24 20:01 498
	else {
c53e6443 sago007 2012-04-17 11:04 499
		int minutes = ((abs((int)SDL_GetTicks()-(int)theGame->GetGameStartedAt())))/60/1000;
c53e6443 sago007 2012-04-17 11:04 500
		int seconds = (((abs((int)SDL_GetTicks()-(int)theGame->GetGameStartedAt())))%(60*1000))/1000;
acd79baf sago007 2015-11-22 19:37 501
		if (theGame->isGameOver()) {
acd79baf sago007 2015-11-22 19:37 502
			minutes=(theGame->GetGameEndedAt()/1000/60)%100;
acd79baf sago007 2015-11-22 19:37 503
		}
acd79baf sago007 2015-11-22 19:37 504
		if (theGame->isGameOver()) {
acd79baf sago007 2015-11-22 19:37 505
			seconds=(theGame->GetGameEndedAt()/1000)%60;
acd79baf sago007 2015-11-22 19:37 506
		}
ecef5838 sago007 2015-11-24 20:01 507
		if (seconds>9) {
549ecfae sago007 2016-10-09 08:37 508
			strHolder = std::to_string(minutes)+":"+std::to_string(seconds);
acd79baf sago007 2015-11-22 19:37 509
		}
ecef5838 sago007 2015-11-24 20:01 510
		else {
549ecfae sago007 2016-10-09 08:37 511
			strHolder = std::to_string(minutes)+":0"+std::to_string(seconds);
acd79baf sago007 2015-11-22 19:37 512
		}
0b8fb137 sago007 2018-03-31 19:53 513
		player1time.SetText(strHolder);
c53e6443 sago007 2012-04-17 11:04 514
	}
a11d40ec sago007 2018-03-31 18:04 515
	player1time.Draw(globalData.screen, theGame->GetTopX()+310,theGame->GetTopY()+150);
549ecfae sago007 2016-10-09 08:37 516
	strHolder = std::to_string(theGame->GetChains());
0b8fb137 sago007 2018-03-31 19:53 517
	player1chain.SetText(strHolder);
a11d40ec sago007 2018-03-31 18:04 518
	player1chain.Draw(globalData.screen, theGame->GetTopX()+310,theGame->GetTopY()+200);
c53e6443 sago007 2012-04-17 11:04 519
	//drawspeedLevel:
549ecfae sago007 2016-10-09 08:37 520
	strHolder = std::to_string(theGame->GetSpeedLevel());
0b8fb137 sago007 2018-03-31 19:53 521
	player1speed.SetText(strHolder);
a11d40ec sago007 2018-03-31 18:04 522
	player1speed.Draw(globalData.screen, theGame->GetTopX()+310,theGame->GetTopY()+250);
ecef5838 sago007 2015-11-24 20:01 523
	if ((theGame->isStageClear()) &&(theGame->GetTopY()+700+50*(theGame->GetStageClearLimit()-theGame->GetLinesCleared())-theGame->GetPixels()-1<600+theGame->GetTopY())) {
c53e6443 sago007 2012-04-17 11:04 524
		oldBubleX = theGame->GetTopX()+280;
c53e6443 sago007 2012-04-17 11:04 525
		oldBubleY = theGame->GetTopY()+650+50*(theGame->GetStageClearLimit()-theGame->GetLinesCleared())-theGame->GetPixels()-1;
72383a9e sago007 2016-10-30 11:56 526
		DrawIMG(stageBobble,globalData.screen,theGame->GetTopX()+280,theGame->GetTopY()+650+50*(theGame->GetStageClearLimit()-theGame->GetLinesCleared())-theGame->GetPixels()-1);
c53e6443 sago007 2012-04-17 11:04 527
	}
c53e6443 sago007 2012-04-17 11:04 528
	//player1 finnish, player2 start
2c6a4bae sago007 2018-05-11 15:20 529
	if (true ) {
c53e6443 sago007 2012-04-17 11:04 530
		/*
c53e6443 sago007 2012-04-17 11:04 531
		 *If single player mode (and not VS)
c53e6443 sago007 2012-04-17 11:04 532
		 */
ecef5838 sago007 2015-11-24 20:01 533
		if (!twoPlayers && !theGame->isGameOver()) {
c53e6443 sago007 2012-04-17 11:04 534
			//Blank player2's board:
72383a9e sago007 2016-10-30 11:56 535
			DrawIMG(backBoard,globalData.screen,theGame2->GetTopX(),theGame2->GetTopY());
c53e6443 sago007 2012-04-17 11:04 536
			//Write a description:
d064f90f sago007 2016-01-26 17:25 537
			string gametypeName;
599a0479 sago007 2016-01-26 17:17 538
			string infostring;
ecef5838 sago007 2015-11-24 20:01 539
			if (theGame->isTimeTrial()) {
d064f90f sago007 2016-01-26 17:25 540
				gametypeName = _("Time Trial");
599a0479 sago007 2016-01-26 17:17 541
				infostring = _("Score as much as possible in 2 minutes");
335635ec sago007 2016-02-06 13:51 542
c53e6443 sago007 2012-04-17 11:04 543
			}
ecef5838 sago007 2015-11-24 20:01 544
			else if (theGame->isStageClear()) {
d064f90f sago007 2016-01-26 17:25 545
				gametypeName = _("Stage Clear");
599a0479 sago007 2016-01-26 17:17 546
				infostring = _("You must clear a number of lines. Speed is rapidly increased.");
c53e6443 sago007 2012-04-17 11:04 547
			}
ecef5838 sago007 2015-11-24 20:01 548
			else if (theGame->isPuzzleMode()) {
d064f90f sago007 2016-01-26 17:25 549
				gametypeName = _("Puzzle");
599a0479 sago007 2016-01-26 17:17 550
				infostring = _("Clear the entire board with a limited number of moves.");
c53e6443 sago007 2012-04-17 11:04 551
			}
ecef5838 sago007 2015-11-24 20:01 552
			else {
d064f90f sago007 2016-01-26 17:25 553
				gametypeName = _("Endless");
599a0479 sago007 2016-01-26 17:17 554
				infostring = _("Score as much as possible. No time limit.");
599a0479 sago007 2016-01-26 17:17 555
			}
599a0479 sago007 2016-01-26 17:17 556
			if (infostring.length() > 0) {
1a09fcd5 sago007 2018-03-25 13:32 557
				static sago::SagoTextBox infoBox;
1a09fcd5 sago007 2018-03-25 13:32 558
				static sago::SagoTextField objectiveField;
1a09fcd5 sago007 2018-03-25 13:32 559
				static sago::SagoTextField gametypeNameField;
9bbdb95d sago007 2018-03-26 11:39 560
				sagoTextSetHelpFont(infoBox);
1a09fcd5 sago007 2018-03-25 13:32 561
				infoBox.SetMaxWidth(290);
0b8fb137 sago007 2018-03-31 19:53 562
				infoBox.SetText(infostring);
9bbdb95d sago007 2018-03-26 11:39 563
				sagoTextSetHelpFont(objectiveField);
1a09fcd5 sago007 2018-03-25 13:32 564
				objectiveField.SetText(_("Objective:"));
9bbdb95d sago007 2018-03-26 11:39 565
				sagoTextSetHelpFont(gametypeNameField);
0b8fb137 sago007 2018-03-31 19:53 566
				gametypeNameField.SetText(gametypeName);
1a09fcd5 sago007 2018-03-25 13:32 567
				gametypeNameField.Draw(globalData.screen, theGame2->GetTopX()+7,theGame2->GetTopY()+10);
1a09fcd5 sago007 2018-03-25 13:32 568
				objectiveField.Draw(globalData.screen, theGame2->GetTopX()+7, theGame2->GetTopY()+160);
1a09fcd5 sago007 2018-03-25 13:32 569
				infoBox.Draw(globalData.screen, theGame2->GetTopX()+7, theGame2->GetTopY()+160+32);
c53e6443 sago007 2012-04-17 11:04 570
			}
c53e6443 sago007 2012-04-17 11:04 571
c53e6443 sago007 2012-04-17 11:04 572
			//Write the keys that are in use
c53e6443 sago007 2012-04-17 11:04 573
			int y = theGame2->GetTopY()+400;
1a09fcd5 sago007 2018-03-25 13:32 574
			std::string controldBoxText = std::string(_("Movement keys:"))+"\n"+getKeyName(keySettings[0].left)+", "+getKeyName(keySettings[0].right)+",\n"
d2ba3cf5 sago007 2018-05-13 15:00 575
			                              + getKeyName(keySettings[0].up)+", "+getKeyName(keySettings[0].down)+"\n"
d2ba3cf5 sago007 2018-05-13 15:00 576
			                              + _("Switch: ") + getKeyName(keySettings[0].change);
ecef5838 sago007 2015-11-24 20:01 577
			if (theGame->isPuzzleMode()) {
1a09fcd5 sago007 2018-03-25 13:32 578
				controldBoxText += std::string("\n") + _("Restart: ")+getKeyName(keySettings[0].push);
acd79baf sago007 2015-11-22 19:37 579
			}
ecef5838 sago007 2015-11-24 20:01 580
			else {
1a09fcd5 sago007 2018-03-25 13:32 581
				controldBoxText += std::string("\n") + _("Push line: ")+getKeyName(keySettings[0].push);
acd79baf sago007 2015-11-22 19:37 582
			}
1a09fcd5 sago007 2018-03-25 13:32 583
			static sago::SagoTextBox controldBox;
1a09fcd5 sago007 2018-03-25 13:32 584
			controldBox.SetHolder(&globalData.spriteHolder->GetDataHolder());
f6e8c704 sago007 2018-04-03 21:39 585
			sagoTextSetHelpFont(controldBox);
1a09fcd5 sago007 2018-03-25 13:32 586
			controldBox.SetMaxWidth(290);
0b8fb137 sago007 2018-03-31 19:53 587
			controldBox.SetText(controldBoxText);
1a09fcd5 sago007 2018-03-25 13:32 588
			controldBox.Draw(globalData.screen, theGame2->GetTopX()+7,y);
c53e6443 sago007 2012-04-17 11:04 589
		}
549ecfae sago007 2016-10-09 08:37 590
		strHolder = std::to_string(theGame2->GetScore()+theGame2->GetHandicap());
0b8fb137 sago007 2018-03-31 19:53 591
		player2score.SetText(strHolder);
a11d40ec sago007 2018-03-31 18:04 592
		player2score.Draw(globalData.screen, theGame2->GetTopX()+310, theGame2->GetTopY()+100);
ecef5838 sago007 2015-11-24 20:01 593
		if (theGame2->GetAIenabled()) {
a11d40ec sago007 2018-03-31 18:04 594
			player2name.SetText(_("AI"));
acd79baf sago007 2015-11-22 19:37 595
		}
ecef5838 sago007 2015-11-24 20:01 596
		else {
0b8fb137 sago007 2018-03-31 19:53 597
			player2name.SetText(theGame2->name);
acd79baf sago007 2015-11-22 19:37 598
		}
a11d40ec sago007 2018-03-31 18:04 599
		player2name.Draw(globalData.screen, theGame2->GetTopX()+10,theGame2->GetTopY()-34);
ecef5838 sago007 2015-11-24 20:01 600
		if (theGame2->isTimeTrial()) {
c53e6443 sago007 2012-04-17 11:04 601
			int tid = (int)SDL_GetTicks()-theGame2->GetGameStartedAt();
c53e6443 sago007 2012-04-17 11:04 602
			int minutes;
c53e6443 sago007 2012-04-17 11:04 603
			int seconds;
ecef5838 sago007 2015-11-24 20:01 604
			if (tid>=0) {
c53e6443 sago007 2012-04-17 11:04 605
				minutes = (2*60*1000-(abs((int)SDL_GetTicks()-(int)theGame2->GetGameStartedAt())))/60/1000;
c53e6443 sago007 2012-04-17 11:04 606
				seconds = ((2*60*1000-(abs((int)SDL_GetTicks()-(int)theGame2->GetGameStartedAt())))%(60*1000))/1000;
c53e6443 sago007 2012-04-17 11:04 607
			}
ecef5838 sago007 2015-11-24 20:01 608
			else {
c53e6443 sago007 2012-04-17 11:04 609
				minutes = ((abs((int)SDL_GetTicks()-(int)theGame2->GetGameStartedAt())))/60/1000;
c53e6443 sago007 2012-04-17 11:04 610
				seconds = (((abs((int)SDL_GetTicks()-(int)theGame2->GetGameStartedAt())))%(60*1000))/1000;
c53e6443 sago007 2012-04-17 11:04 611
			}
acd79baf sago007 2015-11-22 19:37 612
			if (theGame2->isGameOver()) {
acd79baf sago007 2015-11-22 19:37 613
				minutes=0;
acd79baf sago007 2015-11-22 19:37 614
			}
acd79baf sago007 2015-11-22 19:37 615
			if (theGame2->isGameOver()) {
acd79baf sago007 2015-11-22 19:37 616
				seconds=0;
acd79baf sago007 2015-11-22 19:37 617
			}
ecef5838 sago007 2015-11-24 20:01 618
			if (seconds>9) {
549ecfae sago007 2016-10-09 08:37 619
				strHolder = std::to_string(minutes)+":"+std::to_string(seconds);
acd79baf sago007 2015-11-22 19:37 620
			}
ecef5838 sago007 2015-11-24 20:01 621
			else {
549ecfae sago007 2016-10-09 08:37 622
				strHolder = std::to_string(minutes)+":0"+std::to_string(seconds);
acd79baf sago007 2015-11-22 19:37 623
			}
c53e6443 sago007 2012-04-17 11:04 624
		}
ecef5838 sago007 2015-11-24 20:01 625
		else {
c53e6443 sago007 2012-04-17 11:04 626
			int minutes = (abs((int)SDL_GetTicks()-(int)theGame2->GetGameStartedAt()))/60/1000;
c53e6443 sago007 2012-04-17 11:04 627
			int seconds = (abs((int)SDL_GetTicks()-(int)theGame2->GetGameStartedAt())%(60*1000))/1000;
acd79baf sago007 2015-11-22 19:37 628
			if (theGame2->isGameOver()) {
acd79baf sago007 2015-11-22 19:37 629
				minutes=(theGame2->GetGameEndedAt()/1000/60)%100;
acd79baf sago007 2015-11-22 19:37 630
			}
acd79baf sago007 2015-11-22 19:37 631
			if (theGame2->isGameOver()) {
acd79baf sago007 2015-11-22 19:37 632
				seconds=(theGame2->GetGameEndedAt()/1000)%60;
acd79baf sago007 2015-11-22 19:37 633
			}
ecef5838 sago007 2015-11-24 20:01 634
			if (seconds>9) {
549ecfae sago007 2016-10-09 08:37 635
				strHolder = std::to_string(minutes)+":"+std::to_string(seconds);
acd79baf sago007 2015-11-22 19:37 636
			}
ecef5838 sago007 2015-11-24 20:01 637
			else {
549ecfae sago007 2016-10-09 08:37 638
				strHolder = std::to_string(minutes)+":0"+std::to_string(seconds);
acd79baf sago007 2015-11-22 19:37 639
			}
c53e6443 sago007 2012-04-17 11:04 640
		}
0b8fb137 sago007 2018-03-31 19:53 641
		player2time.SetText(strHolder);
a11d40ec sago007 2018-03-31 18:04 642
		player2time.Draw(globalData.screen, theGame2->GetTopX()+310,theGame2->GetTopY()+150);
549ecfae sago007 2016-10-09 08:37 643
		strHolder = std::to_string(theGame2->GetChains());
0b8fb137 sago007 2018-03-31 19:53 644
		player2chain.SetText(strHolder);
a11d40ec sago007 2018-03-31 18:04 645
		player2chain.Draw(globalData.screen, theGame2->GetTopX()+310,theGame2->GetTopY()+200);
549ecfae sago007 2016-10-09 08:37 646
		strHolder = std::to_string(theGame2->GetSpeedLevel());
0b8fb137 sago007 2018-03-31 19:53 647
		player2speed.SetText(strHolder);
a11d40ec sago007 2018-03-31 18:04 648
		player2speed.Draw(globalData.screen, theGame2->GetTopX()+310,theGame2->GetTopY()+250);
c53e6443 sago007 2012-04-17 11:04 649
	}
c53e6443 sago007 2012-04-17 11:04 650
	//player2 finnish
89c4a3a6 sago007 2008-08-29 14:32 651
89c4a3a6 sago007 2008-08-29 14:32 652
7fdb4e65 sago007 2016-04-28 17:54 653
	//draw exit
72383a9e sago007 2016-10-30 11:56 654
	bExit.Draw(globalData.screen,SDL_GetTicks(), xsize-bExitOffset, ysize-bExitOffset);
c53e6443 sago007 2012-04-17 11:04 655
	DrawBalls();
89c4a3a6 sago007 2008-08-29 14:32 656
1572de2b sago007 2008-09-11 18:15 657
#if DEBUG
9feb24fa sago007 2018-03-31 17:42 658
	static sago::SagoTextField fpsField;
9feb24fa sago007 2018-03-31 17:42 659
	sagoTextSetBlueFont(fpsField);
c53e6443 sago007 2012-04-17 11:04 660
	Frames++;
ecef5838 sago007 2015-11-24 20:01 661
	if (SDL_GetTicks() >= Ticks + 1000) {
acd79baf sago007 2015-11-22 19:37 662
		if (Frames > 999) {
acd79baf sago007 2015-11-22 19:37 663
			Frames=999;
acd79baf sago007 2015-11-22 19:37 664
		}
f306e3ca sago007 2015-12-04 21:16 665
		snprintf(FPS, sizeof(FPS), "%lu fps", Frames);
c53e6443 sago007 2012-04-17 11:04 666
		Frames = 0;
c53e6443 sago007 2012-04-17 11:04 667
		Ticks = SDL_GetTicks();
c53e6443 sago007 2012-04-17 11:04 668
	}
9feb24fa sago007 2018-03-31 17:42 669
	fpsField.SetText(FPS);
9feb24fa sago007 2018-03-31 17:42 670
	fpsField.Draw(globalData.screen, 800, 4);
89c4a3a6 sago007 2008-08-29 14:32 671
#endif
89c4a3a6 sago007 2008-08-29 14:32 672
}
89c4a3a6 sago007 2008-08-29 14:32 673
ecef5838 sago007 2015-11-24 20:01 674
static BlockGameSdl* player1;
ecef5838 sago007 2015-11-24 20:01 675
static BlockGameSdl* player2;
30f910dd sago007 2010-11-10 21:02 676
dfdb7d66 sago007 2015-11-29 19:21 677
static bool registerEndlessHighscore = false;
dfdb7d66 sago007 2015-11-29 19:21 678
static bool registerTTHighscorePlayer1 = false;
dfdb7d66 sago007 2015-11-29 19:21 679
static bool registerTTHighscorePlayer2 = false;
76514d10 sago007 2016-06-05 20:25 680
static bool saveReplay = false;
dfdb7d66 sago007 2015-11-29 19:21 681
4379ec19 Poul Sander 2019-01-30 18:27 682
/**
4379ec19 Poul Sander 2019-01-30 18:27 683
 * startSpeed is a value from 0 to 4
4379ec19 Poul Sander 2019-01-30 18:27 684
 * */
4379ec19 Poul Sander 2019-01-30 18:27 685
static void StartSinglePlayerEndless(int startSpeed) {
c53e6443 sago007 2012-04-17 11:04 686
	//1 player - endless
5159dd90 sago007 2016-02-21 15:50 687
	BlockGameStartInfo startInfo;
5159dd90 sago007 2016-02-21 15:50 688
	startInfo.ticks = SDL_GetTicks();
917dac61 sago007 2016-02-21 20:01 689
	startInfo.startBlocks = startInfo.ticks;
4379ec19 Poul Sander 2019-01-30 18:27 690
	startInfo.gameSpeed = startSpeed;
5159dd90 sago007 2016-02-21 15:50 691
	player1->NewGame(startInfo);
c53e6443 sago007 2012-04-17 11:04 692
	twoPlayers =false;
b58caa17 sago007 2016-06-04 10:32 693
	BlockGameAction a;
b58caa17 sago007 2016-06-04 10:32 694
	a.action = BlockGameAction::Action::SET_GAME_OVER;
b58caa17 sago007 2016-06-04 10:32 695
	a.tick = startInfo.ticks;
b58caa17 sago007 2016-06-04 10:32 696
	player2->DoAction(a);
72383a9e sago007 2016-10-30 11:56 697
	player1->name = globalData.player1name;
72383a9e sago007 2016-10-30 11:56 698
	player2->name = globalData.player2name;
dfdb7d66 sago007 2015-11-29 19:21 699
	registerEndlessHighscore = true;
30f910dd sago007 2010-11-10 21:02 700
}
30f910dd sago007 2010-11-10 21:02 701
ecef5838 sago007 2015-11-24 20:01 702
static void StartSinglePlayerTimeTrial() {
5159dd90 sago007 2016-02-21 15:50 703
	BlockGameStartInfo startInfo;
5159dd90 sago007 2016-02-21 15:50 704
	startInfo.ticks = SDL_GetTicks();
5159dd90 sago007 2016-02-21 15:50 705
	startInfo.timeTrial = true;
5159dd90 sago007 2016-02-21 15:50 706
	player1->NewGame(startInfo);
c53e6443 sago007 2012-04-17 11:04 707
	twoPlayers =false;
b58caa17 sago007 2016-06-04 10:32 708
	BlockGameAction a;
b58caa17 sago007 2016-06-04 10:32 709
	a.action = BlockGameAction::Action::SET_GAME_OVER;
b58caa17 sago007 2016-06-04 10:32 710
	a.tick = startInfo.ticks;
b58caa17 sago007 2016-06-04 10:32 711
	player2->DoAction(a);
c53e6443 sago007 2012-04-17 11:04 712
	//vsMode = false;
72383a9e sago007 2016-10-30 11:56 713
	player1->name = globalData.player1name;
72383a9e sago007 2016-10-30 11:56 714
	player2->name = globalData.player2name;
dfdb7d66 sago007 2015-11-29 19:21 715
	registerTTHighscorePlayer1 = true;
76514d10 sago007 2016-06-05 20:25 716
	saveReplay = true;
4f1d27f1 sago007 2011-03-18 15:53 717
}
4f1d27f1 sago007 2011-03-18 15:53 718
f7c1a51d Poul Sander 2016-09-07 21:12 719
static int StartSinglePlayerPuzzle() {
b256fda5 sago007 2016-02-21 16:21 720
	BlockGameStartInfo startInfo;
b256fda5 sago007 2016-02-21 16:21 721
	startInfo.ticks = SDL_GetTicks();
b256fda5 sago007 2016-02-21 16:21 722
	startInfo.puzzleMode = true;
b256fda5 sago007 2016-02-21 16:21 723
	startInfo.level = PuzzleLevelSelect(0);
b256fda5 sago007 2016-02-21 16:21 724
	if (startInfo.level == -1) {
67a32395 sago007 2012-04-19 18:40 725
		return 1;
acd79baf sago007 2015-11-22 19:37 726
	}
b256fda5 sago007 2016-02-21 16:21 727
	player1->NewGame(startInfo);
72383a9e sago007 2016-10-30 11:56 728
	DrawBackground(globalData.screen);
c53e6443 sago007 2012-04-17 11:04 729
	twoPlayers = false;
b58caa17 sago007 2016-06-04 10:32 730
	BlockGameAction a;
b58caa17 sago007 2016-06-04 10:32 731
	a.action = BlockGameAction::Action::SET_GAME_OVER;
b58caa17 sago007 2016-06-04 10:32 732
	a.tick = startInfo.ticks;
b58caa17 sago007 2016-06-04 10:32 733
	player2->DoAction(a);
72383a9e sago007 2016-10-30 11:56 734
	player1->name = globalData.player1name;
72383a9e sago007 2016-10-30 11:56 735
	player2->name = globalData.player2name;
67a32395 sago007 2012-04-19 18:40 736
	return 0;
b7590374 sago007 2011-05-19 16:15 737
}
b7590374 sago007 2011-05-19 16:15 738
4f1d27f1 sago007 2011-03-18 15:53 739
ecef5838 sago007 2015-11-24 20:01 740
static void StarTwoPlayerTimeTrial() {
5159dd90 sago007 2016-02-21 15:50 741
	BlockGameStartInfo startInfo;
5159dd90 sago007 2016-02-21 15:50 742
	startInfo.ticks = SDL_GetTicks();
5159dd90 sago007 2016-02-21 15:50 743
	startInfo.timeTrial = true;
6d6b2788 sago007 2016-02-21 18:51 744
	BlockGameStartInfo startInfo2 = startInfo;
dfdb7d66 sago007 2015-11-29 19:21 745
	registerTTHighscorePlayer1 = true;
dfdb7d66 sago007 2015-11-29 19:21 746
	registerTTHighscorePlayer2 = true;
ecef5838 sago007 2015-11-24 20:01 747
	if (player1AI) {
6d6b2788 sago007 2016-02-21 18:51 748
		startInfo.AI = true;
6d6b2788 sago007 2016-02-21 18:51 749
		startInfo.level = player1AIlevel;
dfdb7d66 sago007 2015-11-29 19:21 750
		registerTTHighscorePlayer1 = false;
acd79baf sago007 2015-11-22 19:37 751
	}
ecef5838 sago007 2015-11-24 20:01 752
	if (player2AI) {
6d6b2788 sago007 2016-02-21 18:51 753
		startInfo2.AI = true;
6d6b2788 sago007 2016-02-21 18:51 754
		startInfo2.level = player2AIlevel;
dfdb7d66 sago007 2015-11-29 19:21 755
		registerTTHighscorePlayer2 = false;
acd79baf sago007 2015-11-22 19:37 756
	}
3e7b8813 sago007 2016-02-22 19:45 757
	startInfo.gameSpeed = player1Speed;
3e7b8813 sago007 2016-02-22 19:45 758
	startInfo2.gameSpeed = player2Speed;
3e7b8813 sago007 2016-02-22 19:45 759
	startInfo.handicap = player1handicap;
3e7b8813 sago007 2016-02-22 19:45 760
	startInfo2.handicap = player2handicap;
6d6b2788 sago007 2016-02-21 18:51 761
	player1->NewGame(startInfo);
6d6b2788 sago007 2016-02-21 18:51 762
	player2->NewGame(startInfo2);
6d6b2788 sago007 2016-02-21 18:51 763
	twoPlayers = true;
72383a9e sago007 2016-10-30 11:56 764
	player1->name = globalData.player1name;
72383a9e sago007 2016-10-30 11:56 765
	player2->name = globalData.player2name;
4f1d27f1 sago007 2011-03-18 15:53 766
}
4f1d27f1 sago007 2011-03-18 15:53 767
ecef5838 sago007 2015-11-24 20:01 768
static void StartTwoPlayerVs() {
c53e6443 sago007 2012-04-17 11:04 769
	//2 player - VsMode
1e5aff60 sago007 2016-02-21 18:23 770
	BlockGameStartInfo startInfo;
1e5aff60 sago007 2016-02-21 18:23 771
	startInfo.ticks = SDL_GetTicks();
1e5aff60 sago007 2016-02-21 18:23 772
	startInfo.vsMode = true;
917dac61 sago007 2016-02-21 20:01 773
	startInfo.startBlocks = startInfo.ticks;
6d6b2788 sago007 2016-02-21 18:51 774
	BlockGameStartInfo startInfo2 = startInfo;
6d6b2788 sago007 2016-02-21 18:51 775
	if (player1AI) {
6d6b2788 sago007 2016-02-21 18:51 776
		startInfo.AI = true;
6d6b2788 sago007 2016-02-21 18:51 777
		startInfo.level = player1AIlevel;
6d6b2788 sago007 2016-02-21 18:51 778
	}
6d6b2788 sago007 2016-02-21 18:51 779
	if (player2AI) {
6d6b2788 sago007 2016-02-21 18:51 780
		startInfo2.AI = true;
6d6b2788 sago007 2016-02-21 18:51 781
		startInfo2.level = player2AIlevel;
6d6b2788 sago007 2016-02-21 18:51 782
	}
3e7b8813 sago007 2016-02-22 19:45 783
	startInfo.gameSpeed = player1Speed;
3e7b8813 sago007 2016-02-22 19:45 784
	startInfo2.gameSpeed = player2Speed;
3e7b8813 sago007 2016-02-22 19:45 785
	startInfo.handicap = player1handicap;
3e7b8813 sago007 2016-02-22 19:45 786
	startInfo2.handicap = player2handicap;
1e5aff60 sago007 2016-02-21 18:23 787
	player1->NewGame(startInfo);
6d6b2788 sago007 2016-02-21 18:51 788
	player2->NewGame(startInfo2);
c53e6443 sago007 2012-04-17 11:04 789
	twoPlayers = true;
72383a9e sago007 2016-10-30 11:56 790
	player1->name = globalData.player1name;
72383a9e sago007 2016-10-30 11:56 791
	player2->name = globalData.player2name;
4f1d27f1 sago007 2011-03-18 15:53 792
}
4f1d27f1 sago007 2011-03-18 15:53 793
7d649ca8 sago007 2016-10-02 16:43 794
static void MoveBlockGameSdls( BlockGameSdl& game1, BlockGameSdl& game2 ) {
88f0d0ab sago007 2017-03-02 19:05 795
	game1.SetTopXY(50, globalData.ysize/2-284);
88f0d0ab sago007 2017-03-02 19:05 796
	game2.SetTopXY(globalData.xsize-500, globalData.ysize/2-284);
7d649ca8 sago007 2016-10-02 16:43 797
}
7d649ca8 sago007 2016-10-02 16:43 798
647f12da sago007 2016-09-12 20:07 799
struct globalConfig {
647f12da sago007 2016-09-12 20:07 800
	string savepath;
647f12da sago007 2016-09-12 20:07 801
	vector<string> search_paths;
647f12da sago007 2016-09-12 20:07 802
	string puzzleName;
08c44684 sago007 2016-10-30 09:19 803
	bool allowResize = true;
68313e95 sago007 2016-09-28 16:52 804
	bool autoScale = true;
9076d3cb sago007 2017-04-22 10:50 805
	bool softwareRenderer = false;
647f12da sago007 2016-09-12 20:07 806
};
647f12da sago007 2016-09-12 20:07 807
647f12da sago007 2016-09-12 20:07 808
static void ParseArguments(int argc, char* argv[], globalConfig& conf) {
647f12da sago007 2016-09-12 20:07 809
	int consoleWidth = boost::program_options::options_description::m_default_line_length;
647f12da sago007 2016-09-12 20:07 810
	const char* columnsEnv = getenv("COLUMNS"); // Allows using "COLUMNS=300 help2man" for generating the man page without bad line breaks.
647f12da sago007 2016-09-12 20:07 811
	if (columnsEnv) {
647f12da sago007 2016-09-12 20:07 812
		consoleWidth = sago::StrToLong(columnsEnv);
647f12da sago007 2016-09-12 20:07 813
	}
647f12da sago007 2016-09-12 20:07 814
	const char* commandname = "blockattack";
647f12da sago007 2016-09-12 20:07 815
	if (argv[0]) {
647f12da sago007 2016-09-12 20:07 816
		//NULL on Windows
647f12da sago007 2016-09-12 20:07 817
		commandname = argv[0];
647f12da sago007 2016-09-12 20:07 818
	}
647f12da sago007 2016-09-12 20:07 819
	boost::program_options::options_description desc("Options", consoleWidth);
647f12da sago007 2016-09-12 20:07 820
	desc.add_options()
647f12da sago007 2016-09-12 20:07 821
	("help,h", "Displays this message")
647f12da sago007 2016-09-12 20:07 822
	("version", "Display the version information")
647f12da sago007 2016-09-12 20:07 823
	("config,c", boost::program_options::value<vector<string> >(), "Read a config file with the values. Can be given multiple times")
647f12da sago007 2016-09-12 20:07 824
	("nosound", "Disables the sound. Can be used if sound errors prevents you from starting")
647f12da sago007 2016-09-12 20:07 825
	("priority", "Causes the game to not sleep between frames.")
9076d3cb sago007 2017-04-22 10:50 826
	("software-renderer", "Asks SDL2 to use software renderer")
647f12da sago007 2016-09-12 20:07 827
	("verbose-basic", "Enables basic verbose messages")
647f12da sago007 2016-09-12 20:07 828
	("verbose-game-controller", "Enables verbose messages regarding controllers")
647f12da sago007 2016-09-12 20:07 829
	("print-search-path", "Prints the search path and quits")
4cd8ea09 Poul Sander 2016-10-05 20:04 830
	("no-auto-scale", "Do not automatically auto scale")
647f12da sago007 2016-09-12 20:07 831
	("puzzle-level-file", boost::program_options::value<string>(), "Sets the default puzzle file to load")
647f12da sago007 2016-09-12 20:07 832
	("puzzle-single-level", boost::program_options::value<int>(), "Start the specific puzzle level directly")
440c4aa8 Poul Sander 2017-03-29 17:21 833
#ifdef REPLAY_IMPLEMENTED
092bdebe sago007 2017-03-19 14:39 834
	("play-replay", boost::program_options::value<string>(), "Start a replay")
440c4aa8 Poul Sander 2017-03-29 17:21 835
#endif
647f12da sago007 2016-09-12 20:07 836
	("bind-text-domain", boost::program_options::value<string>(), SPrintStringF("Overwrites the bind text domain used for finding translations. "
092bdebe sago007 2017-03-19 14:39 837
	        "Default: \"%s\"", LOCALEDIR).c_str())
647f12da sago007 2016-09-12 20:07 838
	("homepath", boost::program_options::value<string>(), SPrintStringF("Set the home folder where settings are saved. The directory will be created if it does not exist."
092bdebe sago007 2017-03-19 14:39 839
	        " Default: \"%s\"", getPathToSaveFiles().c_str()).c_str())
647f12da sago007 2016-09-12 20:07 840
647f12da sago007 2016-09-12 20:07 841
	;
647f12da sago007 2016-09-12 20:07 842
	boost::program_options::variables_map vm;
647f12da sago007 2016-09-12 20:07 843
	try {
647f12da sago007 2016-09-12 20:07 844
		boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
647f12da sago007 2016-09-12 20:07 845
		boost::program_options::notify(vm);
647f12da sago007 2016-09-12 20:07 846
	}
647f12da sago007 2016-09-12 20:07 847
	catch (exception& e) {
647f12da sago007 2016-09-12 20:07 848
		cerr << e.what() << "\n";
647f12da sago007 2016-09-12 20:07 849
		cerr << desc << "\n";
647f12da sago007 2016-09-12 20:07 850
		throw;
647f12da sago007 2016-09-12 20:07 851
	}
647f12da sago007 2016-09-12 20:07 852
	if (vm.count("config")) {
647f12da sago007 2016-09-12 20:07 853
		vector<string> config_filenames = vm["config"].as<vector<string> >();
647f12da sago007 2016-09-12 20:07 854
		for ( const string& s : config_filenames) {
647f12da sago007 2016-09-12 20:07 855
			std::ifstream config_file(s);
647f12da sago007 2016-09-12 20:07 856
			store(parse_config_file(config_file, desc), vm);
647f12da sago007 2016-09-12 20:07 857
			notify(vm);
647f12da sago007 2016-09-12 20:07 858
		}
647f12da sago007 2016-09-12 20:07 859
	}
647f12da sago007 2016-09-12 20:07 860
	if (vm.count("bind-text-domain")) {
647f12da sago007 2016-09-12 20:07 861
		string s = vm["bind-text-domain"].as<string>();
647f12da sago007 2016-09-12 20:07 862
		bindtextdomain (PACKAGE, s.c_str());
647f12da sago007 2016-09-12 20:07 863
	}
647f12da sago007 2016-09-12 20:07 864
	if (vm.count("homepath")) {
647f12da sago007 2016-09-12 20:07 865
		string s = vm["homepath"].as<string>();
647f12da sago007 2016-09-12 20:07 866
		setPathToSaveFiles(s);
647f12da sago007 2016-09-12 20:07 867
		conf.savepath = getPathToSaveFiles();
647f12da sago007 2016-09-12 20:07 868
	}
647f12da sago007 2016-09-12 20:07 869
	if (vm.count("help")) {
647f12da sago007 2016-09-12 20:07 870
		cout << SPrintStringF("Block Attack - Rise of the blocks %s\n\n"
092bdebe sago007 2017-03-19 14:39 871
		                      "Block Attack - Rise of the Blocks is a puzzle/blockfall game inspired by Tetris Attack for the SNES.\n\n"
092bdebe sago007 2017-03-19 14:39 872
		                      "%s\n\n", VERSION_NUMBER, "www.blockattack.net");
647f12da sago007 2016-09-12 20:07 873
		cout << "Usage: "<< commandname << " [OPTION]..." << "\n";
647f12da sago007 2016-09-12 20:07 874
		cout << desc << "\n";
647f12da sago007 2016-09-12 20:07 875
		cout << "Examples:" << "\n";
647f12da sago007 2016-09-12 20:07 876
		cout << "\tblockattack          \tStart the game normally" << "\n";
647f12da sago007 2016-09-12 20:07 877
		cout << "\tblockattack --nosound\tStart the game without sound. Can be used if sound problems prevents the game from starting" << "\n";
3347e1e2 Poul Sander 2018-05-06 13:40 878
		cout << "\tblockattack --puzzle-level-file puzzle.levels --puzzle-single-level 3\tStart the game with the default puzzles in level 3" << "\n";
3347e1e2 Poul Sander 2018-05-06 13:40 879
		cout << "\tblockattack --bind-text-domain /dev/null\t Disables translations" << "\n";
647f12da sago007 2016-09-12 20:07 880
		cout << "\n";
647f12da sago007 2016-09-12 20:07 881
		cout << "Report bugs to the issue tracker here: <https://github.com/blockattack/blockattack-game/issues>" << "\n";
647f12da sago007 2016-09-12 20:07 882
		exit(0);
647f12da sago007 2016-09-12 20:07 883
	}
647f12da sago007 2016-09-12 20:07 884
	if (vm.count("version")) {
647f12da sago007 2016-09-12 20:07 885
		cout << "blockattack " << VERSION_NUMBER << "\n";
647f12da sago007 2016-09-12 20:07 886
		cout << "\n";
647f12da sago007 2016-09-12 20:07 887
		cout << "Copyright (C) 2005-2016 Poul Sander" << "\n";
647f12da sago007 2016-09-12 20:07 888
		cout << "License GPLv2+: GNU GPL version 2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html> or later <http://gnu.org/licenses/gpl.html>" << "\n";
647f12da sago007 2016-09-12 20:07 889
		cout << "This is free software: you are free to change and redistribute it." << "\n";
647f12da sago007 2016-09-12 20:07 890
		cout << "There is NO WARRANTY, to the extent permitted by law." << "\n";
647f12da sago007 2016-09-12 20:07 891
		exit(0);
647f12da sago007 2016-09-12 20:07 892
	}
647f12da sago007 2016-09-12 20:07 893
	if (vm.count("nosound")) {
72383a9e sago007 2016-10-30 11:56 894
		globalData.NoSound = true;
647f12da sago007 2016-09-12 20:07 895
	}
647f12da sago007 2016-09-12 20:07 896
	if (vm.count("priority")) {
72383a9e sago007 2016-10-30 11:56 897
		globalData.highPriority = true;
647f12da sago007 2016-09-12 20:07 898
	}
9076d3cb sago007 2017-04-22 10:50 899
	if (vm.count("software-renderer")) {
9076d3cb sago007 2017-04-22 10:50 900
		conf.softwareRenderer = true;
9076d3cb sago007 2017-04-22 10:50 901
	}
647f12da sago007 2016-09-12 20:07 902
	if (vm.count("verbose-basic")) {
72383a9e sago007 2016-10-30 11:56 903
		globalData.verboseLevel++;
647f12da sago007 2016-09-12 20:07 904
	}
647f12da sago007 2016-09-12 20:07 905
	if (vm.count("verbose-game-controller")) {
647f12da sago007 2016-09-12 20:07 906
		GameControllerSetVerbose(true);
647f12da sago007 2016-09-12 20:07 907
	}
647f12da sago007 2016-09-12 20:07 908
	if (vm.count("print-search-path")) {
647f12da sago007 2016-09-12 20:07 909
		for (const string& s : conf.search_paths) {
647f12da sago007 2016-09-12 20:07 910
			cout << s << "\n";
647f12da sago007 2016-09-12 20:07 911
		}
647f12da sago007 2016-09-12 20:07 912
		cout << conf.savepath << "\n";
647f12da sago007 2016-09-12 20:07 913
		exit(0);
647f12da sago007 2016-09-12 20:07 914
	}
647f12da sago007 2016-09-12 20:07 915
	if (vm.count("puzzle-single-level")) {
647f12da sago007 2016-09-12 20:07 916
		singlePuzzle = true;
647f12da sago007 2016-09-12 20:07 917
		singlePuzzleNr = vm["puzzle-single-level"].as<int>();
647f12da sago007 2016-09-12 20:07 918
	}
092bdebe sago007 2017-03-19 14:39 919
	if (vm.count("no-auto-scale")) {
68313e95 sago007 2016-09-28 16:52 920
		conf.autoScale = false;
68313e95 sago007 2016-09-28 16:52 921
	}
647f12da sago007 2016-09-12 20:07 922
	if (vm.count("puzzle-level-file")) {
647f12da sago007 2016-09-12 20:07 923
		conf.puzzleName = vm["puzzle-level-file"].as<string>();
647f12da sago007 2016-09-12 20:07 924
	}
092bdebe sago007 2017-03-19 14:39 925
	if (vm.count("play-replay")) {
092bdebe sago007 2017-03-19 14:39 926
		globalData.replayArgument = vm["play-replay"].as<string>();
092bdebe sago007 2017-03-19 14:39 927
	}
092bdebe sago007 2017-03-19 14:39 928
647f12da sago007 2016-09-12 20:07 929
}
647f12da sago007 2016-09-12 20:07 930
f31fade1 sago007 2016-02-06 21:37 931
//Warning: the arguments to main must be "int argc, char* argv[]" NO CONST! or SDL_main will fail to find it
f31fade1 sago007 2016-02-06 21:37 932
int main(int argc, char* argv[]) {
c3fc09e9 sago007 2016-01-22 18:31 933
	try {
335635ec sago007 2016-02-06 13:51 934
		//Init the file system abstraction layer
335635ec sago007 2016-02-06 13:51 935
		PHYSFS_init(argv[0]);
647f12da sago007 2016-09-12 20:07 936
		globalConfig config;
647f12da sago007 2016-09-12 20:07 937
		config.puzzleName = "puzzle.levels";
647f12da sago007 2016-09-12 20:07 938
		FsSearchParthMainAppend(config.search_paths);
647f12da sago007 2016-09-12 20:07 939
		config.savepath = getPathToSaveFiles();
72383a9e sago007 2016-10-30 11:56 940
		globalData.highPriority = false;   //if true the game will take most resources, but increase framerate.
72383a9e sago007 2016-10-30 11:56 941
		globalData.bFullscreen = false;
335635ec sago007 2016-02-06 13:51 942
		//Set default Config variables:
335635ec sago007 2016-02-06 13:51 943
		setlocale (LC_ALL, "");
335635ec sago007 2016-02-06 13:51 944
		bindtextdomain (PACKAGE, LOCALEDIR);
5f870c10 sago007 2016-02-07 09:38 945
		bind_textdomain_codeset(PACKAGE, "utf-8");
335635ec sago007 2016-02-06 13:51 946
		textdomain (PACKAGE);
647f12da sago007 2016-09-12 20:07 947
		ParseArguments(argc, argv, config);
77531130 Poul Sander 2016-02-17 18:42 948
		OsCreateSaveFolder();
647f12da sago007 2016-09-12 20:07 949
		PhysFsSetSearchPath(config.search_paths, config.savepath);
7261f4dd Poul Sander 2019-03-13 18:10 950
		//Os create folders must be after the parameters because they can change the home folder
cd724e02 sago007 2016-02-13 16:04 951
		PhysFsCreateFolders();
ccee1ad0 Poul Sander 2019-03-13 18:39 952
		bool gameShutdownProperly = true;
ccee1ad0 Poul Sander 2019-03-13 18:39 953
		if (sago::FileExists("gameRunning")) {
ccee1ad0 Poul Sander 2019-03-13 18:39 954
			gameShutdownProperly = false;
ccee1ad0 Poul Sander 2019-03-13 18:39 955
		}
7261f4dd Poul Sander 2019-03-13 18:10 956
		sago::WriteFileContent("gameRunning", "Started");
72383a9e sago007 2016-10-30 11:56 957
		globalData.SoundEnabled = true;
72383a9e sago007 2016-10-30 11:56 958
		globalData.MusicEnabled = true;
335635ec sago007 2016-02-06 13:51 959
		twoPlayers = false; //true if two players splitscreen
335635ec sago007 2016-02-06 13:51 960
		drawBalls = true;
335635ec sago007 2016-02-06 13:51 961
		puzzleLoaded = false;
335635ec sago007 2016-02-06 13:51 962
		theBallManager = BallManager();
335635ec sago007 2016-02-06 13:51 963
		theExplosionManager = ExplosionManager();
647f12da sago007 2016-09-12 20:07 964
		PuzzleSetName(config.puzzleName);
335635ec sago007 2016-02-06 13:51 965
		//Init SDL
335635ec sago007 2016-02-06 13:51 966
		if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
335635ec sago007 2016-02-06 13:51 967
			sago::SagoFatalErrorF("Unable to init SDL: %s", SDL_GetError());
335635ec sago007 2016-02-06 13:51 968
		}
d0c62e33 sago007 2016-04-02 15:18 969
		if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER ) != 0) {
161a010c sago007 2016-05-06 14:00 970
			cerr << "Warning: Game controller failed to initialize. Reason: " << SDL_GetError() << "\n";
c7f2082f sago007 2016-03-13 11:48 971
		}
826cf176 sago007 2016-03-12 10:53 972
		InitGameControllers();
335635ec sago007 2016-02-06 13:51 973
		TTF_Init();
335635ec sago007 2016-02-06 13:51 974
		atexit(SDL_Quit);       //quits SDL when the game stops for some reason (like you hit exit or Esc)
c310f8a5 sago007 2017-03-25 16:05 975
		globalData.theTextManager = TextManager();
89c4a3a6 sago007 2008-08-29 14:32 976
335635ec sago007 2016-02-06 13:51 977
		//Open Audio
72383a9e sago007 2016-10-30 11:56 978
		if (!globalData.NoSound) {
335635ec sago007 2016-02-06 13:51 979
			//If sound has not been disabled, then load the sound system
335635ec sago007 2016-02-06 13:51 980
			if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048) < 0) {
161a010c sago007 2016-05-06 14:00 981
				cerr << "Warning: Couldn't set 44100 Hz 16-bit audio - Reason: " << SDL_GetError() << "\n"
161a010c sago007 2016-05-06 14:00 982
				     << "Sound will be disabled!" << "\n";
72383a9e sago007 2016-10-30 11:56 983
				globalData.NoSound = true; //Tries to stop all sound from playing/loading
335635ec sago007 2016-02-06 13:51 984
			}
c53e6443 sago007 2012-04-17 11:04 985
		}
c53e6443 sago007 2012-04-17 11:04 986
89c4a3a6 sago007 2008-08-29 14:32 987
72383a9e sago007 2016-10-30 11:56 988
		if (globalData.verboseLevel) {
335635ec sago007 2016-02-06 13:51 989
			//Copyright notice:
161a010c sago007 2016-05-06 14:00 990
			cout << "Block Attack - Rise of the Blocks (" << VERSION_NUMBER << ")" << "\n" << "http://www.blockattack.net" << "\n" << "Copyright 2004-2016 Poul Sander" << "\n" <<
161a010c sago007 2016-05-06 14:00 991
			     "A SDL2 based game (see www.libsdl.org)" << "\n" <<
2b4dbb2b sago007 2016-07-03 18:54 992
			     "The game is available under the GPL, see COPYING for details." << "\n";
161a010c sago007 2016-05-06 14:00 993
			cout << "-------------------------------------------" << "\n";
335635ec sago007 2016-02-06 13:51 994
		}
335635ec sago007 2016-02-06 13:51 995
335635ec sago007 2016-02-06 13:51 996
f204c851 sago007 2016-04-16 14:42 997
		keySettings[player1keys].up= SDLK_UP;
f204c851 sago007 2016-04-16 14:42 998
		keySettings[player1keys].down = SDLK_DOWN;
f204c851 sago007 2016-04-16 14:42 999
		keySettings[player1keys].left = SDLK_LEFT;
f204c851 sago007 2016-04-16 14:42 1000
		keySettings[player1keys].right = SDLK_RIGHT;
f204c851 sago007 2016-04-16 14:42 1001
		keySettings[player1keys].change = SDLK_RCTRL;
f204c851 sago007 2016-04-16 14:42 1002
		keySettings[player1keys].push = SDLK_RSHIFT;
f204c851 sago007 2016-04-16 14:42 1003
f204c851 sago007 2016-04-16 14:42 1004
		keySettings[player2keys].up= SDLK_w;
f204c851 sago007 2016-04-16 14:42 1005
		keySettings[player2keys].down = SDLK_s;
f204c851 sago007 2016-04-16 14:42 1006
		keySettings[player2keys].left = SDLK_a;
f204c851 sago007 2016-04-16 14:42 1007
		keySettings[player2keys].right = SDLK_d;
f204c851 sago007 2016-04-16 14:42 1008
		keySettings[player2keys].change = SDLK_LCTRL;
f204c851 sago007 2016-04-16 14:42 1009
		keySettings[player2keys].push = SDLK_LSHIFT;
335635ec sago007 2016-02-06 13:51 1010
b372dcb1 Poul Sander 2019-02-13 18:53 1011
		globalData.player1name = defaultPlayerName();
72383a9e sago007 2016-10-30 11:56 1012
		globalData.player2name = _("Player 2");
335635ec sago007 2016-02-06 13:51 1013
335635ec sago007 2016-02-06 13:51 1014
		Config* configSettings = Config::getInstance();
335635ec sago007 2016-02-06 13:51 1015
		//configSettings->setString("aNumber"," A string");
335635ec sago007 2016-02-06 13:51 1016
		//configSettings->save();
ede1d46c sago007 2018-06-30 11:18 1017
		int screenHeight = 768;
335635ec sago007 2016-02-06 13:51 1018
		if (configSettings->exists("fullscreen")) { //Test if an configFile exists
72383a9e sago007 2016-10-30 11:56 1019
			globalData.bFullscreen = (bool)configSettings->getInt("fullscreen");
72383a9e sago007 2016-10-30 11:56 1020
			globalData.MusicEnabled = (bool)configSettings->getInt("musicenabled");
72383a9e sago007 2016-10-30 11:56 1021
			globalData.SoundEnabled = (bool)configSettings->getInt("soundenabled");
335635ec sago007 2016-02-06 13:51 1022
335635ec sago007 2016-02-06 13:51 1023
			if (configSettings->exists("sdl2_player1keyup")) {
335635ec sago007 2016-02-06 13:51 1024
				keySettings[0].up = (SDL_Keycode)configSettings->getInt("sdl2_player1keyup");
335635ec sago007 2016-02-06 13:51 1025
			}
335635ec sago007 2016-02-06 13:51 1026
			if (configSettings->exists("sdl2_player1keydown")) {
335635ec sago007 2016-02-06 13:51 1027
				keySettings[0].down = (SDL_Keycode)configSettings->getInt("sdl2_player1keydown");
335635ec sago007 2016-02-06 13:51 1028
			}
335635ec sago007 2016-02-06 13:51 1029
			if (configSettings->exists("sdl2_player1keyleft")) {
335635ec sago007 2016-02-06 13:51 1030
				keySettings[0].left = (SDL_Keycode)configSettings->getInt("sdl2_player1keyleft");
335635ec sago007 2016-02-06 13:51 1031
			}
335635ec sago007 2016-02-06 13:51 1032
			if (configSettings->exists("sdl2_player1keyright")) {
335635ec sago007 2016-02-06 13:51 1033
				keySettings[0].right = (SDL_Keycode)configSettings->getInt("sdl2_player1keyright");
335635ec sago007 2016-02-06 13:51 1034
			}
335635ec sago007 2016-02-06 13:51 1035
			if (configSettings->exists("sdl2_player1keychange")) {
335635ec sago007 2016-02-06 13:51 1036
				keySettings[0].change = (SDL_Keycode)configSettings->getInt("sdl2_player1keychange");
335635ec sago007 2016-02-06 13:51 1037
			}
335635ec sago007 2016-02-06 13:51 1038
			if (configSettings->exists("sdl2_player1keypush")) {
335635ec sago007 2016-02-06 13:51 1039
				keySettings[0].push = (SDL_Keycode)configSettings->getInt("sdl2_player1keypush");
335635ec sago007 2016-02-06 13:51 1040
			}
c53e6443 sago007 2012-04-17 11:04 1041
335635ec sago007 2016-02-06 13:51 1042
			if (configSettings->exists("sdl2_player2keyup")) {
335635ec sago007 2016-02-06 13:51 1043
				keySettings[2].up = (SDL_Keycode)configSettings->getInt("sdl2_player2keyup");
335635ec sago007 2016-02-06 13:51 1044
			}
335635ec sago007 2016-02-06 13:51 1045
			if (configSettings->exists("sdl2_player2keydown")) {
335635ec sago007 2016-02-06 13:51 1046
				keySettings[2].down = (SDL_Keycode)configSettings->getInt("sdl2_player2keydown");
335635ec sago007 2016-02-06 13:51 1047
			}
335635ec sago007 2016-02-06 13:51 1048
			if (configSettings->exists("sdl2_player2keyleft")) {
335635ec sago007 2016-02-06 13:51 1049
				keySettings[2].left = (SDL_Keycode)configSettings->getInt("sdl2_player2keyleft");
335635ec sago007 2016-02-06 13:51 1050
			}
335635ec sago007 2016-02-06 13:51 1051
			if (configSettings->exists("sdl2_player2keyright")) {
335635ec sago007 2016-02-06 13:51 1052
				keySettings[2].right = (SDL_Keycode)configSettings->getInt("sdl2_player2keyright");
335635ec sago007 2016-02-06 13:51 1053
			}
335635ec sago007 2016-02-06 13:51 1054
			if (configSettings->exists("sdl2_player2keychange")) {
335635ec sago007 2016-02-06 13:51 1055
				keySettings[2].change = (SDL_Keycode)configSettings->getInt("sdl2_player2keychange");
335635ec sago007 2016-02-06 13:51 1056
			}
335635ec sago007 2016-02-06 13:51 1057
			if (configSettings->exists("sdl2_player2keypush")) {
335635ec sago007 2016-02-06 13:51 1058
				keySettings[2].push = (SDL_Keycode)configSettings->getInt("sdl2_player2keypush");
335635ec sago007 2016-02-06 13:51 1059
			}
335635ec sago007 2016-02-06 13:51 1060
			if (configSettings->exists("player1name")) {
72383a9e sago007 2016-10-30 11:56 1061
				globalData.player1name = configSettings->getString("player1name");
335635ec sago007 2016-02-06 13:51 1062
			}
335635ec sago007 2016-02-06 13:51 1063
			if (configSettings->exists("player2name")) {
72383a9e sago007 2016-10-30 11:56 1064
				globalData.player2name = configSettings->getString("player2name");
335635ec sago007 2016-02-06 13:51 1065
			}
2d8f4227 sago007 2017-03-02 19:24 1066
			if (configSettings->exists("xsize")) {
2d8f4227 sago007 2017-03-02 19:24 1067
				globalData.xsize = configSettings->getInt("xsize");
2d8f4227 sago007 2017-03-02 19:24 1068
			}
2d8f4227 sago007 2017-03-02 19:24 1069
			if (configSettings->exists("ysize")) {
2d8f4227 sago007 2017-03-02 19:24 1070
				globalData.ysize = configSettings->getInt("ysize");
2d8f4227 sago007 2017-03-02 19:24 1071
			}
ede1d46c sago007 2018-06-30 11:18 1072
			if (configSettings->exists("screenHeight")) {
ede1d46c sago007 2018-06-30 11:18 1073
				screenHeight = configSettings->getInt("screenHeight");
ede1d46c sago007 2018-06-30 11:18 1074
			}
72383a9e sago007 2016-10-30 11:56 1075
			if (globalData.verboseLevel) {
161a010c sago007 2016-05-06 14:00 1076
				cout << "Data loaded from config file" << "\n";
335635ec sago007 2016-02-06 13:51 1077
			}
335635ec sago007 2016-02-06 13:51 1078
		}
335635ec sago007 2016-02-06 13:51 1079
		else {
72383a9e sago007 2016-10-30 11:56 1080
			if (globalData.verboseLevel) {
161a010c sago007 2016-05-06 14:00 1081
				cout << "Unable to load options file, using default values" << "\n";
335635ec sago007 2016-02-06 13:51 1082
			}
335635ec sago007 2016-02-06 13:51 1083
		}
1029dfed Poul Sander 2019-03-13 17:22 1084
		if (configSettings->getInt("always-software")) {
1029dfed Poul Sander 2019-03-13 17:22 1085
			config.softwareRenderer = true;
1029dfed Poul Sander 2019-03-13 17:22 1086
		}
ccee1ad0 Poul Sander 2019-03-13 18:39 1087
		if (!gameShutdownProperly) {
ccee1ad0 Poul Sander 2019-03-13 18:39 1088
			std::cerr << "Game not shotdown. Using software renderer.\n";
ccee1ad0 Poul Sander 2019-03-13 18:39 1089
			config.softwareRenderer = true;
ccee1ad0 Poul Sander 2019-03-13 18:39 1090
		}
c53e6443 sago007 2012-04-17 11:04 1091
335635ec sago007 2016-02-06 13:51 1092
		// "Block Attack - Rise of the Blocks"
82ced7ab sago007 2016-09-16 18:41 1093
		SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");
335635ec sago007 2016-02-06 13:51 1094
		//Open video
28864426 sago007 2016-10-30 09:35 1095
		int createWindowParams = 0;
82ced7ab sago007 2016-09-16 18:41 1096
		if (config.allowResize) {
82ced7ab sago007 2016-09-16 18:41 1097
			createWindowParams |= SDL_WINDOW_RESIZABLE;
82ced7ab sago007 2016-09-16 18:41 1098
		}
abb733e9 Poul Sander 2019-04-24 16:15 1099
		globalData.xsize = 1280;
abb733e9 Poul Sander 2019-04-24 16:15 1100
		globalData.ysize = 720;
cb49f111 sago007 2016-03-16 20:45 1101
		sdlWindow = SDL_CreateWindow("Block Attack - Rise of the Blocks " VERSION_NUMBER,
335635ec sago007 2016-02-06 13:51 1102
		                             SDL_WINDOWPOS_UNDEFINED,
335635ec sago007 2016-02-06 13:51 1103
		                             SDL_WINDOWPOS_UNDEFINED,
abb733e9 Poul Sander 2019-04-24 16:15 1104
		                             (screenHeight)*globalData.xsize/globalData.ysize, screenHeight,
335635ec sago007 2016-02-06 13:51 1105
		                             createWindowParams );
335635ec sago007 2016-02-06 13:51 1106
		dieOnNullptr(sdlWindow, "Unable to create window");
9076d3cb sago007 2017-04-22 10:50 1107
		int rendererFlags = 0;
9076d3cb sago007 2017-04-22 10:50 1108
		if (config.softwareRenderer) {
9076d3cb sago007 2017-04-22 10:50 1109
			rendererFlags |= SDL_RENDERER_SOFTWARE;
9076d3cb sago007 2017-04-22 10:50 1110
		}
9076d3cb sago007 2017-04-22 10:50 1111
		SDL_Renderer* renderer = SDL_CreateRenderer(sdlWindow, -1, rendererFlags);
335635ec sago007 2016-02-06 13:51 1112
		dieOnNullptr(renderer, "Unable to create render");
e17fcc86 sago007 2016-09-28 20:09 1113
		if (config.autoScale) {
abb733e9 Poul Sander 2019-04-24 16:15 1114
			SDL_RenderSetLogicalSize(renderer, globalData.xsize, globalData.ysize);
e17fcc86 sago007 2016-09-28 20:09 1115
			logicalRenderer = true;
82ced7ab sago007 2016-09-16 18:41 1116
		}
80a4a691 sago007 2017-10-25 20:03 1117
		if (globalData.verboseLevel) {
80a4a691 sago007 2017-10-25 20:03 1118
			SDL_RendererInfo info;
80a4a691 sago007 2017-10-25 20:03 1119
			SDL_GetRendererInfo(renderer, &info);
80a4a691 sago007 2017-10-25 20:03 1120
			cout << "Renderer: " << info.name << "\n";
80a4a691 sago007 2017-10-25 20:03 1121
		}
72383a9e sago007 2016-10-30 11:56 1122
		globalData.screen = renderer;
1383de53 sago007 2016-02-21 12:18 1123
		ResetFullscreen();
335635ec sago007 2016-02-06 13:51 1124
		SetSDLIcon(sdlWindow);
c53e6443 sago007 2012-04-17 11:04 1125
72383a9e sago007 2016-10-30 11:56 1126
		if (globalData.verboseLevel) {
161a010c sago007 2016-05-06 14:00 1127
			cout << "Images loaded" << "\n";
c53e6443 sago007 2012-04-17 11:04 1128
		}
89c4a3a6 sago007 2008-08-29 14:32 1129
343fbe43 sago007 2018-03-27 19:53 1130
		BlockGameSdl theGame = BlockGameSdl(50, 100, &globalData.spriteHolder->GetDataHolder());            //creates game objects
343fbe43 sago007 2018-03-27 19:53 1131
		BlockGameSdl theGame2 = BlockGameSdl(globalData.xsize-500, 100, &globalData.spriteHolder->GetDataHolder());
335635ec sago007 2016-02-06 13:51 1132
		player1 = &theGame;
335635ec sago007 2016-02-06 13:51 1133
		player2 = &theGame2;
092bdebe sago007 2017-03-19 14:39 1134
b58caa17 sago007 2016-06-04 10:32 1135
		BlockGameAction a;
b58caa17 sago007 2016-06-04 10:32 1136
		a.action = BlockGameAction::Action::SET_GAME_OVER;
b58caa17 sago007 2016-06-04 10:32 1137
		theGame.DoAction(a);
b58caa17 sago007 2016-06-04 10:32 1138
		theGame2.DoAction(a);
acc78225 sago007 2015-12-29 18:10 1139
b7590374 sago007 2011-05-19 16:15 1140
335635ec sago007 2016-02-06 13:51 1141
		//Takes names from file instead
72383a9e sago007 2016-10-30 11:56 1142
		theGame.name = globalData.player1name;
72383a9e sago007 2016-10-30 11:56 1143
		theGame2.name = globalData.player2name;
b7590374 sago007 2011-05-19 16:15 1144
335635ec sago007 2016-02-06 13:51 1145
		if (singlePuzzle) {
335635ec sago007 2016-02-06 13:51 1146
			LoadPuzzleStages();
b256fda5 sago007 2016-02-21 16:21 1147
			BlockGameStartInfo s;
b256fda5 sago007 2016-02-21 16:21 1148
			s.puzzleMode = true;
b256fda5 sago007 2016-02-21 16:21 1149
			s.level = singlePuzzleNr;
83e4f8fe sago007 2016-02-22 19:22 1150
			s.singlePuzzle = true;
b256fda5 sago007 2016-02-21 16:21 1151
			theGame.NewGame(s);
335635ec sago007 2016-02-06 13:51 1152
		}
72383a9e sago007 2016-10-30 11:56 1153
		DrawBackground(globalData.screen);
7d649ca8 sago007 2016-10-02 16:43 1154
		MoveBlockGameSdls(theGame, theGame2);
72383a9e sago007 2016-10-30 11:56 1155
		DrawEverything(globalData.xsize, globalData.ysize, &theGame, &theGame2);
72383a9e sago007 2016-10-30 11:56 1156
		SDL_RenderPresent(globalData.screen);
7fdb4e65 sago007 2016-04-28 17:54 1157
		if (singlePuzzle) {
de81e07d sago007 2016-07-10 20:22 1158
			runGame(Gametype::Puzzle, singlePuzzleNr);
7fdb4e65 sago007 2016-04-28 17:54 1159
		}
092bdebe sago007 2017-03-19 14:39 1160
		else if (globalData.replayArgument.length()) {
092bdebe sago007 2017-03-19 14:39 1161
			ReplayPlayer rp;
092bdebe sago007 2017-03-19 14:39 1162
			RunGameState(rp);
092bdebe sago007 2017-03-19 14:39 1163
		}
7fdb4e65 sago007 2016-04-28 17:54 1164
		else {
ccee1ad0 Poul Sander 2019-03-13 18:39 1165
			if (!gameShutdownProperly) {
ccee1ad0 Poul Sander 2019-03-13 18:39 1166
				SafeModeMenu();
ccee1ad0 Poul Sander 2019-03-13 18:39 1167
			}
7fdb4e65 sago007 2016-04-28 17:54 1168
			//game loop
7fdb4e65 sago007 2016-04-28 17:54 1169
			MainMenu();
7fdb4e65 sago007 2016-04-28 17:54 1170
		}
b7590374 sago007 2011-05-19 16:15 1171
b7590374 sago007 2011-05-19 16:15 1172
c53e6443 sago007 2012-04-17 11:04 1173
335635ec sago007 2016-02-06 13:51 1174
		//Saves options
2c6a4bae sago007 2018-05-11 15:20 1175
		if (true) {
72383a9e sago007 2016-10-30 11:56 1176
			configSettings->setInt("fullscreen",(int)globalData.bFullscreen);
72383a9e sago007 2016-10-30 11:56 1177
			configSettings->setInt("musicenabled",(int)globalData.MusicEnabled);
72383a9e sago007 2016-10-30 11:56 1178
			configSettings->setInt("soundenabled",(int)globalData.SoundEnabled);
c53e6443 sago007 2012-04-17 11:04 1179
335635ec sago007 2016-02-06 13:51 1180
			configSettings->setInt("sdl2_player1keyup",(int)keySettings[0].up);
335635ec sago007 2016-02-06 13:51 1181
			configSettings->setInt("sdl2_player1keydown",(int)keySettings[0].down);
335635ec sago007 2016-02-06 13:51 1182
			configSettings->setInt("sdl2_player1keyleft",(int)keySettings[0].left);
335635ec sago007 2016-02-06 13:51 1183
			configSettings->setInt("sdl2_player1keyright",(int)keySettings[0].right);
335635ec sago007 2016-02-06 13:51 1184
			configSettings->setInt("sdl2_player1keychange",(int)keySettings[0].change);
335635ec sago007 2016-02-06 13:51 1185
			configSettings->setInt("sdl2_player1keypush",(int)keySettings[0].push);
c53e6443 sago007 2012-04-17 11:04 1186
335635ec sago007 2016-02-06 13:51 1187
			configSettings->setInt("sdl2_player2keyup",(int)keySettings[2].up);
335635ec sago007 2016-02-06 13:51 1188
			configSettings->setInt("sdl2_player2keydown",(int)keySettings[2].down);
335635ec sago007 2016-02-06 13:51 1189
			configSettings->setInt("sdl2_player2keyleft",(int)keySettings[2].left);
335635ec sago007 2016-02-06 13:51 1190
			configSettings->setInt("sdl2_player2keyright",(int)keySettings[2].right);
335635ec sago007 2016-02-06 13:51 1191
			configSettings->setInt("sdl2_player2keychange",(int)keySettings[2].change);
335635ec sago007 2016-02-06 13:51 1192
			configSettings->setInt("sdl2_player2keypush",(int)keySettings[2].push);
c53e6443 sago007 2012-04-17 11:04 1193
72383a9e sago007 2016-10-30 11:56 1194
			configSettings->setString("player1name", globalData.player1name);
72383a9e sago007 2016-10-30 11:56 1195
			configSettings->setString("player2name", globalData.player2name);
ede1d46c sago007 2018-06-30 11:18 1196
			{
ede1d46c sago007 2018-06-30 11:18 1197
				//Store physical height of window
ede1d46c sago007 2018-06-30 11:18 1198
				int height = 0;
ede1d46c sago007 2018-06-30 11:18 1199
				SDL_GetWindowSize(sdlWindow, nullptr, &height);
ede1d46c sago007 2018-06-30 11:18 1200
				configSettings->setInt("screenHeight", height);
2d8f4227 sago007 2017-03-02 19:24 1201
			}
335635ec sago007 2016-02-06 13:51 1202
			configSettings->save();
335635ec sago007 2016-02-06 13:51 1203
		}
c53e6443 sago007 2012-04-17 11:04 1204
335635ec sago007 2016-02-06 13:51 1205
		//calculate uptime:
335635ec sago007 2016-02-06 13:51 1206
		//int hours, mins, secs,
335635ec sago007 2016-02-06 13:51 1207
		commonTime ct = TimeHandler::ms2ct(SDL_GetTicks());
c53e6443 sago007 2012-04-17 11:04 1208
72383a9e sago007 2016-10-30 11:56 1209
		if (globalData.verboseLevel) {
d3f1e001 Poul Sander 2017-03-29 18:17 1210
			cout << SPrintStringF("Block Attack - Rise of the Blocks ran for: %i hours %i mins and %i secs", ct.hours, ct.minutes, ct.seconds) << "\n";
335635ec sago007 2016-02-06 13:51 1211
		}
c53e6443 sago007 2012-04-17 11:04 1212
335635ec sago007 2016-02-06 13:51 1213
		ct = TimeHandler::addTime("totalTime",ct);
72383a9e sago007 2016-10-30 11:56 1214
		if (globalData.verboseLevel) {
161a010c sago007 2016-05-06 14:00 1215
			cout << "Total run time is now: " << ct.days << " days " << ct.hours << " hours " << ct.minutes << " mins and " << ct.seconds << " secs" << "\n";
335635ec sago007 2016-02-06 13:51 1216
		}
c53e6443 sago007 2012-04-17 11:04 1217
335635ec sago007 2016-02-06 13:51 1218
		Stats::getInstance()->save();
335635ec sago007 2016-02-06 13:51 1219
		Config::getInstance()->save();
335635ec sago007 2016-02-06 13:51 1220
	}
335635ec sago007 2016-02-06 13:51 1221
	catch (exception& e) {
c3fc09e9 sago007 2016-01-22 18:31 1222
		sago::SagoFatalError(e.what());
c3fc09e9 sago007 2016-01-22 18:31 1223
	}
7261f4dd Poul Sander 2019-03-13 18:10 1224
	PHYSFS_delete("gameRunning");
59dcb571 sago007 2016-02-14 15:34 1225
	//Close file system Apstraction layer!
59dcb571 sago007 2016-02-14 15:34 1226
	PHYSFS_deinit();
c53e6443 sago007 2012-04-17 11:04 1227
	return 0;
c53e6443 sago007 2012-04-17 11:04 1228
}
b7590374 sago007 2011-05-19 16:15 1229
de81e07d sago007 2016-07-10 20:22 1230
int runGame(Gametype gametype, int level) {
b5321636 sago007 2019-02-09 18:47 1231
	Highscore theTopScoresTimeTrial = Highscore("timetrial", 0.5);
c53e6443 sago007 2012-04-17 11:04 1232
	drawBalls = true;
c53e6443 sago007 2012-04-17 11:04 1233
	puzzleLoaded = false;
343fbe43 sago007 2018-03-27 19:53 1234
	bool bNearDeath = false;  //Play music faster or louder while tru
c53e6443 sago007 2012-04-17 11:04 1235
d8c1f444 sago007 2016-01-31 11:36 1236
	theBallManager = BallManager();
d8c1f444 sago007 2016-01-31 11:36 1237
	theExplosionManager = ExplosionManager();
343fbe43 sago007 2018-03-27 19:53 1238
	BlockGameSdl theGame = BlockGameSdl(50, 100, &globalData.spriteHolder->GetDataHolder());  //creates game objects
343fbe43 sago007 2018-03-27 19:53 1239
	BlockGameSdl theGame2 = BlockGameSdl(globalData.xsize-500, 100, &globalData.spriteHolder->GetDataHolder());
c53e6443 sago007 2012-04-17 11:04 1240
	player1 = &theGame;
c53e6443 sago007 2012-04-17 11:04 1241
	player2 = &theGame2;
343fbe43 sago007 2018-03-27 19:53 1242
	theGame.DoPaintJob();  //Makes sure what there is something to paint
c53e6443 sago007 2012-04-17 11:04 1243
	theGame2.DoPaintJob();
b58caa17 sago007 2016-06-04 10:32 1244
	BlockGameAction a;
b58caa17 sago007 2016-06-04 10:32 1245
	a.action = BlockGameAction::Action::SET_GAME_OVER;
b58caa17 sago007 2016-06-04 10:32 1246
	theGame.DoAction(a);
b58caa17 sago007 2016-06-04 10:32 1247
	theGame2.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1248
c53e6443 sago007 2012-04-17 11:04 1249
	//Takes names from file instead
72383a9e sago007 2016-10-30 11:56 1250
	theGame.name = globalData.player1name;
72383a9e sago007 2016-10-30 11:56 1251
	theGame2.name = globalData.player2name;
c53e6443 sago007 2012-04-17 11:04 1252
7fdb4e65 sago007 2016-04-28 17:54 1253
	bool mustsetupgame = true;
092bdebe sago007 2017-03-19 14:39 1254
ecef5838 sago007 2015-11-24 20:01 1255
	if (singlePuzzle) {
c53e6443 sago007 2012-04-17 11:04 1256
		LoadPuzzleStages();
b256fda5 sago007 2016-02-21 16:21 1257
		BlockGameStartInfo s;
b256fda5 sago007 2016-02-21 16:21 1258
		s.puzzleMode = true;
b256fda5 sago007 2016-02-21 16:21 1259
		s.level = singlePuzzleNr;
83e4f8fe sago007 2016-02-22 19:22 1260
		s.singlePuzzle = true;
b256fda5 sago007 2016-02-21 16:21 1261
		theGame.NewGame(s);
7fdb4e65 sago007 2016-04-28 17:54 1262
		mustsetupgame = false;
c53e6443 sago007 2012-04-17 11:04 1263
	}
c53e6443 sago007 2012-04-17 11:04 1264
	//game loop
c53e6443 sago007 2012-04-17 11:04 1265
	int done = 0;
72383a9e sago007 2016-10-30 11:56 1266
	if (globalData.verboseLevel) {
161a010c sago007 2016-05-06 14:00 1267
		cout << "Starting game loop" << "\n";
acd79baf sago007 2015-11-22 19:37 1268
	}
c53e6443 sago007 2012-04-17 11:04 1269
53203200 sago007 2015-12-25 13:30 1270
ecef5838 sago007 2015-11-24 20:01 1271
	while (done == 0) {
ecef5838 sago007 2015-11-24 20:01 1272
		if (mustsetupgame) {
dfdb7d66 sago007 2015-11-29 19:21 1273
			registerEndlessHighscore = false;
dfdb7d66 sago007 2015-11-29 19:21 1274
			registerTTHighscorePlayer1 = false;
dfdb7d66 sago007 2015-11-29 19:21 1275
			registerTTHighscorePlayer2 = false;
ecef5838 sago007 2015-11-24 20:01 1276
			switch (gametype) {
092bdebe sago007 2017-03-19 14:39 1277
			case Gametype::SinglePlayerTimeTrial:
092bdebe sago007 2017-03-19 14:39 1278
				StartSinglePlayerTimeTrial();
092bdebe sago007 2017-03-19 14:39 1279
				break;
092bdebe sago007 2017-03-19 14:39 1280
			case Gametype::StageClear: {
092bdebe sago007 2017-03-19 14:39 1281
				int myLevel = PuzzleLevelSelect(1);
092bdebe sago007 2017-03-19 14:39 1282
				if (myLevel == -1) {
092bdebe sago007 2017-03-19 14:39 1283
					return 1;
092bdebe sago007 2017-03-19 14:39 1284
				}
092bdebe sago007 2017-03-19 14:39 1285
				BlockGameStartInfo s;
092bdebe sago007 2017-03-19 14:39 1286
				s.ticks = SDL_GetTicks();
092bdebe sago007 2017-03-19 14:39 1287
				s.stageClear = true;
092bdebe sago007 2017-03-19 14:39 1288
				s.level = myLevel;
092bdebe sago007 2017-03-19 14:39 1289
				theGame.NewGame(s);
092bdebe sago007 2017-03-19 14:39 1290
				DrawBackground(globalData.screen);
092bdebe sago007 2017-03-19 14:39 1291
				twoPlayers =false;
092bdebe sago007 2017-03-19 14:39 1292
				BlockGameAction a;
092bdebe sago007 2017-03-19 14:39 1293
				a.action = BlockGameAction::Action::SET_GAME_OVER;
092bdebe sago007 2017-03-19 14:39 1294
				theGame2.DoAction(a);
092bdebe sago007 2017-03-19 14:39 1295
				theGame.name = globalData.player1name;
092bdebe sago007 2017-03-19 14:39 1296
				theGame2.name = globalData.player2name;
092bdebe sago007 2017-03-19 14:39 1297
			}
092bdebe sago007 2017-03-19 14:39 1298
			break;
092bdebe sago007 2017-03-19 14:39 1299
			case Gametype::Puzzle:
092bdebe sago007 2017-03-19 14:39 1300
				if (StartSinglePlayerPuzzle()) {
092bdebe sago007 2017-03-19 14:39 1301
					return 1;
acd79baf sago007 2015-11-22 19:37 1302
				}
c53e6443 sago007 2012-04-17 11:04 1303
				break;
092bdebe sago007 2017-03-19 14:39 1304
			case Gametype::SinglePlayerVs: {
092bdebe sago007 2017-03-19 14:39 1305
				//1 player - Vs mode
092bdebe sago007 2017-03-19 14:39 1306
				int theAIlevel = level; //startSingleVs();
092bdebe sago007 2017-03-19 14:39 1307
				BlockGameStartInfo startInfo;
092bdebe sago007 2017-03-19 14:39 1308
				startInfo.ticks = SDL_GetTicks();
092bdebe sago007 2017-03-19 14:39 1309
				startInfo.vsMode = true;
092bdebe sago007 2017-03-19 14:39 1310
				startInfo.vsAI = true;
092bdebe sago007 2017-03-19 14:39 1311
				startInfo.level = theAIlevel;
092bdebe sago007 2017-03-19 14:39 1312
				theGame.NewGame(startInfo);
092bdebe sago007 2017-03-19 14:39 1313
				startInfo.AI = true;
092bdebe sago007 2017-03-19 14:39 1314
				theGame2.NewGame(startInfo);
092bdebe sago007 2017-03-19 14:39 1315
				DrawBackground(globalData.screen);
092bdebe sago007 2017-03-19 14:39 1316
				twoPlayers = true; //Single player, but AI plays
092bdebe sago007 2017-03-19 14:39 1317
				theGame.name = globalData.player1name;
092bdebe sago007 2017-03-19 14:39 1318
				theGame2.name = globalData.player2name;
092bdebe sago007 2017-03-19 14:39 1319
			}
092bdebe sago007 2017-03-19 14:39 1320
			break;
092bdebe sago007 2017-03-19 14:39 1321
			case Gametype::TwoPlayerTimeTrial:
092bdebe sago007 2017-03-19 14:39 1322
				StarTwoPlayerTimeTrial();
092bdebe sago007 2017-03-19 14:39 1323
				break;
092bdebe sago007 2017-03-19 14:39 1324
			case Gametype::TwoPlayerVs:
092bdebe sago007 2017-03-19 14:39 1325
				StartTwoPlayerVs();
092bdebe sago007 2017-03-19 14:39 1326
				break;
092bdebe sago007 2017-03-19 14:39 1327
			case Gametype::SinglePlayerEndless:
092bdebe sago007 2017-03-19 14:39 1328
			default:
4379ec19 Poul Sander 2019-01-30 18:27 1329
				StartSinglePlayerEndless(level);
c53e6443 sago007 2012-04-17 11:04 1330
			};
c53e6443 sago007 2012-04-17 11:04 1331
			mustsetupgame = false;
72383a9e sago007 2016-10-30 11:56 1332
			DrawBackground(globalData.screen);
7d649ca8 sago007 2016-10-02 16:43 1333
			MoveBlockGameSdls(theGame, theGame2);
72383a9e sago007 2016-10-30 11:56 1334
			DrawEverything(globalData.xsize, globalData.ysize, &theGame, &theGame2);
72383a9e sago007 2016-10-30 11:56 1335
			SDL_RenderPresent(globalData.screen);
c53e6443 sago007 2012-04-17 11:04 1336
		}
b7590374 sago007 2011-05-19 16:15 1337
72383a9e sago007 2016-10-30 11:56 1338
		if (!(globalData.highPriority)) {
571b4d47 sago007 2015-12-05 16:02 1339
			SDL_Delay(1);
c53e6443 sago007 2012-04-17 11:04 1340
		}
335635ec sago007 2016-02-06 13:51 1341
72383a9e sago007 2016-10-30 11:56 1342
		DrawBackground(globalData.screen);
de81e07d sago007 2016-07-10 20:22 1343
		//updates the balls and explosions:g
a6b97827 sago007 2016-01-24 11:15 1344
		theBallManager.update();
a6b97827 sago007 2016-01-24 11:15 1345
		theExplosionManager.update();
c310f8a5 sago007 2017-03-25 16:05 1346
		globalData.theTextManager.update();
c53e6443 sago007 2012-04-17 11:04 1347
fee9551a sago007 2016-01-20 19:05 1348
		bool mustWriteScreenshot = false;
335635ec sago007 2016-02-06 13:51 1349
c9226e8d sago007 2016-06-04 10:17 1350
		BlockGameAction a;
c9226e8d sago007 2016-06-04 10:17 1351
		a.action = BlockGameAction::Action::NONE;
c9226e8d sago007 2016-06-04 10:17 1352
		a.tick = SDL_GetTicks();
59dcb571 sago007 2016-02-14 15:34 1353
		if (true) {
c53e6443 sago007 2012-04-17 11:04 1354
			SDL_Event event;
c53e6443 sago007 2012-04-17 11:04 1355
ecef5838 sago007 2015-11-24 20:01 1356
			while ( SDL_PollEvent(&event) ) {
58cecf42 sago007 2016-12-09 20:35 1357
				UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey);
ecef5838 sago007 2015-11-24 20:01 1358
				if ( event.type == SDL_QUIT ) {
c53e6443 sago007 2012-04-17 11:04 1359
					Config::getInstance()->setShuttingDown(5);
c53e6443 sago007 2012-04-17 11:04 1360
					done = 1;
c53e6443 sago007 2012-04-17 11:04 1361
				}
092bdebe sago007 2017-03-19 14:39 1362
ce5aa7d3 sago007 2016-03-13 11:44 1363
				if (theGame.isGameOver() && isEscapeEvent(event)) {
ce5aa7d3 sago007 2016-03-13 11:44 1364
					done = 1;
ce5aa7d3 sago007 2016-03-13 11:44 1365
				}
c53e6443 sago007 2012-04-17 11:04 1366
146ce3eb sago007 2018-06-30 11:46 1367
				if (event.type == SDL_CONTROLLERBUTTONDOWN) {
146ce3eb sago007 2018-06-30 11:46 1368
					if ( event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK ) {
146ce3eb sago007 2018-06-30 11:46 1369
						done=1;
146ce3eb sago007 2018-06-30 11:46 1370
						DrawBackground(globalData.screen);
146ce3eb sago007 2018-06-30 11:46 1371
					}
146ce3eb sago007 2018-06-30 11:46 1372
				}
146ce3eb sago007 2018-06-30 11:46 1373
ecef5838 sago007 2015-11-24 20:01 1374
				if ( event.type == SDL_KEYDOWN ) {
ecef5838 sago007 2015-11-24 20:01 1375
					if ( event.key.keysym.sym == SDLK_ESCAPE || ( event.key.keysym.sym == SDLK_RETURN && theGame.isGameOver() ) ) {
1a156b60 sago007 2015-08-22 17:29 1376
						done=1;
72383a9e sago007 2016-10-30 11:56 1377
						DrawBackground(globalData.screen);
c53e6443 sago007 2012-04-17 11:04 1378
					}
2c6a4bae sago007 2018-05-11 15:20 1379
					if (!theGame.GetAIenabled()) {
c53e6443 sago007 2012-04-17 11:04 1380
						//player1:
ecef5838 sago007 2015-11-24 20:01 1381
						if ( event.key.keysym.sym == keySettings[player1keys].up ) {
b58caa17 sago007 2016-06-04 10:32 1382
							a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1383
							a.way = 'N';
b58caa17 sago007 2016-06-04 10:32 1384
							theGame.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1385
						}
ecef5838 sago007 2015-11-24 20:01 1386
						if ( event.key.keysym.sym == keySettings[player1keys].down ) {
b58caa17 sago007 2016-06-04 10:32 1387
							a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1388
							a.way = 'S';
b58caa17 sago007 2016-06-04 10:32 1389
							theGame.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1390
						}
ecef5838 sago007 2015-11-24 20:01 1391
						if ( (event.key.keysym.sym == keySettings[player1keys].left) ) {
b58caa17 sago007 2016-06-04 10:32 1392
							a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1393
							a.way = 'W';
b58caa17 sago007 2016-06-04 10:32 1394
							theGame.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1395
						}
ecef5838 sago007 2015-11-24 20:01 1396
						if ( (event.key.keysym.sym == keySettings[player1keys].right) ) {
b58caa17 sago007 2016-06-04 10:32 1397
							a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1398
							a.way = 'E';
b58caa17 sago007 2016-06-04 10:32 1399
							theGame.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1400
						}
ecef5838 sago007 2015-11-24 20:01 1401
						if ( event.key.keysym.sym == keySettings[player1keys].push ) {
c9226e8d sago007 2016-06-04 10:17 1402
							a.action = BlockGameAction::Action::PUSH;
c9226e8d sago007 2016-06-04 10:17 1403
							theGame.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1404
						}
ecef5838 sago007 2015-11-24 20:01 1405
						if ( event.key.keysym.sym == keySettings[player1keys].change ) {
b58caa17 sago007 2016-06-04 10:32 1406
							a.action = BlockGameAction::Action::SWITCH;
b58caa17 sago007 2016-06-04 10:32 1407
							theGame.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1408
						}
c53e6443 sago007 2012-04-17 11:04 1409
					}
2c6a4bae sago007 2018-05-11 15:20 1410
					if (!theGame2.GetAIenabled()) {
c53e6443 sago007 2012-04-17 11:04 1411
						//player2:
ecef5838 sago007 2015-11-24 20:01 1412
						if ( event.key.keysym.sym == keySettings[player2keys].up ) {
b58caa17 sago007 2016-06-04 10:32 1413
							a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1414
							a.way = 'N';
b58caa17 sago007 2016-06-04 10:32 1415
							theGame2.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1416
						}
ecef5838 sago007 2015-11-24 20:01 1417
						if ( event.key.keysym.sym == keySettings[player2keys].down ) {
b58caa17 sago007 2016-06-04 10:32 1418
							a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1419
							a.way = 'S';
b58caa17 sago007 2016-06-04 10:32 1420
							theGame2.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1421
						}
ecef5838 sago007 2015-11-24 20:01 1422
						if ( (event.key.keysym.sym == keySettings[player2keys].left) ) {
b58caa17 sago007 2016-06-04 10:32 1423
							a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1424
							a.way = 'W';
b58caa17 sago007 2016-06-04 10:32 1425
							theGame2.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1426
						}
ecef5838 sago007 2015-11-24 20:01 1427
						if ( (event.key.keysym.sym == keySettings[player2keys].right) ) {
b58caa17 sago007 2016-06-04 10:32 1428
							a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1429
							a.way = 'E';
b58caa17 sago007 2016-06-04 10:32 1430
							theGame2.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1431
						}
ecef5838 sago007 2015-11-24 20:01 1432
						if ( event.key.keysym.sym == keySettings[player2keys].push ) {
c9226e8d sago007 2016-06-04 10:17 1433
							a.action = BlockGameAction::Action::PUSH;
c9226e8d sago007 2016-06-04 10:17 1434
							theGame2.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1435
						}
ecef5838 sago007 2015-11-24 20:01 1436
						if ( event.key.keysym.sym == keySettings[player2keys].change ) {
b58caa17 sago007 2016-06-04 10:32 1437
							a.action = BlockGameAction::Action::SWITCH;
b58caa17 sago007 2016-06-04 10:32 1438
							theGame2.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1439
						}
c53e6443 sago007 2012-04-17 11:04 1440
					}
c53e6443 sago007 2012-04-17 11:04 1441
					//common:
2c6a4bae sago007 2018-05-11 15:20 1442
					if (!singlePuzzle) {
ecef5838 sago007 2015-11-24 20:01 1443
						if ( event.key.keysym.sym == SDLK_F2 ) {
c53e6443 sago007 2012-04-17 11:04 1444
							/*#if NETWORK
c53e6443 sago007 2012-04-17 11:04 1445
							if ((!showOptions)&&(!networkActive)){
c53e6443 sago007 2012-04-17 11:04 1446
							#else
c53e6443 sago007 2012-04-17 11:04 1447
							if ((!showOptions)){
c53e6443 sago007 2012-04-17 11:04 1448
							#endif
c53e6443 sago007 2012-04-17 11:04 1449
							    StartSinglePlayerEndless();
c53e6443 sago007 2012-04-17 11:04 1450
							}
c53e6443 sago007 2012-04-17 11:04 1451
							 */
c53e6443 sago007 2012-04-17 11:04 1452
							mustsetupgame = true;
c53e6443 sago007 2012-04-17 11:04 1453
						}
ecef5838 sago007 2015-11-24 20:01 1454
						if ( event.key.keysym.sym == SDLK_F10 ) {
ca486238 sago007 2015-08-23 15:12 1455
							//StartReplay("/home/poul/.gamesaves/blockattack/quicksave");
b283e0f7 sago007 2012-07-29 14:01 1456
						}
ecef5838 sago007 2015-11-24 20:01 1457
						if ( event.key.keysym.sym == SDLK_F9 ) {
fee9551a sago007 2016-01-20 19:05 1458
							mustWriteScreenshot = true;
c53e6443 sago007 2012-04-17 11:04 1459
						}
ca486238 sago007 2015-08-23 15:12 1460
						if ( event.key.keysym.sym == SDLK_F5 ) {
b283e0f7 sago007 2012-07-29 14:01 1461
						}
ecef5838 sago007 2015-11-24 20:01 1462
						if ( event.key.keysym.sym == SDLK_F11 ) {
c53e6443 sago007 2012-04-17 11:04 1463
						} //F11
c53e6443 sago007 2012-04-17 11:04 1464
					}
ecef5838 sago007 2015-11-24 20:01 1465
					if ( event.key.keysym.sym == SDLK_F12 ) {
c53e6443 sago007 2012-04-17 11:04 1466
						done=1;
c53e6443 sago007 2012-04-17 11:04 1467
					}
c53e6443 sago007 2012-04-17 11:04 1468
				}
c7f2082f sago007 2016-03-13 11:48 1469
ce5aa7d3 sago007 2016-03-13 11:44 1470
				if (isPlayerUpEvent(1, event)) {
b58caa17 sago007 2016-06-04 10:32 1471
					a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1472
					a.way = 'N';
b58caa17 sago007 2016-06-04 10:32 1473
					theGame.DoAction(a);
ce5aa7d3 sago007 2016-03-13 11:44 1474
				}
ce5aa7d3 sago007 2016-03-13 11:44 1475
				if (isPlayerDownEvent(1, event)) {
b58caa17 sago007 2016-06-04 10:32 1476
					a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1477
					a.way = 'S';
b58caa17 sago007 2016-06-04 10:32 1478
					theGame.DoAction(a);
ce5aa7d3 sago007 2016-03-13 11:44 1479
				}
ce5aa7d3 sago007 2016-03-13 11:44 1480
				if (isPlayerLeftEvent(1, event)) {
b58caa17 sago007 2016-06-04 10:32 1481
					a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1482
					a.way = 'W';
b58caa17 sago007 2016-06-04 10:32 1483
					theGame.DoAction(a);
ce5aa7d3 sago007 2016-03-13 11:44 1484
				}
ce5aa7d3 sago007 2016-03-13 11:44 1485
				if (isPlayerRightEvent (1, event)) {
b58caa17 sago007 2016-06-04 10:32 1486
					a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1487
					a.way = 'E';
b58caa17 sago007 2016-06-04 10:32 1488
					theGame.DoAction(a);
ce5aa7d3 sago007 2016-03-13 11:44 1489
				}
ce5aa7d3 sago007 2016-03-13 11:44 1490
				if (isPlayerSwitchEvent(1, event)) {
b58caa17 sago007 2016-06-04 10:32 1491
					a.action = BlockGameAction::Action::SWITCH;
b58caa17 sago007 2016-06-04 10:32 1492
					theGame.DoAction(a);
ce5aa7d3 sago007 2016-03-13 11:44 1493
				}
ce5aa7d3 sago007 2016-03-13 11:44 1494
				if (isPlayerPushEvent(1, event)) {
c9226e8d sago007 2016-06-04 10:17 1495
					a.action = BlockGameAction::Action::PUSH;
c9226e8d sago007 2016-06-04 10:17 1496
					theGame.DoAction(a);
ce5aa7d3 sago007 2016-03-13 11:44 1497
				}
f2dac7e4 sago007 2016-04-09 19:24 1498
				if (isPlayerUpEvent(2, event)) {
b58caa17 sago007 2016-06-04 10:32 1499
					a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1500
					a.way = 'N';
b58caa17 sago007 2016-06-04 10:32 1501
					theGame2.DoAction(a);
f2dac7e4 sago007 2016-04-09 19:24 1502
				}
f2dac7e4 sago007 2016-04-09 19:24 1503
				if (isPlayerDownEvent(2, event)) {
b58caa17 sago007 2016-06-04 10:32 1504
					a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1505
					a.way = 'S';
b58caa17 sago007 2016-06-04 10:32 1506
					theGame2.DoAction(a);
f2dac7e4 sago007 2016-04-09 19:24 1507
				}
f2dac7e4 sago007 2016-04-09 19:24 1508
				if (isPlayerLeftEvent(2, event)) {
b58caa17 sago007 2016-06-04 10:32 1509
					a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1510
					a.way = 'W';
b58caa17 sago007 2016-06-04 10:32 1511
					theGame2.DoAction(a);
f2dac7e4 sago007 2016-04-09 19:24 1512
				}
f2dac7e4 sago007 2016-04-09 19:24 1513
				if (isPlayerRightEvent (2, event)) {
b58caa17 sago007 2016-06-04 10:32 1514
					a.action = BlockGameAction::Action::MOVE;
e5a23912 sago007 2016-06-04 10:52 1515
					a.way = 'E';
b58caa17 sago007 2016-06-04 10:32 1516
					theGame2.DoAction(a);
f2dac7e4 sago007 2016-04-09 19:24 1517
				}
f2dac7e4 sago007 2016-04-09 19:24 1518
				if (isPlayerSwitchEvent(2, event)) {
b58caa17 sago007 2016-06-04 10:32 1519
					a.action = BlockGameAction::Action::SWITCH;
b58caa17 sago007 2016-06-04 10:32 1520
					theGame2.DoAction(a);
f2dac7e4 sago007 2016-04-09 19:24 1521
				}
f2dac7e4 sago007 2016-04-09 19:24 1522
				if (isPlayerPushEvent(2, event)) {
c9226e8d sago007 2016-06-04 10:17 1523
					a.action = BlockGameAction::Action::PUSH;
c9226e8d sago007 2016-06-04 10:17 1524
					theGame2.DoAction(a);
f2dac7e4 sago007 2016-04-09 19:24 1525
				}
706e81fe sago007 2016-04-05 19:17 1526
				static int mouseDownX = 0;
706e81fe sago007 2016-04-05 19:17 1527
				static int mouseDownY = 0;
d0c62e33 sago007 2016-04-02 15:18 1528
				if (event.type == SDL_MOUSEBUTTONDOWN) {
d0c62e33 sago007 2016-04-02 15:18 1529
					if (event.button.button == SDL_BUTTON_LEFT) {
d0c62e33 sago007 2016-04-02 15:18 1530
						bool pressed = false;
d0c62e33 sago007 2016-04-02 15:18 1531
						int x = 0;
d0c62e33 sago007 2016-04-02 15:18 1532
						int y = 0;
d0c62e33 sago007 2016-04-02 15:18 1533
						theGame.GetBrickCoordinateFromMouse(pressed, event.button.x, event.button.y, x, y);
d0c62e33 sago007 2016-04-02 15:18 1534
						if (pressed) {
167c2a1d sago007 2016-06-05 12:45 1535
							a.action = BlockGameAction::Action::MOUSE_DOWN;
167c2a1d sago007 2016-06-05 12:45 1536
							a.x = x;
167c2a1d sago007 2016-06-05 12:45 1537
							a.y = y;
167c2a1d sago007 2016-06-05 12:45 1538
							theGame.DoAction(a);
d0c62e33 sago007 2016-04-02 15:18 1539
						}
d0c62e33 sago007 2016-04-02 15:18 1540
						theGame2.GetBrickCoordinateFromMouse(pressed, event.button.x, event.button.y, x, y);
d0c62e33 sago007 2016-04-02 15:18 1541
						if (pressed) {
167c2a1d sago007 2016-06-05 12:45 1542
							a.action = BlockGameAction::Action::MOUSE_DOWN;
167c2a1d sago007 2016-06-05 12:45 1543
							a.x = x;
167c2a1d sago007 2016-06-05 12:45 1544
							a.y = y;
167c2a1d sago007 2016-06-05 12:45 1545
							theGame2.DoAction(a);
d0c62e33 sago007 2016-04-02 15:18 1546
						}
706e81fe sago007 2016-04-05 19:17 1547
						mouseDownX = event.button.x;
706e81fe sago007 2016-04-05 19:17 1548
						mouseDownY = event.button.y;
092bdebe sago007 2017-03-19 14:39 1549
					}
243ef769 sago007 2016-04-04 18:00 1550
					if (event.button.button == SDL_BUTTON_RIGHT) {
243ef769 sago007 2016-04-04 18:00 1551
						bool pressed = false;
243ef769 sago007 2016-04-04 18:00 1552
						int x = 0;
243ef769 sago007 2016-04-04 18:00 1553
						int y = 0;
243ef769 sago007 2016-04-04 18:00 1554
						theGame.GetBrickCoordinateFromMouse(pressed, event.button.x, event.button.y, x, y);
243ef769 sago007 2016-04-04 18:00 1555
						if (pressed) {
c9226e8d sago007 2016-06-04 10:17 1556
							a.action = BlockGameAction::Action::PUSH;
c9226e8d sago007 2016-06-04 10:17 1557
							theGame.DoAction(a);
243ef769 sago007 2016-04-04 18:00 1558
						}
243ef769 sago007 2016-04-04 18:00 1559
						theGame2.GetBrickCoordinateFromMouse(pressed, event.button.x, event.button.y, x, y);
243ef769 sago007 2016-04-04 18:00 1560
						if (pressed) {
c9226e8d sago007 2016-06-04 10:17 1561
							a.action = BlockGameAction::Action::PUSH;
c9226e8d sago007 2016-06-04 10:17 1562
							theGame2.DoAction(a);
243ef769 sago007 2016-04-04 18:00 1563
						}
243ef769 sago007 2016-04-04 18:00 1564
					}
c53e6443 sago007 2012-04-17 11:04 1565
				}
d0c62e33 sago007 2016-04-02 15:18 1566
				if (event.type == SDL_MOUSEBUTTONUP) {
d0c62e33 sago007 2016-04-02 15:18 1567
					if (event.button.button == SDL_BUTTON_LEFT) {
706e81fe sago007 2016-04-05 19:17 1568
						int x = event.button.x;
706e81fe sago007 2016-04-05 19:17 1569
						int y = event.button.y;
167c2a1d sago007 2016-06-05 12:45 1570
						a.action = BlockGameAction::Action::MOUSE_UP;
167c2a1d sago007 2016-06-05 12:45 1571
						theGame.DoAction(a);
167c2a1d sago007 2016-06-05 12:45 1572
						theGame2.DoAction(a);
706e81fe sago007 2016-04-05 19:17 1573
						if (theGame.IsInTheBoard(x,y) && theGame.IsUnderTheBoard(mouseDownX, mouseDownY)) {
c9226e8d sago007 2016-06-04 10:17 1574
							a.action = BlockGameAction::Action::PUSH;
c9226e8d sago007 2016-06-04 10:17 1575
							theGame.DoAction(a);
706e81fe sago007 2016-04-05 19:17 1576
						}
706e81fe sago007 2016-04-05 19:17 1577
						if (theGame2.IsInTheBoard(x,y) && theGame2.IsUnderTheBoard(mouseDownX, mouseDownY)) {
c9226e8d sago007 2016-06-04 10:17 1578
							a.action = BlockGameAction::Action::PUSH;
c9226e8d sago007 2016-06-04 10:17 1579
							theGame2.DoAction(a);
706e81fe sago007 2016-04-05 19:17 1580
						}
acd79baf sago007 2015-11-22 19:37 1581
					}
d0c62e33 sago007 2016-04-02 15:18 1582
				}
d0c62e33 sago007 2016-04-02 15:18 1583
				if (event.type == SDL_MOUSEMOTION) {
161a010c sago007 2016-05-06 14:00 1584
					//cout << "Moved" << "\n";
d0c62e33 sago007 2016-04-02 15:18 1585
					bool pressed = false;
d0c62e33 sago007 2016-04-02 15:18 1586
					int x = 0;
d0c62e33 sago007 2016-04-02 15:18 1587
					int y = 0;
d0c62e33 sago007 2016-04-02 15:18 1588
					theGame.GetMouseCursor(pressed, x, y);
d0c62e33 sago007 2016-04-02 15:18 1589
					if (pressed) {
d0c62e33 sago007 2016-04-02 15:18 1590
						int mx = 0;
d0c62e33 sago007 2016-04-02 15:18 1591
						int my = 0;
d0c62e33 sago007 2016-04-02 15:18 1592
						theGame.GetBrickCoordinateFromMouse(pressed, event.motion.x, event.motion.y, mx, my);
d0c62e33 sago007 2016-04-02 15:18 1593
						if (pressed) {
d0c62e33 sago007 2016-04-02 15:18 1594
							if (mx != x) {
167c2a1d sago007 2016-06-05 12:45 1595
								a.action = BlockGameAction::Action::MOUSE_MOVE;
167c2a1d sago007 2016-06-05 12:45 1596
								a.x = mx;
167c2a1d sago007 2016-06-05 12:45 1597
								theGame.DoAction(a);
d0c62e33 sago007 2016-04-02 15:18 1598
							}
d0c62e33 sago007 2016-04-02 15:18 1599
						}
acd79baf sago007 2015-11-22 19:37 1600
					}
d0c62e33 sago007 2016-04-02 15:18 1601
					theGame2.GetMouseCursor(pressed, x, y);
d0c62e33 sago007 2016-04-02 15:18 1602
					if (pressed) {
d0c62e33 sago007 2016-04-02 15:18 1603
						int mx = 0;
d0c62e33 sago007 2016-04-02 15:18 1604
						int my = 0;
d0c62e33 sago007 2016-04-02 15:18 1605
						theGame2.GetBrickCoordinateFromMouse(pressed, event.motion.x, event.motion.y, mx, my);
d0c62e33 sago007 2016-04-02 15:18 1606
						if (pressed) {
d0c62e33 sago007 2016-04-02 15:18 1607
							if (mx != x) {
167c2a1d sago007 2016-06-05 12:45 1608
								a.action = BlockGameAction::Action::MOUSE_MOVE;
167c2a1d sago007 2016-06-05 12:45 1609
								a.x = mx;
167c2a1d sago007 2016-06-05 12:45 1610
								theGame2.DoAction(a);
d0c62e33 sago007 2016-04-02 15:18 1611
							}
d0c62e33 sago007 2016-04-02 15:18 1612
						}
acd79baf sago007 2015-11-22 19:37 1613
					}
092bdebe sago007 2017-03-19 14:39 1614
c53e6443 sago007 2012-04-17 11:04 1615
				}
d0c62e33 sago007 2016-04-02 15:18 1616
			} //while event PollEvent - read keys
c53e6443 sago007 2012-04-17 11:04 1617
c53e6443 sago007 2012-04-17 11:04 1618
			// If the mouse button is released, make bMouseUp equal true
08c44684 sago007 2016-10-30 09:19 1619
			if (! (SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
c53e6443 sago007 2012-04-17 11:04 1620
				bMouseUp=true;
c53e6443 sago007 2012-04-17 11:04 1621
			}
c53e6443 sago007 2012-04-17 11:04 1622
c53e6443 sago007 2012-04-17 11:04 1623
			// If the mouse button 2 is released, make bMouseUp2 equal true
ecef5838 sago007 2015-11-24 20:01 1624
			if ((SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(3))!=SDL_BUTTON(3)) {
c53e6443 sago007 2012-04-17 11:04 1625
				bMouseUp2=true;
c53e6443 sago007 2012-04-17 11:04 1626
			}
c53e6443 sago007 2012-04-17 11:04 1627
2c6a4bae sago007 2018-05-11 15:20 1628
			if (true ) {
c53e6443 sago007 2012-04-17 11:04 1629
				//read mouse events
ecef5838 sago007 2015-11-24 20:01 1630
				if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) {
38fa9b68 sago007 2016-03-12 08:46 1631
					//This is the mouse events
c53e6443 sago007 2012-04-17 11:04 1632
					bMouseUp = false;
72383a9e sago007 2016-10-30 11:56 1633
					DrawBackground(globalData.screen);
c53e6443 sago007 2012-04-17 11:04 1634
58cecf42 sago007 2016-12-09 20:35 1635
					if (stageButtonStatus != SBdontShow && (globalData.mousex > theGame.GetTopX()+cordNextButton.x)
58cecf42 sago007 2016-12-09 20:35 1636
					        && (globalData.mousex < theGame.GetTopX()+cordNextButton.x+cordNextButton.xsize)
58cecf42 sago007 2016-12-09 20:35 1637
					        && (globalData.mousey > theGame.GetTopY()+cordNextButton.y)
092bdebe sago007 2017-03-19 14:39 1638
					        && (globalData.mousey < theGame.GetTopY()+cordNextButton.y+cordNextButton.ysize)) {
c53e6443 sago007 2012-04-17 11:04 1639
						//Clicked the next button after a stage clear or puzzle
c1785ec5 sago007 2016-02-21 16:07 1640
						nextLevel(theGame, SDL_GetTicks());
c53e6443 sago007 2012-04-17 11:04 1641
					}
092bdebe sago007 2017-03-19 14:39 1642
58cecf42 sago007 2016-12-09 20:35 1643
					if (stageButtonStatus != SBdontShow && (globalData.mousex > theGame.GetTopX()+cordRetryButton .x)
58cecf42 sago007 2016-12-09 20:35 1644
					        &&(globalData.mousex < theGame.GetTopX()+cordRetryButton.x+cordRetryButton.xsize)
58cecf42 sago007 2016-12-09 20:35 1645
					        &&(globalData.mousey > theGame.GetTopY()+cordRetryButton.y)
092bdebe sago007 2017-03-19 14:39 1646
					        &&(globalData.mousey < theGame.GetTopY()+cordRetryButton.y+cordRetryButton.ysize)) {
c53e6443 sago007 2012-04-17 11:04 1647
						//Clicked the retry button
fde1f2dd sago007 2016-02-21 15:24 1648
						retryLevel(theGame, SDL_GetTicks());
c53e6443 sago007 2012-04-17 11:04 1649
					}
c53e6443 sago007 2012-04-17 11:04 1650
58cecf42 sago007 2016-12-09 20:35 1651
					if (globalData.mousex > globalData.xsize-bExitOffset && globalData.mousex < globalData.xsize-bExitOffset+bExitSize &&
58cecf42 sago007 2016-12-09 20:35 1652
					        globalData.mousey > globalData.ysize-bExitOffset && globalData.mousey < globalData.ysize-bExitOffset+bExitSize) {
38fa9b68 sago007 2016-03-12 08:46 1653
						done = 1;
38fa9b68 sago007 2016-03-12 08:46 1654
					}
c53e6443 sago007 2012-04-17 11:04 1655
				}
c53e6443 sago007 2012-04-17 11:04 1656
c53e6443 sago007 2012-04-17 11:04 1657
				//Mouse button 2:
ecef5838 sago007 2015-11-24 20:01 1658
				if ((SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(3))==SDL_BUTTON(3) && bMouseUp2) {
c53e6443 sago007 2012-04-17 11:04 1659
					bMouseUp2=false; //The button is pressed
c53e6443 sago007 2012-04-17 11:04 1660
				}
c53e6443 sago007 2012-04-17 11:04 1661
			}
c53e6443 sago007 2012-04-17 11:04 1662
		} //if !bScreenBocked;
c53e6443 sago007 2012-04-17 11:04 1663
c53e6443 sago007 2012-04-17 11:04 1664
c53e6443 sago007 2012-04-17 11:04 1665
		//Sees if music is stopped and if music is enabled
72383a9e sago007 2016-10-30 11:56 1666
		if ((!globalData.NoSound)&&(!Mix_PlayingMusic())&&(globalData.MusicEnabled)&&(!bNearDeath)) {
c53e6443 sago007 2012-04-17 11:04 1667
			// then starts playing it.
524f188c sago007 2017-04-22 09:01 1668
			Mix_PlayMusic(bgMusic.get(), -1); //music loop
32090628 sago007 2016-04-10 14:13 1669
			Mix_VolumeMusic((MIX_MAX_VOLUME*3)/10);
c53e6443 sago007 2012-04-17 11:04 1670
		}
b7590374 sago007 2011-05-19 16:15 1671
ecef5838 sago007 2015-11-24 20:01 1672
		if (bNearDeath!=bNearDeathPrev) {
ecef5838 sago007 2015-11-24 20:01 1673
			if (bNearDeath) {
72383a9e sago007 2016-10-30 11:56 1674
				if (!globalData.NoSound &&(globalData.MusicEnabled)) {
524f188c sago007 2017-04-22 09:01 1675
					Mix_PlayMusic(highbeatMusic.get(), 1);
32090628 sago007 2016-04-10 14:13 1676
					Mix_VolumeMusic((MIX_MAX_VOLUME*5)/10);
c53e6443 sago007 2012-04-17 11:04 1677
				}
c53e6443 sago007 2012-04-17 11:04 1678
			}
ecef5838 sago007 2015-11-24 20:01 1679
			else {
72383a9e sago007 2016-10-30 11:56 1680
				if (!globalData.NoSound &&(globalData.MusicEnabled)) {
524f188c sago007 2017-04-22 09:01 1681
					Mix_PlayMusic(bgMusic.get(), -1);
32090628 sago007 2016-04-10 14:13 1682
					Mix_VolumeMusic((MIX_MAX_VOLUME*3)/10);
c53e6443 sago007 2012-04-17 11:04 1683
				}
c53e6443 sago007 2012-04-17 11:04 1684
			}
c53e6443 sago007 2012-04-17 11:04 1685
		}
b7590374 sago007 2011-05-19 16:15 1686
c53e6443 sago007 2012-04-17 11:04 1687
		bNearDeathPrev = bNearDeath;
b7590374 sago007 2011-05-19 16:15 1688
b7590374 sago007 2011-05-19 16:15 1689
c53e6443 sago007 2012-04-17 11:04 1690
		//set bNearDeath to false theGame*.Update() will change to true as needed
18055aa0 sago007 2012-06-05 19:43 1691
		bNearDeath = theGame.IsNearDeath() || theGame2.IsNearDeath();
c53e6443 sago007 2012-04-17 11:04 1692
		//Updates the objects
62b3f619 sago007 2016-05-20 07:01 1693
		a.action = BlockGameAction::Action::UPDATE;
4fb2e351 sago007 2016-05-19 19:27 1694
		theGame.DoAction(a);
4fb2e351 sago007 2016-05-19 19:27 1695
		theGame2.DoAction(a);
b7590374 sago007 2011-05-19 16:15 1696
b7590374 sago007 2011-05-19 16:15 1697
//see if anyone has won (two players only)
c53e6443 sago007 2012-04-17 11:04 1698
#if NETWORK
c53e6443 sago007 2012-04-17 11:04 1699
		if (!networkPlay)
c53e6443 sago007 2012-04-17 11:04 1700
#endif
ecef5838 sago007 2015-11-24 20:01 1701
			if (twoPlayers) {
0be2dfb3 sago007 2016-10-30 12:07 1702
				if (theGame.isGameOver() && theGame2.isGameOver() && !theGame.GetIsWinner() && !theGame2.GetIsWinner()  ) {
ecef5838 sago007 2015-11-24 20:01 1703
					if (theGame.GetScore()+theGame.GetHandicap()>theGame2.GetScore()+theGame2.GetHandicap()) {
07f5b3f3 sago007 2016-05-19 19:37 1704
						BlockGameAction a;
62b3f619 sago007 2016-05-20 07:01 1705
						a.action = BlockGameAction::Action::SET_WON;
07f5b3f3 sago007 2016-05-19 19:37 1706
						theGame.DoAction(a);
acd79baf sago007 2015-11-22 19:37 1707
					}
ecef5838 sago007 2015-11-24 20:01 1708
					else if (theGame.GetScore()+theGame.GetHandicap()<theGame2.GetScore()+theGame2.GetHandicap()) {
07f5b3f3 sago007 2016-05-19 19:37 1709
						BlockGameAction a;
62b3f619 sago007 2016-05-20 07:01 1710
						a.action = BlockGameAction::Action::SET_WON;
07f5b3f3 sago007 2016-05-19 19:37 1711
						theGame2.DoAction(a);
acd79baf sago007 2015-11-22 19:37 1712
					}
ecef5838 sago007 2015-11-24 20:01 1713
					else {
07f5b3f3 sago007 2016-05-19 19:37 1714
						BlockGameAction a;
62b3f619 sago007 2016-05-20 07:01 1715
						a.action = BlockGameAction::Action::SET_DRAW;
07f5b3f3 sago007 2016-05-19 19:37 1716
						theGame.DoAction(a);
07f5b3f3 sago007 2016-05-19 19:37 1717
						theGame2.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1718
					}
b283e0f7 sago007 2012-07-29 14:01 1719
					//twoPlayers = false;
c53e6443 sago007 2012-04-17 11:04 1720
				}
ecef5838 sago007 2015-11-24 20:01 1721
				if ((theGame.isGameOver()) && (!theGame2.isGameOver())) {
07f5b3f3 sago007 2016-05-19 19:37 1722
					BlockGameAction a;
62b3f619 sago007 2016-05-20 07:01 1723
					a.action = BlockGameAction::Action::SET_WON;
07f5b3f3 sago007 2016-05-19 19:37 1724
					theGame2.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1725
				}
ecef5838 sago007 2015-11-24 20:01 1726
				if ((!theGame.isGameOver()) && (theGame2.isGameOver())) {
07f5b3f3 sago007 2016-05-19 19:37 1727
					BlockGameAction a;
62b3f619 sago007 2016-05-20 07:01 1728
					a.action = BlockGameAction::Action::SET_WON;
07f5b3f3 sago007 2016-05-19 19:37 1729
					theGame.DoAction(a);
c53e6443 sago007 2012-04-17 11:04 1730
				}
3d12f4a8 sago007 2016-03-21 18:40 1731
				vector<GarbageStruct> gs;
3d12f4a8 sago007 2016-03-21 18:40 1732
				theGame.PopSendGarbage(gs);
3d12f4a8 sago007 2016-03-21 18:40 1733
				for (const GarbageStruct& g : gs ) {
8d5e5953 sago007 2016-06-04 13:15 1734
					BlockGameAction a;
8d5e5953 sago007 2016-06-04 13:15 1735
					a.action = BlockGameAction::Action::PUSH_GARBAGE;
8d5e5953 sago007 2016-06-04 13:15 1736
					a.garbage.push_back(g);
8d5e5953 sago007 2016-06-04 13:15 1737
					theGame2.DoAction(a);
3d12f4a8 sago007 2016-03-21 18:40 1738
				}
3d12f4a8 sago007 2016-03-21 18:40 1739
				gs.clear();
3d12f4a8 sago007 2016-03-21 18:40 1740
				theGame2.PopSendGarbage(gs);
3d12f4a8 sago007 2016-03-21 18:40 1741
				for (const GarbageStruct& g : gs ) {
8d5e5953 sago007 2016-06-04 13:15 1742
					BlockGameAction a;
8d5e5953 sago007 2016-06-04 13:15 1743
					a.action = BlockGameAction::Action::PUSH_GARBAGE;
8d5e5953 sago007 2016-06-04 13:15 1744
					a.garbage.push_back(g);
8d5e5953 sago007 2016-06-04 13:15 1745
					theGame.DoAction(a);
3d12f4a8 sago007 2016-03-21 18:40 1746
				}
c53e6443 sago007 2012-04-17 11:04 1747
			}
53203200 sago007 2015-12-25 13:30 1748
dfdb7d66 sago007 2015-11-29 19:21 1749
		if (theGame.isGameOver() && registerTTHighscorePlayer1) {
dfdb7d66 sago007 2015-11-29 19:21 1750
			registerTTHighscorePlayer1 = false;
905de2a2 sago007 2019-02-08 18:46 1751
			theTopScoresTimeTrial.addScore(theGame.name, theGame.GetScore());
dfdb7d66 sago007 2015-11-29 19:21 1752
		}
dfdb7d66 sago007 2015-11-29 19:21 1753
		if (theGame2.isGameOver() && registerTTHighscorePlayer2) {
dfdb7d66 sago007 2015-11-29 19:21 1754
			registerTTHighscorePlayer2 = false;
905de2a2 sago007 2019-02-08 18:46 1755
			theTopScoresTimeTrial.addScore(theGame2.name, theGame2.GetScore());
dfdb7d66 sago007 2015-11-29 19:21 1756
		}
dfdb7d66 sago007 2015-11-29 19:21 1757
		if (theGame.isGameOver() && registerEndlessHighscore) {
b5321636 sago007 2019-02-09 18:47 1758
			Highscore theTopScoresEndless = Highscore("endless", theGame.GetBaseSpeed());
dfdb7d66 sago007 2015-11-29 19:21 1759
			registerEndlessHighscore = false;
905de2a2 sago007 2019-02-08 18:46 1760
			theTopScoresEndless.addScore(theGame.name, theGame.GetScore());
dfdb7d66 sago007 2015-11-29 19:21 1761
			theGame.EndlessHighscoreEvent();
dfdb7d66 sago007 2015-11-29 19:21 1762
		}
440c4aa8 Poul Sander 2017-03-29 17:21 1763
#ifdef REPLAY_IMPLEMENTED
76514d10 sago007 2016-06-05 20:25 1764
		if (theGame.isGameOver() && saveReplay) {
76514d10 sago007 2016-06-05 20:25 1765
			if (twoPlayers && theGame2.isGameOver()) {
76514d10 sago007 2016-06-05 20:25 1766
				saveReplay = false;
76514d10 sago007 2016-06-05 20:25 1767
				SaveReplay(theGame.GetBlockGameInfo(), theGame2.GetBlockGameInfo());
76514d10 sago007 2016-06-05 20:25 1768
			}
76514d10 sago007 2016-06-05 20:25 1769
			if (!twoPlayers) {
76514d10 sago007 2016-06-05 20:25 1770
				saveReplay = false;
76514d10 sago007 2016-06-05 20:25 1771
				SaveReplay(theGame.GetBlockGameInfo());
76514d10 sago007 2016-06-05 20:25 1772
			}
76514d10 sago007 2016-06-05 20:25 1773
		}
440c4aa8 Poul Sander 2017-03-29 17:21 1774
#endif
c53e6443 sago007 2012-04-17 11:04 1775
c53e6443 sago007 2012-04-17 11:04 1776
		//Once evrything has been checked, update graphics
7d649ca8 sago007 2016-10-02 16:43 1777
		MoveBlockGameSdls(theGame, theGame2);
72383a9e sago007 2016-10-30 11:56 1778
		DrawEverything(globalData.xsize, globalData.ysize, &theGame, &theGame2);
c53e6443 sago007 2012-04-17 11:04 1779
		//Draw the mouse:
58cecf42 sago007 2016-12-09 20:35 1780
		globalData.mouse.Draw(globalData.screen, SDL_GetTicks(), globalData.mousex, globalData.mousey);
72383a9e sago007 2016-10-30 11:56 1781
		SDL_RenderPresent(globalData.screen);
fee9551a sago007 2016-01-20 19:05 1782
		if (mustWriteScreenshot) {
fee9551a sago007 2016-01-20 19:05 1783
			writeScreenShot();
fee9551a sago007 2016-01-20 19:05 1784
		}
c53e6443 sago007 2012-04-17 11:04 1785
	} //game loop
80b830cd sago007 2012-08-19 17:51 1786
	return 0;
605138b3 sago007 2012-04-14 15:12 1787
}
1970-01-01 00:00 1788