git repos / blockattack-game

blame: source/code/main.cpp

normal view · raw

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