File: ScoresDisplay.cpp

package info (click to toggle)
blockattack 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,292 kB
  • sloc: cpp: 93,645; sh: 143; pascal: 111; xml: 82; makefile: 14
file content (333 lines) | stat: -rw-r--r-- 10,138 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
===========================================================================
blockattack - Block Attack - Rise of the Blocks
Copyright (C) 2005-2016 Poul Sander

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see http://www.gnu.org/licenses/

Source information and contacts persons can be found at
http://www.blockattack.net
===========================================================================
*/

#include "ScoresDisplay.hpp"
#include "global.hpp"
#include "common.h"
#include "stats.h"
#include "MenuSystem.h"
#include <fmt/core.h>


static void setButtonFont(const sago::SagoDataHolder* holder, sago::SagoTextField& field, const char* text) {
	field.SetHolder(holder);
	field.SetFont("freeserif");
	field.SetColor({255,255,255,255});
	field.SetFontSize(24);
	field.SetOutline(1, {128,128,128,255});
	field.SetText(text);
}

sago::SagoTextField* ScoresDisplay::getCachedText(const std::string& text) {
	std::shared_ptr<sago::SagoTextField> ptr = fieldCache[text];
	if (!ptr) {
		std::shared_ptr<sago::SagoTextField> newText = std::make_shared<sago::SagoTextField>();
		sagoTextSetBlueFont(*newText.get());
		newText->SetText(text);
		fieldCache[text] = newText;
	}
	return fieldCache[text].get();
}

void ScoresDisplay::Write(SDL_Renderer* target, int x, int y, const char* text) {
	getCachedText(text)->Draw(target, x, y, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::top, &globalData.logicalResize);
}


void ScoresDisplay::Write(SDL_Renderer* target, int x, int y, const std::string& text) {
	Write(target, x, y, text.c_str());
}

const int numberOfPages = 13;

void ScoresDisplay::DrawBackgroundAndCalcPlacements() {
	DrawBackground(globalData.screen);
	nextX = globalData.xsize-buttonXsize-20;
	backY = globalData.ysize-buttonYsize-18;
	nextY = backY;
}

//Draws the highscores
void ScoresDisplay::DrawHighscores(int x, int y, bool endless, int level = 0) {
	DrawBackgroundAndCalcPlacements();
	if (endless) {
		std::string header;
		switch (level) {
		case 1:
			header = _("Endless (Fast):");
			break;
		case 2:
			header = _("Endless (Faster):");
			break;
		case 3:
			header = _("Endless (Even faster):");
			break;
		case 4:
			header = _("Endless (Fastest):");
			break;
		case 5:
			header = _("Endless (5 blocks):");
			break;
		case 6:
			header = _("Endless (5 blocks, Fast):");
			break;
		case 7:
			header = _("Endless (5 blocks, Faster):");
			break;
		case 8:
			header = _("Endless (5 blocks, Even faster):");
			break;
		case 9:
			header = _("Endless (5 blocks, Fastest):");
			break;
		default:
			header = _("Endless:");
		};
		Write(globalData.screen, x+100,y+100, header );
	}
	else {
		std::string header = _("Time Trial:");
		if (level == 1) {
			header = _("Time Trial (5 blocks):");
		}
		Write(globalData.screen, x+100,y+100, header );
	}
	for (int i =0; i<10; i++) {
		record r;
		if (endless) {
			switch (level) {
			case 1:
				r = theTopScoresEndless1.getScoreNumber(i);
				break;
			case 2:
				r = theTopScoresEndless2.getScoreNumber(i);
				break;
			case 3:
				r = theTopScoresEndless3.getScoreNumber(i);
				break;
			case 4:
				r = theTopScoresEndless4.getScoreNumber(i);
				break;
			case 5:
				r = theTopScoresEndless0_5.getScoreNumber(i);
				break;
			case 6:
				r = theTopScoresEndless1_5.getScoreNumber(i);
				break;
			case 7:
				r = theTopScoresEndless2_5.getScoreNumber(i);
				break;
			case 8:
				r = theTopScoresEndless3_5.getScoreNumber(i);
				break;
			case 9:
				r = theTopScoresEndless4_5.getScoreNumber(i);
				break;
			default:
				r = theTopScoresEndless0.getScoreNumber(i);
			}
		}
		else {
			if (level == 1) {
				r = theTopScoresTimeTrial_5.getScoreNumber(i);
			}
			else {
				r = theTopScoresTimeTrial.getScoreNumber(i);
			}
		}
		char playerScore[32];
		char playerName[32];
		snprintf(playerScore, sizeof(playerScore), "%i", r.score);
		snprintf(playerName, sizeof(playerName), "%s", r.name.c_str());
		Write(globalData.screen, x+420,y+150+i*35, playerScore);
		Write(globalData.screen, x+60,y+150+i*35, playerName);
	}
}

void ScoresDisplay::DrawStats() {
	DrawBackgroundAndCalcPlacements();
	int y = 5;
	const int y_spacing = 30;
	Write(globalData.screen, 10,y,_("Stats") );
	y+=y_spacing*2;
	Write(globalData.screen, 10,y,_("Chains") );
	for (int i=2; i<13; i++) {
		y+=y_spacing;
		Write(globalData.screen, 10,y,std::to_string(i)+"X");
		std::string numberAsString = std::to_string(Stats::getInstance()->getNumberOf("chainX"+std::to_string(i)));
		Write(globalData.screen, 300,y,numberAsString);
	}
	y+=y_spacing*2;
	Write(globalData.screen, 10,y,_("Lines Pushed: ") );
	std::string numberAsString = std::to_string(Stats::getInstance()->getNumberOf("linesPushed"));
	Write(globalData.screen, 300,y,numberAsString);

	y+=y_spacing;
	Write(globalData.screen, 10,y, _("Puzzles solved: ") );
	numberAsString = std::to_string(Stats::getInstance()->getNumberOf("puzzlesSolved"));
	Write(globalData.screen, 300,y,numberAsString);

	y+=y_spacing*2;
	Write(globalData.screen, 10,y, _("Run time: ") );
	commonTime ct = TimeHandler::peekTime("totalTime",TimeHandler::ms2ct(SDL_GetTicks()));
	y+=y_spacing;
	Write(globalData.screen, 10, y, fmt::format( _("Days: {}"), ct.days) );
	y+=y_spacing;
	Write(globalData.screen, 10, y, fmt::format( _("Hours: {:02}"), ct.hours) );
	y+=y_spacing;
	Write(globalData.screen, 10, y, fmt::format( _("Minutes: {:02}"), ct.minutes) );
	y+=y_spacing;
	Write(globalData.screen, 10, y, fmt::format( _("Seconds: {:02}"), ct.seconds) );

	y-=y_spacing*4; //Four rows back
	const int x_offset3 = globalData.xsize/3+10; //Ofset for three rows
	Write(globalData.screen, x_offset3,y, _("Play time: ") );
	ct = TimeHandler::getTime("playTime");
	y+=y_spacing;
	Write(globalData.screen, x_offset3, y, fmt::format( _("Days: {}"), ct.days) );
	y+=y_spacing;
	Write(globalData.screen, x_offset3, y, fmt::format( _("Hours: {:02}"), ct.hours) );
	y+=y_spacing;
	Write(globalData.screen, x_offset3, y, fmt::format( _("Minutes: {:02}"), ct.minutes) );
	y+=y_spacing;
	Write(globalData.screen, x_offset3, y, fmt::format( _("Seconds: {:02}"), ct.seconds) );

	const int x_offset = globalData.xsize/2+10;
	y = 5+y_spacing*2;
	Write(globalData.screen, x_offset,y, _("VS CPU (win/loss)") );
	for (int i=0; i<7; i++) {
		y += y_spacing;
		Write(globalData.screen, x_offset,y, fmt::format("AI {}",i+1) );
		numberAsString = std::to_string(Stats::getInstance()->getNumberOf("defeatedAI"+std::to_string(i)));
		std::string numberAsString2 = std::to_string(Stats::getInstance()->getNumberOf("defeatedByAI"+std::to_string(i)));
		std::string toPrint = numberAsString + "/" + numberAsString2;
		Write(globalData.screen, x_offset+230,y,toPrint);
	}
}

ScoresDisplay::ScoresDisplay() {
}

ScoresDisplay::~ScoresDisplay() {
}

void ScoresDisplay::Draw(SDL_Renderer* target) {
	switch (page) {
	case 0:
	case 1:
	case 2:
	case 3:
	case 4:
		//Highscores, endless
		DrawHighscores(100,100,true, page);
		break;
	case 5:
		//Highscores, Time Trial
		DrawHighscores(100,100,false);
		break;
	case 6:
	case 7:
	case 8:
	case 9:
	case 10:
		//Highscores, endless 5 blocks
		DrawHighscores(100,100,true, page-1);
		break;
	case 11:
		//Highscores, Time Trial 5 blocks
		DrawHighscores(100,100,false, 1);
		break;
	case 12:
	default:
		DrawStats();
	};

	const sago::SagoDataHolder* holder = &globalData.spriteHolder->GetDataHolder();
	//Draw buttons:
	globalData.bBack.Draw(globalData.screen, 0, backX, backY, &globalData.logicalResize);
	static sago::SagoTextField backLabel;
	setButtonFont(holder, backLabel, _("Back"));
	backLabel.Draw(globalData.screen, backX+60,backY+10, sago::SagoTextField::Alignment::center, sago::SagoTextField::VerticalAlignment::top, &globalData.logicalResize);
	globalData.bNext.Draw(globalData.screen, 0, nextX, nextY, &globalData.logicalResize);
	static sago::SagoTextField nextLabel;
	setButtonFont(holder, nextLabel, _("Next"));
	nextLabel.Draw(globalData.screen, nextX+60, nextY+10, sago::SagoTextField::Alignment::center, sago::SagoTextField::VerticalAlignment::top, &globalData.logicalResize);

	//Draw page number
	std::string pageXofY = fmt::format(_("Page {} of {}"), page+1, numberOfPages);
	getCachedText(pageXofY)->Draw(globalData.screen,  globalData.xsize/2, globalData.ysize-60, sago::SagoTextField::Alignment::center, sago::SagoTextField::VerticalAlignment::top, &globalData.logicalResize);
	HelpCommonState::Draw(target);
}

void ScoresDisplay::ProcessInput(const SDL_Event& event, bool& processed) {

	UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey);

	if (isRightEvent(event)) {
		page++;
		if (page>=numberOfPages) {
			page = 0;
		}
		processed = true;
	}

	if (isLeftEvent(event)) {
		page--;
		if (page<0) {
			page = numberOfPages-1;
		}
		processed = true;
	}
	HelpCommonState::ProcessInput(event, processed);
}

void ScoresDisplay::Update() {
	// If the mouse button is released, make bMouseUp equal true
	if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
		bMouseUp=true;
	}

	if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) {
		bMouseUp = false;
		int mousex;
		int mousey;
		globalData.logicalResize.PhysicalToLogical(globalData.mousex, globalData.mousey, mousex, mousey);

		//The back button:
		if ((mousex>backX) && (mousex<backX+buttonXsize) && (mousey>backY) && (mousey<backY+buttonYsize)) {
			page--;
			if (page<0) {
				page = numberOfPages-1;
			}
		}

		//The next button:
		if ((mousex>nextX) && (mousex<nextX+buttonXsize) && (mousey>nextY) && (mousey<nextY+buttonYsize)) {
			page++;
			if (page>=numberOfPages) {
				page = 0;
			}
		}
	}
	HelpCommonState::Update();
}