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