]> git.mxchange.org Git - quix0rs-blobwars.git/blobdiff - src/CGraphics.cpp
Don't link pak tool with SDL.
[quix0rs-blobwars.git] / src / CGraphics.cpp
index f69dcc4108c58a1c2867a1e26a1413abad2ddd86..d9e45fa252cdcf7cac0bd5117db72811ca3997c2 100644 (file)
@@ -1,5 +1,5 @@
 /*
-Copyright (C) 2004 Parallel Realities
+Copyright (C) 2004-2011 Parallel Realities
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
@@ -159,7 +159,7 @@ void Graphics::setTransparent(SDL_Surface *sprite)
        SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(sprite->format, 0, 0, 0));
 }
 
-bool Graphics::canShowMedalMessage()
+bool Graphics::canShowMedalMessage() const
 {
        return (medalMessageTimer <= 0);
 }
@@ -616,7 +616,7 @@ void Graphics::loadFont(int i, const char *filename, int pixelSize)
 Sprite *Graphics::addSprite(const char *name)
 {
        Sprite *sprite = new Sprite;
-       strncpy(sprite->name, name, sizeof sprite->name);
+       strlcpy(sprite->name, name, sizeof sprite->name);
 
        spriteList.add(sprite);
 
@@ -662,17 +662,17 @@ void Graphics::animateSprites()
        }
 }
 
-int Graphics::getWaterAnim()
+int Graphics::getWaterAnim() const
 {
        return waterAnim;
 }
 
-int Graphics::getSlimeAnim()
+int Graphics::getSlimeAnim() const
 {
        return slimeAnim;
 }
 
-int Graphics::getLavaAnim()
+int Graphics::getLavaAnim() const
 {
        return lavaAnim;
 }
@@ -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;
@@ -914,8 +946,8 @@ void Graphics::clearChatString()
 
 void Graphics::createChatString(const char *in)
 {
-       strncat(chatString, " ", sizeof chatString);
-       strncat(chatString, in, sizeof chatString);
+       strlcat(chatString, " ", sizeof chatString);
+       strlcat(chatString, in, sizeof chatString);
 }
 
 void Graphics::drawChatString(SDL_Surface *surface, int y)
@@ -963,10 +995,10 @@ void Graphics::drawChatString(SDL_Surface *surface, int y)
 
                blit(wordSurface, x, y, surface, false);
 
-               SDL_FreeSurface(wordSurface);
-
                x += wordSurface->w;
 
+               SDL_FreeSurface(wordSurface);
+
                word = strtok(NULL, " ");
        }
 }