they're very hasty

This commit is contained in:
cat 2025-05-26 15:02:11 +10:00
parent 3840fb0321
commit d65a270aab
10 changed files with 102 additions and 17 deletions

26
img.c
View file

@ -1,4 +1,7 @@
#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"
@ -13,9 +16,13 @@ free_image(Image *i)
babble("free img %s texture...", i->name);
SDL_DestroyTexture(i->tex);
}
if (i->sur) {
babble("destroy image %s surface...", i->name);
SDL_DestroySurface(i->sur);
}
if (i->data) {
babble("free img %s data...", i->name);
SDL_free(i->data);
stbi_image_free(i->data);
}
if (i->name) {
babble("free img %s name...", i->name);
@ -30,7 +37,6 @@ Image *
load_image(SDL_Renderer *ren, char *path)
{
Image *i;
SDL_Surface *sur;
babble("create image %s...", path);
@ -41,7 +47,7 @@ load_image(SDL_Renderer *ren, char *path)
return NULL;
}
babble("alloc image name...", path);
babble("alloc image %s name...", path);
i->name = SDL_strdup(path);
if (!i->name) {
shit("strdup fail: %s", SDL_GetError());
@ -49,31 +55,33 @@ load_image(SDL_Renderer *ren, char *path)
return NULL;
}
babble("alloc image %s data...", i->name);
i->data = stbi_load(path, &i->w, &i->h,
&i->bpp, 4);
if (!i->data) {
shit("mem alloc fail: %s", SDL_GetError());
shit("img load fail: %s", stbi_failure_reason());
free_image(i);
return NULL;
}
sur = SDL_CreateSurfaceFrom(i->w, i->h,
babble("alloc image %s surface...", i->name);
i->sur = SDL_CreateSurfaceFrom(i->w, i->h,
SDL_PIXELFORMAT_RGBA32, i->data,
i->w * 4);
if (!sur) {
if (!i->sur) {
shit("surface creation fail: %s", SDL_GetError());
free_image(i);
return NULL;
}
i->tex = SDL_CreateTextureFromSurface(ren, sur);
babble("alloc image %s texture...", i->name);
i->tex = SDL_CreateTextureFromSurface(ren, i->sur);
if (!i->tex) {
shit("surface creation fail: %s", SDL_GetError());
SDL_DestroySurface(sur);
SDL_DestroySurface(i->sur);
free_image(i);
return NULL;
}
SDL_DestroySurface(sur);
babble("image %s created", path);
return i;