diff --git a/Makefile b/Makefile index 413e745..377ac21 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ PREFIX ?= /usr/local CFLAGS := $(CFLAGS) `pkg-config --cflags --libs sdl3` LDFLAGS := $(LDFLAGS) -lm -SRCS = slutpet.c img.c +SRCS = slutpet.c img.c samply.c all: slutpet diff --git a/dat.h b/dat.h index 75a7876..6457e27 100644 --- a/dat.h +++ b/dat.h @@ -1,12 +1,20 @@ +typedef struct Samply Samply; typedef struct Image Image; typedef struct State State; +struct Samply { + Image *idle; + Image *walka; + Image *walkb; +}; + struct Image { char *name; /* used for debugging purposes */ int w; int h; int bpp; unsigned char *data; + SDL_Surface *sur; SDL_Texture *tex; }; @@ -14,5 +22,5 @@ struct State { SDL_Window *win; SDL_Renderer *ren; - Image *test; + Samply *sam; }; diff --git a/fns.h b/fns.h index a4fec5a..6e26cb6 100644 --- a/fns.h +++ b/fns.h @@ -5,3 +5,6 @@ void free_image(Image *i); Image *load_image(SDL_Renderer *ren, char *path); + +void put_samply_to_bed(Samply *sam); +Samply *wake_samply_up(SDL_Renderer *ren); diff --git a/img.c b/img.c index 83ae0c9..9072882 100644 --- a/img.c +++ b/img.c @@ -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 #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; diff --git a/samply.c b/samply.c new file mode 100644 index 0000000..16ce3b1 --- /dev/null +++ b/samply.c @@ -0,0 +1,59 @@ +#include + +#include "dat.h" +#include "fns.h" + +void +put_samply_to_bed(Samply *sam) +{ + if (sam) { + babble("free samply idle!!"); + free_image(sam->idle); + babble("free samply walk a!!"); + free_image(sam->walka); + babble("free samply walk b!!"); + free_image(sam->walkb); + } + + babble("good night, samply!!"); + SDL_free(sam); +} + +Samply * +wake_samply_up(SDL_Renderer *ren) +{ + Samply *sam; + + babble("wake up, samply!!"); + sam = SDL_calloc(1, sizeof(Samply)); + if (!sam) { + fuck("samply would rather stay in bed. too bad! %s", SDL_GetError()); + return NULL; + } + + babble("stand samply up!!"); + sam->idle = load_image(ren, "samply/Samply.png"); + if (!sam->idle) { + fuck("samply would rather stay in bed. too bad! %s", SDL_GetError()); + put_samply_to_bed(sam); + return NULL; + } + + babble("samply walk! part 1!!"); + sam->walka = load_image(ren, "samply/Samply_Walk1.png"); + if (!sam->walka) { + fuck("samply would rather stay in bed. too bad! %s", SDL_GetError()); + put_samply_to_bed(sam); + return NULL; + } + + babble("samply walk! part 2!!"); + sam->walkb = load_image(ren, "samply/Samply_Walk2.png"); + if (!sam->walkb) { + fuck("samply would rather stay in bed. too bad! %s", SDL_GetError()); + put_samply_to_bed(sam); + return NULL; + } + + return sam; +} diff --git a/samply/Samply.png b/samply/Samply.png new file mode 100644 index 0000000..7494ad9 Binary files /dev/null and b/samply/Samply.png differ diff --git a/samply/Samply_Walk1.png b/samply/Samply_Walk1.png new file mode 100644 index 0000000..41b0f9b Binary files /dev/null and b/samply/Samply_Walk1.png differ diff --git a/samply/Samply_Walk2.png b/samply/Samply_Walk2.png new file mode 100644 index 0000000..0fd6db0 Binary files /dev/null and b/samply/Samply_Walk2.png differ diff --git a/samply/readme b/samply/readme new file mode 100644 index 0000000..4761668 --- /dev/null +++ b/samply/readme @@ -0,0 +1,3 @@ +theyre very hasty + +h/t zyre diff --git a/slutpet.c b/slutpet.c index 837f23a..69ae2b2 100644 --- a/slutpet.c +++ b/slutpet.c @@ -13,7 +13,8 @@ SDL_AppQuit(void *as, SDL_AppResult res) st = as; if (st) { - free_image(st->test); + babble("put samply to bed!!"); + put_samply_to_bed(st->sam); babble("free state..."); SDL_free(st); } @@ -50,7 +51,10 @@ SDL_AppIterate(void *as) SDL_SetRenderDrawColor(st->ren, 0, 0, 0, SDL_ALPHA_TRANSPARENT); SDL_RenderClear(st->ren); - SDL_RenderTexture(st->ren, st->test->tex, NULL, NULL); + if (SDL_RenderTexture(st->ren, st->sam->idle->tex, NULL, NULL) == false) { + fuck("rendertexture fail: %s", SDL_GetError()); + return SDL_APP_FAILURE; + } SDL_RenderPresent(st->ren); return SDL_APP_CONTINUE; } @@ -86,10 +90,10 @@ SDL_AppInit(void **as, int argc, char **argv) return SDL_APP_FAILURE; } - babble("create test image..."); - st->test = load_image(st->ren, "test.png"); - if (!st->test) { - fuck("img creation fail: %s", SDL_GetError()); + babble("create samply!!..."); + st->sam = wake_samply_up(st->ren); + if (!st->sam) { + fuck("samply said no. too bad!"); return SDL_APP_FAILURE; }