diff --git a/Makefile b/Makefile index f8a3bc6..413e745 100644 --- a/Makefile +++ b/Makefile @@ -4,9 +4,11 @@ PREFIX ?= /usr/local CFLAGS := $(CFLAGS) `pkg-config --cflags --libs sdl3` LDFLAGS := $(LDFLAGS) -lm +SRCS = slutpet.c img.c + all: slutpet -slutpet: slutpet.c +slutpet: ${SRCS} .PHONY: clean clean: diff --git a/dat.h b/dat.h index d9daf4d..75a7876 100644 --- a/dat.h +++ b/dat.h @@ -2,6 +2,7 @@ typedef struct Image Image; typedef struct State State; struct Image { + char *name; /* used for debugging purposes */ int w; int h; int bpp; diff --git a/fns.h b/fns.h index 75806f5..a4fec5a 100644 --- a/fns.h +++ b/fns.h @@ -2,3 +2,6 @@ #define info(...) SDL_Log(__VA_ARGS__) #define shit(...) SDL_LogError(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); diff --git a/img.c b/img.c new file mode 100644 index 0000000..83ae0c9 --- /dev/null +++ b/img.c @@ -0,0 +1,80 @@ +#define STB_IMAGE_IMPLEMENTATION +#include +#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; +} diff --git a/slutpet.c b/slutpet.c index 1f209a0..837f23a 100644 --- a/slutpet.c +++ b/slutpet.c @@ -1,11 +1,10 @@ #define SDL_MAIN_USE_CALLBACKS -#define STB_IMAGE_IMPLEMENTATION #include #include #include "stb_image.h" -#include "fns.h" #include "dat.h" +#include "fns.h" void SDL_AppQuit(void *as, SDL_AppResult res) @@ -14,18 +13,7 @@ SDL_AppQuit(void *as, SDL_AppResult res) st = as; if (st) { - if (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); - } + free_image(st->test); babble("free state..."); SDL_free(st); } @@ -71,7 +59,6 @@ SDL_AppResult SDL_AppInit(void **as, int argc, char **argv) { State *st; - SDL_Surface *sur; babble("init sdl..."); if (!SDL_Init(SDL_INIT_VIDEO)) { @@ -100,31 +87,9 @@ SDL_AppInit(void **as, int argc, char **argv) } babble("create test image..."); - st->test = SDL_calloc(1, sizeof(Image)); + st->test = load_image(st->ren, "test.png"); if (!st->test) { - fuck("mem alloc 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); + fuck("img creation fail: %s", SDL_GetError()); return SDL_APP_FAILURE; }