updated font asset handling

pull/1/head
Sara 2023-04-21 00:25:05 +02:00
parent 57c507bb00
commit a5d9d5ec96
2 changed files with 23 additions and 4 deletions

View File

@ -53,10 +53,13 @@ TTF_Font* load_font(const char* file, int size) {
.type = RESOURCETYPE_FONT, .type = RESOURCETYPE_FONT,
.hash = hashstr(file), .hash = hashstr(file),
.name = calloc(len+1, sizeof(char)), .name = calloc(len+1, sizeof(char)),
.font = {
.size = size,
.font = TTF_OpenFont(file, size) .font = TTF_OpenFont(file, size)
}
}; };
strcpy(res.name, file); strcpy(res.name, file);
return insert_asset(&res)->font; return insert_asset(&res)->font.font;
} }
SDL_Texture* get_texture(const char* file) { SDL_Texture* get_texture(const char* file) {
@ -68,6 +71,19 @@ SDL_Texture* get_texture(const char* file) {
} }
} }
TTF_Font* get_font(const char *file, int size) {
uint32_t hash = hashstr(file);
for(resource_t* res = g_assets; res != g_assets_endptr; ++res) {
if(res->hash == hash
&& strcmp(res->name, file) == 0
&& res->type == RESOURCETYPE_FONT
&& res->font.size == size) {
return res->font.font;
}
}
return load_font(file, size);
}
resource_t* get_asset(const char* file) { resource_t* get_asset(const char* file) {
uint32_t hash = hashstr(file); uint32_t hash = hashstr(file);
for(resource_t* res = g_assets; res != g_assets_endptr; ++res) { for(resource_t* res = g_assets; res != g_assets_endptr; ++res) {
@ -88,7 +104,7 @@ void _delete_referenced_asset(resource_t* res) {
SDL_DestroyTexture(res->texture); SDL_DestroyTexture(res->texture);
break; break;
case RESOURCETYPE_FONT: case RESOURCETYPE_FONT:
TTF_CloseFont(res->font); TTF_CloseFont(res->font.font);
break; break;
case RESOURCETYPE_ARBITRARY: case RESOURCETYPE_ARBITRARY:
res->arbitrary_type.deleter(res->arbitrary_type.memory); res->arbitrary_type.deleter(res->arbitrary_type.memory);

View File

@ -25,7 +25,10 @@ typedef struct resource_t {
char* name; char* name;
union { union {
SDL_Texture* texture; SDL_Texture* texture;
struct {
int size;
TTF_Font* font; TTF_Font* font;
} font;
struct { struct {
void* memory; void* memory;
deleter_t deleter; deleter_t deleter;