From 9be0cdab6c7c73c45dcc18fdec058362f5b8384c Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 20 Dec 2015 16:13:35 +0000 Subject: [PATCH] Expire texts, based on age in cache. --- src/defs.h | 2 ++ src/draw/text.c | 40 ++++++++++++++++++++++------------------ src/draw/text.h | 2 -- src/main.c | 9 +++++++++ src/main.h | 1 + src/structs.h | 1 + src/system/init.c | 2 +- src/system/init.h | 2 +- src/system/transition.c | 2 +- src/system/transition.h | 2 +- 10 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/defs.h b/src/defs.h index fb44f5e..6402b71 100644 --- a/src/defs.h +++ b/src/defs.h @@ -42,6 +42,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define FPS 60 #define LOGIC_RATE (1000 / FPS) +#define TEXT_TTL (1000 * 20) + #define MAX_NAME_LENGTH 32 #define MAX_DESCRIPTION_LENGTH 512 #define MAX_FILENAME_LENGTH 1024 diff --git a/src/draw/text.c b/src/draw/text.c index 22d2ed1..3966657 100644 --- a/src/draw/text.c +++ b/src/draw/text.c @@ -20,7 +20,6 @@ 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); @@ -89,11 +88,6 @@ 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); } @@ -209,6 +203,7 @@ static SDL_Texture *getCachedText(unsigned long hash) { if (t->hash == hash) { + t->ttl = SDL_GetTicks() + TEXT_TTL; return t->texture; } } @@ -236,36 +231,45 @@ static void cacheText(unsigned long hash, SDL_Texture *texture) new->hash = hash; new->texture = texture; + new->ttl = SDL_GetTicks() + TEXT_TTL; t->next = new; cacheSize++; } -void expireTexts(void) +void expireTexts(int all) { - Texture *t; + Texture *t, *prev; int i, n; + long now; n = 0; + now = SDL_GetTicks(); for (i = 0 ; i < NUM_TEXT_BUCKETS ; i++) { - while (textures[i].next) + prev = &textures[i]; + + for (t = textures[i].next ; t != NULL ; t = t->next) { - t = textures[i].next; - textures[i].next = t->next; - SDL_DestroyTexture(t->texture); - free(t); + if (t->ttl <= now || all) + { + prev->next = t->next; + SDL_DestroyTexture(t->texture); + free(t); + + cacheSize--; + + n++; + + t = prev; + } - n++; + prev = t; } - - 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 8d4ec45..3dd4b70 100644 --- a/src/draw/text.h +++ b/src/draw/text.h @@ -22,8 +22,6 @@ 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); diff --git a/src/main.c b/src/main.c index b009fa5..52b260b 100644 --- a/src/main.c +++ b/src/main.c @@ -26,6 +26,7 @@ int main(int argc, char *argv[]) { float td; long then, lastFrameTime, frames; + long expireTextTimer; SDL_Event event; @@ -47,6 +48,7 @@ int main(int argc, char *argv[]) dev.fps = frames = td = 0; then = SDL_GetTicks(); lastFrameTime = SDL_GetTicks() + 1000; + expireTextTimer = SDL_GetTicks() + (1000 * 10); while (1) { @@ -115,6 +117,13 @@ int main(int argc, char *argv[]) saveScreenshot(); } } + + if (SDL_GetTicks() > expireTextTimer) + { + expireTexts(0); + + expireTextTimer = SDL_GetTicks() + (1000 * 10); + } SDL_Delay(1); } diff --git a/src/main.h b/src/main.h index 2faa380..5615343 100644 --- a/src/main.h +++ b/src/main.h @@ -35,6 +35,7 @@ extern void doMouseDown(SDL_MouseButtonEvent *event); extern void doMouseUp(SDL_MouseButtonEvent *event); extern void doMouseWheel(SDL_MouseWheelEvent *event); extern void doDevKeys(void); +extern void expireTexts(int all); App app; Colors colors; diff --git a/src/structs.h b/src/structs.h index 153ccdd..dc103e9 100644 --- a/src/structs.h +++ b/src/structs.h @@ -56,6 +56,7 @@ typedef struct { struct Texture { char name[MAX_DESCRIPTION_LENGTH]; long hash; + long ttl; SDL_Texture *texture; Texture *next; }; diff --git a/src/system/init.c b/src/system/init.c index e78def6..8d49d1d 100644 --- a/src/system/init.c +++ b/src/system/init.c @@ -211,7 +211,7 @@ void cleanup(void) destroyTextures(); - expireTexts(); + expireTexts(1); destroyFonts(); diff --git a/src/system/init.h b/src/system/init.h index 672801b..92c9e65 100644 --- a/src/system/init.h +++ b/src/system/init.h @@ -54,7 +54,7 @@ extern void destroyBattle(void); extern void destroyTextures(void); extern void destroyGalacticMap(void); extern void destroyWidgets(void); -extern void expireTexts(void); +extern void expireTexts(int all); extern void initInput(void); extern void createSaveFolder(void); extern char *getFileLocation(char *filename); diff --git a/src/system/transition.c b/src/system/transition.c index 7904dc9..2db2ff9 100644 --- a/src/system/transition.c +++ b/src/system/transition.c @@ -30,7 +30,7 @@ void startSectionTransition(void) presentScene(); - expireTexts(); + expireTexts(1); } void endSectionTransition(void) diff --git a/src/system/transition.h b/src/system/transition.h index df46a42..103ae06 100644 --- a/src/system/transition.h +++ b/src/system/transition.h @@ -22,4 +22,4 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern void prepareScene(void); extern void presentScene(void); -extern void expireTexts(void); +extern void expireTexts(int all);