]> git.lizzy.rs Git - irrlicht.git/commitdiff
Update CIrrDeviceSDL to support SDL2 (#85)
authorEmmanuel Hansen <emmausssss@gmail.com>
Sun, 16 Jan 2022 11:16:15 +0000 (11:16 +0000)
committerGitHub <noreply@github.com>
Sun, 16 Jan 2022 11:16:15 +0000 (12:16 +0100)
include/IrrCompileConfig.h
source/Irrlicht/CIrrDeviceSDL.cpp
source/Irrlicht/CIrrDeviceSDL.h
source/Irrlicht/CMakeLists.txt
source/Irrlicht/COpenGLCommon.h
source/Irrlicht/COpenGLDriver.cpp
source/Irrlicht/os.cpp

index beeade34f6e5af53dc7fcd928d920c82d08db813..729a3d06fa06f9eec07780792e84236719bedfbf 100644 (file)
 #if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64)\r
 #define _IRR_WINDOWS_\r
 #define _IRR_WINDOWS_API_\r
+#ifndef _IRR_COMPILE_WITH_SDL_DEVICE_\r
 #define _IRR_COMPILE_WITH_WINDOWS_DEVICE_\r
 #endif\r
+#endif\r
 \r
 #if defined(_MSC_VER) && (_MSC_VER < 1500)\r
 #  error "Only Microsoft Visual Studio 9.0 and later are supported."\r
 #endif\r
 \r
-// XBox is deprecated (as DX8 is removed). Use Irrlicht 1.8 if you still want to work on this.\r
-#if defined(_XBOX)\r
-       #undef _IRR_WINDOWS_\r
-       #define _IRR_XBOX_PLATFORM_     // deprecated\r
-       #define _IRR_WINDOWS_API_\r
-       //#define _IRR_COMPILE_WITH_WINDOWS_DEVICE_\r
-       #undef _IRR_COMPILE_WITH_WINDOWS_DEVICE_\r
-       //#define _IRR_COMPILE_WITH_SDL_DEVICE_\r
-\r
-       #include <xtl.h>\r
-#endif\r
-\r
 #if defined(__APPLE__) || defined(MACOSX)\r
 #if !defined(MACOSX)\r
 #define MACOSX // legacy support\r
@@ -98,7 +88,9 @@
 #define _IRR_COMPILE_WITH_IOS_BUILTIN_MAIN_\r
 #else\r
 #define _IRR_OSX_PLATFORM_\r
+#ifndef _IRR_COMPILE_WITH_SDL_DEVICE_\r
 #define _IRR_COMPILE_WITH_OSX_DEVICE_\r
+#endif\r
 #define NO_IRR_COMPILE_WITH_OGLES1_\r
 #define NO_IRR_COMPILE_WITH_OGLES2_\r
 #define NO_IRR_COMPILE_WITH_WEBGL1_\r
 #define _IRR_LINUX_PLATFORM_\r
 #endif\r
 #define _IRR_POSIX_API_\r
+#ifndef _IRR_COMPILE_WITH_SDL_DEVICE_\r
 #define _IRR_COMPILE_WITH_X11_DEVICE_\r
 #endif\r
+#endif\r
 \r
 \r
 //! Define _IRR_COMPILE_WITH_JOYSTICK_SUPPORT_ if you want joystick events.\r
index e3e7713eeef09c218740e17795ace2de3d48df58..68f50668d0cf0646075e9edd00da7b7038e7d3af 100644 (file)
@@ -17,8 +17,7 @@
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include "SIrrCreationParameters.h"\r
-#include <SDL/SDL_syswm.h>\r
-#include <SDL/SDL_video.h>\r
+#include <SDL2/SDL_video.h>\r
 \r
 #ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
 #ifdef _IRR_COMPILE_WITH_OGLES2_\r
 #include <emscripten.h>\r
 #endif\r
 \r
-#ifdef _MSC_VER\r
-#pragma comment(lib, "SDL.lib")\r
-#endif // _MSC_VER\r
-\r
 static int SDLDeviceInstances = 0;\r
 \r
 namespace irr\r
@@ -111,6 +106,7 @@ EM_BOOL CIrrDeviceSDL::MouseLeaveCallback(int eventType, const EmscriptenMouseEv
 \r
        return EM_FALSE;\r
 }\r
+#endif\r
 \r
 bool CIrrDeviceSDL::isNoUnicodeKey(EKEY_CODE key) const\r
 {\r
@@ -179,12 +175,11 @@ bool CIrrDeviceSDL::isNoUnicodeKey(EKEY_CODE key) const
                return false;\r
        }\r
 }\r
-#endif\r
 \r
 //! constructor\r
 CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)\r
        : CIrrDeviceStub(param),\r
-       Screen((SDL_Surface*)param.WindowId), SDL_Flags(SDL_ANYFORMAT),\r
+       Window((SDL_Window*)param.WindowId), SDL_Flags(0),\r
        MouseX(0), MouseY(0), MouseXRel(0), MouseYRel(0), MouseButtonStates(0),\r
        Width(param.WindowSize.Width), Height(param.WindowSize.Height),\r
        Resizable(param.WindowResizable == 1 ? true : false), WindowMinimized(false)\r
@@ -210,22 +205,36 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
                {\r
                        os::Printer::log("SDL initialized", ELL_INFORMATION);\r
                }\r
+       }\r
 \r
-#if defined(_IRR_WINDOWS_)\r
-               SDL_putenv("SDL_VIDEODRIVER=directx");\r
-#elif defined(_IRR_OSX_PLATFORM_)\r
-               SDL_putenv("SDL_VIDEODRIVER=Quartz");\r
-#elif !defined(_IRR_EMSCRIPTEN_PLATFORM_)\r
-               SDL_putenv("SDL_VIDEODRIVER=x11");\r
-#endif\r
+       // create keymap\r
+       createKeyMap();\r
+\r
+       if ( CreationParams.Fullscreen )\r
+               SDL_Flags |= SDL_WINDOW_FULLSCREEN;\r
+       else if ( Resizable )\r
+               SDL_Flags |= SDL_WINDOW_RESIZABLE;\r
+       if (CreationParams.DriverType == video::EDT_OPENGL)\r
+       {\r
+               SDL_Flags |= SDL_WINDOW_OPENGL;\r
+               if (!CreationParams.Doublebuffer)\r
+                       SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);\r
        }\r
+#ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
+       SDL_Flags |= SDL_WINDOW_OPENGL;\r
+#endif //_IRR_EMSCRIPTEN_PLATFORM_\r
 \r
-//     SDL_putenv("SDL_WINDOWID=");\r
+       // create window\r
+       if (CreationParams.DriverType != video::EDT_NULL)\r
+       {\r
+               // create the window, only if we do not use the null device\r
+               createWindow();\r
+       }\r
 \r
        SDL_VERSION(&Info.version);\r
 \r
 #ifndef _IRR_EMSCRIPTEN_PLATFORM_\r
-       SDL_GetWMInfo(&Info);\r
+       SDL_GetWindowWMInfo(Window,&Info);\r
 #endif //_IRR_EMSCRIPTEN_PLATFORM_\r
        core::stringc sdlversion = "SDL Version ";\r
        sdlversion += Info.version.major;\r
@@ -235,45 +244,20 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
        sdlversion += Info.version.patch;\r
 \r
        Operator = new COSOperator(sdlversion);\r
-       if ( SDLDeviceInstances == 1 )\r
-       {\r
+       if (SDLDeviceInstances == 1) {\r
                os::Printer::log(sdlversion.c_str(), ELL_INFORMATION);\r
        }\r
 \r
-       // create keymap\r
-       createKeyMap();\r
-       // enable key to character translation\r
-       SDL_EnableUNICODE(1);\r
-\r
-       (void)SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);\r
-\r
-       if ( CreationParams.Fullscreen )\r
-               SDL_Flags |= SDL_FULLSCREEN;\r
-       else if ( Resizable )\r
-               SDL_Flags |= SDL_RESIZABLE;\r
-       if (CreationParams.DriverType == video::EDT_OPENGL)\r
-               SDL_Flags |= SDL_OPENGL;\r
-       else if (CreationParams.Doublebuffer)\r
-               SDL_Flags |= SDL_DOUBLEBUF;\r
-#ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
-       SDL_Flags |= SDL_OPENGL;\r
-#endif //_IRR_EMSCRIPTEN_PLATFORM_\r
-\r
-       // create window\r
-       if (CreationParams.DriverType != video::EDT_NULL)\r
-       {\r
-               // create the window, only if we do not use the null device\r
-               createWindow();\r
-       }\r
-\r
        // create cursor control\r
        CursorControl = new CCursorControl(this);\r
 \r
        // create driver\r
        createDriver();\r
 \r
-       if (VideoDriver)\r
+       if (VideoDriver) {\r
                createGUIAndScene();\r
+               VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));\r
+       }\r
 }\r
 \r
 \r
@@ -287,6 +271,12 @@ CIrrDeviceSDL::~CIrrDeviceSDL()
                for (u32 i=0; i<numJoysticks; ++i)\r
                        SDL_JoystickClose(Joysticks[i]);\r
 #endif\r
+               if (Window)\r
+               {\r
+                       SDL_GL_MakeCurrent(Window, NULL);\r
+                       SDL_GL_DeleteContext(Context);\r
+                       SDL_DestroyWindow(Window);\r
+               }\r
                SDL_Quit();\r
 \r
                os::Printer::log("Quit SDL", ELL_INFORMATION);\r
@@ -351,9 +341,9 @@ bool CIrrDeviceSDL::createWindow()
        {\r
                SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);\r
                SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);\r
-       }\r
+       }       \r
 \r
-       Screen = SDL_SetVideoMode( 0, 0, 32, SDL_Flags); // 0,0 will use the canvas size\r
+       SDL_CreateWindowAndRenderer(0, 0, SDL_Flags, &Window, &Renderer); // 0,0 will use the canvas size\r
 \r
        logAttributes();\r
 \r
@@ -368,66 +358,63 @@ bool CIrrDeviceSDL::createWindow()
        if ( Close )\r
                return false;\r
 \r
-       if (CreationParams.DriverType == video::EDT_OPENGL)\r
-       {\r
-               if (CreationParams.Bits==16)\r
-               {\r
-                       SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 4 );\r
-                       SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 4 );\r
-                       SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 4 );\r
-                       SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, CreationParams.WithAlphaChannel?1:0 );\r
-               }\r
-               else\r
-               {\r
-                       SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );\r
-                       SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );\r
-                       SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );\r
-                       SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, CreationParams.WithAlphaChannel?8:0 );\r
+       if (CreationParams.DriverType == video::EDT_OPENGL) {\r
+               if (CreationParams.Bits == 16) {\r
+                       SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 4);\r
+                       SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 4);\r
+                       SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 4);\r
+                       SDL_GL_SetAttribute(\r
+                                       SDL_GL_ALPHA_SIZE, CreationParams.WithAlphaChannel ? 1 : 0);\r
+               } else {\r
+                       SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);\r
+                       SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);\r
+                       SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);\r
+                       SDL_GL_SetAttribute(\r
+                                       SDL_GL_ALPHA_SIZE, CreationParams.WithAlphaChannel ? 8 : 0);\r
                }\r
-               SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, CreationParams.ZBufferBits);\r
+               SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, CreationParams.ZBufferBits);\r
                if (CreationParams.Doublebuffer)\r
-                       SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );\r
-               SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, CreationParams.Stencilbuffer ? 8 : 0);\r
+                       SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);\r
+               SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, CreationParams.Stencilbuffer ? 8 : 0);\r
                if (CreationParams.Stereobuffer)\r
-                       SDL_GL_SetAttribute( SDL_GL_STEREO, 1 );\r
-               if (CreationParams.AntiAlias>1)\r
-               {\r
-                       SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );\r
-                       SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, CreationParams.AntiAlias );\r
+                       SDL_GL_SetAttribute(SDL_GL_STEREO, 1);\r
+               if (CreationParams.AntiAlias > 1) {\r
+                       SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);\r
+                       SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, CreationParams.AntiAlias);\r
                }\r
-               if ( !Screen )\r
-                       Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );\r
-               if ( !Screen && CreationParams.AntiAlias>1)\r
-               {\r
-                       while (--CreationParams.AntiAlias>1)\r
-                       {\r
-                               SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, CreationParams.AntiAlias );\r
-                               Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );\r
-                               if (Screen)\r
+               if (!Window)\r
+                       Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);\r
+               if (!Window && CreationParams.AntiAlias > 1) {\r
+                       while (--CreationParams.AntiAlias > 1) {\r
+                               SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, CreationParams.AntiAlias);\r
+                               Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);\r
+                               if (Window)\r
                                        break;\r
                        }\r
-                       if ( !Screen )\r
-                       {\r
-                               SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 0 );\r
-                               SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 0 );\r
-                               Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );\r
-                               if (Screen)\r
-                                       os::Printer::log("AntiAliasing disabled due to lack of support!" );\r
+                       if (!Window) {\r
+                               SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);\r
+                               SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);\r
+                               Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);\r
+                               if (Window)\r
+                                       os::Printer::log("AntiAliasing disabled due to lack of support!");\r
                        }\r
                }\r
-       }\r
-       else if ( !Screen )\r
-               Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );\r
 \r
-       if ( !Screen && CreationParams.Doublebuffer)\r
+               if (Window)\r
+               {\r
+                       Context = SDL_GL_CreateContext(Window);\r
+               }\r
+       } else if (!Window)\r
+               Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);\r
+\r
+       if ( !Window && CreationParams.Doublebuffer)\r
        {\r
                // Try single buffer\r
                if (CreationParams.DriverType == video::EDT_OPENGL)\r
-                       SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );\r
-               SDL_Flags &= ~SDL_DOUBLEBUF;\r
-               Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );\r
+                       SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);\r
+               Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);\r
        }\r
-       if ( !Screen )\r
+       if ( !Window )\r
        {\r
                os::Printer::log( "Could not initialize display!" );\r
                return false;\r
@@ -523,9 +510,9 @@ void CIrrDeviceSDL::createDriver()
        if ( VideoDriver && CreationParams.WindowSize.Width == 0 && CreationParams.WindowSize.Height == 0 && Width > 0 && Height > 0 )\r
        {\r
 #ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
-               Screen = SDL_SetVideoMode( Width, Height, 32, SDL_Flags );\r
+               SDL_CreateWindowAndRenderer(Width, Height, SDL_Flags, &Window, &Renderer);\r
 #else //_IRR_EMSCRIPTEN_PLATFORM_\r
-               Screen = SDL_SetVideoMode( Width, Height, 0, SDL_Flags );\r
+               Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);\r
 #endif //_IRR_EMSCRIPTEN_PLATFOR\r
                VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));\r
        }\r
@@ -557,10 +544,14 @@ bool CIrrDeviceSDL::run()
 \r
                        postEventFromUser(irrevent);\r
                        break;\r
-\r
+               case SDL_MOUSEWHEEL:\r
+                       irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;\r
+                       irrevent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;\r
+                       irrevent.MouseInput.Wheel = static_cast<float>(SDL_event.wheel.y);\r
+                       postEventFromUser(irrevent);\r
+                       break;\r
                case SDL_MOUSEBUTTONDOWN:\r
                case SDL_MOUSEBUTTONUP:\r
-\r
                        irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;\r
                        irrevent.MouseInput.X = SDL_event.button.x;\r
                        irrevent.MouseInput.Y = SDL_event.button.y;\r
@@ -605,7 +596,7 @@ bool CIrrDeviceSDL::run()
                                else\r
                                {\r
                                        irrevent.MouseInput.Event = irr::EMIE_LMOUSE_LEFT_UP;\r
-                                       MouseButtonStates &= !irr::EMBSM_LEFT;\r
+                                       MouseButtonStates &= ~irr::EMBSM_LEFT;\r
                                }\r
                                break;\r
 \r
@@ -618,7 +609,7 @@ bool CIrrDeviceSDL::run()
                                else\r
                                {\r
                                        irrevent.MouseInput.Event = irr::EMIE_RMOUSE_LEFT_UP;\r
-                                       MouseButtonStates &= !irr::EMBSM_RIGHT;\r
+                                       MouseButtonStates &= ~irr::EMBSM_RIGHT;\r
                                }\r
                                break;\r
 \r
@@ -631,19 +622,9 @@ bool CIrrDeviceSDL::run()
                                else\r
                                {\r
                                        irrevent.MouseInput.Event = irr::EMIE_MMOUSE_LEFT_UP;\r
-                                       MouseButtonStates &= !irr::EMBSM_MIDDLE;\r
+                                       MouseButtonStates &= ~irr::EMBSM_MIDDLE;\r
                                }\r
                                break;\r
-\r
-                       case SDL_BUTTON_WHEELUP:\r
-                               irrevent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;\r
-                               irrevent.MouseInput.Wheel = 1.0f;\r
-                               break;\r
-\r
-                       case SDL_BUTTON_WHEELDOWN:\r
-                               irrevent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;\r
-                               irrevent.MouseInput.Wheel = -1.0f;\r
-                               break;\r
                        }\r
 \r
                        irrevent.MouseInput.ButtonStates = MouseButtonStates;\r
@@ -691,19 +672,25 @@ bool CIrrDeviceSDL::run()
                                }\r
 #endif\r
                                irrevent.EventType = irr::EET_KEY_INPUT_EVENT;\r
-                               irrevent.KeyInput.Char = SDL_event.key.keysym.unicode;\r
-                               irrevent.KeyInput.Key = key;\r
-#ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
-                               // On emscripten SDL does not (yet?) return 0 for invalid keysym.unicode's.\r
-                               // Instead it sets keysym.unicode to keysym.sym.\r
-                               // But we need to distinguish control keys from characters here as that info\r
-                               // is necessary in other places like the editbox.\r
-                               if ( isNoUnicodeKey(key) )\r
+\r
+                               if (isNoUnicodeKey(key))\r
                                        irrevent.KeyInput.Char = 0;\r
-#endif\r
+                               else\r
+                                       irrevent.KeyInput.Char = SDL_event.key.keysym.sym;\r
+\r
+                               irrevent.KeyInput.Key = key;\r
                                irrevent.KeyInput.PressedDown = (SDL_event.type == SDL_KEYDOWN);\r
                                irrevent.KeyInput.Shift = (SDL_event.key.keysym.mod & KMOD_SHIFT) != 0;\r
                                irrevent.KeyInput.Control = (SDL_event.key.keysym.mod & KMOD_CTRL ) != 0;\r
+\r
+                               // SDL2 no longer provides unicode character in the key event so we ensure the character is in the correct case\r
+                               // TODO: Unicode input doesn't work like this, should probably use SDL_TextInputEvent\r
+                               bool convertCharToUppercase =\r
+                                       irrevent.KeyInput.Shift != !!(SDL_event.key.keysym.mod & KMOD_CAPS);\r
+\r
+                               if (convertCharToUppercase)\r
+                                       irrevent.KeyInput.Char = irr::core::locale_upper(irrevent.KeyInput.Char);\r
+\r
                                postEventFromUser(irrevent);\r
                        }\r
                        break;\r
@@ -712,25 +699,25 @@ bool CIrrDeviceSDL::run()
                        Close = true;\r
                        break;\r
 \r
-               case SDL_ACTIVEEVENT:\r
-                       if (SDL_event.active.state == SDL_APPACTIVE)\r
-                               WindowMinimized = (SDL_event.active.gain!=1);\r
-                       break;\r
-\r
-               case SDL_VIDEORESIZE:\r
-                       if ((SDL_event.resize.w != (int)Width) || (SDL_event.resize.h != (int)Height))\r
+               case SDL_WINDOWEVENT:\r
+                       switch (SDL_event.window.event)\r
                        {\r
-                               Width = SDL_event.resize.w;\r
-                               Height = SDL_event.resize.h;\r
-#ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
-                               Screen = SDL_SetVideoMode( 0, 0, 32, SDL_Flags ); // 0,0 will use the canvas size\r
-#else //_IRR_EMSCRIPTEN_PLATFORM_\r
-                               Screen = SDL_SetVideoMode( Width, Height, 0, SDL_Flags );\r
-#endif //_IRR_EMSCRIPTEN_PLATFOR\r
-                               if (VideoDriver)\r
-                                       VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));\r
+                       case SDL_WINDOWEVENT_MAXIMIZED:\r
+                               WindowMinimized = true;\r
+                               break;\r
+                       case SDL_WINDOWEVENT_RESTORED:\r
+                               WindowMinimized = false;\r
+                               break;\r
+                       case SDL_WINDOWEVENT_RESIZED:\r
+                               if ((SDL_event.window.data1 != (int)Width) || (SDL_event.window.data2 != (int)Height))\r
+                               {\r
+                                       Width = SDL_event.window.data1;\r
+                                       Height = SDL_event.window.data2;\r
+                                       if (VideoDriver)\r
+                                               VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));\r
+                               }\r
+                               break;\r
                        }\r
-                       break;\r
 \r
                case SDL_USEREVENT:\r
                        irrevent.EventType = irr::EET_USER_EVENT;\r
@@ -843,13 +830,13 @@ bool CIrrDeviceSDL::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
        int joystick = 0;\r
        for (; joystick<numJoysticks; ++joystick)\r
        {\r
-               Joysticks.push_back(SDL_JoystickOpen(joystick));\r
+               Joysticks.push_back( SDL_JoystickOpen(joystick));\r
                SJoystickInfo info;\r
 \r
                info.Joystick = joystick;\r
                info.Axes = SDL_JoystickNumAxes(Joysticks[joystick]);\r
                info.Buttons = SDL_JoystickNumButtons(Joysticks[joystick]);\r
-               info.Name = SDL_JoystickName(joystick);\r
+               info.Name = SDL_JoystickName(Joysticks[joystick]);\r
                info.PovHat = (SDL_JoystickNumHats(Joysticks[joystick]) > 0)\r
                                                ? SJoystickInfo::POV_HAT_PRESENT : SJoystickInfo::POV_HAT_ABSENT;\r
 \r
@@ -872,6 +859,11 @@ bool CIrrDeviceSDL::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
        return false;\r
 }\r
 \r
+void CIrrDeviceSDL::SwapWindow()\r
+{\r
+       SDL_GL_SwapWindow(Window);\r
+}\r
+\r
 \r
 \r
 //! pause execution temporarily\r
@@ -898,8 +890,9 @@ void CIrrDeviceSDL::sleep(u32 timeMs, bool pauseTimer)
 //! sets the caption of the window\r
 void CIrrDeviceSDL::setWindowCaption(const wchar_t* text)\r
 {\r
-       core::stringc textc = text;\r
-       SDL_WM_SetCaption( textc.c_str( ), textc.c_str( ) );\r
+       core::stringc textc;\r
+       core::wStringToMultibyte(textc, text);\r
+       SDL_SetWindowTitle(Window, textc.c_str());\r
 }\r
 \r
 \r
@@ -915,7 +908,7 @@ bool CIrrDeviceSDL::present(video::IImage* surface, void* windowId, core::rect<s
                        surface->getRedMask(), surface->getGreenMask(), surface->getBlueMask(), surface->getAlphaMask());\r
        if (!sdlSurface)\r
                return false;\r
-       SDL_SetAlpha(sdlSurface, 0, 0);\r
+       SDL_SetSurfaceAlphaMod(sdlSurface, 0);\r
        SDL_SetColorKey(sdlSurface, 0, 0);\r
        sdlSurface->format->BitsPerPixel=surface->getBitsPerPixel();\r
        sdlSurface->format->BytesPerPixel=surface->getBytesPerPixel();\r
@@ -964,7 +957,7 @@ bool CIrrDeviceSDL::present(video::IImage* surface, void* windowId, core::rect<s
 \r
        SDL_Surface* scr = (SDL_Surface* )windowId;\r
        if (!scr)\r
-               scr = Screen;\r
+               scr = SDL_GetWindowSurface(Window);\r
        if (scr)\r
        {\r
                if (srcClip)\r
@@ -978,7 +971,7 @@ bool CIrrDeviceSDL::present(video::IImage* surface, void* windowId, core::rect<s
                }\r
                else\r
                        SDL_BlitSurface(sdlSurface, NULL, scr, NULL);\r
-               SDL_Flip(scr);\r
+               SDL_RenderPresent(SDL_GetRenderer(Window));\r
        }\r
 \r
        SDL_FreeSurface(sdlSurface);\r
@@ -994,53 +987,6 @@ void CIrrDeviceSDL::closeDevice()
 }\r
 \r
 \r
-//! \return Pointer to a list with all video modes supported\r
-video::IVideoModeList* CIrrDeviceSDL::getVideoModeList()\r
-{\r
-#ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
-       os::Printer::log("VideoModeList not available on the web." , ELL_WARNING);\r
-       return VideoModeList;\r
-#else // !_IRR_EMSCRIPTEN_PLATFORM_\r
-       if (!VideoModeList->getVideoModeCount())\r
-       {\r
-               // enumerate video modes.\r
-               const SDL_VideoInfo *vi = SDL_GetVideoInfo();\r
-\r
-               SDL_PixelFormat pixelFormat = *(vi->vfmt);\r
-\r
-               core::array<Uint8> checkBitsPerPixel;\r
-               checkBitsPerPixel.push_back(8);\r
-               checkBitsPerPixel.push_back(16);\r
-               checkBitsPerPixel.push_back(24);\r
-               checkBitsPerPixel.push_back(32);\r
-               if ( pixelFormat.BitsPerPixel > 32 )\r
-                       checkBitsPerPixel.push_back(pixelFormat.BitsPerPixel);\r
-\r
-               for ( u32 i=0; i<checkBitsPerPixel.size(); ++i)\r
-               {\r
-                       pixelFormat.BitsPerPixel = checkBitsPerPixel[i];\r
-                       SDL_Rect **modes = SDL_ListModes(&pixelFormat, SDL_Flags|SDL_FULLSCREEN);\r
-                       if (modes != 0)\r
-                       {\r
-                               if (modes == (SDL_Rect **)-1)\r
-                               {\r
-                                       core::stringc strLog("All modes available for bit-depth ");\r
-                                       strLog += core::stringc(pixelFormat.BitsPerPixel);\r
-                                       os::Printer::log(strLog.c_str());\r
-                               }\r
-                               else\r
-                               {\r
-                                       for (u32 i=0; modes[i]; ++i)\r
-                                               VideoModeList->addMode(core::dimension2d<u32>(modes[i]->w, modes[i]->h), vi->vfmt->BitsPerPixel);\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-\r
-       return VideoModeList;\r
-#endif // !_IRR_EMSCRIPTEN_PLATFORM_\r
-}\r
-\r
 //! Sets if the window should be resizable in windowed mode.\r
 void CIrrDeviceSDL::setResizable(bool resize)\r
 {\r
@@ -1048,24 +994,15 @@ void CIrrDeviceSDL::setResizable(bool resize)
        os::Printer::log("Resizable not available on the web." , ELL_WARNING);\r
        return;\r
 #else // !_IRR_EMSCRIPTEN_PLATFORM_\r
-       if (resize != Resizable)\r
-       {\r
-#if defined(_IRR_COMPILE_WITH_OPENGL_) && defined(_IRR_WINDOWS_)\r
-               if ( SDL_Flags & SDL_OPENGL )\r
-               {\r
-                       // For unknown reasons the hack with sharing resources which was added in Irrlicht 1.8.5 for this no longer works in 1.9\r
-                       // But at least we got a new WindowResizable flag since Irrlicht 1.9.\r
-                       os::Printer::log("setResizable not supported with this device/driver combination. Use SIrrCreationParameters.WindowResizable instead.", ELL_WARNING);\r
-                       return;\r
-               }\r
-#endif\r
-\r
+       if (resize != Resizable) { \r
                if (resize)\r
-                       SDL_Flags |= SDL_RESIZABLE;\r
+                       SDL_Flags |= SDL_WINDOW_RESIZABLE;\r
                else\r
-                       SDL_Flags &= ~SDL_RESIZABLE;\r
+                       SDL_Flags &= ~SDL_WINDOW_RESIZABLE;\r
 \r
-               Screen = SDL_SetVideoMode( 0, 0, 0, SDL_Flags );\r
+               if (Window) {\r
+                       SDL_SetWindowResizable(Window, (SDL_bool)resize);\r
+               }\r
                Resizable = resize;\r
        }\r
 #endif // !_IRR_EMSCRIPTEN_PLATFORM_\r
@@ -1075,7 +1012,9 @@ void CIrrDeviceSDL::setResizable(bool resize)
 //! Minimizes window if possible\r
 void CIrrDeviceSDL::minimizeWindow()\r
 {\r
-       SDL_WM_IconifyWindow();\r
+       if (Window) {\r
+               SDL_MinimizeWindow(Window);\r
+       }\r
 }\r
 \r
 \r
@@ -1122,15 +1061,15 @@ bool CIrrDeviceSDL::isWindowActive() const
                        return false;\r
        }\r
 #endif\r
-       const Uint8 appState = SDL_GetAppState();\r
-       return (appState&SDL_APPACTIVE && appState&SDL_APPINPUTFOCUS) ? true : false;\r
+       const u32 windowFlags = SDL_GetWindowFlags(Window);\r
+       return windowFlags & SDL_WINDOW_SHOWN && windowFlags & SDL_WINDOW_INPUT_FOCUS;\r
 }\r
 \r
 \r
 //! returns if window has focus.\r
 bool CIrrDeviceSDL::isWindowFocused() const\r
 {\r
-       return (SDL_GetAppState()&SDL_APPINPUTFOCUS) ? true : false;\r
+       return SDL_GetWindowFlags(Window) & SDL_WINDOW_INPUT_FOCUS;\r
 }\r
 \r
 \r
@@ -1163,18 +1102,19 @@ bool CIrrDeviceSDL::getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightne
 //! returns color format of the window.\r
 video::ECOLOR_FORMAT CIrrDeviceSDL::getColorFormat() const\r
 {\r
-       if (Screen)\r
+       if (Window)\r
        {\r
-               if (Screen->format->BitsPerPixel==16)\r
+               SDL_Surface *surface = SDL_GetWindowSurface(Window);\r
+               if (surface->format->BitsPerPixel == 16)\r
                {\r
-                       if (Screen->format->Amask != 0)\r
+                       if (surface->format->Amask != 0)\r
                                return video::ECF_A1R5G5B5;\r
                        else\r
                                return video::ECF_R5G6B5;\r
                }\r
                else\r
                {\r
-                       if (Screen->format->Amask != 0)\r
+                       if (surface->format->Amask != 0)\r
                                return video::ECF_A8R8G8B8;\r
                        else\r
                                return video::ECF_R8G8B8;\r
@@ -1222,9 +1162,9 @@ void CIrrDeviceSDL::createKeyMap()
        KeyMap.push_back(SKeyMap(SDLK_DOWN, KEY_DOWN));\r
 \r
        // select missing\r
-       KeyMap.push_back(SKeyMap(SDLK_PRINT, KEY_PRINT));\r
+       KeyMap.push_back(SKeyMap(SDLK_PRINTSCREEN, KEY_PRINT));\r
        // execute missing\r
-       KeyMap.push_back(SKeyMap(SDLK_PRINT, KEY_SNAPSHOT));\r
+       KeyMap.push_back(SKeyMap(SDLK_PRINTSCREEN, KEY_SNAPSHOT));\r
 \r
        KeyMap.push_back(SKeyMap(SDLK_INSERT, KEY_INSERT));\r
        KeyMap.push_back(SKeyMap(SDLK_DELETE, KEY_DELETE));\r
@@ -1268,21 +1208,21 @@ void CIrrDeviceSDL::createKeyMap()
        KeyMap.push_back(SKeyMap(SDLK_y, KEY_KEY_Y));\r
        KeyMap.push_back(SKeyMap(SDLK_z, KEY_KEY_Z));\r
 \r
-       KeyMap.push_back(SKeyMap(SDLK_LSUPER, KEY_LWIN));\r
-       KeyMap.push_back(SKeyMap(SDLK_RSUPER, KEY_RWIN));\r
+       KeyMap.push_back(SKeyMap(SDLK_LGUI, KEY_LWIN));\r
+       KeyMap.push_back(SKeyMap(SDLK_RGUI, KEY_RWIN));\r
        // apps missing\r
        KeyMap.push_back(SKeyMap(SDLK_POWER, KEY_SLEEP)); //??\r
 \r
-       KeyMap.push_back(SKeyMap(SDLK_KP0, KEY_NUMPAD0));\r
-       KeyMap.push_back(SKeyMap(SDLK_KP1, KEY_NUMPAD1));\r
-       KeyMap.push_back(SKeyMap(SDLK_KP2, KEY_NUMPAD2));\r
-       KeyMap.push_back(SKeyMap(SDLK_KP3, KEY_NUMPAD3));\r
-       KeyMap.push_back(SKeyMap(SDLK_KP4, KEY_NUMPAD4));\r
-       KeyMap.push_back(SKeyMap(SDLK_KP5, KEY_NUMPAD5));\r
-       KeyMap.push_back(SKeyMap(SDLK_KP6, KEY_NUMPAD6));\r
-       KeyMap.push_back(SKeyMap(SDLK_KP7, KEY_NUMPAD7));\r
-       KeyMap.push_back(SKeyMap(SDLK_KP8, KEY_NUMPAD8));\r
-       KeyMap.push_back(SKeyMap(SDLK_KP9, KEY_NUMPAD9));\r
+       KeyMap.push_back(SKeyMap(SDLK_KP_0, KEY_NUMPAD0));\r
+       KeyMap.push_back(SKeyMap(SDLK_KP_1, KEY_NUMPAD1));\r
+       KeyMap.push_back(SKeyMap(SDLK_KP_2, KEY_NUMPAD2));\r
+       KeyMap.push_back(SKeyMap(SDLK_KP_3, KEY_NUMPAD3));\r
+       KeyMap.push_back(SKeyMap(SDLK_KP_4, KEY_NUMPAD4));\r
+       KeyMap.push_back(SKeyMap(SDLK_KP_5, KEY_NUMPAD5));\r
+       KeyMap.push_back(SKeyMap(SDLK_KP_6, KEY_NUMPAD6));\r
+       KeyMap.push_back(SKeyMap(SDLK_KP_7, KEY_NUMPAD7));\r
+       KeyMap.push_back(SKeyMap(SDLK_KP_8, KEY_NUMPAD8));\r
+       KeyMap.push_back(SKeyMap(SDLK_KP_9, KEY_NUMPAD9));\r
        KeyMap.push_back(SKeyMap(SDLK_KP_MULTIPLY, KEY_MULTIPLY));\r
        KeyMap.push_back(SKeyMap(SDLK_KP_PLUS, KEY_ADD));\r
 //     KeyMap.push_back(SKeyMap(SDLK_KP_, KEY_SEPARATOR));\r
@@ -1307,8 +1247,8 @@ void CIrrDeviceSDL::createKeyMap()
        KeyMap.push_back(SKeyMap(SDLK_F15, KEY_F15));\r
        // no higher F-keys\r
 \r
-       KeyMap.push_back(SKeyMap(SDLK_NUMLOCK, KEY_NUMLOCK));\r
-       KeyMap.push_back(SKeyMap(SDLK_SCROLLOCK, KEY_SCROLL));\r
+       KeyMap.push_back(SKeyMap(SDLK_NUMLOCKCLEAR, KEY_NUMLOCK));\r
+       KeyMap.push_back(SKeyMap(SDLK_SCROLLLOCK, KEY_SCROLL));\r
        KeyMap.push_back(SKeyMap(SDLK_LSHIFT, KEY_LSHIFT));\r
        KeyMap.push_back(SKeyMap(SDLK_RSHIFT, KEY_RSHIFT));\r
        KeyMap.push_back(SKeyMap(SDLK_LCTRL,  KEY_LCONTROL));\r
index 0cf863a32c2e8ea5346760229eb68c5899f9c74c..e5e3d723acdb95de249c1ea769e86a864dca6d11 100644 (file)
@@ -20,8 +20,8 @@
 #include <emscripten/html5.h>\r
 #endif\r
 \r
-#include <SDL/SDL.h>\r
-#include <SDL/SDL_syswm.h>\r
+#include <SDL2/SDL.h>\r
+#include <SDL2/SDL_syswm.h>\r
 \r
 namespace irr\r
 {\r
@@ -66,9 +66,6 @@ namespace irr
                //! notifies the device that it should close itself\r
                virtual void closeDevice() _IRR_OVERRIDE_;\r
 \r
-               //! \return Returns a pointer to a list with all video modes supported\r
-               virtual video::IVideoModeList* getVideoModeList() _IRR_OVERRIDE_;\r
-\r
                //! Sets if the window should be resizable in windowed mode.\r
                virtual void setResizable(bool resize=false) _IRR_OVERRIDE_;\r
 \r
@@ -103,6 +100,8 @@ namespace irr
                        return EIDT_SDL;\r
                }\r
 \r
+               void SwapWindow();\r
+\r
                //! Implementation of the linux cursor control\r
                class CCursorControl : public gui::ICursorControl\r
                {\r
@@ -152,7 +151,7 @@ namespace irr
                        //! Sets the new position of the cursor.\r
                        virtual void setPosition(s32 x, s32 y) _IRR_OVERRIDE_\r
                        {\r
-                               SDL_WarpMouse( x, y );\r
+                               SDL_WarpMouseInWindow(Device->Window, x, y);\r
                        }\r
 \r
                        //! Returns the current position of the mouse cursor.\r
@@ -224,9 +223,9 @@ namespace irr
        static EM_BOOL MouseEnterCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);\r
        static EM_BOOL MouseLeaveCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);\r
 \r
-       // Check if it's a special key like left, right, up down and so on which shouldn't have a Unicode character.\r
-       bool isNoUnicodeKey(EKEY_CODE key) const;\r
 #endif\r
+               // Check if it's a special key like left, right, up down and so on which shouldn't have a Unicode character.\r
+               bool isNoUnicodeKey(EKEY_CODE key) const;\r
 \r
                //! create the driver\r
                void createDriver();\r
@@ -236,8 +235,8 @@ namespace irr
                void createKeyMap();\r
 \r
                void logAttributes();\r
-\r
-               SDL_Surface* Screen;\r
+               SDL_GLContext Context;\r
+               SDL_Window *Window;\r
                int SDL_Flags;\r
 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)\r
                core::array<SDL_Joystick*> Joysticks;\r
index 4db72a973e81c418db4066e2110e937712807340..b2e53eeececc900c3779e34726239bfe0fe58d6b 100644 (file)
@@ -56,6 +56,7 @@ unset(OGLES1_ENABLED CACHE)
 unset(OGLES2_ENABLED CACHE)
 unset(OGL_ENABLED CACHE)
 unset(XINPUT2_ENABLED CACHE)
+unset(SDL_ENABLED CACHE)
 
 check_symbol_exists(_IRR_COMPILE_WITH_OGLES1_ "IrrCompileConfig.h" OGLES1_ENABLED)
 if(OGLES1_ENABLED)
@@ -79,6 +80,12 @@ if(XINPUT2_ENABLED)
        find_library(XINPUT_LIBRARY Xi REQUIRED)
 endif()
 
+check_symbol_exists(_IRR_COMPILE_WITH_SDL_DEVICE_ "IrrCompileConfig.h" SDL_ENABLED)
+if(SDL_ENABLED)
+       find_package(SDL2 CONFIG REQUIRED)
+       message(STATUS "Found SDL2: ${SDL2_LIBRARIES}")
+endif()
+
 # Platform-specific libs
 
 if(ANDROID)
@@ -101,6 +108,7 @@ set(link_includes
        "${ZLIB_INCLUDE_DIR}"
        "${JPEG_INCLUDE_DIR}"
        "${PNG_INCLUDE_DIR}"
+       "${SDL2_INCLUDE_DIRS}"
 
        ${OPENGL_INCLUDE_DIR}
        ${OPENGLES2_INCLUDE_DIR}
@@ -114,6 +122,7 @@ set(link_libs
        "${ZLIB_LIBRARY}"
        "${JPEG_LIBRARY}"
        "${PNG_LIBRARY}"
+       "${SDL2_LIBRARIES}"
 
        ${OPENGL_LIBRARIES}
        ${OPENGLES_LIBRARY}
index d8ecc2efa6ee0621f846639870b807359209e972..256868ebe55ef783ec49cb11448e106db8054bb7 100644 (file)
 #elif defined(_IRR_COMPILE_WITH_SDL_DEVICE_) && !defined(_IRR_COMPILE_WITH_X11_DEVICE_)\r
        #if defined(_IRR_OPENGL_USE_EXTPOINTER_)\r
                #define GL_GLEXT_LEGACY 1\r
-               #define GLX_GLXEXT_LEGACY 1\r
        #else\r
                #define GL_GLEXT_PROTOTYPES 1\r
-               #define GLX_GLXEXT_PROTOTYPES 1\r
        #endif\r
-       #include <SDL/SDL_video.h>\r
-       #include <SDL/SDL_opengl.h>\r
+       #include <SDL2/SDL_video.h>\r
+       #include <SDL2/SDL_opengl.h>\r
+       #if defined(_IRR_OPENGL_USE_EXTPOINTER_)\r
+               // The SDL2 header doesn't cut it for extensions\r
+               #include <GL/glext.h>\r
+       #endif\r
 #else\r
        #if defined(_IRR_OPENGL_USE_EXTPOINTER_)\r
                #define GL_GLEXT_LEGACY 1\r
index 39ec18e15c2e5135d2a485e31fb3bd5ff11d011f..bde8ddc84d540a0f89eef222ee68a8e8579d75c6 100644 (file)
@@ -21,7 +21,7 @@
 #include "mt_opengl.h"\r
 \r
 #ifdef _IRR_COMPILE_WITH_SDL_DEVICE_\r
-#include <SDL/SDL.h>\r
+#include "CIrrDeviceSDL.h"\r
 #endif\r
 \r
 namespace irr\r
@@ -268,7 +268,7 @@ bool COpenGLDriver::beginScene(u16 clearFlag, SColor clearColor, f32 clearDepth,
                ContextManager->activateContext(videoData, true);\r
 \r
 #if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)\r
-       if ( DeviceType == EIDT_SDL )\r
+       if (SDLDevice)\r
                glFrontFace(GL_CW);\r
 #endif\r
 \r
@@ -289,9 +289,9 @@ bool COpenGLDriver::endScene()
                status = ContextManager->swapBuffers();\r
 \r
 #ifdef _IRR_COMPILE_WITH_SDL_DEVICE_\r
-       if ( DeviceType == EIDT_SDL )\r
+       if (SDLDevice)\r
        {\r
-               SDL_GL_SwapBuffers();\r
+               SDLDevice->SwapWindow();\r
                status = true;\r
        }\r
 #endif\r
index b736eafd633336e8c0f37fb8dc32b9026e304b23..684205739432e3d300f1c4f52ae645a2374dc4f2 100644 (file)
@@ -8,7 +8,7 @@
 #include "irrMath.h"\r
 \r
 #if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)\r
-       #include <SDL/SDL_endian.h>\r
+       #include <SDL2/SDL_endian.h>\r
        #define bswap_16(X) SDL_Swap16(X)\r
        #define bswap_32(X) SDL_Swap32(X)\r
        #define bswap_64(X) SDL_Swap64(X)\r