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