59 lines
1.3 KiB
C
59 lines
1.3 KiB
C
#define STB_IMAGE_IMPLEMENTATION
|
|
#define STBI_MALLOC(sz) SDL_malloc(sz)
|
|
#define STBI_REALLOC(p,newsz) SDL_realloc(p,newsz)
|
|
#define STBI_FREE(p) SDL_free(p)
|
|
#include <SDL3/SDL.h>
|
|
#include "stb_image.h"
|
|
|
|
#include "dat.h"
|
|
#include "fns.h"
|
|
|
|
void
|
|
free_image(Image *i)
|
|
{
|
|
if (i->tex) {
|
|
babble("destroying texture for image %s...", i->name);
|
|
SDL_DestroyTexture(i->tex);
|
|
}
|
|
if (i->sur) {
|
|
babble("destroying surface for image %s...", i->name);
|
|
SDL_DestroySurface(i->sur);
|
|
}
|
|
if (i->data) {
|
|
babble("destroying data for image %s...", i->name);
|
|
stbi_image_free(i->data);
|
|
}
|
|
}
|
|
|
|
int
|
|
load_image(Image *i, SDL_Renderer *ren)
|
|
{
|
|
babble("opening image %s...", i->name);
|
|
i->data = stbi_load(i->name, &i->w, &i->h, &i->bpp, 4);
|
|
if (!i->data) {
|
|
shit("stbi_load: %s", stbi_failure_reason());
|
|
goto err;
|
|
}
|
|
|
|
babble("creating surface for image %s...", i->name);
|
|
i->sur = SDL_CreateSurfaceFrom(i->w, i->h, SDL_PIXELFORMAT_RGBA32,
|
|
i->data, i->w * 4);
|
|
if (!i->sur) {
|
|
shit("SDL_CreateSurfaceFrom: %s", SDL_GetError());
|
|
goto err;
|
|
}
|
|
|
|
babble("creating texture for image %s...", i->name);
|
|
i->tex = SDL_CreateTextureFromSurface(ren, i->sur);
|
|
if (!i->tex) {
|
|
shit("SDL_CreateTextureFromSurface: %s", SDL_GetError());
|
|
goto err;
|
|
}
|
|
|
|
babble("created image %s!", i->name);
|
|
return 0;
|
|
|
|
err:
|
|
free_image(i);
|
|
return -1;
|
|
}
|