diff --git a/build/autotools/m4/lol-sdl.m4 b/build/autotools/m4/lol-sdl.m4 index 42d8ca8d..f22e9bb1 100644 --- a/build/autotools/m4/lol-sdl.m4 +++ b/build/autotools/m4/lol-sdl.m4 @@ -20,26 +20,28 @@ dnl SDL_CFLAGS -- flags for SDL compilation dnl SDL_LIBS -- flags for SDL linking ac_cv_my_have_sdl="no" +ac_cv_my_have_sdl2="no" ac_cv_my_have_sdl_image="no" ac_cv_my_have_sdl_mixer="no" dnl First, try the proper pkg-config check for SDL2 -dnl if test "x${ac_cv_my_have_sdl}" = xno; then -dnl PKG_CHECK_MODULES(SDL2, sdl2, -dnl [ac_cv_my_have_sdl="yes" -dnl PKG_CHECK_MODULES(SDL2MIXER, SDL2_mixer, -dnl [ac_cv_my_have_sdl_mixer="yes" -dnl AC_DEFINE(HAVE_SDL_MIXER_H, 1, Define to 1 to use SDL_mixer.h)], -dnl [:]) -dnl PKG_CHECK_MODULES(SDL2IMAGE, SDL2_image, -dnl [ac_cv_my_have_sdl_image="yes" -dnl AC_DEFINE(HAVE_SDL_IMAGE_H, 1, Define to 1 to use SDL_image.h)], -dnl [:]) -dnl SDL_CFLAGS="${SDL2_CFLAGS} ${SDL2MIXER_CFLAGS} ${SDL2IMAGE_CFLAGS}" -dnl SDL_LIBS="${SDL2_LIBS} ${SDL2MIXER_LIBS} ${SDL2IMAGE_LIBS}" -dnl AC_DEFINE(HAVE_SDL_H, 1, Define to 1 to use SDL.h)], -dnl [:]) -dnl fi +if test "x${ac_cv_my_have_sdl}" = xno; then + PKG_CHECK_MODULES(SDL2, sdl2, + [ac_cv_my_have_sdl="yes" + ac_cv_my_have_sdl2="yes" + PKG_CHECK_MODULES(SDL2MIXER, SDL2_mixer, + [ac_cv_my_have_sdl_mixer="yes" + AC_DEFINE(HAVE_SDL_MIXER_H, 1, Define to 1 to use SDL_mixer.h)], + [:]) + PKG_CHECK_MODULES(SDL2IMAGE, SDL2_image, + [ac_cv_my_have_sdl_image="yes" + AC_DEFINE(HAVE_SDL_IMAGE_H, 1, Define to 1 to use SDL_image.h)], + [:]) + SDL_CFLAGS="${SDL2_CFLAGS} ${SDL2MIXER_CFLAGS} ${SDL2IMAGE_CFLAGS}" + SDL_LIBS="${SDL2_LIBS} ${SDL2MIXER_LIBS} ${SDL2IMAGE_LIBS}" + AC_DEFINE(HAVE_SDL_H, 1, Define to 1 to use SDL.h)], + [:]) +fi dnl Then, try the proper pkg-config check for SDL 1.x if test "x${ac_cv_my_have_sdl}" = xno; then @@ -105,6 +107,11 @@ else fi AM_CONDITIONAL(USE_SDL, test "x${ac_cv_my_have_sdl}" = xyes) +if test "x${ac_cv_my_have_sdl2}" != xno; then + AC_DEFINE(USE_SDL2, 1, Define to 1 to use SDL) +fi +AM_CONDITIONAL(USE_SDL2, test "x${ac_cv_my_have_sdl2}" = xyes) + if test "x${ac_cv_my_have_sdl_mixer}" = xno; then AC_MSG_WARN([SDL_mixer not found]) else diff --git a/src/platform/sdl/sdlapp.cpp b/src/platform/sdl/sdlapp.cpp index 6cfc5aa4..db9d3b1f 100644 --- a/src/platform/sdl/sdlapp.cpp +++ b/src/platform/sdl/sdlapp.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright: (c) 2010-2011 Sam Hocevar +// Copyright: (c) 2010-2014 Sam Hocevar // This program is free software; you can redistribute it and/or // modify it under the terms of the Do What The Fuck You Want To // Public License, Version 2, as published by Sam Hocevar. See @@ -10,13 +10,13 @@ #include -#if defined USE_SDL -# if defined HAVE_SDL_SDL_H +#if USE_SDL +# if HAVE_SDL_SDL_H # include # else # include # endif -# if defined USE_D3D9 +# if USE_D3D9 # include # include # endif @@ -25,11 +25,11 @@ #include "lolgl.h" #include "platform/sdl/sdlapp.h" #include "platform/sdl/sdlinput.h" -#if defined USE_XINPUT +#if USE_XINPUT # include "platform/d3d9/d3d9input.h" #endif -#if defined USE_SDL && defined USE_D3D9 +#if USE_SDL && USE_D3D9 HWND g_hwnd = nullptr; #endif @@ -45,7 +45,12 @@ class SdlAppData friend class SdlApp; private: - int unused; +#if USE_SDL2 + SDL_Window *m_window; + SDL_GLContext m_glcontext; +#else + SDL_Surface *m_window; +#endif }; /* @@ -55,55 +60,77 @@ private: SdlApp::SdlApp(char const *title, ivec2 res, float fps) : data(new SdlAppData()) { -#if defined USE_SDL +#if USE_SDL + ivec2 window_size = res; + ivec2 screen_size = res; + /* Initialise SDL */ - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE) < 0) { Log::Error("cannot initialise SDL: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } +#if USE_SDL2 + data->m_window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + window_size.x, window_size.y, + SDL_WINDOW_OPENGL); + if (!data->m_window) + { + Log::Error("cannot create rendering window: %s\n", SDL_GetError()); + SDL_Quit(); + exit(EXIT_FAILURE); + } + SDL_GetWindowSize(data->m_window, &res.x, &res.y); + + data->m_glcontext = SDL_GL_CreateContext(data->m_window); + +#else const SDL_VideoInfo* vidinfo = SDL_GetVideoInfo(); - int screen_w = vidinfo->current_w; - int screen_h = vidinfo->current_h; + screen_size = ivec2(vidinfo->current_w, vidinfo->current_h); + + SDL_WM_SetCaption(title, nullptr); -# if defined USE_D3D9 +# if USE_D3D9 SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 16, 0); SDL_SysWMinfo wminfo; SDL_VERSION(&wminfo.version); SDL_GetWMInfo(&wminfo); g_hwnd = wminfo.window; -# elif SDL_VERSION_ATLEAST(2,0,0) - TODO # else SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); - // TODO: when implementing fullscreen, be sure to overwrite screen_w and screen_h with the value of vidinfo after the call to SDL_SetVideoMode - SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL); + // TODO: when implementing fullscreen, be sure to overwrite screen_w + // and screen_h with the value of vidinfo after the call to + // SDL_SetVideoMode. + data->m_window = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL); # endif - if (!video) + + if (!data->m_window) { Log::Error("cannot create rendering window: %s\n", SDL_GetError()); SDL_Quit(); exit(EXIT_FAILURE); } - SDL_WM_SetCaption(title, nullptr); + res = ivec2(data->m_window->w, data->m_window->h); +#endif /* Initialise everything */ Ticker::Setup(fps); - Video::Setup(ivec2(video->w, video->h)); + Video::Setup(res); Audio::Setup(2); /* Autoreleased objects */ -# if defined USE_XINPUT +#if defined USE_XINPUT /* Prefer D3d9 for joysticks on Windows, because the X360 pads are not * advertised with the proper number of axes. */ new D3d9Input(); -# endif +#endif - new SdlInput(video->w, video->h, screen_w, screen_h); + new SdlInput(res.x, res.y, screen_size.x, screen_size.y); #endif } @@ -127,23 +154,36 @@ void SdlApp::Tick() /* Tick the renderer, show the frame and clamp to desired framerate. */ Ticker::TickDraw(); -#if defined USE_SDL && defined USE_D3D9 +#if USE_SDL2 + SDL_GL_SwapWindow(data->m_window); +#elif USE_SDL && USE_D3D9 hr = d3d_dev->EndScene(); if (FAILED(hr)) Abort(); hr = d3d_dev->Present(nullptr, nullptr, nullptr, nullptr); if (FAILED(hr)) Abort(); -#elif defined USE_SDL +#elif USE_SDL SDL_GL_SwapBuffers(); #endif } SdlApp::~SdlApp() { -#if defined USE_SDL +#if USE_SDL + if (data->m_window) + { +# if USE_SDL2 + SDL_GL_DeleteContext(data->m_glcontext); + SDL_DestroyWindow(data->m_window); +# else + SDL_DestroySurface(data->m_window); +# endif + } + SDL_Quit(); #endif + delete data; } diff --git a/src/platform/sdl/sdlinput.cpp b/src/platform/sdl/sdlinput.cpp index 42b00eaa..c6ddc21f 100644 --- a/src/platform/sdl/sdlinput.cpp +++ b/src/platform/sdl/sdlinput.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright: (c) 2010-2013 Sam Hocevar +// Copyright: (c) 2010-2014 Sam Hocevar // This program is free software; you can redistribute it and/or // modify it under the terms of the Do What The Fuck You Want To // Public License, Version 2, as published by Sam Hocevar. See @@ -76,10 +76,12 @@ SdlInput::SdlInput(int app_w, int app_h, int screen_w, int screen_h) : m_data(new SdlInputData(app_w, app_h, screen_w, screen_h)) #endif //USE_SDL { -#if USE_SDL +#if USE_SDL && !USE_SDL2 /* Enable Unicode translation of keyboard events */ SDL_EnableUNICODE(1); +#endif +#if USE_SDL SDL_Init(SDL_INIT_TIMER | SDL_INIT_JOYSTICK); m_data->m_keyboard = InputDeviceInternal::CreateStandardKeyboard(); @@ -102,7 +104,11 @@ SdlInput::SdlInput(int app_w, int app_h, int screen_w, int screen_h) * - HDAPS, it's not a real joystick. * - X360 controllers, Xinput handles them better since * it won't think there is only one trigger axis. */ +# if USE_SDL2 + char const *name = SDL_JoystickName(sdlstick); +# else char const *name = SDL_JoystickName(i); +# endif if (strstr(name, "HDAPS") # if USE_XINPUT || strstr(name, "XBOX 360 For Windows") @@ -196,10 +202,12 @@ void SdlInputData::Tick(float seconds) case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: { +# if !USE_SDL2 if (event.button.button != SDL_BUTTON_WHEELUP && event.button.button != SDL_BUTTON_WHEELDOWN) m_mouse->SetKey(event.button.button - 1, event.type == SDL_MOUSEBUTTONDOWN); else m_mouse->SetAxis(4, (event.button.button != SDL_BUTTON_WHEELUP) ? (1) : (-1)); +# endif // TODO: mouse wheel as axis break; } @@ -222,7 +230,11 @@ void SdlInputData::Tick(float seconds) if (InputDeviceInternal::GetMouseCapture() != m_mousecapture) { m_mousecapture = InputDeviceInternal::GetMouseCapture(); +# if USE_SDL2 + SDL_SetRelativeMouseMode(m_mousecapture ? SDL_TRUE : SDL_FALSE); +# else SDL_WM_GrabInput(m_mousecapture ? SDL_GRAB_ON : SDL_GRAB_OFF); +# endif mouse = (ivec2)m_app / 2; SdlInputData::SetMousePos(mouse); //SDL_ShowCursor(m_mousecapture ? SDL_DISABLE : SDL_ENABLE); @@ -246,9 +258,11 @@ void SdlInputData::Tick(float seconds) //Mouse is focused, Validate the InScreen Key //Hardcoded 3, not very nice. -# if !EMSCRIPTEN +# if !EMSCRIPTEN && !USE_SDL2 m_mouse->SetKey(3, !!(SDL_GetAppState() & SDL_APPMOUSEFOCUS)); -#else //Emscripten doesn't seem to handle SDL_APPMOUSEFOCUS +#else + // Emscripten doesn't seem to handle SDL_APPMOUSEFOCUS + // SDL2 doesn't have SDL_APPMOUSEFOCUS either m_mouse->SetKey(3, true); #endif @@ -260,8 +274,8 @@ void SdlInputData::Tick(float seconds) m_prevmouse = mouse; -# if SDL_VERSION_ATLEAST(1,3,0) - Uint8 *sdlstate = SDL_GetKeyboardState(nullptr); +# if USE_SDL2 + Uint8 const *sdlstate = SDL_GetKeyboardState(nullptr); # else Uint8 *sdlstate = SDL_GetKeyState(nullptr); # endif @@ -285,7 +299,7 @@ ivec2 SdlInputData::GetMousePos() ivec2 ret(-1, -1); #if USE_SDL -# if !EMSCRIPTEN +# if !EMSCRIPTEN && !USE_SDL2 if (SDL_GetAppState() & SDL_APPMOUSEFOCUS) # endif { @@ -298,7 +312,9 @@ ivec2 SdlInputData::GetMousePos() void SdlInputData::SetMousePos(ivec2 position) { -#if USE_SDL +#if USE_SDL2 + // FIXME: how do I warped mouse? +#elif USE_SDL SDL_WarpMouse((uint16_t)position.x, (uint16_t)position.y); #else UNUSED(position);