MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
HighScores Class Reference

Public Member Functions

void addScore (const std::string &name, int score)
const std::vector< Score > & getScores () const
 HighScores ()
void init ()
bool qualifiesForHighScore (int score)
void read ()
void sort ()
void write ()
 ~HighScores ()

Detailed Description

Definition at line 142 of file acid.drop.cpp.

Constructor & Destructor Documentation

◆ HighScores()

Definition at line 144 of file acid.drop.cpp.

144 {
145 read();
146 }
void read()

◆ ~HighScores()

Definition at line 147 of file acid.drop.cpp.

147 {
148 write();
149 }
void write()

Member Function Documentation

◆ addScore()

void HighScores::addScore ( const std::string & name,
int score )
inline

Definition at line 151 of file acid.drop.cpp.

151 {
152 scores.push_back({name, score});
153 sort();
154
155 if (scores.size() > 10) {
156 scores.resize(10);
157 }
158 }
void sort()

◆ getScores()

const std::vector< Score > & HighScores::getScores ( ) const
inline

Definition at line 169 of file acid.drop.cpp.

169 {
170 return scores;
171 }

◆ init()

void HighScores::init ( )
inline

Definition at line 173 of file acid.drop.cpp.

173 {
174 scores.clear();
175 for (int i = 0; i < 10; ++i)
176 scores.push_back(Score("Anonymous", (10 - i) * 100));
177 }

◆ qualifiesForHighScore()

bool HighScores::qualifiesForHighScore ( int score)
inline

Definition at line 163 of file acid.drop.cpp.

163 {
164 if (scores.size() < 10)
165 return true;
166 return score > scores.back().score;
167 }

◆ read()

void HighScores::read ( )
inline

Definition at line 179 of file acid.drop.cpp.

179 {
180 std::fstream file;
181 file.open("./scores.dat", std::ios::in);
182 if (!file.is_open()) {
183 std::cout << "No scores file.\n";
184 init();
185 return;
186 }
187 std::string input;
188 int count = 0;
189 while (std::getline(file, input)) {
190 if (count >= 10)
191 break;
192 if (input.length() > 0) {
193 auto pos = input.find(":");
194 if (pos != std::string::npos) {
195 std::string left = input.substr(0, pos);
196 std::string right = input.substr(pos + 1);
197 scores.push_back(Score(left, std::strtol(right.c_str(), 0, 10)));
198 count++;
199 }
200 }
201 }
202 file.close();
203 sort();
204 }
void init()

◆ sort()

void HighScores::sort ( )
inline

Definition at line 159 of file acid.drop.cpp.

159 {
160 std::sort(scores.begin(), scores.end());
161 }

◆ write()

void HighScores::write ( )
inline

Definition at line 205 of file acid.drop.cpp.

205 {
206 std::fstream file;
207 file.open("./scores.dat", std::ios::out);
208 if (!file.is_open()) {
209 std::cerr << "Could not open score file..\n";
210 return;
211 }
212 for (auto &s : scores) {
213 file << s.name << ":" << s.score << std::endl;
214 }
215 file.close();
216 }

The documentation for this class was generated from the following file: