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