Add SDL 2 support to tools/sdl_image_test/showimage.c

This commit is contained in:
Thomas Bernard 2018-09-21 21:49:58 +02:00
parent 18d35f5bf0
commit c8d2851d63
2 changed files with 56 additions and 7 deletions

View File

@ -1,18 +1,35 @@
CFLAGS = -Wall -O -g
CFLAGS += $(shell sdl-config --cflags)
ifeq ($(API),sdl2)
# SDL 2.x
SDLCONFIG = sdl2-config
SDLIMAGE = SDL2_image
BIN = showimage-sdl2
CFLAGS += -DUSE_SDL2
else
# SDL 1.2.x
SDLCONFIG = sdl-config
SDLIMAGE = SDL_image
BIN = showimage
CFLAGS += -DUSE_SDL
endif
LDLIBS = $(shell sdl-config --libs)
LDLIBS += $(shell pkg-config SDL_image --libs-only-l)
CFLAGS += $(shell $(SDLCONFIG) --cflags)
LDFLAGS = $(shell pkg-config SDL_image --libs-only-L)
LDLIBS = $(shell $(SDLCONFIG) --libs)
LDLIBS += $(shell pkg-config $(SDLIMAGE) --libs-only-l)
LDFLAGS = $(shell pkg-config $(SDLIMAGE) --libs-only-L)
.PHONY: all clean
all: showimage
all: $(BIN)
clean:
$(RM) showimage *.o
$(RM) $(BIN) *.o
showimage: showimage.o
$(BIN): $(BIN).o
$(BIN).o: showimage.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

View File

@ -23,7 +23,12 @@
#include <SDL.h>
#include <SDL_image.h>
#if defined(USE_SDL)
SDL_Surface * screen = NULL;
#elif defined(USE_SDL2)
SDL_Window * window = NULL;
SDL_Renderer * renderer = NULL;
#endif
int Show_IMG(const char * filename)
{
@ -35,19 +40,38 @@ int Show_IMG(const char * filename)
fprintf(stderr, "Cannot load image \"%s\"\n", filename);
return -1;
}
#if defined(USE_SDL)
if(screen == NULL) {
screen = SDL_SetVideoMode(image->w, image->h, image->format->BitsPerPixel, 0);
}
#elif defined(USE_SDL2)
if(window == NULL) {
window = SDL_CreateWindow(filename, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
image->w, image->h, 0);
renderer = SDL_CreateRenderer(window, -1, 0);
}
#endif
dest.x = 0;
dest.y = 0;
dest.w = image->w;
dest.h = image->h;
#if defined(USE_SDL)
if(screen != NULL) {
SDL_BlitSurface(image, NULL, screen, &dest);
SDL_UpdateRects(screen, 1, &dest);
} else {
fprintf(stderr, "no SDL screen open\n");
}
#elif defined(USE_SDL2)
if(renderer != NULL) {
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, image);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_DestroyTexture(texture);
} else {
fprintf(stderr, "no SDL2 renderer\n");
}
#endif
SDL_FreeSurface(image);
return 0;
}
@ -87,10 +111,18 @@ int main(int argc, char * * argv)
}
atexit(SDL_Quit);
#ifdef USE_SDL
SDL_WM_SetCaption(filename, filename); /* window caption, icon caption */
#endif
if(Show_IMG(filename) < 0)
return 1;
Manage_Events();
#if defined(USE_SDL2)
if(renderer != NULL)
SDL_DestroyRenderer(renderer);
if(window != NULL)
SDL_DestroyWindow(window);
#endif
return 0;
}