Files
dusk/src2/sdl_assets_loader.c
2026-02-04 15:54:09 -06:00

74 lines
1.8 KiB
C

#include "sdl_assets_loader.h"
Sprite loadSprite(SDL_Renderer *renderer, const char *filePath, int positionX, int positionY)
{
SDL_Rect textureBounds = {positionX, positionY, 0, 0};
SDL_Texture *texture = IMG_LoadTexture(renderer, filePath);
if (texture != NULL)
{
SDL_QueryTexture(texture, NULL, NULL, &textureBounds.w, &textureBounds.h);
}
Sprite sprite = {texture, textureBounds};
return sprite;
}
Mix_Chunk *loadSound(const char *filePath)
{
Mix_Chunk *sound = NULL;
sound = Mix_LoadWAV(filePath);
if (sound == NULL)
{
printf("Failed to load scratch sound effect! SDL_mixer Error: %s\n", Mix_GetError());
}
return sound;
}
Mix_Music *loadMusic(const char *filePath)
{
Mix_Music *music = NULL;
music = Mix_LoadMUS(filePath);
if (music == NULL)
{
printf("Failed to load music! SDL_mixer Error: %s\n", Mix_GetError());
}
return music;
}
void updateTextureText(SDL_Texture **texture, const char *text, TTF_Font **fontSquare, SDL_Renderer *renderer)
{
SDL_Color fontColor = {255, 255, 255};
if (*fontSquare == NULL)
{
printf("TTF_OpenFont fontSquare: %s\n", TTF_GetError());
}
SDL_Surface *surface = TTF_RenderUTF8_Blended(*fontSquare, text, fontColor);
if (surface == NULL)
{
printf("TTF_OpenFont: %s\n", TTF_GetError());
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Unable to create text surface! SDL Error: %s\n", SDL_GetError());
exit(3);
}
SDL_DestroyTexture(*texture);
*texture = SDL_CreateTextureFromSurface(renderer, surface);
if (*texture == NULL)
{
printf("TTF_OpenFont: %s\n", TTF_GetError());
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Unable to create texture from surface! SDL Error: %s\n", SDL_GetError());
}
SDL_FreeSurface(surface);
}