git repos / blockattack-game

blame: source/code/main.cpp

normal view · raw

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