Expire texts, based on age in cache.

This commit is contained in:
Steve 2015-12-20 16:13:35 +00:00
parent 1849a20831
commit 9be0cdab6c
10 changed files with 39 additions and 24 deletions

View File

@ -42,6 +42,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define FPS 60 #define FPS 60
#define LOGIC_RATE (1000 / FPS) #define LOGIC_RATE (1000 / FPS)
#define TEXT_TTL (1000 * 20)
#define MAX_NAME_LENGTH 32 #define MAX_NAME_LENGTH 32
#define MAX_DESCRIPTION_LENGTH 512 #define MAX_DESCRIPTION_LENGTH 512
#define MAX_FILENAME_LENGTH 1024 #define MAX_FILENAME_LENGTH 1024

View File

@ -20,7 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "text.h" #include "text.h"
void expireTexts(void);
static void loadFont(int size); static void loadFont(int size);
static SDL_Texture *getCachedText(unsigned long hash); static SDL_Texture *getCachedText(unsigned long hash);
static void cacheText(unsigned long hash, SDL_Texture *t); 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); t = SDL_CreateTextureFromSurface(app.renderer, surface);
SDL_FreeSurface(surface); SDL_FreeSurface(surface);
if (cacheSize >= TEXT_CACHE_SIZE)
{
expireTexts();
}
cacheText(hash, t); cacheText(hash, t);
} }
@ -209,6 +203,7 @@ static SDL_Texture *getCachedText(unsigned long hash)
{ {
if (t->hash == hash) if (t->hash == hash)
{ {
t->ttl = SDL_GetTicks() + TEXT_TTL;
return t->texture; return t->texture;
} }
} }
@ -236,36 +231,45 @@ static void cacheText(unsigned long hash, SDL_Texture *texture)
new->hash = hash; new->hash = hash;
new->texture = texture; new->texture = texture;
new->ttl = SDL_GetTicks() + TEXT_TTL;
t->next = new; t->next = new;
cacheSize++; cacheSize++;
} }
void expireTexts(void) void expireTexts(int all)
{ {
Texture *t; Texture *t, *prev;
int i, n; int i, n;
long now;
n = 0; n = 0;
now = SDL_GetTicks();
for (i = 0 ; i < NUM_TEXT_BUCKETS ; i++) 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; if (t->ttl <= now || all)
textures[i].next = t->next; {
SDL_DestroyTexture(t->texture); prev->next = t->next;
free(t); SDL_DestroyTexture(t->texture);
free(t);
n++; cacheSize--;
n++;
t = prev;
}
prev = t;
} }
textures[i].next = NULL;
} }
cacheSize = 0;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Expired %d texts", n); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Expired %d texts", n);
} }

View File

@ -22,8 +22,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "SDL2/SDL_ttf.h" #include "SDL2/SDL_ttf.h"
#define TEXT_CACHE_SIZE 256
extern void blit(SDL_Texture *texture, int x, int y, int centered); extern void blit(SDL_Texture *texture, int x, int y, int centered);
extern char *getFileLocation(char *filename); extern char *getFileLocation(char *filename);

View File

@ -26,6 +26,7 @@ int main(int argc, char *argv[])
{ {
float td; float td;
long then, lastFrameTime, frames; long then, lastFrameTime, frames;
long expireTextTimer;
SDL_Event event; SDL_Event event;
@ -47,6 +48,7 @@ int main(int argc, char *argv[])
dev.fps = frames = td = 0; dev.fps = frames = td = 0;
then = SDL_GetTicks(); then = SDL_GetTicks();
lastFrameTime = SDL_GetTicks() + 1000; lastFrameTime = SDL_GetTicks() + 1000;
expireTextTimer = SDL_GetTicks() + (1000 * 10);
while (1) while (1)
{ {
@ -116,6 +118,13 @@ int main(int argc, char *argv[])
} }
} }
if (SDL_GetTicks() > expireTextTimer)
{
expireTexts(0);
expireTextTimer = SDL_GetTicks() + (1000 * 10);
}
SDL_Delay(1); SDL_Delay(1);
} }

View File

@ -35,6 +35,7 @@ extern void doMouseDown(SDL_MouseButtonEvent *event);
extern void doMouseUp(SDL_MouseButtonEvent *event); extern void doMouseUp(SDL_MouseButtonEvent *event);
extern void doMouseWheel(SDL_MouseWheelEvent *event); extern void doMouseWheel(SDL_MouseWheelEvent *event);
extern void doDevKeys(void); extern void doDevKeys(void);
extern void expireTexts(int all);
App app; App app;
Colors colors; Colors colors;

View File

@ -56,6 +56,7 @@ typedef struct {
struct Texture { struct Texture {
char name[MAX_DESCRIPTION_LENGTH]; char name[MAX_DESCRIPTION_LENGTH];
long hash; long hash;
long ttl;
SDL_Texture *texture; SDL_Texture *texture;
Texture *next; Texture *next;
}; };

View File

@ -211,7 +211,7 @@ void cleanup(void)
destroyTextures(); destroyTextures();
expireTexts(); expireTexts(1);
destroyFonts(); destroyFonts();

View File

@ -54,7 +54,7 @@ extern void destroyBattle(void);
extern void destroyTextures(void); extern void destroyTextures(void);
extern void destroyGalacticMap(void); extern void destroyGalacticMap(void);
extern void destroyWidgets(void); extern void destroyWidgets(void);
extern void expireTexts(void); extern void expireTexts(int all);
extern void initInput(void); extern void initInput(void);
extern void createSaveFolder(void); extern void createSaveFolder(void);
extern char *getFileLocation(char *filename); extern char *getFileLocation(char *filename);

View File

@ -30,7 +30,7 @@ void startSectionTransition(void)
presentScene(); presentScene();
expireTexts(); expireTexts(1);
} }
void endSectionTransition(void) void endSectionTransition(void)

View File

@ -22,4 +22,4 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern void prepareScene(void); extern void prepareScene(void);
extern void presentScene(void); extern void presentScene(void);
extern void expireTexts(void); extern void expireTexts(int all);