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