]> git.mxchange.org Git - quix0rs-blobwars.git/blobdiff - src/CGraphics.cpp
Use strlcat() and strlcpy(), #define wrappers when these functions are not available.
[quix0rs-blobwars.git] / src / CGraphics.cpp
index 5c3071e381c22df784b1760960c5bbda99998c4a..6dde3df167f10191bccce5397e3f1db6a6d40e1b 100644 (file)
@@ -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);
 
@@ -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)