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