git repos / blockattack-game

blame: source/code/main.cpp

normal view · raw

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