Compare commits

..

2 commits

Author SHA1 Message Date
cat
51d85c011b image display test
bring your own test.png
2025-05-25 14:40:05 +10:00
cat
380f715903 +stb_image, rm i think unnecessary part of quit 2025-05-25 03:29:04 +10:00
5 changed files with 8061 additions and 7 deletions

View file

@ -2,7 +2,7 @@
PREFIX ?= /usr/local
CFLAGS := $(CFLAGS) `pkg-config --cflags --libs sdl3`
LDFLAGS := $(LDFLAGS)
LDFLAGS := $(LDFLAGS) -lm
all: slutpet

11
dat.h
View file

@ -1,5 +1,16 @@
typedef struct Image Image;
struct Image {
int w;
int h;
int bpp;
unsigned char *data;
SDL_Texture *tex;
};
typedef struct State State;
struct State {
SDL_Window *win;
SDL_Renderer *ren;
Image *test;
};

4
readme
View file

@ -1 +1,5 @@
horny desktop pet thing.
i think it's probably best to use some sort of extension language.
i want people to be able to create their own pets without having to
figure out how to recompile.

View file

@ -1,6 +1,8 @@
#define SDL_MAIN_USE_CALLBACKS
#define STB_IMAGE_IMPLEMENTATION
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include "stb_image.h"
#include "dat.h"
@ -11,12 +13,20 @@ SDL_AppQuit(void *as, SDL_AppResult res)
st = as;
if (st) {
SDL_Log("destroy renderer...");
SDL_DestroyRenderer(st->ren);
SDL_Log("destroy window...");
SDL_DestroyWindow(st->win);
SDL_Log("free state...");
SDL_free(st);
if (st->test) {
if (st->test->tex) {
SDL_Log("free test texture...");
SDL_DestroyTexture(st->test->tex);
}
if (st->test->data) {
SDL_Log("free test img data...");
SDL_free(st->test->data);
}
SDL_Log("free test img...");
SDL_free(st->test);
}
SDL_Log("free state...");
SDL_free(st);
}
SDL_Log("bye!");
@ -39,6 +49,13 @@ SDL_AppEvent(void *as, SDL_Event *ev)
SDL_AppResult
SDL_AppIterate(void *as)
{
State *st = as;
SDL_SetRenderDrawColor(st->ren, 0, 0, 0, SDL_ALPHA_TRANSPARENT);
SDL_RenderClear(st->ren);
SDL_RenderTexture(st->ren, st->test->tex, NULL, NULL);
SDL_RenderPresent(st->ren);
return SDL_APP_CONTINUE;
}
@ -46,6 +63,7 @@ SDL_AppResult
SDL_AppInit(void **as, int argc, char **argv)
{
State *st;
SDL_Surface *sur;
SDL_Log("init sdl...");
if (!SDL_Init(SDL_INIT_VIDEO)) {
@ -72,6 +90,39 @@ SDL_AppInit(void **as, int argc, char **argv)
return SDL_APP_FAILURE;
}
SDL_Log("create test image...");
st->test = SDL_calloc(1, sizeof(Image));
if (!st->test) {
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION,
"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) {
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION,
"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) {
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION,
"surface creation fail: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
st->test->tex = SDL_CreateTextureFromSurface(st->ren, sur);
if (!st->test->tex) {
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION,
"surface creation fail: %s", SDL_GetError());
SDL_DestroySurface(sur);
return SDL_APP_FAILURE;
}
SDL_Log("it work");
return SDL_APP_CONTINUE;
}

7988
stb_image.h Normal file

File diff suppressed because it is too large Load diff