git repos / blockattack-game

blame: source/code/main.cpp

normal view · raw

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