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