/
HighScores.cpp
351 lines (300 loc) · 8.78 KB
/
HighScores.cpp
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//
// HighScores.cpp - Implementation of high scores screen.
// Copyright (C) 2006-2009 Nick Gasson
//
// 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, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "HighScores.hpp"
#include "Input.hpp"
#include "InterfaceSounds.hpp"
HighScores::HighScores()
: hscoreImage("images/hscore.png"),
largeFont(LocateResource("Default_Font.ttf"), 15),
scoreNameFont(LocateResource("Default_Font.ttf"), 14),
fwBang(LocateResource("sounds/firework_1.wav"))
{
}
//
// Called just before the screen is to be displayed. Resets variables
// and loads data.
//
void HighScores::Load()
{
// Set state
state = hssDisplay;
}
//
// Processes user input. Called at the start of each frame.
//
void HighScores::Process()
{
Input& input = Input::GetInstance();
OpenGL& opengl = OpenGL::GetInstance();
int i;
// Check for input
if (state == hssDisplay) {
if (input.QueryAction(Input::SKIP)
|| input.QueryAction(Input::ABORT)
|| input.QueryAction(Input::FIRE)) {
// Go back to main menu
fade = HS_FADE_OUT_SPEED;
for (i = 0; i < MAX_FIREWORKS; i++) {
fw[i].em->maxspeed = 200;
fw[i].speed = 0;
fw[i].em->createrate = 2.0f;
fw[i].timeout = 5;
fw[i].em->life = 0.5f;
}
input.ResetAction(Input::SKIP);
input.ResetAction(Input::FIRE);
input.ResetAction(Input::ABORT);
InterfaceSounds::PlaySelect();
}
}
else if (state == hssEnterName) {
if (input.QueryAction(Input::FIRE)
&& strlen(input.GetInput()) > 0) {
// Enter name into high score chart
scoreFile.Insert(input.GetInput(), newscore);
input.CloseCharBuffer();
scoreFile.Save();
state = hssDisplay;
flAlpha = 0.0f;
fade = HS_FADE_IN_SPEED;
input.ResetAction(Input::FIRE);
InterfaceSounds::PlaySelect();
}
}
// Process fireworks
for (i = 0; i < MAX_FIREWORKS; i++) {
if (fw[i].active) {
fw[i].y-=fw[i].speed;
fw[i].em->xpos = (float)fw[i].x;
fw[i].em->ypos = (float)fw[i].y;
if (fw[i].y < fw[i].life && fw[i].timeout < 0) {
// Blow it up
fw[i].em->maxspeed = 200;
fw[i].speed = 0;
fw[i].em->createrate = 2.0f;
fw[i].timeout = 5;
fw[i].em->life = 0.5f;
fwBang.Play();
}
else if (fw[i].timeout > 0) {
if (--fw[i].timeout == 0)
fw[i].active = false;
}
}
else if (rand() % 30 == 0 && fade >= -0.005f) {
fw[i].x = rand() % (opengl.GetWidth());
fw[i].y = opengl.GetHeight();
fw[i].em->r = (rand() % 100) / 100.0f;
fw[i].em->g = (rand() % 100) / 100.0f;
fw[i].em->b = (rand() % 100) / 100.0f;
fw[i].em->life = 0.5f;
fw[i].speed = 2 + rand() % 2;
fw[i].life = rand() % (opengl.GetHeight()-100) + 100;
fw[i].timeout = -1;
fw[i].em->createrate = 64.0f;
fw[i].em->maxspeed = 10.0f;
fw[i].em->xpos = (float)fw[i].x;
fw[i].em->ypos = (float)fw[i].y;
fw[i].active = true;
}
fw[i].em->Process(fw[i].active);
}
// Fade in or out
if (fade >= 0.001f) {
flAlpha += fade;
if (flAlpha >= 1.0f)
fade = 0.000000f;
}
else if (fade <= -0.001f) {
flAlpha += fade;
if (flAlpha < 0.0f) {
fade = 0.000000f;
// Fade to something else
if (state == hssDisplay)
ScreenManager::GetInstance().SelectScreen("MAIN MENU");
}
}
}
//
// Renders the next frame.
//
void HighScores::Display()
{
OpenGL& opengl = OpenGL::GetInstance();
// Draw the fireworks
for (int i = 0; i < MAX_FIREWORKS; i++)
fw[i].em->Draw(0, 0);
// Draw scores
if (state == hssDisplay) {
int x = (opengl.GetWidth() - 280) / 2;
int y = (opengl.GetHeight() - 250) / 2;
glColor4f(0.0f, 1.0f, 0.0f, flAlpha);
for (int i = 0; i < 10; i++) {
scoreNameFont.Print(x, y + 22*i, scoreFile[i].GetName());
scoreNameFont.Print(x + 230, y + 22*i, "%d", scoreFile[i].GetScore());
}
}
// Draw other stuff
const char* hsnext = i18n("Press SPACE or FIRE to return");
if (state == hssDisplay) {
int title_x = (opengl.GetWidth() - hscoreImage.GetWidth()) / 2;
int title_y = 50;
hscoreImage.Draw(title_x, title_y, 0.0, 1.0, flAlpha);
int x = (opengl.GetWidth() - largeFont.GetStringWidth(hsnext)) / 2;
int y = opengl.GetHeight() - 50;
glColor4f(0.0f, 0.5f, 1.0f, flAlpha);
largeFont.Print(x, y, hsnext);
}
else if (state == hssEnterName) {
Input& input = Input::GetInstance();
const char* hsscore = i18n("Well done - You got a high score");
int x = (opengl.GetWidth() - largeFont.GetStringWidth(hsscore)) / 2;
glColor4f(0.0f, 1.0f, 0.0f, flAlpha);
largeFont.Print(x, 100, hsscore);
const char* hscont = i18n("Press ENTER or FIRE to continue");
x = (opengl.GetWidth() - largeFont.GetStringWidth(hscont)) / 2;
int y = opengl.GetHeight() - 60;
largeFont.Print(x, y, hscont);
const char* name = input.GetInput();
const char* hsname = i18n("Name? %s");
x = (opengl.GetWidth() - largeFont.GetStringWidth(hsname, name)) / 2;
y = (opengl.GetHeight() - 50) / 2;
glColor4f(0.8f, 0.0f, 1.0f, flAlpha);
largeFont.Print(x, y, hsname, name);
}
}
//
// Loads the highsores from disk.
//
void HighScores::LoadHighScores()
{
scoreFile.Load();
}
//
// Writes high scores to disk.
//
void HighScores::WriteHighScores()
{
scoreFile.Save();
}
//
// Displays the highest scores screen to the user.
//
void HighScores::DisplayScores()
{
LoadHighScores();
ScreenManager::GetInstance().SelectScreen("HIGH SCORES");
state = hssDisplay;
for (int i = 0; i < MAX_FIREWORKS; i++) {
fw[i].em->Reset();
fw[i].active = false;
}
// Fade in
flAlpha = 0.0f;
fade = HS_FADE_IN_SPEED;
}
//
// Check to see if the player has a high score.
//
void HighScores::CheckScore(int score)
{
LoadHighScores();
ScreenManager::GetInstance().SelectScreen("HIGH SCORES");
if (score > scoreFile[9].GetScore()) {
// New high score
state = hssEnterName;
Input::GetInstance().OpenCharBuffer(ScoreFile::ScoreEntry::MAX_NAME);
newscore = score;
}
else
state = hssDisplay;
for (int i = 0; i < MAX_FIREWORKS; i++) {
fw[i].em->Reset();
fw[i].active = false;
}
// Fade in
flAlpha = 0.0f;
fade = HS_FADE_IN_SPEED;
}
ScoreFile::ScoreFile()
: needsWrite(false), scores(NUM_SCORES, ScoreEntry("Nobody", 0))
{
}
ScoreFile::~ScoreFile()
{
if (needsWrite)
Save();
}
string ScoreFile::GetHighScoreFile()
{
return GetConfigDir() + ".lander.scores";
}
bool operator<(const ScoreFile::ScoreEntry& a, const ScoreFile::ScoreEntry& b)
{
return a.GetScore() > b.GetScore();
}
void ScoreFile::Sort()
{
sort(scores.begin(), scores.end());
}
void ScoreFile::Load()
{
// Check for file's existence
string hsname(GetHighScoreFile());
if (!FileExists(hsname)) {
// Write a dummy score file
Save();
}
else {
// Open highscores file
ifstream fin(hsname.c_str());
for (ScoreEntryVecIt it = scores.begin(); it != scores.end(); ++it)
(*it).ReadFromStream(fin);
Sort();
}
}
void ScoreFile::Save()
{
if (!needsWrite)
return;
ofstream fout(GetHighScoreFile().c_str());
for (ScoreEntryVecIt it = scores.begin(); it != scores.end(); ++it)
(*it).WriteOnStream(fout);
}
void ScoreFile::Insert(const char* name, int score)
{
scores[9] = ScoreEntry(name, score);
Sort();
needsWrite = true;
}
ScoreFile::ScoreEntry::ScoreEntry(const char* name, int score)
: score(score)
{
strncpy(this->name, name, MAX_NAME);
}
void ScoreFile::ScoreEntry::WriteOnStream(ostream& os)
{
os.write((const char*)&score, sizeof(int));
os.write(name, MAX_NAME);
}
void ScoreFile::ScoreEntry::ReadFromStream(istream& is)
{
is.read((char*)&score, sizeof(int));
is.read(name, MAX_NAME);
}