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