From aaa00423ed4255c81d1e0a2ebf98696e3c48d64d Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Wed, 3 Jun 2009 17:03:08 +0100 Subject: [PATCH] Use std::string everywhere --- src/HighScores.cpp | 16 ++++++++-------- src/HighScores.hpp | 2 +- src/Image.cpp | 2 +- src/Image.hpp | 2 +- src/Input.cpp | 17 +++++++++++------ src/Input.hpp | 8 +++++--- src/Platform.hpp | 26 -------------------------- 7 files changed, 27 insertions(+), 46 deletions(-) diff --git a/src/HighScores.cpp b/src/HighScores.cpp index ad87525..3f70570 100644 --- a/src/HighScores.cpp +++ b/src/HighScores.cpp @@ -79,7 +79,7 @@ void HighScores::Process() } else if (state == hssEnterName) { if (input.QueryAction(Input::FIRE) - && strlen(input.GetInput()) > 0) { + && input.GetInput().size() > 0) { // Enter name into high score chart scoreFile.Insert(input.GetInput(), newscore); @@ -204,12 +204,12 @@ void HighScores::Display() int y = opengl.GetHeight() - 60; largeFont.Print(x, y, hscont); - const char* name = input.GetInput(); + const string name(input.GetInput()); const char* hsname = i18n("Name? %s"); - x = (opengl.GetWidth() - largeFont.GetStringWidth(hsname, name)) / 2; + x = (opengl.GetWidth() - largeFont.GetStringWidth(hsname, name.c_str())) / 2; y = (opengl.GetHeight() - 50) / 2; glColor4f(0.8f, 0.0f, 1.0f, flAlpha); - largeFont.Print(x, y, hsname, name); + largeFont.Print(x, y, hsname, name.c_str()); } } @@ -330,9 +330,9 @@ void ScoreFile::Save() (*it).WriteOnStream(fout); } -void ScoreFile::Insert(const char* name, int score) +void ScoreFile::Insert(const string& name, int score) { - scores[9] = ScoreEntry(name, score); + scores[9] = ScoreEntry(name.c_str(), score); Sort(); needsWrite = true; } @@ -345,12 +345,12 @@ ScoreFile::ScoreEntry::ScoreEntry(const char* name, int score) void ScoreFile::ScoreEntry::WriteOnStream(ostream& os) { - os.write((const char*)&score, sizeof(int)); + os.write(reinterpret_cast(&score), sizeof(int)); os.write(name, MAX_NAME); } void ScoreFile::ScoreEntry::ReadFromStream(istream& is) { - is.read((char*)&score, sizeof(int)); + is.read(reinterpret_cast(&score), sizeof(int)); is.read(name, MAX_NAME); } diff --git a/src/HighScores.hpp b/src/HighScores.hpp index 09443f4..bb2b1ba 100644 --- a/src/HighScores.hpp +++ b/src/HighScores.hpp @@ -34,7 +34,7 @@ public: void Load(); void Save(); - void Insert(const char* name, int score); + void Insert(const string& name, int score); // An entry in the highscores chart class ScoreEntry { diff --git a/src/Image.cpp b/src/Image.cpp index 4d33164..1b9a1a6 100644 --- a/src/Image.cpp +++ b/src/Image.cpp @@ -18,7 +18,7 @@ #include "Image.hpp" #include "OpenGL.hpp" -Image::Image(const char* file) +Image::Image(const string& file) : Texture(file) { diff --git a/src/Image.hpp b/src/Image.hpp index 0db9725..2f7783e 100644 --- a/src/Image.hpp +++ b/src/Image.hpp @@ -23,7 +23,7 @@ class Image : public Texture { public: - Image(const char* file); + Image(const string& file); virtual ~Image(); void Draw(int x, int y, double rotate=0.0, double scale=1.0, diff --git a/src/Input.cpp b/src/Input.cpp index 80b7ef1..d6091e2 100644 --- a/src/Input.cpp +++ b/src/Input.cpp @@ -20,6 +20,8 @@ #include "Input.hpp" #include "OpenGL.hpp" +#include + const int Input::RESET_TIMEOUT(7); // @@ -91,14 +93,17 @@ void Input::Update() if ((e.key.keysym.sym >= SDLK_a && e.key.keysym.sym <= SDLK_z) || (e.key.keysym.sym == SDLK_SPACE)) { char ch = (char)e.key.keysym.sym; - text += shift ? toupper(ch) : ch; + ch = shift ? toupper(ch) : ch; + text.write(&ch, 1); } else if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT) { shift = true; } - else if (e.key.keysym.sym == SDLK_BACKSPACE && text.length() > 0) { - text.erase(text.length() - 1, 1); + else if (e.key.keysym.sym == SDLK_BACKSPACE && text.tellp() > 0) { + long pos = text.tellp(); + if (pos > 0) + text.seekp(pos - 1); } } break; @@ -238,7 +243,7 @@ void Input::OpenCharBuffer(int max) shift = false; maxchar = max; - text = ""; + text.str(""); textinput = true; } @@ -256,7 +261,7 @@ void Input::CloseCharBuffer() // // Returns a pointer to the data read in text input mode. // -const char* Input::GetInput() const +string Input::GetInput() const { - return text.c_str(); + return text.str(); } diff --git a/src/Input.hpp b/src/Input.hpp index 5ee7ce6..6da1f10 100644 --- a/src/Input.hpp +++ b/src/Input.hpp @@ -22,6 +22,8 @@ #include "Platform.hpp" +#include + // // A singleton class to manage SDL input. // @@ -44,7 +46,7 @@ public: void OpenCharBuffer(int max=256); void CloseCharBuffer(); - const char* GetInput() const; + string GetInput() const; private: Input(); @@ -58,8 +60,8 @@ private: bool shift; bool textinput; // Is a character buffer open? - string text; // Text read so far - int maxchar; // Maximum number of characters to read + ostringstream text; // Text read so far + int maxchar; // Maximum number of characters to read // Record joystick state bool joyLeft, joyRight, joyUp, joyDown, joyButton0, joyButton1; diff --git a/src/Platform.hpp b/src/Platform.hpp index 35e010d..45e60a7 100644 --- a/src/Platform.hpp +++ b/src/Platform.hpp @@ -133,34 +133,8 @@ using namespace std; #endif /* #ifdef LINUX */ - -#ifdef LANDER_BIG_ENDIAN - -// -// All data files are currently stored in little endian format. -// - -#define Flip16(n) (((n >> 8) & 0x00ff) | \ - ((n << 8) & 0xff00)) - -#define Flip32(n) (((n >>24) & 0x000000ff) | \ - ((n >> 8) & 0x0000ff00) | \ - ((n << 8) & 0x00ff0000) | \ - ((n <<24) & 0xff000000)) - -#define LittleEndian32(l) l = Flip32(l) -#define LittleEndian16(s) s = Flip16(s) - -#else - -#define LittleEndian32(l) -#define LittleEndian16(s) - -#endif /* #ifdef LANDER_BIG_ENDIAN */ - void RecreateScreens(); string LocateResource(const string& file); -bool FileExists(const string& file); string GetConfigDir(); #endif /* #ifdef INC_PLATFORM_HPP */ -- 2.39.2