extract image loading

This commit is contained in:
cat 2025-05-26 13:08:18 +10:00
parent 9ece614c73
commit 3840fb0321
5 changed files with 91 additions and 40 deletions

View file

@ -4,9 +4,11 @@ PREFIX ?= /usr/local
CFLAGS := $(CFLAGS) `pkg-config --cflags --libs sdl3` CFLAGS := $(CFLAGS) `pkg-config --cflags --libs sdl3`
LDFLAGS := $(LDFLAGS) -lm LDFLAGS := $(LDFLAGS) -lm
SRCS = slutpet.c img.c
all: slutpet all: slutpet
slutpet: slutpet.c slutpet: ${SRCS}
.PHONY: clean .PHONY: clean
clean: clean:

1
dat.h
View file

@ -2,6 +2,7 @@ typedef struct Image Image;
typedef struct State State; typedef struct State State;
struct Image { struct Image {
char *name; /* used for debugging purposes */
int w; int w;
int h; int h;
int bpp; int bpp;

3
fns.h
View file

@ -2,3 +2,6 @@
#define info(...) SDL_Log(__VA_ARGS__) #define info(...) SDL_Log(__VA_ARGS__)
#define shit(...) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) #define shit(...) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
#define fuck(...) SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) #define fuck(...) SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
void free_image(Image *i);
Image *load_image(SDL_Renderer *ren, char *path);

80
img.c Normal file
View file

@ -0,0 +1,80 @@
#define STB_IMAGE_IMPLEMENTATION
#include <SDL3/SDL.h>
#include "stb_image.h"
#include "dat.h"
#include "fns.h"
void
free_image(Image *i)
{
if (i) {
if (i->tex) {
babble("free img %s texture...", i->name);
SDL_DestroyTexture(i->tex);
}
if (i->data) {
babble("free img %s data...", i->name);
SDL_free(i->data);
}
if (i->name) {
babble("free img %s name...", i->name);
SDL_free(i->name);
}
babble("free aforementioned img...");
SDL_free(i);
}
}
Image *
load_image(SDL_Renderer *ren, char *path)
{
Image *i;
SDL_Surface *sur;
babble("create image %s...", path);
babble("alloc image...", path);
i = SDL_calloc(1, sizeof(Image));
if (!i) {
shit("mem alloc fail: %s", SDL_GetError());
return NULL;
}
babble("alloc image name...", path);
i->name = SDL_strdup(path);
if (!i->name) {
shit("strdup fail: %s", SDL_GetError());
free_image(i);
return NULL;
}
i->data = stbi_load(path, &i->w, &i->h,
&i->bpp, 4);
if (!i->data) {
shit("mem alloc fail: %s", SDL_GetError());
free_image(i);
return NULL;
}
sur = SDL_CreateSurfaceFrom(i->w, i->h,
SDL_PIXELFORMAT_RGBA32, i->data,
i->w * 4);
if (!sur) {
shit("surface creation fail: %s", SDL_GetError());
free_image(i);
return NULL;
}
i->tex = SDL_CreateTextureFromSurface(ren, sur);
if (!i->tex) {
shit("surface creation fail: %s", SDL_GetError());
SDL_DestroySurface(sur);
free_image(i);
return NULL;
}
SDL_DestroySurface(sur);
babble("image %s created", path);
return i;
}

View file

@ -1,11 +1,10 @@
#define SDL_MAIN_USE_CALLBACKS #define SDL_MAIN_USE_CALLBACKS
#define STB_IMAGE_IMPLEMENTATION
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
#include "stb_image.h" #include "stb_image.h"
#include "fns.h"
#include "dat.h" #include "dat.h"
#include "fns.h"
void void
SDL_AppQuit(void *as, SDL_AppResult res) SDL_AppQuit(void *as, SDL_AppResult res)
@ -14,18 +13,7 @@ SDL_AppQuit(void *as, SDL_AppResult res)
st = as; st = as;
if (st) { if (st) {
if (st->test) { free_image(st->test);
if (st->test->tex) {
babble("free test texture...");
SDL_DestroyTexture(st->test->tex);
}
if (st->test->data) {
babble("free test img data...");
SDL_free(st->test->data);
}
babble("free test img...");
SDL_free(st->test);
}
babble("free state..."); babble("free state...");
SDL_free(st); SDL_free(st);
} }
@ -71,7 +59,6 @@ SDL_AppResult
SDL_AppInit(void **as, int argc, char **argv) SDL_AppInit(void **as, int argc, char **argv)
{ {
State *st; State *st;
SDL_Surface *sur;
babble("init sdl..."); babble("init sdl...");
if (!SDL_Init(SDL_INIT_VIDEO)) { if (!SDL_Init(SDL_INIT_VIDEO)) {
@ -100,31 +87,9 @@ SDL_AppInit(void **as, int argc, char **argv)
} }
babble("create test image..."); babble("create test image...");
st->test = SDL_calloc(1, sizeof(Image)); st->test = load_image(st->ren, "test.png");
if (!st->test) { if (!st->test) {
fuck("mem alloc fail: %s", SDL_GetError()); fuck("img creation fail: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
st->test->data = stbi_load("test.png", &st->test->w, &st->test->h,
&st->test->bpp, 4);
if (!st->test->data) {
fuck("mem alloc fail: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
sur = SDL_CreateSurfaceFrom(st->test->w, st->test->h,
SDL_PIXELFORMAT_RGBA32, st->test->data,
st->test->w * 4);
if (!sur) {
fuck("surface creation fail: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
st->test->tex = SDL_CreateTextureFromSurface(st->ren, sur);
if (!st->test->tex) {
fuck("surface creation fail: %s", SDL_GetError());
SDL_DestroySurface(sur);
return SDL_APP_FAILURE; return SDL_APP_FAILURE;
} }