dice-gui/src/resources.c
2025-09-18 14:57:05 +02:00

90 lines
3 KiB
C

#include "resources.h"
#include "defs.h"
#include "style.h"
#include <SDL3/SDL_log.h>
#include <SDL3/SDL_render.h>
#include <SDL3_image/SDL_image.h>
TTF_Font *fonts[FONT_MAX];
SDL_Texture *diceImages[DICE_IMAGE_MAX];
TTF_TextEngine *textEngine = nullptr;
static inline
void LoadFonts() {
fonts[FONT_DEFAULT] = TTF_OpenFont("assets/AdwaitaSans-Regular.ttf", baseFontSize * 5);
if (fonts[FONT_DEFAULT] == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load adwaita sans: %s", SDL_GetError());
exit(6);
}
TTF_SetFontHinting(fonts[FONT_DEFAULT], TTF_HINTING_LIGHT_SUBPIXEL);
fonts[FONT_BOLD] = TTF_OpenFont("assets/AdwaitaSans-Regular.ttf", baseFontSize * 5);
if (fonts[FONT_BOLD] == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load adwaita sans bold: %s", SDL_GetError());
exit(6);
}
TTF_SetFontHinting(fonts[FONT_BOLD], TTF_HINTING_LIGHT_SUBPIXEL);
TTF_SetFontStyle(fonts[FONT_BOLD], TTF_STYLE_BOLD);
}
static inline
void LoadDiceImages() {
if(!(diceImages[COIN_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d2.svg"))) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "IMG_LoadTexture Failed to load die texture: %s", SDL_GetError());
exit(7);
}
if(!(diceImages[D4_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d4.svg"))) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "IMG_LoadTexture Failed to load die texture: %s", SDL_GetError());
exit(7);
}
if(!(diceImages[D6_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d6.svg"))) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "IMG_LoadTexture Failed to load die texture: %s", SDL_GetError());
exit(7);
}
if(!(diceImages[D8_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d8.svg"))) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "IMG_LoadTexture Failed to load die texture: %s", SDL_GetError());
exit(7);
}
if(!(diceImages[D10_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d10.svg"))) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "IMG_LoadTexture Failed to load die texture: %s", SDL_GetError());
exit(7);
}
if(!(diceImages[D12_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d12.svg"))) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "IMG_LoadTexture Failed to load die texture: %s", SDL_GetError());
exit(7);
}
if(!(diceImages[D20_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d20.svg"))) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "IMG_LoadTexture Failed to load die texture: %s", SDL_GetError());
exit(7);
}
for (size_t i = 0; i < DICE_IMAGE_MAX; ++i) {
Clay_Color color = dieColors[i];
SDL_SetTextureColorMod(diceImages[i], color.r, color.g, color.b);
}
}
void LoadResources() {
LoadFonts();
LoadDiceImages();
}
SDL_Texture *GetDiceImage(enum Dice_Die die) {
switch (die) {
case COIN:
return diceImages[COIN_IMAGE];
case D4:
return diceImages[D4_IMAGE];
case D6:
return diceImages[D6_IMAGE];
case D8:
return diceImages[D8_IMAGE];
case D10:
return diceImages[D10_IMAGE];
case D12:
return diceImages[D12_IMAGE];
case D20:
return diceImages[D20_IMAGE];
case D100:
return diceImages[D10_IMAGE];
}
}