72 lines
2.2 KiB
C++
72 lines
2.2 KiB
C++
#include "resources.h"
|
|
#include "style.h"
|
|
#include <clay/clay.h>
|
|
#include <SDL3/SDL_log.h>
|
|
#include <SDL3/SDL_render.h>
|
|
#include <SDL3_image/SDL_image.h>
|
|
#include <cstddef>
|
|
|
|
#ifndef CERA_RESOURCE_REGISTRY_SIZE
|
|
#define CERA_RESOURCE_REGISTRY_SIZE 64
|
|
#endif
|
|
|
|
namespace cera {
|
|
struct ResourceRegistry {
|
|
void *data;
|
|
DestructorFn destructor;
|
|
};
|
|
|
|
TTF_Font *defaultFont[FONT_MAX];
|
|
TTF_TextEngine *textEngine = nullptr;
|
|
|
|
ResourceRegistry resources[CERA_RESOURCE_REGISTRY_SIZE]{};
|
|
size_t usedResources{0};
|
|
|
|
void SetDefaultFont(char const *path) {
|
|
// LOAD REGULAR FONT
|
|
defaultFont[FONT_REGULAR] = TTF_OpenFont(path, cera::baseFontSize * 5);
|
|
if (defaultFont[FONT_REGULAR] == nullptr) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load default font '%s': %s", path, SDL_GetError());
|
|
exit(6);
|
|
}
|
|
TTF_SetFontHinting(defaultFont[FONT_REGULAR], TTF_HINTING_LIGHT_SUBPIXEL);
|
|
StoreResource(defaultFont[FONT_REGULAR], (DestructorFn)&TTF_CloseFont);
|
|
// LOAD BOLD FONT
|
|
defaultFont[FONT_BOLD] = TTF_OpenFont(path, cera::baseFontSize * 5);
|
|
if (defaultFont[FONT_BOLD] == nullptr) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load default bold font '%s': %s", path, SDL_GetError());
|
|
exit(6);
|
|
}
|
|
TTF_SetFontHinting(defaultFont[FONT_BOLD], TTF_HINTING_LIGHT_SUBPIXEL);
|
|
TTF_SetFontStyle(defaultFont[FONT_BOLD], TTF_STYLE_BOLD);
|
|
StoreResource(defaultFont[FONT_BOLD], (DestructorFn)&TTF_CloseFont);
|
|
SDL_Log("SetDefaultFont: Success");
|
|
}
|
|
|
|
void StoreResource(void *data, DestructorFn destructor) {
|
|
for (size_t i{0}; i < usedResources; ++i) {
|
|
if(resources[i].data == nullptr) {
|
|
resources[i].data = data;
|
|
resources[i].destructor = destructor;
|
|
return;
|
|
}
|
|
}
|
|
if(usedResources < CERA_RESOURCE_REGISTRY_SIZE) {
|
|
resources[usedResources].data = data;
|
|
resources[usedResources].destructor = destructor;
|
|
++usedResources;
|
|
return;
|
|
}
|
|
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to register resource, expect memory leaks. Consider using less resources or defining a higher CERA_RESOURCE_REGISTRY_SIZE at compile time");
|
|
}
|
|
|
|
void CleanupResources() {
|
|
for (size_t i{0}; i < usedResources; ++i) {
|
|
if(resources[i].data) {
|
|
resources[i].destructor(resources[i].data);
|
|
}
|
|
}
|
|
usedResources = 0;
|
|
}
|
|
}
|