From 01286b042bb73562e7f82b8b67590e4bf4faeb4f Mon Sep 17 00:00:00 2001
From: Steve <stephenjsweeney@battleforthesolarsystem.com>
Date: Fri, 18 Dec 2015 17:09:23 +0000
Subject: [PATCH] Limit text cache size.

---
 src/draw/text.c | 11 +++++++++++
 src/draw/text.h |  2 ++
 2 files changed, 13 insertions(+)

diff --git a/src/draw/text.c b/src/draw/text.c
index c6ead8f..22d2ed1 100644
--- a/src/draw/text.c
+++ b/src/draw/text.c
@@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include "text.h"
 
+void expireTexts(void);
 static void loadFont(int size);
 static SDL_Texture *getCachedText(unsigned long hash);
 static void cacheText(unsigned long hash, SDL_Texture *t);
@@ -32,6 +33,7 @@ static char drawTextBuffer[MAX_DESCRIPTION_LENGTH];
 static TTF_Font *font[MAX_FONTS];
 static Texture textures[NUM_TEXT_BUCKETS]; 
 static int maxWidth = 0;
+static int cacheSize = 0;
 
 void initFonts(void)
 {
@@ -87,6 +89,11 @@ static void drawTextNormal(int x, int y, int size, int align, SDL_Color c, char
 		t = SDL_CreateTextureFromSurface(app.renderer, surface);
 		SDL_FreeSurface(surface);
 		
+		if (cacheSize >= TEXT_CACHE_SIZE)
+		{
+			expireTexts();
+		}
+		
 		cacheText(hash, t);
 	}
 
@@ -231,6 +238,8 @@ static void cacheText(unsigned long hash, SDL_Texture *texture)
 	new->texture = texture;
 
 	t->next = new;
+	
+	cacheSize++;
 }
 
 void expireTexts(void)
@@ -255,6 +264,8 @@ void expireTexts(void)
 		textures[i].next = NULL;
 	}
 	
+	cacheSize = 0;
+	
 	SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Expired %d texts", n);
 }
 
diff --git a/src/draw/text.h b/src/draw/text.h
index 3dd4b70..8d4ec45 100644
--- a/src/draw/text.h
+++ b/src/draw/text.h
@@ -22,6 +22,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include "SDL2/SDL_ttf.h"
 
+#define TEXT_CACHE_SIZE 256
+
 extern void blit(SDL_Texture *texture, int x, int y, int centered);
 extern char *getFileLocation(char *filename);