This repository has been archived by the owner on Apr 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighscore.cpp
136 lines (106 loc) · 2.74 KB
/
Highscore.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
#include "Highscore.h"
Highscore::Highscore()
{
DisplaySurface = SDL_GetVideoSurface();
Running = true;
GameCurrentscore = 0;
}
Highscore::~Highscore()
{ }
int Highscore::get_highscore()
{
return GameHighscore;
}
int Highscore::get_current_score()
{
return GameCurrentscore;
}
void Highscore::run_highscore()
{
SDL_Event Events;
Draw *d = new Draw();
std::ifstream HighscoreFile;
std::map<int, std::string> HighscoreMap;
// Used for the map
std::string Nickname;
int Highscore = 0;
// Used for the run-loop
std::string ScoreString;
int YValue = 100;
int ShowNumberOfScores = 0;
int Score = 0;
HighscoreFile.open("score/HIGHSCORE", std::ios::in | std::ios::binary);
if (HighscoreFile.is_open())
{
while (HighscoreFile.good())
{
HighscoreFile >> Nickname >> Highscore;
HighscoreMap[Highscore] = Nickname;
}
HighscoreFile.close();
}
std::map<int, std::string>::reverse_iterator ReversedHighscoreIterator = HighscoreMap.rbegin();
while (Running)
{
// Set the background to black
SDL_FillRect(DisplaySurface, NULL, 0x000000);
d->draw_text(DisplaySurface, "Top 15", 55, 20, -1, 51, 108, 184);
d->draw_text(DisplaySurface, "Initials", 30, 80, 50, 176, 54, 56);
d->draw_text(DisplaySurface, "Score", 30, 80, 640, 176, 54, 56);
while (ReversedHighscoreIterator != HighscoreMap.rend() && ShowNumberOfScores < 15)
{
// If there isn't any highscores yet
if (ReversedHighscoreIterator->first == 0)
{
d->draw_text(DisplaySurface, "No score", 15, 140, 50, 255, 255, 255);
}
else
{
Score = ReversedHighscoreIterator->first;
std::stringstream ScoreStream;
ScoreStream << Score;
ScoreString = ScoreStream.str();
d->draw_text(DisplaySurface, ReversedHighscoreIterator->second, 15, YValue+40, 50, 255, 255, 255);
d->draw_text(DisplaySurface, ScoreString, 15, YValue+40, 640, 255, 255, 255);
YValue += 30;
}
++ShowNumberOfScores;
++ReversedHighscoreIterator;
SDL_Flip(DisplaySurface);
SDL_Delay(1000/60);
}
while (SDL_PollEvent(&Events))
{
if (Events.type == SDL_KEYDOWN)
{
if (Events.key.keysym.sym == SDLK_ESCAPE)
{
Running = false;
}
}
}
}
return;
}
void Highscore::set_highscore(const std::string Nickname, const int Highscore)
{
std::ofstream HighscoreFile;
HighscoreFile.open("score/HIGHSCORE", std::ios::out | std::ios::binary | std::ios::app);
if (HighscoreFile.is_open())
{
HighscoreFile << Nickname;
HighscoreFile << " ";
HighscoreFile << Highscore;
HighscoreFile << "\n";
}
HighscoreFile.close();
return;
}
void Highscore::add_to_current_score(const int Currentscore)
{
GameCurrentscore += Currentscore;
}
void Highscore::set_current_score(const int Currentscore)
{
GameCurrentscore = Currentscore;
}