add font caching
This commit is contained in:
+50
-9
@@ -1,8 +1,50 @@
|
||||
#include "text.h"
|
||||
#include <SDL2/SDL_log.h>
|
||||
|
||||
RenderObject_t text_renderText(Position position, SDL_Color color, TTF_Font* font, char* format, ...){
|
||||
#define FONT_CACHE_SIZE 100
|
||||
#define MAX_TEXT_SIZE 100
|
||||
|
||||
char text[100];
|
||||
typedef struct FontCacheObject {
|
||||
char* file;
|
||||
int size;
|
||||
TTF_Font* font;
|
||||
} FontCacheObject_t;
|
||||
|
||||
FontCacheObject_t fontCache[FONT_CACHE_SIZE];
|
||||
unsigned fontCacheSize = 0;
|
||||
|
||||
void text_freeFontCache(){
|
||||
for(unsigned i = 0; i < fontCacheSize; ++i){
|
||||
free(fontCache[i].file);
|
||||
TTF_CloseFont(fontCache[i].font);
|
||||
}
|
||||
}
|
||||
|
||||
int text_renderText(RenderObject_t* renderObject, Position position, SDL_Color color, const char* fontFilename, int fontSize, char* format, ...){
|
||||
|
||||
TTF_Font* font = NULL;
|
||||
for(unsigned i = 0; i < fontCacheSize; ++i){
|
||||
if(strcmp(fontCache[i].file, fontFilename) && fontCache[i].size == fontSize){
|
||||
font = fontCache[i].font;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(font == NULL) {
|
||||
font = TTF_OpenFont(fontFilename, fontSize);
|
||||
if(font == NULL) {
|
||||
SDL_LogError(SDL_LOG_PRIORITY_ERROR, "TTF_OpenFont");
|
||||
return -1;
|
||||
}
|
||||
FontCacheObject_t fontCacheObject;
|
||||
fontCacheObject.file = (char*)malloc(strlen(fontFilename) + 1);
|
||||
strcpy(fontCacheObject.file, fontFilename);
|
||||
fontCacheObject.size = fontSize;
|
||||
fontCacheObject.font = font;
|
||||
fontCache[fontCacheSize] = fontCacheObject;
|
||||
++fontCacheSize;
|
||||
}
|
||||
|
||||
char text[MAX_TEXT_SIZE];
|
||||
va_list args;
|
||||
|
||||
memset(&text, '\0', sizeof(text));
|
||||
@@ -11,14 +53,13 @@ RenderObject_t text_renderText(Position position, SDL_Color color, TTF_Font* fon
|
||||
vsprintf(text, format, args);
|
||||
va_end(args);
|
||||
|
||||
|
||||
RenderObject_t renderObject;
|
||||
SDL_Surface* surface = TTF_RenderText_Blended(font, text, color);
|
||||
|
||||
|
||||
renderObject.x = position.x;
|
||||
renderObject.y = position.y;
|
||||
renderObject.z = position.z;
|
||||
renderObject.surface = surface;
|
||||
return renderObject;
|
||||
renderObject->x = position.x;
|
||||
renderObject->y = position.y;
|
||||
renderObject->z = position.z;
|
||||
renderObject->surface = surface;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,8 +1,6 @@
|
||||
#include <SDL/SDL_ttf.h>
|
||||
#include "renderer.h"
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
int x;
|
||||
int y;
|
||||
@@ -10,4 +8,6 @@ typedef struct {
|
||||
} Position;
|
||||
|
||||
|
||||
RenderObject_t text_renderText(Position position, SDL_Color color, TTF_Font* font, char* format, ...);
|
||||
int text_renderText(RenderObject_t* renderObject, Position position, SDL_Color color, const char* fontFilename, int fontSize, char* format, ...);
|
||||
|
||||
void text_freeFontCache();
|
||||
Reference in New Issue
Block a user