diff --git a/src/platform/sdl/CMakeLists.txt b/src/platform/sdl/CMakeLists.txt index 7745464d6..2136addd2 100644 --- a/src/platform/sdl/CMakeLists.txt +++ b/src/platform/sdl/CMakeLists.txt @@ -1,4 +1,19 @@ -find_package(SDL 1.2 REQUIRED) +set(SDL_VERSION "2" CACHE STRING "Version of SDL to use (1.2 or 2)") + +if (SDL_VERSION EQUAL "2") + include(FindPkgConfig) + pkg_search_module(SDL2 sdl2) + if (SDL2_FOUND) + set(SDL_INCLUDE_DIR ${SDL2_INCLUDE_DIRS}) + set(SDL_LIBRARY ${SDL2_LIBRARIES}) + set(SDLMAIN_LIBRARY "") + endif() +endif() + +if(SDL_VERSION EQUAL "1.2" OR NOT SDL2_FOUND) + find_package(SDL 1.2 REQUIRED) +endif() + file(GLOB PLATFORM_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/sdl-*.c) set(PLATFORM_LIBRARY "${SDL_LIBRARY};${SDLMAIN_LIBRARY}") include_directories(${CMAKE_SOURCE_DIR}/src/platform/sdl) diff --git a/src/platform/sdl/gl-main.c b/src/platform/sdl/gl-main.c index 56039614f..813486b5c 100644 --- a/src/platform/sdl/gl-main.c +++ b/src/platform/sdl/gl-main.c @@ -22,6 +22,9 @@ struct GLSoftwareRenderer { struct GBAVideoSoftwareRenderer d; struct GBASDLAudio audio; struct GBASDLEvents events; +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_Window* window; +#endif int viewportWidth; int viewportHeight; @@ -105,15 +108,25 @@ static int _GBASDLInit(struct GLSoftwareRenderer* renderer) { GBASDLInitEvents(&renderer->events); GBASDLInitAudio(&renderer->audio); +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); +#else SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1); +#endif SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); +#if SDL_VERSION_ATLEAST(2, 0, 0) + renderer->window = SDL_CreateWindow("GBAc", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer->viewportWidth, renderer->viewportHeight, SDL_WINDOW_OPENGL); + SDL_GL_CreateContext(renderer->window); + SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight); +#else #ifdef COLOR_16_BIT SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_OPENGL); #else SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_OPENGL); +#endif #endif renderer->d.outputBuffer = malloc(256 * 256 * 4); @@ -158,7 +171,11 @@ static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* } } GBASyncWaitFrameEnd(&context->sync); +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_GL_SwapWindow(renderer->window); +#else SDL_GL_SwapBuffers(); +#endif while (SDL_PollEvent(&event)) { GBASDLHandleEvent(context, &event); @@ -171,6 +188,9 @@ static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer) { GBASDLDeinitEvents(&renderer->events); GBASDLDeinitAudio(&renderer->audio); +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_DestroyWindow(renderer->window); +#endif SDL_Quit(); } diff --git a/src/platform/sdl/sdl-events.c b/src/platform/sdl/sdl-events.c index 3161629d0..05ba606e1 100644 --- a/src/platform/sdl/sdl-events.c +++ b/src/platform/sdl/sdl-events.c @@ -11,7 +11,9 @@ int GBASDLInitEvents(struct GBASDLEvents* context) { } SDL_JoystickEventState(SDL_ENABLE); context->joystick = SDL_JoystickOpen(0); +#if !SDL_VERSION_ATLEAST(2, 0, 0) SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); +#endif return 1; }