MXVK Vulkan Framework 0.35.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 119 of file acid.drop.cpp.

Constructor & Destructor Documentation

◆ HighScores()

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

121{ read(); }

◆ ~HighScores()

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

Member Function Documentation

◆ addScore()

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

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

124 {
125 scores.push_back({name, score});
126 sort();
127
128 if (scores.size() > 10) {
129 scores.resize(10);
130 }
131 }

◆ getScores()

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

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

140{ return scores; }

◆ init()

void HighScores::init ( )
inline

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

142 {
143 scores.clear();
144 for (int i = 0; i < 10; ++i)
145 scores.push_back(Score("Anonymous", (10 - i) * 100));
146 }

◆ qualifiesForHighScore()

bool HighScores::qualifiesForHighScore ( int score)
inline

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

134 {
135 if (scores.size() < 10)
136 return true;
137 return score > scores.back().score;
138 }

◆ read()

void HighScores::read ( )
inline

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

148 {
149 std::fstream file;
150 file.open("./scores.dat", std::ios::in);
151 if (!file.is_open()) {
152 std::cout << "No scores file.\n";
153 init();
154 return;
155 }
156 std::string input;
157 int count = 0;
158 while (std::getline(file, input)) {
159 if (count >= 10)
160 break;
161 if (input.length() > 0) {
162 auto pos = input.find(":");
163 if (pos != std::string::npos) {
164 std::string left = input.substr(0, pos);
165 std::string right = input.substr(pos + 1);
166 scores.push_back(Score(left, std::strtol(right.c_str(), 0, 10)));
167 count++;
168 }
169 }
170 }
171 file.close();
172 sort();
173 }
void init()

◆ sort()

void HighScores::sort ( )
inline

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

132{ std::sort(scores.begin(), scores.end()); }

◆ write()

void HighScores::write ( )
inline

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

174 {
175 std::fstream file;
176 file.open("./scores.dat", std::ios::out);
177 if (!file.is_open()) {
178 std::cerr << "Could not open score file..\n";
179 return;
180 }
181 for (auto &s : scores) {
182 file << s.name << ":" << s.score << std::endl;
183 }
184 file.close();
185 }

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