From 5b7ae5d25e51de6b0fbe4d0fb98b65dd6b779893 Mon Sep 17 00:00:00 2001 From: Matt Mullins Date: Fri, 15 Jun 2012 21:47:00 -0700 Subject: [PATCH] Move getFont() into Game. This avoids having to do several conditionals to find the right Level object that would previously be needed to find the font. This assumes that fonts are global across the game, and will have to be revisited if level-specific fonts are ever introduced. --- Game.cpp | 16 ++++++++++++++++ Game.h | 13 +++++++++++++ Level.cpp | 22 +--------------------- Text.cpp | 4 ++-- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/Game.cpp b/Game.cpp index b26ef84..4b7cec4 100644 --- a/Game.cpp +++ b/Game.cpp @@ -237,3 +237,19 @@ Uint32 Game::timerCallback(Uint32 interval, void* data) return interval; } +Font* Game::getFont( int s = 24, FontType t = English){ + map font = t == English ? mEnglishFonts : mJapaneseFonts; + map::iterator it = font.find(s); + //if the font doesn't exist + if (it != font.end()) { + //create it + Font* f = new Font(t, s); + font[s] = f; + return f; + } else { + Font* f = it->second; + f->count++; + return f; + } +} + diff --git a/Game.h b/Game.h index 3b53c4d..9145942 100644 --- a/Game.h +++ b/Game.h @@ -57,6 +57,16 @@ public: */ int getLevelCount(); + /** + Gets a font to be used to display text. This uses a cache of loaded + fonts in order to conserve rendering time. + + \param size Size (in pixels) of the desired font + \param type Type of text to be displayed + \return Pointer to Font structure + */ + Font* getFont(int size, FontType type); + /** Makes the game loop begin and load objects. */ void run(); @@ -92,6 +102,9 @@ private: SDL_Surface *mScreen;/**< The surface to draw to. */ map mBehaviors;/**< This level's Behaviors */ + map mEnglishFonts; + map mJapaneseFonts; + static Uint32 timerCallback(Uint32 interval, void* data); //fps data calc diff --git a/Level.cpp b/Level.cpp index 38a7e07..f6ed40b 100644 --- a/Level.cpp +++ b/Level.cpp @@ -8,8 +8,6 @@ Level::Level() { mLoadBehavior = DoNothing; mUpdateBehavior = DoNothing; - mFonts.push_back(map()); - mFonts.push_back(map()); } Level::Level(string n, Condition winCond, Behavior loadBehave, Behavior updateBehave, Behavior endBehave) : mName(n), mBackground(NULL), Loaded(false), mTextures(map()), ToAdd(vector()), ToRemove(vector()), mFonts(vector< map >()) @@ -19,8 +17,6 @@ Level::Level(string n, Condition winCond, Behavior loadBehave, Behavior updateBe mUpdateBehavior = updateBehave; mUnloadBehavior = endBehave; mWinCondition = winCond; - mFonts.push_back(map()); - mFonts.push_back(map()); } Level::~Level() @@ -278,22 +274,6 @@ Sprite* Level::findSpriteByName(string name) { return NULL;//if a sprite wasn't found return null } -Font* Level::getFont( int s = 24, FontType t = English) { - map font = t == English ? mFonts[0] : mFonts[1]; - map::iterator it = font.find(s); - //if the font doesn't exist - if(it!=font.end()) { - //create it - Font* f = new Font(t,s); - font[s] = f; - return f; - } else{ - Font* f = it->second; - f->count++; - return f; - } -} - void Level::closeFont( Font* f) { map font = f->type == English ? mFonts[0] : mFonts[1]; map::iterator it = font.find(f->size); @@ -309,4 +289,4 @@ void Level::closeFont( Font* f) { font.erase(it); } } -} \ No newline at end of file +} diff --git a/Text.cpp b/Text.cpp index 7d3f4af..050a854 100644 --- a/Text.cpp +++ b/Text.cpp @@ -8,7 +8,7 @@ using namespace std; Text::Text(string id, FontType t, int s, SDL_Color c, int w, int h) : WorldObject(id), - mFont(Game::game()->getCurrentLevel()->getFont(s, t)), + mFont(Game::game()->getFont(s, t)), color(c), width(w), height(h) @@ -19,7 +19,7 @@ Text::Text(string id, FontType t, int s, SDL_Color c, int w, int h) Text::Text(string name, string t, int s, SDL_Color c, int w, int h) : WorldObject(name), text(t), - mFont(Game::game()->getCurrentLevel()->getFont(s, (FontType)0)), + mFont(Game::game()->getFont(s, (FontType)0)), color(c), width(w), height(h) -- 2.11.0