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