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