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