From 3a66af25028d31a5fe305e039aeae4cc360e1e67 Mon Sep 17 00:00:00 2001 From: nick Date: Sat, 29 Mar 2008 23:06:43 +0000 Subject: [PATCH] Refactor the high scores a bit git-svn-id: http://svn.nickg.me.uk/work/lander@337 a97b1542-0b21-0410-a459-e47997c36f34 --- trunk/src/HighScores.cpp | 143 +++++++++++++++++++++++---------------- trunk/src/HighScores.hpp | 54 ++++++++++++--- trunk/src/Main.cpp | 1 - trunk/src/Platform.hpp | 2 + 4 files changed, 130 insertions(+), 70 deletions(-) diff --git a/trunk/src/HighScores.cpp b/trunk/src/HighScores.cpp index 291ce4e..4b9b48b 100644 --- a/trunk/src/HighScores.cpp +++ b/trunk/src/HighScores.cpp @@ -88,11 +88,9 @@ void HighScores::Process() && strlen(input.GetInput()) > 0) { // Enter name into high score chart - scores[9].score = newscore; - strncpy(scores[9].name, input.GetInput(), ScoreEntry::MAX_NAME_LEN); + scoreFile.Insert(input.GetInput(), newscore); input.CloseCharBuffer(); - WriteHighScores(); - SortScores(); + scoreFile.Save(); state = hssDisplay; flAlpha = 0.0f; fade = HS_FADE_IN_SPEED; @@ -183,8 +181,8 @@ void HighScores::Display() opengl.Colour(0.0f, 1.0f, 0.0f, flAlpha); for (int i = 0; i < 10; i++) { - ft.Print(ftScoreName, x, y + 22*i, "%s", scores[i].name); - ft.Print(ftScoreName, x + 230, y + 22*i, "%d", scores[i].score); + ft.Print(ftScoreName, x, y + 22*i, "%s", scoreFile[i].GetName()); + ft.Print(ftScoreName, x + 230, y + 22*i, "%d", scoreFile[i].GetScore()); } } @@ -239,64 +237,15 @@ void HighScores::Display() */ void HighScores::LoadHighScores() { - int i; - - // Check for file's existence - if (!File::Exists(File::LocateResource("Highscores", "dat"))) { - // Create dummy highscores file - for (i = 0; i < 10; i++) { - strncpy(scores[i].name, "Nobody", ScoreEntry::MAX_NAME_LEN); - scores[i].score = 0; - } - File f(File::LocateResource("Highscores", "dat"), false); - f.Write(&scores, sizeof(ScoreEntry) * 10); - } - - // Open highscores file - File f(File::LocateResource("Highscores", "dat")); - f.Read(&scores, sizeof(ScoreEntry) * 10); - - // Sort the scores - SortScores(); -} - - -/* - * Sorts the scores into ascending order. - */ -void HighScores::SortScores() -{ - int i, j; - - for (i = 0; i < 9; i++) { - for (j = 0; j < 9 - i; j++) { - if (scores[j+1].score > scores[j].score) - SwapScores(j+1, j); - } - } -} - - -/* - * Swaps two entries in the score array. - */ -void HighScores::SwapScores(int a, int b) -{ - ScoreEntry temp; - - temp = scores[a]; - scores[a] = scores[b]; - scores[b] = temp; + scoreFile.Load(); } - /* * Writes high scores to disk. */ void HighScores::WriteHighScores() { - File f(File::LocateResource("Highscores", "dat"), false); - f.Write(&scores, sizeof(ScoreEntry) * 10); + scoreFile.Save(); } @@ -328,10 +277,10 @@ void HighScores::CheckScore(int score) LoadHighScores(); ScreenManager::GetInstance().SelectScreen("HIGH SCORES"); - if (score > scores[9].score) { + if (score > scoreFile[9].GetScore()) { // New high score state = hssEnterName; - Input::GetInstance().OpenCharBuffer(ScoreEntry::MAX_NAME_LEN - 1); + Input::GetInstance().OpenCharBuffer(ScoreFile::ScoreEntry::MAX_NAME); newscore = score; } else @@ -346,3 +295,79 @@ void HighScores::CheckScore(int score) flAlpha = 0.0f; fade = HS_FADE_IN_SPEED; } + +ScoreFile::ScoreFile() + : needsWrite(false), scores(NUM_SCORES, ScoreEntry("Nobody", 0)) +{ + +} + +ScoreFile::~ScoreFile() +{ + if (needsWrite) + Save(); +} + +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 + if (!File::Exists(File::LocateResource("Highscores", "dat"))) { + // Write a dummy score file + Save(); + } + else { + // Open highscores file + const char *fname = File::LocateResource("Highscores", "dat"); + ifstream fin(fname); + for (ScoreEntryVecIt it = scores.begin(); it != scores.end(); ++it) + (*it).ReadFromStream(fin); + + Sort(); + } +} + +void ScoreFile::Save() +{ + if (!needsWrite) + return; + + const char *fname = File::LocateResource("Highscores", "dat"); + ofstream fout(fname); + 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); +} diff --git a/trunk/src/HighScores.hpp b/trunk/src/HighScores.hpp index 0b3ada1..af63137 100644 --- a/trunk/src/HighScores.hpp +++ b/trunk/src/HighScores.hpp @@ -25,6 +25,47 @@ #define HS_FADE_IN_SPEED 0.2f #define HS_FADE_OUT_SPEED -0.02f +class ScoreFile { +public: + ScoreFile(); + ~ScoreFile(); + + void Load(); + void Save(); + void Insert(const char *name, int score); + + // An entry in the highscores chart + class ScoreEntry { + public: + ScoreEntry(const char *name, int score); + + static const int MAX_NAME = 16; + + const char *GetName() const { return name; } + int GetScore() const { return score; } + + void WriteOnStream(ostream &os); + void ReadFromStream(istream &is); + + private: + char name[MAX_NAME]; + int score; + }; + + static const int NUM_SCORES = 10; + + const ScoreEntry &operator[](int n) const { return scores[n]; } + +private: + void Sort(); + void SwapScores(int a, int b); + + bool needsWrite; + typedef vector ScoreEntryVec; + typedef ScoreEntryVec::iterator ScoreEntryVecIt; + ScoreEntryVec scores; +}; + class HighScores : public Screen { public: HighScores() : hasloaded(false) { } @@ -39,8 +80,6 @@ public: void CheckScore(int score); private: - void SortScores(); - void SwapScores(int a, int b); enum HighScoreState { hssDisplay, hssEnterName }; @@ -49,16 +88,11 @@ private: TextureQuad hscore; float flAlpha, fade; HighScoreState state; - + ScoreFile scoreFile; + GLuint uHighScore; - // An entry in the highscores chart - static const int NUM_SCORES = 10; - struct ScoreEntry { - static const int MAX_NAME_LEN = 32; - char name[MAX_NAME_LEN]; - int score; - } scores[NUM_SCORES]; + // Fireworks static const int MAX_FIREWORKS = 7; diff --git a/trunk/src/Main.cpp b/trunk/src/Main.cpp index 186e773..f92bb8c 100644 --- a/trunk/src/Main.cpp +++ b/trunk/src/Main.cpp @@ -23,7 +23,6 @@ #define DEBUG_WIN_X 800 #define DEBUG_WIN_Y 600 -/* Globals */ DataFile *g_pData; #include diff --git a/trunk/src/Platform.hpp b/trunk/src/Platform.hpp index cd7e3aa..c8c4aa4 100644 --- a/trunk/src/Platform.hpp +++ b/trunk/src/Platform.hpp @@ -34,7 +34,9 @@ #include #include #include +#include #include +#include #include #include FT_FREETYPE_H -- 2.39.2