Move getFont() into Game.
authorMatt Mullins <mokomull@gmail.com>
Sat, 16 Jun 2012 04:47:00 +0000 (21:47 -0700)
committerMatt Mullins <mokomull@gmail.com>
Sat, 16 Jun 2012 05:04:20 +0000 (22:04 -0700)
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
Game.h
Level.cpp
Text.cpp

index b26ef84..4b7cec4 100644 (file)
--- 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<int, Font*> font = t == English ? mEnglishFonts : mJapaneseFonts;
+       map<int, Font*>::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 (file)
--- 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<string,Behavior*> mBehaviors;/**< This level's Behaviors */
 
+       map<int, Font*> mEnglishFonts;
+       map<int, Font*> mJapaneseFonts;
+
        static Uint32 timerCallback(Uint32 interval, void* data);
        
        //fps data calc
index 38a7e07..f6ed40b 100644 (file)
--- a/Level.cpp
+++ b/Level.cpp
@@ -8,8 +8,6 @@ Level::Level()
 {
        mLoadBehavior = DoNothing;
        mUpdateBehavior = DoNothing;
-       mFonts.push_back(map<int,Font*>());
-       mFonts.push_back(map<int,Font*>());
 }
 
 Level::Level(string n, Condition winCond, Behavior loadBehave, Behavior updateBehave, Behavior endBehave) : mName(n), mBackground(NULL), Loaded(false), mTextures(map<string, Texture>()), ToAdd(vector<WorldObject*>()), ToRemove(vector<WorldObject*>()), mFonts(vector< map<int,Font*> >())
@@ -19,8 +17,6 @@ Level::Level(string n, Condition winCond, Behavior loadBehave, Behavior updateBe
        mUpdateBehavior = updateBehave;
        mUnloadBehavior = endBehave;
        mWinCondition = winCond;
-       mFonts.push_back(map<int,Font*>());
-       mFonts.push_back(map<int,Font*>());
 }
 
 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<int, Font*> font = t == English ? mFonts[0] : mFonts[1];
-       map<int, Font*>::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<int, Font*> font = f->type == English ? mFonts[0] : mFonts[1];
        map<int, Font*>::iterator it = font.find(f->size);
@@ -309,4 +289,4 @@ void Level::closeFont( Font* f) {
                        font.erase(it);
                }
        }
-}
\ No newline at end of file
+}
index 7d3f4af..050a854 100644 (file)
--- 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)