100 lines
4 KiB
C++
100 lines
4 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];
|
|
SDL_Texture *diceImagesSelected[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);
|
|
}
|
|
diceImagesSelected[COIN_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d2.svg");
|
|
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);
|
|
}
|
|
diceImagesSelected[D4_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d4.svg");
|
|
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);
|
|
}
|
|
diceImagesSelected[D6_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d6.svg");
|
|
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);
|
|
}
|
|
diceImagesSelected[D8_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d8.svg");
|
|
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);
|
|
}
|
|
diceImagesSelected[D10_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d10.svg");
|
|
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);
|
|
}
|
|
diceImagesSelected[D12_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d12.svg");
|
|
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);
|
|
}
|
|
diceImagesSelected[D20_IMAGE] = IMG_LoadTexture(renderer, "assets/icons/d20.svg");
|
|
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);
|
|
color = ToHoveredColor(dieColors[i]);
|
|
SDL_SetTextureColorMod(diceImagesSelected[i], color.r, color.g, color.b);
|
|
}
|
|
}
|
|
|
|
void LoadResources() {
|
|
LoadFonts();
|
|
LoadDiceImages();
|
|
}
|
|
|
|
SDL_Texture *GetDiceImage(enum Dice_Die die, bool selected) {
|
|
switch (die) {
|
|
default:
|
|
case COIN:
|
|
return selected ? diceImagesSelected[COIN_IMAGE] : diceImages[COIN_IMAGE];
|
|
case D4:
|
|
return selected ? diceImagesSelected[D4_IMAGE] : diceImages[D4_IMAGE];
|
|
case D6:
|
|
return selected ? diceImagesSelected[D6_IMAGE] : diceImages[D6_IMAGE];
|
|
case D8:
|
|
return selected ? diceImagesSelected[D8_IMAGE] : diceImages[D8_IMAGE];
|
|
case D10:
|
|
case D100:
|
|
return selected ? diceImagesSelected[D10_IMAGE] : diceImages[D10_IMAGE];
|
|
case D12:
|
|
return selected ? diceImagesSelected[D12_IMAGE] : diceImages[D12_IMAGE];
|
|
case D20:
|
|
return selected ? diceImagesSelected[D20_IMAGE] : diceImages[D20_IMAGE];
|
|
}
|
|
}
|