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
c3b697c6 sago007 2016-02-07 17:30 34
static void NFont_Write(SDL_Renderer* target, int x, int y, const char* text) {
c3b697c6 sago007 2016-02-07 17:30 35
	nf_standard_blue_font.draw(target, x, y, "%s", text);
c3b697c6 sago007 2016-02-07 17:30 36
}
c3b697c6 sago007 2016-02-07 17:30 37
c3b697c6 sago007 2016-02-07 17:30 38
const int numberOfPages = 3;
c3b697c6 sago007 2016-02-07 17:30 39
c3b697c6 sago007 2016-02-07 17:30 40
//Draws the highscores
c3b697c6 sago007 2016-02-07 17:30 41
void ScoresDisplay::DrawHighscores(int x, int y, bool endless) {
c3b697c6 sago007 2016-02-07 17:30 42
	backgroundImage.Draw(screen, 0, 0, 0);
c3b697c6 sago007 2016-02-07 17:30 43
	if (endless) {
c3b697c6 sago007 2016-02-07 17:30 44
		nf_standard_blue_font.draw(screen, x+100,y+100, "%s",_("Endless:") );
c3b697c6 sago007 2016-02-07 17:30 45
	}
c3b697c6 sago007 2016-02-07 17:30 46
	else {
c3b697c6 sago007 2016-02-07 17:30 47
		nf_standard_blue_font.draw(screen, x+100,y+100, "%s",_("Time Trial:") );
c3b697c6 sago007 2016-02-07 17:30 48
	}
c3b697c6 sago007 2016-02-07 17:30 49
	for (int i =0; i<10; i++) {
7a956470 sago007 2016-02-14 17:09 50
		record r;
c3b697c6 sago007 2016-02-07 17:30 51
		if (endless) {
7a956470 sago007 2016-02-14 17:09 52
			r = theTopScoresEndless.getScoreNumber(i);
c3b697c6 sago007 2016-02-07 17:30 53
		}
c3b697c6 sago007 2016-02-07 17:30 54
		else {
7a956470 sago007 2016-02-14 17:09 55
			r = theTopScoresTimeTrial.getScoreNumber(i);
c3b697c6 sago007 2016-02-07 17:30 56
		}
7a956470 sago007 2016-02-14 17:09 57
		char playerScore[32];
7a956470 sago007 2016-02-14 17:09 58
		char playerName[32];
7a956470 sago007 2016-02-14 17:09 59
		snprintf(playerScore, sizeof(playerScore), "%i", r.score);
7a956470 sago007 2016-02-14 17:09 60
		snprintf(playerName, sizeof(playerName), "%s", r.name.c_str());
c3b697c6 sago007 2016-02-07 17:30 61
		nf_standard_blue_font.draw(screen, x+420,y+150+i*35, "%s",playerScore);
c3b697c6 sago007 2016-02-07 17:30 62
		nf_standard_blue_font.draw(screen, x+60,y+150+i*35, "%s",playerName);
c3b697c6 sago007 2016-02-07 17:30 63
	}
c3b697c6 sago007 2016-02-07 17:30 64
}
c3b697c6 sago007 2016-02-07 17:30 65
c3b697c6 sago007 2016-02-07 17:30 66
void ScoresDisplay::DrawStats() {
c3b697c6 sago007 2016-02-07 17:30 67
	backgroundImage.Draw(screen, 0, 0, 0);
c3b697c6 sago007 2016-02-07 17:30 68
	int y = 5;
c3b697c6 sago007 2016-02-07 17:30 69
	const int y_spacing = 30;
c3b697c6 sago007 2016-02-07 17:30 70
	NFont_Write(screen, 10,y,_("Stats") );
c3b697c6 sago007 2016-02-07 17:30 71
	y+=y_spacing*2;
c3b697c6 sago007 2016-02-07 17:30 72
	NFont_Write(screen, 10,y,_("Chains") );
c3b697c6 sago007 2016-02-07 17:30 73
	for (int i=2; i<13; i++) {
c3b697c6 sago007 2016-02-07 17:30 74
		y+=y_spacing;
c3b697c6 sago007 2016-02-07 17:30 75
		NFont_Write(screen, 10,y,(itoa(i)+"X").c_str());
c3b697c6 sago007 2016-02-07 17:30 76
		string numberAsString = itoa(Stats::getInstance()->getNumberOf("chainX"+itoa(i)));
c3b697c6 sago007 2016-02-07 17:30 77
		NFont_Write(screen, 300,y,numberAsString.c_str());
c3b697c6 sago007 2016-02-07 17:30 78
	}
c3b697c6 sago007 2016-02-07 17:30 79
	y+=y_spacing*2;
c3b697c6 sago007 2016-02-07 17:30 80
	NFont_Write(screen, 10,y,_("Lines Pushed: ") );
c3b697c6 sago007 2016-02-07 17:30 81
	string numberAsString = itoa(Stats::getInstance()->getNumberOf("linesPushed"));
c3b697c6 sago007 2016-02-07 17:30 82
	NFont_Write(screen, 300,y,numberAsString.c_str());
c3b697c6 sago007 2016-02-07 17:30 83
c3b697c6 sago007 2016-02-07 17:30 84
	y+=y_spacing;
c3b697c6 sago007 2016-02-07 17:30 85
	NFont_Write(screen, 10,y, _("Puzzles solved: ") );
c3b697c6 sago007 2016-02-07 17:30 86
	numberAsString = itoa(Stats::getInstance()->getNumberOf("puzzlesSolved"));
c3b697c6 sago007 2016-02-07 17:30 87
	NFont_Write(screen, 300,y,numberAsString.c_str());
c3b697c6 sago007 2016-02-07 17:30 88
c3b697c6 sago007 2016-02-07 17:30 89
	y+=y_spacing*2;
c3b697c6 sago007 2016-02-07 17:30 90
	NFont_Write(screen, 10,y, _("Run time: ") );
c3b697c6 sago007 2016-02-07 17:30 91
	commonTime ct = TimeHandler::peekTime("totalTime",TimeHandler::ms2ct(SDL_GetTicks()));
c3b697c6 sago007 2016-02-07 17:30 92
	y+=y_spacing;
c3b697c6 sago007 2016-02-07 17:30 93
	NFont_Write(screen, 10, y, SPrintCF( _("Days: %i"), ct.days) );
c3b697c6 sago007 2016-02-07 17:30 94
	y+=y_spacing;
c3b697c6 sago007 2016-02-07 17:30 95
	NFont_Write(screen, 10, y, SPrintCF( _("Hours: %i"), ct.hours) );
c3b697c6 sago007 2016-02-07 17:30 96
	y+=y_spacing;
c3b697c6 sago007 2016-02-07 17:30 97
	NFont_Write(screen, 10, y, SPrintCF( _("Minutes: %i"), ct.minutes) );
c3b697c6 sago007 2016-02-07 17:30 98
	y+=y_spacing;
c3b697c6 sago007 2016-02-07 17:30 99
	NFont_Write(screen, 10, y, SPrintCF( _("Seconds: %i"), ct.seconds) );
c3b697c6 sago007 2016-02-07 17:30 100
c3b697c6 sago007 2016-02-07 17:30 101
	y-=y_spacing*4; //Four rows back
c3b697c6 sago007 2016-02-07 17:30 102
	const int x_offset3 = xsize/3+10; //Ofset for three rows
c3b697c6 sago007 2016-02-07 17:30 103
	NFont_Write(screen, x_offset3,y, _("Play time: ") );
c3b697c6 sago007 2016-02-07 17:30 104
	ct = TimeHandler::getTime("playTime");
c3b697c6 sago007 2016-02-07 17:30 105
	y+=y_spacing;
c3b697c6 sago007 2016-02-07 17:30 106
	NFont_Write(screen, x_offset3, y, SPrintCF( _("Days: %i"), ct.days) );
c3b697c6 sago007 2016-02-07 17:30 107
	y+=y_spacing;
c3b697c6 sago007 2016-02-07 17:30 108
	NFont_Write(screen, x_offset3, y, SPrintCF( _("Hours: %i"), ct.hours) );
c3b697c6 sago007 2016-02-07 17:30 109
	y+=y_spacing;
c3b697c6 sago007 2016-02-07 17:30 110
	NFont_Write(screen, x_offset3, y, SPrintCF( _("Minutes: %i"), ct.minutes) );
c3b697c6 sago007 2016-02-07 17:30 111
	y+=y_spacing;
c3b697c6 sago007 2016-02-07 17:30 112
	NFont_Write(screen, x_offset3, y, SPrintCF( _("Seconds: %i"), ct.seconds) );
c3b697c6 sago007 2016-02-07 17:30 113
c3b697c6 sago007 2016-02-07 17:30 114
	const int x_offset = xsize/2+10;
c3b697c6 sago007 2016-02-07 17:30 115
	y = 5+y_spacing*2;
c3b697c6 sago007 2016-02-07 17:30 116
	NFont_Write(screen, x_offset,y, _("VS CPU (win/loss)") );
c3b697c6 sago007 2016-02-07 17:30 117
	for (int i=0; i<7; i++) {
c3b697c6 sago007 2016-02-07 17:30 118
		y += y_spacing;
c3b697c6 sago007 2016-02-07 17:30 119
		NFont_Write(screen, x_offset,y,string("AI "+itoa(i+1)).c_str());
c3b697c6 sago007 2016-02-07 17:30 120
		numberAsString = itoa(Stats::getInstance()->getNumberOf("defeatedAI"+itoa(i)));
c3b697c6 sago007 2016-02-07 17:30 121
		string numberAsString2 = itoa(Stats::getInstance()->getNumberOf("defeatedByAI"+itoa(i)));
c3b697c6 sago007 2016-02-07 17:30 122
		string toPrint = numberAsString + "/" + numberAsString2;
c3b697c6 sago007 2016-02-07 17:30 123
		NFont_Write(screen, x_offset+230,y,toPrint.c_str());
c3b697c6 sago007 2016-02-07 17:30 124
	}
c3b697c6 sago007 2016-02-07 17:30 125
}
c3b697c6 sago007 2016-02-07 17:30 126
c3b697c6 sago007 2016-02-07 17:30 127
ScoresDisplay::ScoresDisplay() {
c3b697c6 sago007 2016-02-07 17:30 128
}
c3b697c6 sago007 2016-02-07 17:30 129
c3b697c6 sago007 2016-02-07 17:30 130
ScoresDisplay::~ScoresDisplay() {
c3b697c6 sago007 2016-02-07 17:30 131
}
c3b697c6 sago007 2016-02-07 17:30 132
c3b697c6 sago007 2016-02-07 17:30 133
bool ScoresDisplay::IsActive() {
c3b697c6 sago007 2016-02-07 17:30 134
	return isActive;
c3b697c6 sago007 2016-02-07 17:30 135
}
c3b697c6 sago007 2016-02-07 17:30 136
c3b697c6 sago007 2016-02-07 17:30 137
void ScoresDisplay::Draw(SDL_Renderer* target) {
c3b697c6 sago007 2016-02-07 17:30 138
	switch (page) {
c3b697c6 sago007 2016-02-07 17:30 139
	case 0:
c3b697c6 sago007 2016-02-07 17:30 140
		//Highscores, endless
c3b697c6 sago007 2016-02-07 17:30 141
		DrawHighscores(100,100,true);
c3b697c6 sago007 2016-02-07 17:30 142
		break;
c3b697c6 sago007 2016-02-07 17:30 143
	case 1:
c3b697c6 sago007 2016-02-07 17:30 144
		//Highscores, Time Trial
c3b697c6 sago007 2016-02-07 17:30 145
		DrawHighscores(100,100,false);
c3b697c6 sago007 2016-02-07 17:30 146
		break;
c3b697c6 sago007 2016-02-07 17:30 147
	case 2:
c3b697c6 sago007 2016-02-07 17:30 148
	default:
c3b697c6 sago007 2016-02-07 17:30 149
		DrawStats();
c3b697c6 sago007 2016-02-07 17:30 150
	};
c3b697c6 sago007 2016-02-07 17:30 151
c3b697c6 sago007 2016-02-07 17:30 152
	//Draw buttons:
c3b697c6 sago007 2016-02-07 17:30 153
	bHighScore.Draw(screen, 0, scoreX,scoreY);
c3b697c6 sago007 2016-02-07 17:30 154
	bBack.Draw(screen, 0, backX, backY);
c3b697c6 sago007 2016-02-07 17:30 155
	nf_button_font.draw(screen, backX+60,backY+10, NFont::CENTER ,_("Back"));
c3b697c6 sago007 2016-02-07 17:30 156
	bNext.Draw(screen, 0, nextX, nextY);
c3b697c6 sago007 2016-02-07 17:30 157
	nf_button_font.draw(screen, nextX+60,nextY+10, NFont::CENTER,_("Next"));
c3b697c6 sago007 2016-02-07 17:30 158
c3b697c6 sago007 2016-02-07 17:30 159
	//Draw page number
c3b697c6 sago007 2016-02-07 17:30 160
	string pageXofY = (boost::format(_("Page %1% of %2%") )%(page+1)%numberOfPages).str();
c3b697c6 sago007 2016-02-07 17:30 161
	NFont_Write(screen, xsize/2-nf_standard_blue_font.getWidth( "%s", pageXofY.c_str())/2,ysize-60,pageXofY.c_str());
c3b697c6 sago007 2016-02-07 17:30 162
}
c3b697c6 sago007 2016-02-07 17:30 163
58a2d7e1 sago007 2016-02-12 18:53 164
void ScoresDisplay::ProcessInput(const SDL_Event& event, bool& processed) {
c7f2082f sago007 2016-03-13 11:48 165
d21782d4 sago007 2016-03-13 11:17 166
	if (isLeftEvent(event)) {
d21782d4 sago007 2016-03-13 11:17 167
		page++;
d21782d4 sago007 2016-03-13 11:17 168
		if (page>=numberOfPages) {
d21782d4 sago007 2016-03-13 11:17 169
			page = 0;
c3b697c6 sago007 2016-02-07 17:30 170
		}
d21782d4 sago007 2016-03-13 11:17 171
	}
8e3fb7e1 sago007 2016-03-13 11:26 172
d21782d4 sago007 2016-03-13 11:17 173
	if (isRightEvent(event)) {
d21782d4 sago007 2016-03-13 11:17 174
		page--;
d21782d4 sago007 2016-03-13 11:17 175
		if (page<0) {
d21782d4 sago007 2016-03-13 11:17 176
			page = numberOfPages-1;
c3b697c6 sago007 2016-02-07 17:30 177
		}
c3b697c6 sago007 2016-02-07 17:30 178
	}
8e3fb7e1 sago007 2016-03-13 11:26 179
d21782d4 sago007 2016-03-13 11:17 180
	if (isConfirmEvent(event) || isEscapeEvent(event)) {
d21782d4 sago007 2016-03-13 11:17 181
		isActive = false;
d21782d4 sago007 2016-03-13 11:17 182
	}
c3b697c6 sago007 2016-02-07 17:30 183
}
c3b697c6 sago007 2016-02-07 17:30 184
c3b697c6 sago007 2016-02-07 17:30 185
void ScoresDisplay::Update() {
c3b697c6 sago007 2016-02-07 17:30 186
	int mousex, mousey;
c3b697c6 sago007 2016-02-07 17:30 187
	SDL_GetMouseState(&mousex,&mousey);
58a2d7e1 sago007 2016-02-12 18:53 188
c3b697c6 sago007 2016-02-07 17:30 189
	// If the mouse button is released, make bMouseUp equal true
c3b697c6 sago007 2016-02-07 17:30 190
	if (!SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) {
c3b697c6 sago007 2016-02-07 17:30 191
		bMouseUp=true;
c3b697c6 sago007 2016-02-07 17:30 192
	}
c3b697c6 sago007 2016-02-07 17:30 193
c3b697c6 sago007 2016-02-07 17:30 194
	if (SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(1) && bMouseUp) {
c3b697c6 sago007 2016-02-07 17:30 195
		bMouseUp = false;
c3b697c6 sago007 2016-02-07 17:30 196
c3b697c6 sago007 2016-02-07 17:30 197
		//The Score button:
c3b697c6 sago007 2016-02-07 17:30 198
		if ((mousex>scoreX) && (mousex<scoreX+buttonXsize) && (mousey>scoreY) && (mousey<scoreY+buttonYsize)) {
c3b697c6 sago007 2016-02-07 17:30 199
			isActive = false;
c3b697c6 sago007 2016-02-07 17:30 200
		}
c3b697c6 sago007 2016-02-07 17:30 201
c3b697c6 sago007 2016-02-07 17:30 202
		//The back button:
c3b697c6 sago007 2016-02-07 17:30 203
		if ((mousex>backX) && (mousex<backX+buttonXsize) && (mousey>backY) && (mousey<backY+buttonYsize)) {
c3b697c6 sago007 2016-02-07 17:30 204
			page--;
c3b697c6 sago007 2016-02-07 17:30 205
			if (page<0) {
c3b697c6 sago007 2016-02-07 17:30 206
				page = numberOfPages-1;
c3b697c6 sago007 2016-02-07 17:30 207
			}
c3b697c6 sago007 2016-02-07 17:30 208
		}
c3b697c6 sago007 2016-02-07 17:30 209
c3b697c6 sago007 2016-02-07 17:30 210
		//The next button:
c3b697c6 sago007 2016-02-07 17:30 211
		if ((mousex>nextX) && (mousex<nextX+buttonXsize) && (mousey>nextY) && (mousey<nextY+buttonYsize)) {
c3b697c6 sago007 2016-02-07 17:30 212
			page++;
c3b697c6 sago007 2016-02-07 17:30 213
			if (page>=numberOfPages) {
c3b697c6 sago007 2016-02-07 17:30 214
				page = 0;
c3b697c6 sago007 2016-02-07 17:30 215
			}
c3b697c6 sago007 2016-02-07 17:30 216
		}
c3b697c6 sago007 2016-02-07 17:30 217
	}
c3b697c6 sago007 2016-02-07 17:30 218
}