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