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