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