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