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