git repos / blockattack-game

blame: source/code/ScoresDisplay.cpp

normal view · raw

c3b697c6 sago007 2016-02-07 17:30 1
/*
7a956470 sago007 2016-02-14 17:09 2
===========================================================================
7a956470 sago007 2016-02-14 17:09 3
blockattack - Block Attack - Rise of the Blocks
7a956470 sago007 2016-02-14 17:09 4
Copyright (C) 2005-2016 Poul Sander
c3b697c6 sago007 2016-02-07 17:30 5
7a956470 sago007 2016-02-14 17:09 6
This program is free software: you can redistribute it and/or modify
7a956470 sago007 2016-02-14 17:09 7
it under the terms of the GNU General Public License as published by
7a956470 sago007 2016-02-14 17:09 8
the Free Software Foundation, either version 2 of the License, or
7a956470 sago007 2016-02-14 17:09 9
(at your option) any later version.
7a956470 sago007 2016-02-14 17:09 10
7a956470 sago007 2016-02-14 17:09 11
This program is distributed in the hope that it will be useful,
7a956470 sago007 2016-02-14 17:09 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
7a956470 sago007 2016-02-14 17:09 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7a956470 sago007 2016-02-14 17:09 14
GNU General Public License for more details.
7a956470 sago007 2016-02-14 17:09 15
7a956470 sago007 2016-02-14 17:09 16
You should have received a copy of the GNU General Public License
7a956470 sago007 2016-02-14 17:09 17
along with this program.  If not, see http://www.gnu.org/licenses/
7a956470 sago007 2016-02-14 17:09 18
7a956470 sago007 2016-02-14 17:09 19
Source information and contacts persons can be found at
7a956470 sago007 2016-02-14 17:09 20
http://www.blockattack.net
7a956470 sago007 2016-02-14 17:09 21
===========================================================================
7a956470 sago007 2016-02-14 17:09 22
*/
c3b697c6 sago007 2016-02-07 17:30 23
c3b697c6 sago007 2016-02-07 17:30 24
#include "ScoresDisplay.hpp"
c3b697c6 sago007 2016-02-07 17:30 25
#include "global.hpp"
c3b697c6 sago007 2016-02-07 17:30 26
#include "common.h"
c3b697c6 sago007 2016-02-07 17:30 27
#include "stats.h"
d21782d4 sago007 2016-03-13 11:17 28
#include "MenuSystem.h"
c3b697c6 sago007 2016-02-07 17:30 29
96eb7a16 sago007 2016-04-29 17:01 30
using std::string;
96eb7a16 sago007 2016-04-29 17:01 31
using std::cerr;
96eb7a16 sago007 2016-04-29 17:01 32
using std::vector;
c3b697c6 sago007 2016-02-07 17:30 33
34ab7861 sago007 2018-03-28 14:47 34
static void setButtonFont(const sago::SagoDataHolder* holder, sago::SagoTextField& field, const char* text){
34ab7861 sago007 2018-03-28 14:47 35
	field.SetHolder(holder);
34ab7861 sago007 2018-03-28 14:47 36
	field.SetFont("freeserif");
34ab7861 sago007 2018-03-28 14:47 37
	field.SetColor({255,255,255,255});
34ab7861 sago007 2018-03-28 14:47 38
	field.SetFontSize(24);
34ab7861 sago007 2018-03-28 14:47 39
	field.SetOutline(1, {0,0,0,255});
34ab7861 sago007 2018-03-28 14:47 40
	field.SetText(text);
34ab7861 sago007 2018-03-28 14:47 41
}
e1b2e653 sago007 2017-11-28 18:10 42
69d8f1dc sago007 2018-03-31 16:19 43
sago::SagoTextField* ScoresDisplay::getCachedText(const std::string& text) {
69d8f1dc sago007 2018-03-31 16:19 44
	std::shared_ptr<sago::SagoTextField> ptr = fieldCache[text];
69d8f1dc sago007 2018-03-31 16:19 45
	if (!ptr) {
69d8f1dc sago007 2018-03-31 16:19 46
		std::shared_ptr<sago::SagoTextField> newText = std::make_shared<sago::SagoTextField>();
69d8f1dc sago007 2018-03-31 16:19 47
		sagoTextSetBlueFont(*newText.get());
69d8f1dc sago007 2018-03-31 16:19 48
		newText->SetText(text.c_str());
69d8f1dc sago007 2018-03-31 16:19 49
		fieldCache[text] = newText;
69d8f1dc sago007 2018-03-31 16:19 50
	}
69d8f1dc sago007 2018-03-31 16:19 51
	return fieldCache[text].get();
69d8f1dc sago007 2018-03-31 16:19 52
}
69d8f1dc sago007 2018-03-31 16:19 53
69d8f1dc sago007 2018-03-31 16:19 54
void ScoresDisplay::Write(SDL_Renderer* target, int x, int y, const char* text) {
69d8f1dc sago007 2018-03-31 16:19 55
	getCachedText(text)->Draw(target, x, y);
c3b697c6 sago007 2016-02-07 17:30 56
}
c3b697c6 sago007 2016-02-07 17:30 57
c3b697c6 sago007 2016-02-07 17:30 58
const int numberOfPages = 3;
c3b697c6 sago007 2016-02-07 17:30 59
13476fc2 sago007 2016-09-28 18:34 60
void ScoresDisplay::DrawBackgroundAndCalcPlacements() {
72383a9e sago007 2016-10-30 11:56 61
	DrawBackground(globalData.screen);
72383a9e sago007 2016-10-30 11:56 62
	nextX = globalData.xsize-buttonXsize-20;
72383a9e sago007 2016-10-30 11:56 63
	backY = globalData.ysize-buttonYsize-20;
13476fc2 sago007 2016-09-28 18:34 64
	nextY = backY;
13476fc2 sago007 2016-09-28 18:34 65
}
13476fc2 sago007 2016-09-28 18:34 66
c3b697c6 sago007 2016-02-07 17:30 67
//Draws the highscores
c3b697c6 sago007 2016-02-07 17:30 68
void ScoresDisplay::DrawHighscores(int x, int y, bool endless) {
13476fc2 sago007 2016-09-28 18:34 69
	DrawBackgroundAndCalcPlacements();
c3b697c6 sago007 2016-02-07 17:30 70
	if (endless) {
69d8f1dc sago007 2018-03-31 16:19 71
		Write(globalData.screen, x+100,y+100, _("Endless:") );
c3b697c6 sago007 2016-02-07 17:30 72
	}
c3b697c6 sago007 2016-02-07 17:30 73
	else {
69d8f1dc sago007 2018-03-31 16:19 74
		Write(globalData.screen, x+100,y+100, _("Time Trial:") );
c3b697c6 sago007 2016-02-07 17:30 75
	}
c3b697c6 sago007 2016-02-07 17:30 76
	for (int i =0; i<10; i++) {
7a956470 sago007 2016-02-14 17:09 77
		record r;
c3b697c6 sago007 2016-02-07 17:30 78
		if (endless) {
72383a9e sago007 2016-10-30 11:56 79
			r = globalData.theTopScoresEndless.getScoreNumber(i);
c3b697c6 sago007 2016-02-07 17:30 80
		}
c3b697c6 sago007 2016-02-07 17:30 81
		else {
72383a9e sago007 2016-10-30 11:56 82
			r = globalData.theTopScoresTimeTrial.getScoreNumber(i);
c3b697c6 sago007 2016-02-07 17:30 83
		}
7a956470 sago007 2016-02-14 17:09 84
		char playerScore[32];
7a956470 sago007 2016-02-14 17:09 85
		char playerName[32];
7a956470 sago007 2016-02-14 17:09 86
		snprintf(playerScore, sizeof(playerScore), "%i", r.score);
7a956470 sago007 2016-02-14 17:09 87
		snprintf(playerName, sizeof(playerName), "%s", r.name.c_str());
69d8f1dc sago007 2018-03-31 16:19 88
		Write(globalData.screen, x+420,y+150+i*35, playerScore);
69d8f1dc sago007 2018-03-31 16:19 89
		Write(globalData.screen, x+60,y+150+i*35, playerName);
c3b697c6 sago007 2016-02-07 17:30 90
	}
c3b697c6 sago007 2016-02-07 17:30 91
}
c3b697c6 sago007 2016-02-07 17:30 92
c3b697c6 sago007 2016-02-07 17:30 93
void ScoresDisplay::DrawStats() {
13476fc2 sago007 2016-09-28 18:34 94
	DrawBackgroundAndCalcPlacements();
c3b697c6 sago007 2016-02-07 17:30 95
	int y = 5;
c3b697c6 sago007 2016-02-07 17:30 96
	const int y_spacing = 30;
69d8f1dc sago007 2018-03-31 16:19 97
	Write(globalData.screen, 10,y,_("Stats") );
c3b697c6 sago007 2016-02-07 17:30 98
	y+=y_spacing*2;
69d8f1dc sago007 2018-03-31 16:19 99
	Write(globalData.screen, 10,y,_("Chains") );
c3b697c6 sago007 2016-02-07 17:30 100
	for (int i=2; i<13; i++) {
c3b697c6 sago007 2016-02-07 17:30 101
		y+=y_spacing;
69d8f1dc sago007 2018-03-31 16:19 102
		Write(globalData.screen, 10,y,(std::to_string(i)+"X").c_str());
549ecfae sago007 2016-10-09 08:37 103
		string numberAsString = std::to_string(Stats::getInstance()->getNumberOf("chainX"+std::to_string(i)));
69d8f1dc sago007 2018-03-31 16:19 104
		Write(globalData.screen, 300,y,numberAsString.c_str());
c3b697c6 sago007 2016-02-07 17:30 105
	}
c3b697c6 sago007 2016-02-07 17:30 106
	y+=y_spacing*2;
69d8f1dc sago007 2018-03-31 16:19 107
	Write(globalData.screen, 10,y,_("Lines Pushed: ") );
549ecfae sago007 2016-10-09 08:37 108
	string numberAsString = std::to_string(Stats::getInstance()->getNumberOf("linesPushed"));
69d8f1dc sago007 2018-03-31 16:19 109
	Write(globalData.screen, 300,y,numberAsString.c_str());
c3b697c6 sago007 2016-02-07 17:30 110
c3b697c6 sago007 2016-02-07 17:30 111
	y+=y_spacing;
69d8f1dc sago007 2018-03-31 16:19 112
	Write(globalData.screen, 10,y, _("Puzzles solved: ") );
549ecfae sago007 2016-10-09 08:37 113
	numberAsString = std::to_string(Stats::getInstance()->getNumberOf("puzzlesSolved"));
69d8f1dc sago007 2018-03-31 16:19 114
	Write(globalData.screen, 300,y,numberAsString.c_str());
c3b697c6 sago007 2016-02-07 17:30 115
c3b697c6 sago007 2016-02-07 17:30 116
	y+=y_spacing*2;
69d8f1dc sago007 2018-03-31 16:19 117
	Write(globalData.screen, 10,y, _("Run time: ") );
c3b697c6 sago007 2016-02-07 17:30 118
	commonTime ct = TimeHandler::peekTime("totalTime",TimeHandler::ms2ct(SDL_GetTicks()));
c3b697c6 sago007 2016-02-07 17:30 119
	y+=y_spacing;
69d8f1dc sago007 2018-03-31 16:19 120
	Write(globalData.screen, 10, y, SPrintCF( _("Days: %i"), ct.days) );
c3b697c6 sago007 2016-02-07 17:30 121
	y+=y_spacing;
69d8f1dc sago007 2018-03-31 16:19 122
	Write(globalData.screen, 10, y, SPrintCF( _("Hours: %i"), ct.hours) );
c3b697c6 sago007 2016-02-07 17:30 123
	y+=y_spacing;
69d8f1dc sago007 2018-03-31 16:19 124
	Write(globalData.screen, 10, y, SPrintCF( _("Minutes: %i"), ct.minutes) );
c3b697c6 sago007 2016-02-07 17:30 125
	y+=y_spacing;
69d8f1dc sago007 2018-03-31 16:19 126
	Write(globalData.screen, 10, y, SPrintCF( _("Seconds: %i"), ct.seconds) );
c3b697c6 sago007 2016-02-07 17:30 127
c3b697c6 sago007 2016-02-07 17:30 128
	y-=y_spacing*4; //Four rows back
72383a9e sago007 2016-10-30 11:56 129
	const int x_offset3 = globalData.xsize/3+10; //Ofset for three rows
69d8f1dc sago007 2018-03-31 16:19 130
	Write(globalData.screen, x_offset3,y, _("Play time: ") );
c3b697c6 sago007 2016-02-07 17:30 131
	ct = TimeHandler::getTime("playTime");
c3b697c6 sago007 2016-02-07 17:30 132
	y+=y_spacing;
69d8f1dc sago007 2018-03-31 16:19 133
	Write(globalData.screen, x_offset3, y, SPrintCF( _("Days: %i"), ct.days) );
c3b697c6 sago007 2016-02-07 17:30 134
	y+=y_spacing;
69d8f1dc sago007 2018-03-31 16:19 135
	Write(globalData.screen, x_offset3, y, SPrintCF( _("Hours: %i"), ct.hours) );
c3b697c6 sago007 2016-02-07 17:30 136
	y+=y_spacing;
69d8f1dc sago007 2018-03-31 16:19 137
	Write(globalData.screen, x_offset3, y, SPrintCF( _("Minutes: %i"), ct.minutes) );
c3b697c6 sago007 2016-02-07 17:30 138
	y+=y_spacing;
69d8f1dc sago007 2018-03-31 16:19 139
	Write(globalData.screen, x_offset3, y, SPrintCF( _("Seconds: %i"), ct.seconds) );
c3b697c6 sago007 2016-02-07 17:30 140
72383a9e sago007 2016-10-30 11:56 141
	const int x_offset = globalData.xsize/2+10;
c3b697c6 sago007 2016-02-07 17:30 142
	y = 5+y_spacing*2;
69d8f1dc sago007 2018-03-31 16:19 143
	Write(globalData.screen, x_offset,y, _("VS CPU (win/loss)") );
c3b697c6 sago007 2016-02-07 17:30 144
	for (int i=0; i<7; i++) {
c3b697c6 sago007 2016-02-07 17:30 145
		y += y_spacing;
69d8f1dc sago007 2018-03-31 16:19 146
		Write(globalData.screen, x_offset,y,string("AI "+std::to_string(i+1)).c_str());
549ecfae sago007 2016-10-09 08:37 147
		numberAsString = std::to_string(Stats::getInstance()->getNumberOf("defeatedAI"+std::to_string(i)));
549ecfae sago007 2016-10-09 08:37 148
		string numberAsString2 = std::to_string(Stats::getInstance()->getNumberOf("defeatedByAI"+std::to_string(i)));
c3b697c6 sago007 2016-02-07 17:30 149
		string toPrint = numberAsString + "/" + numberAsString2;
69d8f1dc sago007 2018-03-31 16:19 150
		Write(globalData.screen, x_offset+230,y,toPrint.c_str());
c3b697c6 sago007 2016-02-07 17:30 151
	}
c3b697c6 sago007 2016-02-07 17:30 152
}
c3b697c6 sago007 2016-02-07 17:30 153
c3b697c6 sago007 2016-02-07 17:30 154
ScoresDisplay::ScoresDisplay() {
c3b697c6 sago007 2016-02-07 17:30 155
}
c3b697c6 sago007 2016-02-07 17:30 156
c3b697c6 sago007 2016-02-07 17:30 157
ScoresDisplay::~ScoresDisplay() {
c3b697c6 sago007 2016-02-07 17:30 158
}
c3b697c6 sago007 2016-02-07 17:30 159
c3b697c6 sago007 2016-02-07 17:30 160
bool ScoresDisplay::IsActive() {
c3b697c6 sago007 2016-02-07 17:30 161
	return isActive;
c3b697c6 sago007 2016-02-07 17:30 162
}
c3b697c6 sago007 2016-02-07 17:30 163
647f12da sago007 2016-09-12 20:07 164
void ScoresDisplay::Draw(SDL_Renderer*) {
c3b697c6 sago007 2016-02-07 17:30 165
	switch (page) {
c3b697c6 sago007 2016-02-07 17:30 166
	case 0:
c3b697c6 sago007 2016-02-07 17:30 167
		//Highscores, endless
c3b697c6 sago007 2016-02-07 17:30 168
		DrawHighscores(100,100,true);
c3b697c6 sago007 2016-02-07 17:30 169
		break;
c3b697c6 sago007 2016-02-07 17:30 170
	case 1:
c3b697c6 sago007 2016-02-07 17:30 171
		//Highscores, Time Trial
c3b697c6 sago007 2016-02-07 17:30 172
		DrawHighscores(100,100,false);
c3b697c6 sago007 2016-02-07 17:30 173
		break;
c3b697c6 sago007 2016-02-07 17:30 174
	case 2:
c3b697c6 sago007 2016-02-07 17:30 175
	default:
c3b697c6 sago007 2016-02-07 17:30 176
		DrawStats();
c3b697c6 sago007 2016-02-07 17:30 177
	};
c3b697c6 sago007 2016-02-07 17:30 178
34ab7861 sago007 2018-03-28 14:47 179
	const sago::SagoDataHolder* holder = &globalData.spriteHolder->GetDataHolder();
c3b697c6 sago007 2016-02-07 17:30 180
	//Draw buttons:
72383a9e sago007 2016-10-30 11:56 181
	globalData.bHighScore.Draw(globalData.screen, 0, scoreX,scoreY);
72383a9e sago007 2016-10-30 11:56 182
	globalData.bBack.Draw(globalData.screen, 0, backX, backY);
34ab7861 sago007 2018-03-28 14:47 183
	static sago::SagoTextField backLabel;
34ab7861 sago007 2018-03-28 14:47 184
	setButtonFont(holder, backLabel, _("Back"));
34ab7861 sago007 2018-03-28 14:47 185
	backLabel.Draw(globalData.screen, backX+60,backY+10, sago::SagoTextField::Alignment::center);
72383a9e sago007 2016-10-30 11:56 186
	globalData.bNext.Draw(globalData.screen, 0, nextX, nextY);
34ab7861 sago007 2018-03-28 14:47 187
	static sago::SagoTextField nextLabel;
34ab7861 sago007 2018-03-28 14:47 188
	setButtonFont(holder, nextLabel, _("Next"));
34ab7861 sago007 2018-03-28 14:47 189
	nextLabel.Draw(globalData.screen, nextX+60, nextY+10, sago::SagoTextField::Alignment::center);
c3b697c6 sago007 2016-02-07 17:30 190
c3b697c6 sago007 2016-02-07 17:30 191
	//Draw page number
d3f1e001 Poul Sander 2017-03-29 18:17 192
	string pageXofY = SPrintStringF(_("Page %i of %i"), page+1, numberOfPages);
c9a65d81 sago007 2018-03-31 16:22 193
	getCachedText(pageXofY)->Draw(globalData.screen,  globalData.xsize/2, globalData.ysize-60, sago::SagoTextField::Alignment::center);
c3b697c6 sago007 2016-02-07 17:30 194
}
c3b697c6 sago007 2016-02-07 17:30 195
58a2d7e1 sago007 2016-02-12 18:53 196
void ScoresDisplay::ProcessInput(const SDL_Event& event, bool& processed) {
c7f2082f sago007 2016-03-13 11:48 197
20d77c6e sago007 2016-11-12 16:28 198
	UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey);
092bdebe sago007 2017-03-19 14:39 199
d21782d4 sago007 2016-03-13 11:17 200
	if (isLeftEvent(event)) {
d21782d4 sago007 2016-03-13 11:17 201
		page++;
d21782d4 sago007 2016-03-13 11:17 202
		if (page>=numberOfPages) {
d21782d4 sago007 2016-03-13 11:17 203
			page = 0;
c3b697c6 sago007 2016-02-07 17:30 204
		}
647f12da sago007 2016-09-12 20:07 205
		processed = true;
d21782d4 sago007 2016-03-13 11:17 206
	}
8e3fb7e1 sago007 2016-03-13 11:26 207
d21782d4 sago007 2016-03-13 11:17 208
	if (isRightEvent(event)) {
d21782d4 sago007 2016-03-13 11:17 209
		page--;
d21782d4 sago007 2016-03-13 11:17 210
		if (page<0) {
d21782d4 sago007 2016-03-13 11:17 211
			page = numberOfPages-1;
c3b697c6 sago007 2016-02-07 17:30 212
		}
647f12da sago007 2016-09-12 20:07 213
		processed = true;
c3b697c6 sago007 2016-02-07 17:30 214
	}
8e3fb7e1 sago007 2016-03-13 11:26 215
d21782d4 sago007 2016-03-13 11:17 216
	if (isConfirmEvent(event) || isEscapeEvent(event)) {
d21782d4 sago007 2016-03-13 11:17 217
		isActive = false;
647f12da sago007 2016-09-12 20:07 218
		processed = true;
d21782d4 sago007 2016-03-13 11:17 219
	}
c3b697c6 sago007 2016-02-07 17:30 220
}
c3b697c6 sago007 2016-02-07 17:30 221
c3b697c6 sago007 2016-02-07 17:30 222
void ScoresDisplay::Update() {
c3b697c6 sago007 2016-02-07 17:30 223
	// If the mouse button is released, make bMouseUp equal true
08c44684 sago007 2016-10-30 09:19 224
	if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
c3b697c6 sago007 2016-02-07 17:30 225
		bMouseUp=true;
c3b697c6 sago007 2016-02-07 17:30 226
	}
c3b697c6 sago007 2016-02-07 17:30 227
c3b697c6 sago007 2016-02-07 17:30 228
	if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) {
c3b697c6 sago007 2016-02-07 17:30 229
		bMouseUp = false;
c3b697c6 sago007 2016-02-07 17:30 230
c3b697c6 sago007 2016-02-07 17:30 231
		//The Score button:
20d77c6e sago007 2016-11-12 16:28 232
		if ((globalData.mousex>scoreX) && (globalData.mousex<scoreX+buttonXsize) && (globalData.mousey>scoreY) && (globalData.mousey<scoreY+buttonYsize)) {
c3b697c6 sago007 2016-02-07 17:30 233
			isActive = false;
c3b697c6 sago007 2016-02-07 17:30 234
		}
c3b697c6 sago007 2016-02-07 17:30 235
c3b697c6 sago007 2016-02-07 17:30 236
		//The back button:
20d77c6e sago007 2016-11-12 16:28 237
		if ((globalData.mousex>backX) && (globalData.mousex<backX+buttonXsize) && (globalData.mousey>backY) && (globalData.mousey<backY+buttonYsize)) {
c3b697c6 sago007 2016-02-07 17:30 238
			page--;
c3b697c6 sago007 2016-02-07 17:30 239
			if (page<0) {
c3b697c6 sago007 2016-02-07 17:30 240
				page = numberOfPages-1;
c3b697c6 sago007 2016-02-07 17:30 241
			}
c3b697c6 sago007 2016-02-07 17:30 242
		}
c3b697c6 sago007 2016-02-07 17:30 243
c3b697c6 sago007 2016-02-07 17:30 244
		//The next button:
20d77c6e sago007 2016-11-12 16:28 245
		if ((globalData.mousex>nextX) && (globalData.mousex<nextX+buttonXsize) && (globalData.mousey>nextY) && (globalData.mousey<nextY+buttonYsize)) {
c3b697c6 sago007 2016-02-07 17:30 246
			page++;
c3b697c6 sago007 2016-02-07 17:30 247
			if (page>=numberOfPages) {
c3b697c6 sago007 2016-02-07 17:30 248
				page = 0;
c3b697c6 sago007 2016-02-07 17:30 249
			}
c3b697c6 sago007 2016-02-07 17:30 250
		}
c3b697c6 sago007 2016-02-07 17:30 251
	}
c3b697c6 sago007 2016-02-07 17:30 252
}