128 lines
2.8 KiB
C
128 lines
2.8 KiB
C
#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"
|
|
|
|
void
|
|
SDL_AppQuit(void *as, SDL_AppResult res)
|
|
{
|
|
State *st;
|
|
|
|
st = as;
|
|
if (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!");
|
|
return;
|
|
}
|
|
|
|
SDL_AppResult
|
|
SDL_AppEvent(void *as, SDL_Event *ev)
|
|
{
|
|
switch (ev->type) {
|
|
case SDL_EVENT_QUIT:
|
|
SDL_Log("quit requested");
|
|
return SDL_APP_SUCCESS;
|
|
break; /* unreachable; here for visual symmetry */
|
|
}
|
|
|
|
return SDL_APP_CONTINUE;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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)) {
|
|
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION,
|
|
"sdl init fail: %s", SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
SDL_Log("alloc state...");
|
|
st = SDL_calloc(1, sizeof(State));
|
|
if (!st) {
|
|
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION,
|
|
"mem alloc fail: %s", SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
*as = st;
|
|
|
|
SDL_Log("create win+render...");
|
|
if (!SDL_CreateWindowAndRenderer("slutpet", 320, 240,
|
|
SDL_WINDOW_TRANSPARENT | SDL_WINDOW_BORDERLESS,
|
|
&st->win, &st->ren)) {
|
|
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION,
|
|
"win creation fail: %s", SDL_GetError());
|
|
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;
|
|
}
|