image display test
bring your own test.png
This commit is contained in:
parent
380f715903
commit
51d85c011b
2 changed files with 72 additions and 0 deletions
11
dat.h
11
dat.h
|
@ -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;
|
typedef struct State State;
|
||||||
struct State {
|
struct State {
|
||||||
SDL_Window *win;
|
SDL_Window *win;
|
||||||
SDL_Renderer *ren;
|
SDL_Renderer *ren;
|
||||||
|
|
||||||
|
Image *test;
|
||||||
};
|
};
|
||||||
|
|
61
slutpet.c
61
slutpet.c
|
@ -9,6 +9,26 @@
|
||||||
void
|
void
|
||||||
SDL_AppQuit(void *as, SDL_AppResult res)
|
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!");
|
SDL_Log("bye!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -29,6 +49,13 @@ SDL_AppEvent(void *as, SDL_Event *ev)
|
||||||
SDL_AppResult
|
SDL_AppResult
|
||||||
SDL_AppIterate(void *as)
|
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;
|
return SDL_APP_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,6 +63,7 @@ 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;
|
||||||
|
|
||||||
SDL_Log("init sdl...");
|
SDL_Log("init sdl...");
|
||||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||||
|
@ -62,6 +90,39 @@ SDL_AppInit(void **as, int argc, char **argv)
|
||||||
return SDL_APP_FAILURE;
|
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");
|
SDL_Log("it work");
|
||||||
return SDL_APP_CONTINUE;
|
return SDL_APP_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue