]> git.mxchange.org Git - quix0rs-blobwars.git/commitdiff
Cache text surfaces.
authorGuus Sliepen <guus@debian.org>
Wed, 7 Jul 2010 17:08:12 +0000 (19:08 +0200)
committerGuus Sliepen <guus@debian.org>
Wed, 7 Jul 2010 17:08:12 +0000 (19:08 +0200)
According to callgrind, a lot of time was spent drawing the same text over and
over to a surface.  A caching version of Graphics::drawString has been added,
that keeps the surface around when the text hasn't changed.

src/CGraphics.cpp
src/CGraphics.h
src/game.cpp
src/hub.cpp
src/info.cpp
src/mias.cpp
src/mission.cpp

index 5c3071e381c22df784b1760960c5bbda99998c4a..353a837febd36331bb01bf4c5bd3e319fc8e614b 100644 (file)
@@ -895,18 +895,50 @@ void Graphics::drawString(const char *in, int x, int y, int alignment, SDL_Surfa
        SDL_Surface *text = TTF_RenderUTF8_Shaded(font[fontSize], in, fontForeground, fontBackground);
 
        if (!text)
-       {
                text = TTF_RenderUTF8_Shaded(font[fontSize], "FONT_ERROR", fontForeground, fontBackground);
-       }
+
+       if (!text)
+               return;
+
+       setTransparent(text);
 
        if (alignment == TXT_RIGHT) x -= text->w;
        if (alignment == TXT_CENTERED) center = true;
 
-       setTransparent(text);
        blit(text, x, y, dest, center);
        SDL_FreeSurface(text);
 }
 
+void Graphics::drawString(const char *in, int x, int y, int alignment, SDL_Surface *dest, SurfaceCache &cache)
+{
+       bool center = false;
+
+       if(!cache.text || strcmp(in, cache.text)) {
+               if(cache.surface)
+                       SDL_FreeSurface(cache.surface);
+
+               if(cache.text)
+                       ::free(cache.text);
+
+               cache.text = strdup(in);
+
+               cache.surface = TTF_RenderUTF8_Shaded(font[fontSize], in, fontForeground, fontBackground);
+
+               if (!cache.surface)
+                       cache.surface = TTF_RenderUTF8_Shaded(font[fontSize], "FONT_ERROR", fontForeground, fontBackground);
+
+               if(!cache.surface)
+                       return;
+
+               setTransparent(cache.surface);
+       }
+
+       if (alignment == TXT_RIGHT) x -= cache.surface->w;
+       if (alignment == TXT_CENTERED) center = true;
+
+       blit(cache.surface, x, y, dest, center);
+}
+
 void Graphics::clearChatString()
 {
        chatString[0] = 0;
index be8a21a815c397e0cf4bc30cdfd30edf0247e393..3ad2c5213d439cad412fe134ed3f7168f51c6de4 100755 (executable)
@@ -49,6 +49,11 @@ class Graphics {
                SDL_Surface *infoMessage;
 
        public:
+               struct SurfaceCache {
+                       char *text;
+                       SDL_Surface *surface;
+                       SurfaceCache(): text(NULL), surface(NULL) {}    
+               };
 
                bool takeRandomScreenShots;
 
@@ -101,6 +106,7 @@ class Graphics {
        void setFontSize(int size);
        SDL_Surface *getString(const char *in, bool transparent);
        void drawString(const char *in, int x, int y, int alignment, SDL_Surface *dest);
+       void drawString(const char *in, int x, int y, int alignment, SDL_Surface *dest, SurfaceCache &cache);
        void clearChatString();
        void createChatString(const char *in);
        void showMedalMessage(int type, const char *in);
index c6e4e430ccd0a62714267a8ce985f87a95a65790..1e04687b08b11cc87095cb83eabda4b1a6b6e65d 100644 (file)
@@ -843,7 +843,8 @@ int doGame()
                        frameLimit = SDL_GetTicks() + 64;
 
                #if DEBUG
-               graphics.drawString(fps, 600, 30, true, graphics.screen);
+               static Graphics::SurfaceCache fpsCache;
+               graphics.drawString(fps, 600, 30, true, graphics.screen, fpsCache);
 
                if (SDL_GetTicks() > frameCounter + 500)
                {
index 4a11741ea48cc0a7a1f40f7e1bb7dd065d25c8b3..bcfe2e15ef3e61dc5f3ae0644cc503fcf13c3dfa 100644 (file)
@@ -609,16 +609,18 @@ int doHub()
 
                if (validStage)
                {
+                       static Graphics::SurfaceCache cache;
                        graphics.drawRect(10, 400, 620, 20, graphics.black, graphics.white, graphics.screen);
                        snprintf(string, sizeof string, "%s : %s", _("Selected Destination"), _(game.stageName));
-                       graphics.drawString(string, 320, 409, true, graphics.screen);
+                       graphics.drawString(string, 320, 409, true, graphics.screen, cache);
                }
                
                graphics.drawRect(10, 430, 620, 40, graphics.black, graphics.white, graphics.screen);
 
                #if DEBUG
+               static Graphics::SurfaceCache posCache;
                snprintf(pos, sizeof pos, "%.3d:%.3d", engine.getMouseX(), engine.getMouseY());
-               graphics.drawString(pos, 320, 15, true, graphics.screen);
+               graphics.drawString(pos, 320, 15, true, graphics.screen, posCache);
                #endif
 
                engine.getInput();
index 7225629ce0fc52be0b83702569d75c6dc3413100..61742664090ddc1bdf2938fb55743686b4ed1a90 100644 (file)
@@ -51,6 +51,10 @@ void doTimeRemaining()
 
 void doStatusBar()
 {
+       static Graphics::SurfaceCache healthCache;
+       static Graphics::SurfaceCache oxygenCache;
+       static Graphics::SurfaceCache jetpackCache;
+
        graphics.setFontSize(0);
        graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
 
@@ -58,7 +62,7 @@ void doStatusBar()
 
        graphics.blit(graphics.infoBar, 0, 0, graphics.screen, false);
 
-       graphics.drawString(_("Health"), 50, 5, TXT_RIGHT, graphics.screen);
+       graphics.drawString(_("Health"), 50, 5, TXT_RIGHT, graphics.screen, healthCache);
 
        for (int i = 0 ; i < MAX_HEALTH ; i++)
        {
@@ -75,7 +79,7 @@ void doStatusBar()
 
        if ((!game.hasAquaLung) && (!engine.cheatExtras))
        {
-               graphics.drawString(_("Oxygen"), 305, 5, TXT_RIGHT, graphics.screen);
+               graphics.drawString(_("Oxygen"), 305, 5, TXT_RIGHT, graphics.screen, oxygenCache);
 
                for (int i = 0 ; i < 7 ; i++)
                {
@@ -92,7 +96,7 @@ void doStatusBar()
        }
        else if ((game.hasJetPack) || (engine.cheatExtras))
        {
-               graphics.drawString(_("Jetpack"), 305, 5, TXT_RIGHT, graphics.screen);
+               graphics.drawString(_("Jetpack"), 305, 5, TXT_RIGHT, graphics.screen, jetpackCache);
 
                for (int i = 0 ; i < 7 ; i++)
                {
@@ -131,8 +135,9 @@ void doStatusBar()
                                        break;
                        }
        
+                       static Graphics::SurfaceCache cache;
                        graphics.blit(graphics.infoBar, 0, 455, graphics.screen, false);
-                       graphics.drawString(_(engine.message), 320, 466, true, graphics.screen);
+                       graphics.drawString(_(engine.message), 320, 466, true, graphics.screen, cache);
                        
                        engine.messageTime--;
                        if (engine.messageTime == -1)
@@ -150,7 +155,8 @@ void doStatusBar()
                {
                        graphics.blit(graphics.infoBar, 0, 455, graphics.screen, false);
                        
-                       graphics.drawString(_(map.mainBossPart->name), 255, 460, TXT_RIGHT, graphics.screen);
+                       static Graphics::SurfaceCache cache;
+                       graphics.drawString(_(map.mainBossPart->name), 255, 460, TXT_RIGHT, graphics.screen, cache);
                        graphics.drawRect(265 - 1, 463 - 1, 200 + 2, 10 + 2, graphics.white, graphics.screen);
                        graphics.drawRect(265, 463, 200, 10, graphics.black, graphics.screen);
                        
@@ -161,8 +167,9 @@ void doStatusBar()
                }
        }
 
+       static Graphics::SurfaceCache weaponCache;
        snprintf(string, sizeof string, "%s %s", _("Weapon:"), _(player.currentWeapon->name));
-       graphics.drawString(string, 630, 5, TXT_RIGHT, graphics.screen);
+       graphics.drawString(string, 630, 5, TXT_RIGHT, graphics.screen, weaponCache);
        
        if (game.skill == 3)
        {
@@ -185,13 +192,15 @@ void doStatusBar()
                                        }
                                }
                        }
-                       graphics.drawString(string, 320, 35, TXT_CENTERED, graphics.screen);
+                       static Graphics::SurfaceCache cache;
+                       graphics.drawString(string, 320, 35, TXT_CENTERED, graphics.screen, cache);
                }
                else
                {
+                       static Graphics::SurfaceCache cache;
                        graphics.setFontColor(0xff, 0x00, 0x00, 0x00, 0x00, 0x00);
                        graphics.setFontSize(3);
-                       graphics.drawString(_("Mission Failed! Time Up!"), 320, 220, TXT_CENTERED, graphics.screen);
+                       graphics.drawString(_("Mission Failed! Time Up!"), 320, 220, TXT_CENTERED, graphics.screen, cache);
                        graphics.setFontSize(0);
                        game.canContinue = 0;
                }
index e8df50d56c758b5fd39486c81e789ce92c07d2bb..2415a3c8835709330dcf93d7fbffb770077a80d5 100644 (file)
@@ -99,8 +99,9 @@ void doMIAs()
 
                                if ((mia->value != 100) && (!(mia->flags & ENT_DYING)))
                                {
+                                       static Graphics::SurfaceCache cache;
                                        graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
-                                       graphics.drawString(_((char*)mia_scared[mia->value]), x + 10, y - 10, true, graphics.screen);
+                                       graphics.drawString(_((char*)mia_scared[mia->value]), x + 10, y - 10, true, graphics.screen, cache);
                                }
 
                                graphics.blit(mia->getFaceImage(), x, y, graphics.screen, false);
index d562df80fe7a4c953d9ac53f12a407653878d917..7fca1903efc34ff57a0c5b028a4cd54a84dc37dd 100644 (file)
@@ -365,8 +365,9 @@ void showMissionClear()
                        }
                }
 
+               static Graphics::SurfaceCache cache;
                snprintf(message, sizeof message, "%s - %.2d:%.2d:%.2d", _("Mission Time"), game.currentMissionHours, game.currentMissionMinutes, game.currentMissionSeconds);
-               graphics.drawString(message, 320, 420, true, graphics.screen);
+               graphics.drawString(message, 320, 420, true, graphics.screen, cache);
 
                engine.delay(frameLimit);
                frameLimit = SDL_GetTicks() + 16;