Update CIrrDeviceSDL to support SDL2 (#85)

This commit is contained in:
Emmanuel Hansen 2022-01-16 11:16:15 +00:00 committed by GitHub
parent 195759100f
commit 53db262bd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 207 additions and 263 deletions

@ -64,25 +64,15 @@
#if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64)
#define _IRR_WINDOWS_
#define _IRR_WINDOWS_API_
#ifndef _IRR_COMPILE_WITH_SDL_DEVICE_
#define _IRR_COMPILE_WITH_WINDOWS_DEVICE_
#endif
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1500)
# error "Only Microsoft Visual Studio 9.0 and later are supported."
#endif
// XBox is deprecated (as DX8 is removed). Use Irrlicht 1.8 if you still want to work on this.
#if defined(_XBOX)
#undef _IRR_WINDOWS_
#define _IRR_XBOX_PLATFORM_ // deprecated
#define _IRR_WINDOWS_API_
//#define _IRR_COMPILE_WITH_WINDOWS_DEVICE_
#undef _IRR_COMPILE_WITH_WINDOWS_DEVICE_
//#define _IRR_COMPILE_WITH_SDL_DEVICE_
#include <xtl.h>
#endif
#if defined(__APPLE__) || defined(MACOSX)
#if !defined(MACOSX)
#define MACOSX // legacy support
@ -98,7 +88,9 @@
#define _IRR_COMPILE_WITH_IOS_BUILTIN_MAIN_
#else
#define _IRR_OSX_PLATFORM_
#ifndef _IRR_COMPILE_WITH_SDL_DEVICE_
#define _IRR_COMPILE_WITH_OSX_DEVICE_
#endif
#define NO_IRR_COMPILE_WITH_OGLES1_
#define NO_IRR_COMPILE_WITH_OGLES2_
#define NO_IRR_COMPILE_WITH_WEBGL1_
@ -137,8 +129,10 @@
#define _IRR_LINUX_PLATFORM_
#endif
#define _IRR_POSIX_API_
#ifndef _IRR_COMPILE_WITH_SDL_DEVICE_
#define _IRR_COMPILE_WITH_X11_DEVICE_
#endif
#endif
//! Define _IRR_COMPILE_WITH_JOYSTICK_SUPPORT_ if you want joystick events.

@ -17,8 +17,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "SIrrCreationParameters.h"
#include <SDL/SDL_syswm.h>
#include <SDL/SDL_video.h>
#include <SDL2/SDL_video.h>
#ifdef _IRR_EMSCRIPTEN_PLATFORM_
#ifdef _IRR_COMPILE_WITH_OGLES2_
@ -27,10 +26,6 @@
#include <emscripten.h>
#endif
#ifdef _MSC_VER
#pragma comment(lib, "SDL.lib")
#endif // _MSC_VER
static int SDLDeviceInstances = 0;
namespace irr
@ -111,6 +106,7 @@ EM_BOOL CIrrDeviceSDL::MouseLeaveCallback(int eventType, const EmscriptenMouseEv
return EM_FALSE;
}
#endif
bool CIrrDeviceSDL::isNoUnicodeKey(EKEY_CODE key) const
{
@ -179,12 +175,11 @@ bool CIrrDeviceSDL::isNoUnicodeKey(EKEY_CODE key) const
return false;
}
}
#endif
//! constructor
CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
: CIrrDeviceStub(param),
Screen((SDL_Surface*)param.WindowId), SDL_Flags(SDL_ANYFORMAT),
Window((SDL_Window*)param.WindowId), SDL_Flags(0),
MouseX(0), MouseY(0), MouseXRel(0), MouseYRel(0), MouseButtonStates(0),
Width(param.WindowSize.Width), Height(param.WindowSize.Height),
Resizable(param.WindowResizable == 1 ? true : false), WindowMinimized(false)
@ -210,53 +205,23 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
{
os::Printer::log("SDL initialized", ELL_INFORMATION);
}
#if defined(_IRR_WINDOWS_)
SDL_putenv("SDL_VIDEODRIVER=directx");
#elif defined(_IRR_OSX_PLATFORM_)
SDL_putenv("SDL_VIDEODRIVER=Quartz");
#elif !defined(_IRR_EMSCRIPTEN_PLATFORM_)
SDL_putenv("SDL_VIDEODRIVER=x11");
#endif
}
// SDL_putenv("SDL_WINDOWID=");
SDL_VERSION(&Info.version);
#ifndef _IRR_EMSCRIPTEN_PLATFORM_
SDL_GetWMInfo(&Info);
#endif //_IRR_EMSCRIPTEN_PLATFORM_
core::stringc sdlversion = "SDL Version ";
sdlversion += Info.version.major;
sdlversion += ".";
sdlversion += Info.version.minor;
sdlversion += ".";
sdlversion += Info.version.patch;
Operator = new COSOperator(sdlversion);
if ( SDLDeviceInstances == 1 )
{
os::Printer::log(sdlversion.c_str(), ELL_INFORMATION);
}
// create keymap
createKeyMap();
// enable key to character translation
SDL_EnableUNICODE(1);
(void)SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
if ( CreationParams.Fullscreen )
SDL_Flags |= SDL_FULLSCREEN;
SDL_Flags |= SDL_WINDOW_FULLSCREEN;
else if ( Resizable )
SDL_Flags |= SDL_RESIZABLE;
SDL_Flags |= SDL_WINDOW_RESIZABLE;
if (CreationParams.DriverType == video::EDT_OPENGL)
SDL_Flags |= SDL_OPENGL;
else if (CreationParams.Doublebuffer)
SDL_Flags |= SDL_DOUBLEBUF;
{
SDL_Flags |= SDL_WINDOW_OPENGL;
if (!CreationParams.Doublebuffer)
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
}
#ifdef _IRR_EMSCRIPTEN_PLATFORM_
SDL_Flags |= SDL_OPENGL;
SDL_Flags |= SDL_WINDOW_OPENGL;
#endif //_IRR_EMSCRIPTEN_PLATFORM_
// create window
@ -266,14 +231,33 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
createWindow();
}
SDL_VERSION(&Info.version);
#ifndef _IRR_EMSCRIPTEN_PLATFORM_
SDL_GetWindowWMInfo(Window,&Info);
#endif //_IRR_EMSCRIPTEN_PLATFORM_
core::stringc sdlversion = "SDL Version ";
sdlversion += Info.version.major;
sdlversion += ".";
sdlversion += Info.version.minor;
sdlversion += ".";
sdlversion += Info.version.patch;
Operator = new COSOperator(sdlversion);
if (SDLDeviceInstances == 1) {
os::Printer::log(sdlversion.c_str(), ELL_INFORMATION);
}
// create cursor control
CursorControl = new CCursorControl(this);
// create driver
createDriver();
if (VideoDriver)
if (VideoDriver) {
createGUIAndScene();
VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));
}
}
@ -287,6 +271,12 @@ CIrrDeviceSDL::~CIrrDeviceSDL()
for (u32 i=0; i<numJoysticks; ++i)
SDL_JoystickClose(Joysticks[i]);
#endif
if (Window)
{
SDL_GL_MakeCurrent(Window, NULL);
SDL_GL_DeleteContext(Context);
SDL_DestroyWindow(Window);
}
SDL_Quit();
os::Printer::log("Quit SDL", ELL_INFORMATION);
@ -351,9 +341,9 @@ bool CIrrDeviceSDL::createWindow()
{
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
}
}
Screen = SDL_SetVideoMode( 0, 0, 32, SDL_Flags); // 0,0 will use the canvas size
SDL_CreateWindowAndRenderer(0, 0, SDL_Flags, &Window, &Renderer); // 0,0 will use the canvas size
logAttributes();
@ -368,66 +358,63 @@ bool CIrrDeviceSDL::createWindow()
if ( Close )
return false;
if (CreationParams.DriverType == video::EDT_OPENGL)
{
if (CreationParams.Bits==16)
{
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 4 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 4 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 4 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, CreationParams.WithAlphaChannel?1:0 );
if (CreationParams.DriverType == video::EDT_OPENGL) {
if (CreationParams.Bits == 16) {
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 4);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 4);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 4);
SDL_GL_SetAttribute(
SDL_GL_ALPHA_SIZE, CreationParams.WithAlphaChannel ? 1 : 0);
} else {
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_ALPHA_SIZE, CreationParams.WithAlphaChannel ? 8 : 0);
}
else
{
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_ALPHA_SIZE, CreationParams.WithAlphaChannel?8:0 );
}
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, CreationParams.ZBufferBits);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, CreationParams.ZBufferBits);
if (CreationParams.Doublebuffer)
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, CreationParams.Stencilbuffer ? 8 : 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, CreationParams.Stencilbuffer ? 8 : 0);
if (CreationParams.Stereobuffer)
SDL_GL_SetAttribute( SDL_GL_STEREO, 1 );
if (CreationParams.AntiAlias>1)
{
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, CreationParams.AntiAlias );
SDL_GL_SetAttribute(SDL_GL_STEREO, 1);
if (CreationParams.AntiAlias > 1) {
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, CreationParams.AntiAlias);
}
if ( !Screen )
Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );
if ( !Screen && CreationParams.AntiAlias>1)
{
while (--CreationParams.AntiAlias>1)
{
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, CreationParams.AntiAlias );
Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );
if (Screen)
if (!Window)
Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);
if (!Window && CreationParams.AntiAlias > 1) {
while (--CreationParams.AntiAlias > 1) {
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, CreationParams.AntiAlias);
Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);
if (Window)
break;
}
if ( !Screen )
{
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 0 );
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 0 );
Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );
if (Screen)
os::Printer::log("AntiAliasing disabled due to lack of support!" );
if (!Window) {
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);
if (Window)
os::Printer::log("AntiAliasing disabled due to lack of support!");
}
}
}
else if ( !Screen )
Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );
if ( !Screen && CreationParams.Doublebuffer)
if (Window)
{
Context = SDL_GL_CreateContext(Window);
}
} else if (!Window)
Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);
if ( !Window && CreationParams.Doublebuffer)
{
// Try single buffer
if (CreationParams.DriverType == video::EDT_OPENGL)
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_Flags &= ~SDL_DOUBLEBUF;
Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);
}
if ( !Screen )
if ( !Window )
{
os::Printer::log( "Could not initialize display!" );
return false;
@ -523,9 +510,9 @@ void CIrrDeviceSDL::createDriver()
if ( VideoDriver && CreationParams.WindowSize.Width == 0 && CreationParams.WindowSize.Height == 0 && Width > 0 && Height > 0 )
{
#ifdef _IRR_EMSCRIPTEN_PLATFORM_
Screen = SDL_SetVideoMode( Width, Height, 32, SDL_Flags );
SDL_CreateWindowAndRenderer(Width, Height, SDL_Flags, &Window, &Renderer);
#else //_IRR_EMSCRIPTEN_PLATFORM_
Screen = SDL_SetVideoMode( Width, Height, 0, SDL_Flags );
Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);
#endif //_IRR_EMSCRIPTEN_PLATFOR
VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));
}
@ -557,10 +544,14 @@ bool CIrrDeviceSDL::run()
postEventFromUser(irrevent);
break;
case SDL_MOUSEWHEEL:
irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
irrevent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
irrevent.MouseInput.Wheel = static_cast<float>(SDL_event.wheel.y);
postEventFromUser(irrevent);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
irrevent.MouseInput.X = SDL_event.button.x;
irrevent.MouseInput.Y = SDL_event.button.y;
@ -605,7 +596,7 @@ bool CIrrDeviceSDL::run()
else
{
irrevent.MouseInput.Event = irr::EMIE_LMOUSE_LEFT_UP;
MouseButtonStates &= !irr::EMBSM_LEFT;
MouseButtonStates &= ~irr::EMBSM_LEFT;
}
break;
@ -618,7 +609,7 @@ bool CIrrDeviceSDL::run()
else
{
irrevent.MouseInput.Event = irr::EMIE_RMOUSE_LEFT_UP;
MouseButtonStates &= !irr::EMBSM_RIGHT;
MouseButtonStates &= ~irr::EMBSM_RIGHT;
}
break;
@ -631,19 +622,9 @@ bool CIrrDeviceSDL::run()
else
{
irrevent.MouseInput.Event = irr::EMIE_MMOUSE_LEFT_UP;
MouseButtonStates &= !irr::EMBSM_MIDDLE;
MouseButtonStates &= ~irr::EMBSM_MIDDLE;
}
break;
case SDL_BUTTON_WHEELUP:
irrevent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
irrevent.MouseInput.Wheel = 1.0f;
break;
case SDL_BUTTON_WHEELDOWN:
irrevent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
irrevent.MouseInput.Wheel = -1.0f;
break;
}
irrevent.MouseInput.ButtonStates = MouseButtonStates;
@ -691,19 +672,25 @@ bool CIrrDeviceSDL::run()
}
#endif
irrevent.EventType = irr::EET_KEY_INPUT_EVENT;
irrevent.KeyInput.Char = SDL_event.key.keysym.unicode;
irrevent.KeyInput.Key = key;
#ifdef _IRR_EMSCRIPTEN_PLATFORM_
// On emscripten SDL does not (yet?) return 0 for invalid keysym.unicode's.
// Instead it sets keysym.unicode to keysym.sym.
// But we need to distinguish control keys from characters here as that info
// is necessary in other places like the editbox.
if ( isNoUnicodeKey(key) )
if (isNoUnicodeKey(key))
irrevent.KeyInput.Char = 0;
#endif
else
irrevent.KeyInput.Char = SDL_event.key.keysym.sym;
irrevent.KeyInput.Key = key;
irrevent.KeyInput.PressedDown = (SDL_event.type == SDL_KEYDOWN);
irrevent.KeyInput.Shift = (SDL_event.key.keysym.mod & KMOD_SHIFT) != 0;
irrevent.KeyInput.Control = (SDL_event.key.keysym.mod & KMOD_CTRL ) != 0;
// SDL2 no longer provides unicode character in the key event so we ensure the character is in the correct case
// TODO: Unicode input doesn't work like this, should probably use SDL_TextInputEvent
bool convertCharToUppercase =
irrevent.KeyInput.Shift != !!(SDL_event.key.keysym.mod & KMOD_CAPS);
if (convertCharToUppercase)
irrevent.KeyInput.Char = irr::core::locale_upper(irrevent.KeyInput.Char);
postEventFromUser(irrevent);
}
break;
@ -712,25 +699,25 @@ bool CIrrDeviceSDL::run()
Close = true;
break;
case SDL_ACTIVEEVENT:
if (SDL_event.active.state == SDL_APPACTIVE)
WindowMinimized = (SDL_event.active.gain!=1);
break;
case SDL_VIDEORESIZE:
if ((SDL_event.resize.w != (int)Width) || (SDL_event.resize.h != (int)Height))
case SDL_WINDOWEVENT:
switch (SDL_event.window.event)
{
Width = SDL_event.resize.w;
Height = SDL_event.resize.h;
#ifdef _IRR_EMSCRIPTEN_PLATFORM_
Screen = SDL_SetVideoMode( 0, 0, 32, SDL_Flags ); // 0,0 will use the canvas size
#else //_IRR_EMSCRIPTEN_PLATFORM_
Screen = SDL_SetVideoMode( Width, Height, 0, SDL_Flags );
#endif //_IRR_EMSCRIPTEN_PLATFOR
if (VideoDriver)
VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));
case SDL_WINDOWEVENT_MAXIMIZED:
WindowMinimized = true;
break;
case SDL_WINDOWEVENT_RESTORED:
WindowMinimized = false;
break;
case SDL_WINDOWEVENT_RESIZED:
if ((SDL_event.window.data1 != (int)Width) || (SDL_event.window.data2 != (int)Height))
{
Width = SDL_event.window.data1;
Height = SDL_event.window.data2;
if (VideoDriver)
VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));
}
break;
}
break;
case SDL_USEREVENT:
irrevent.EventType = irr::EET_USER_EVENT;
@ -843,13 +830,13 @@ bool CIrrDeviceSDL::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
int joystick = 0;
for (; joystick<numJoysticks; ++joystick)
{
Joysticks.push_back(SDL_JoystickOpen(joystick));
Joysticks.push_back( SDL_JoystickOpen(joystick));
SJoystickInfo info;
info.Joystick = joystick;
info.Axes = SDL_JoystickNumAxes(Joysticks[joystick]);
info.Buttons = SDL_JoystickNumButtons(Joysticks[joystick]);
info.Name = SDL_JoystickName(joystick);
info.Name = SDL_JoystickName(Joysticks[joystick]);
info.PovHat = (SDL_JoystickNumHats(Joysticks[joystick]) > 0)
? SJoystickInfo::POV_HAT_PRESENT : SJoystickInfo::POV_HAT_ABSENT;
@ -872,6 +859,11 @@ bool CIrrDeviceSDL::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
return false;
}
void CIrrDeviceSDL::SwapWindow()
{
SDL_GL_SwapWindow(Window);
}
//! pause execution temporarily
@ -898,8 +890,9 @@ void CIrrDeviceSDL::sleep(u32 timeMs, bool pauseTimer)
//! sets the caption of the window
void CIrrDeviceSDL::setWindowCaption(const wchar_t* text)
{
core::stringc textc = text;
SDL_WM_SetCaption( textc.c_str( ), textc.c_str( ) );
core::stringc textc;
core::wStringToMultibyte(textc, text);
SDL_SetWindowTitle(Window, textc.c_str());
}
@ -915,7 +908,7 @@ bool CIrrDeviceSDL::present(video::IImage* surface, void* windowId, core::rect<s
surface->getRedMask(), surface->getGreenMask(), surface->getBlueMask(), surface->getAlphaMask());
if (!sdlSurface)
return false;
SDL_SetAlpha(sdlSurface, 0, 0);
SDL_SetSurfaceAlphaMod(sdlSurface, 0);
SDL_SetColorKey(sdlSurface, 0, 0);
sdlSurface->format->BitsPerPixel=surface->getBitsPerPixel();
sdlSurface->format->BytesPerPixel=surface->getBytesPerPixel();
@ -964,7 +957,7 @@ bool CIrrDeviceSDL::present(video::IImage* surface, void* windowId, core::rect<s
SDL_Surface* scr = (SDL_Surface* )windowId;
if (!scr)
scr = Screen;
scr = SDL_GetWindowSurface(Window);
if (scr)
{
if (srcClip)
@ -978,7 +971,7 @@ bool CIrrDeviceSDL::present(video::IImage* surface, void* windowId, core::rect<s
}
else
SDL_BlitSurface(sdlSurface, NULL, scr, NULL);
SDL_Flip(scr);
SDL_RenderPresent(SDL_GetRenderer(Window));
}
SDL_FreeSurface(sdlSurface);
@ -994,53 +987,6 @@ void CIrrDeviceSDL::closeDevice()
}
//! \return Pointer to a list with all video modes supported
video::IVideoModeList* CIrrDeviceSDL::getVideoModeList()
{
#ifdef _IRR_EMSCRIPTEN_PLATFORM_
os::Printer::log("VideoModeList not available on the web." , ELL_WARNING);
return VideoModeList;
#else // !_IRR_EMSCRIPTEN_PLATFORM_
if (!VideoModeList->getVideoModeCount())
{
// enumerate video modes.
const SDL_VideoInfo *vi = SDL_GetVideoInfo();
SDL_PixelFormat pixelFormat = *(vi->vfmt);
core::array<Uint8> checkBitsPerPixel;
checkBitsPerPixel.push_back(8);
checkBitsPerPixel.push_back(16);
checkBitsPerPixel.push_back(24);
checkBitsPerPixel.push_back(32);
if ( pixelFormat.BitsPerPixel > 32 )
checkBitsPerPixel.push_back(pixelFormat.BitsPerPixel);
for ( u32 i=0; i<checkBitsPerPixel.size(); ++i)
{
pixelFormat.BitsPerPixel = checkBitsPerPixel[i];
SDL_Rect **modes = SDL_ListModes(&pixelFormat, SDL_Flags|SDL_FULLSCREEN);
if (modes != 0)
{
if (modes == (SDL_Rect **)-1)
{
core::stringc strLog("All modes available for bit-depth ");
strLog += core::stringc(pixelFormat.BitsPerPixel);
os::Printer::log(strLog.c_str());
}
else
{
for (u32 i=0; modes[i]; ++i)
VideoModeList->addMode(core::dimension2d<u32>(modes[i]->w, modes[i]->h), vi->vfmt->BitsPerPixel);
}
}
}
}
return VideoModeList;
#endif // !_IRR_EMSCRIPTEN_PLATFORM_
}
//! Sets if the window should be resizable in windowed mode.
void CIrrDeviceSDL::setResizable(bool resize)
{
@ -1048,24 +994,15 @@ void CIrrDeviceSDL::setResizable(bool resize)
os::Printer::log("Resizable not available on the web." , ELL_WARNING);
return;
#else // !_IRR_EMSCRIPTEN_PLATFORM_
if (resize != Resizable)
{
#if defined(_IRR_COMPILE_WITH_OPENGL_) && defined(_IRR_WINDOWS_)
if ( SDL_Flags & SDL_OPENGL )
{
// For unknown reasons the hack with sharing resources which was added in Irrlicht 1.8.5 for this no longer works in 1.9
// But at least we got a new WindowResizable flag since Irrlicht 1.9.
os::Printer::log("setResizable not supported with this device/driver combination. Use SIrrCreationParameters.WindowResizable instead.", ELL_WARNING);
return;
}
#endif
if (resize != Resizable) {
if (resize)
SDL_Flags |= SDL_RESIZABLE;
SDL_Flags |= SDL_WINDOW_RESIZABLE;
else
SDL_Flags &= ~SDL_RESIZABLE;
SDL_Flags &= ~SDL_WINDOW_RESIZABLE;
Screen = SDL_SetVideoMode( 0, 0, 0, SDL_Flags );
if (Window) {
SDL_SetWindowResizable(Window, (SDL_bool)resize);
}
Resizable = resize;
}
#endif // !_IRR_EMSCRIPTEN_PLATFORM_
@ -1075,7 +1012,9 @@ void CIrrDeviceSDL::setResizable(bool resize)
//! Minimizes window if possible
void CIrrDeviceSDL::minimizeWindow()
{
SDL_WM_IconifyWindow();
if (Window) {
SDL_MinimizeWindow(Window);
}
}
@ -1122,15 +1061,15 @@ bool CIrrDeviceSDL::isWindowActive() const
return false;
}
#endif
const Uint8 appState = SDL_GetAppState();
return (appState&SDL_APPACTIVE && appState&SDL_APPINPUTFOCUS) ? true : false;
const u32 windowFlags = SDL_GetWindowFlags(Window);
return windowFlags & SDL_WINDOW_SHOWN && windowFlags & SDL_WINDOW_INPUT_FOCUS;
}
//! returns if window has focus.
bool CIrrDeviceSDL::isWindowFocused() const
{
return (SDL_GetAppState()&SDL_APPINPUTFOCUS) ? true : false;
return SDL_GetWindowFlags(Window) & SDL_WINDOW_INPUT_FOCUS;
}
@ -1163,18 +1102,19 @@ bool CIrrDeviceSDL::getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightne
//! returns color format of the window.
video::ECOLOR_FORMAT CIrrDeviceSDL::getColorFormat() const
{
if (Screen)
if (Window)
{
if (Screen->format->BitsPerPixel==16)
SDL_Surface *surface = SDL_GetWindowSurface(Window);
if (surface->format->BitsPerPixel == 16)
{
if (Screen->format->Amask != 0)
if (surface->format->Amask != 0)
return video::ECF_A1R5G5B5;
else
return video::ECF_R5G6B5;
}
else
{
if (Screen->format->Amask != 0)
if (surface->format->Amask != 0)
return video::ECF_A8R8G8B8;
else
return video::ECF_R8G8B8;
@ -1222,9 +1162,9 @@ void CIrrDeviceSDL::createKeyMap()
KeyMap.push_back(SKeyMap(SDLK_DOWN, KEY_DOWN));
// select missing
KeyMap.push_back(SKeyMap(SDLK_PRINT, KEY_PRINT));
KeyMap.push_back(SKeyMap(SDLK_PRINTSCREEN, KEY_PRINT));
// execute missing
KeyMap.push_back(SKeyMap(SDLK_PRINT, KEY_SNAPSHOT));
KeyMap.push_back(SKeyMap(SDLK_PRINTSCREEN, KEY_SNAPSHOT));
KeyMap.push_back(SKeyMap(SDLK_INSERT, KEY_INSERT));
KeyMap.push_back(SKeyMap(SDLK_DELETE, KEY_DELETE));
@ -1268,21 +1208,21 @@ void CIrrDeviceSDL::createKeyMap()
KeyMap.push_back(SKeyMap(SDLK_y, KEY_KEY_Y));
KeyMap.push_back(SKeyMap(SDLK_z, KEY_KEY_Z));
KeyMap.push_back(SKeyMap(SDLK_LSUPER, KEY_LWIN));
KeyMap.push_back(SKeyMap(SDLK_RSUPER, KEY_RWIN));
KeyMap.push_back(SKeyMap(SDLK_LGUI, KEY_LWIN));
KeyMap.push_back(SKeyMap(SDLK_RGUI, KEY_RWIN));
// apps missing
KeyMap.push_back(SKeyMap(SDLK_POWER, KEY_SLEEP)); //??
KeyMap.push_back(SKeyMap(SDLK_KP0, KEY_NUMPAD0));
KeyMap.push_back(SKeyMap(SDLK_KP1, KEY_NUMPAD1));
KeyMap.push_back(SKeyMap(SDLK_KP2, KEY_NUMPAD2));
KeyMap.push_back(SKeyMap(SDLK_KP3, KEY_NUMPAD3));
KeyMap.push_back(SKeyMap(SDLK_KP4, KEY_NUMPAD4));
KeyMap.push_back(SKeyMap(SDLK_KP5, KEY_NUMPAD5));
KeyMap.push_back(SKeyMap(SDLK_KP6, KEY_NUMPAD6));
KeyMap.push_back(SKeyMap(SDLK_KP7, KEY_NUMPAD7));
KeyMap.push_back(SKeyMap(SDLK_KP8, KEY_NUMPAD8));
KeyMap.push_back(SKeyMap(SDLK_KP9, KEY_NUMPAD9));
KeyMap.push_back(SKeyMap(SDLK_KP_0, KEY_NUMPAD0));
KeyMap.push_back(SKeyMap(SDLK_KP_1, KEY_NUMPAD1));
KeyMap.push_back(SKeyMap(SDLK_KP_2, KEY_NUMPAD2));
KeyMap.push_back(SKeyMap(SDLK_KP_3, KEY_NUMPAD3));
KeyMap.push_back(SKeyMap(SDLK_KP_4, KEY_NUMPAD4));
KeyMap.push_back(SKeyMap(SDLK_KP_5, KEY_NUMPAD5));
KeyMap.push_back(SKeyMap(SDLK_KP_6, KEY_NUMPAD6));
KeyMap.push_back(SKeyMap(SDLK_KP_7, KEY_NUMPAD7));
KeyMap.push_back(SKeyMap(SDLK_KP_8, KEY_NUMPAD8));
KeyMap.push_back(SKeyMap(SDLK_KP_9, KEY_NUMPAD9));
KeyMap.push_back(SKeyMap(SDLK_KP_MULTIPLY, KEY_MULTIPLY));
KeyMap.push_back(SKeyMap(SDLK_KP_PLUS, KEY_ADD));
// KeyMap.push_back(SKeyMap(SDLK_KP_, KEY_SEPARATOR));
@ -1307,8 +1247,8 @@ void CIrrDeviceSDL::createKeyMap()
KeyMap.push_back(SKeyMap(SDLK_F15, KEY_F15));
// no higher F-keys
KeyMap.push_back(SKeyMap(SDLK_NUMLOCK, KEY_NUMLOCK));
KeyMap.push_back(SKeyMap(SDLK_SCROLLOCK, KEY_SCROLL));
KeyMap.push_back(SKeyMap(SDLK_NUMLOCKCLEAR, KEY_NUMLOCK));
KeyMap.push_back(SKeyMap(SDLK_SCROLLLOCK, KEY_SCROLL));
KeyMap.push_back(SKeyMap(SDLK_LSHIFT, KEY_LSHIFT));
KeyMap.push_back(SKeyMap(SDLK_RSHIFT, KEY_RSHIFT));
KeyMap.push_back(SKeyMap(SDLK_LCTRL, KEY_LCONTROL));

@ -20,8 +20,8 @@
#include <emscripten/html5.h>
#endif
#include <SDL/SDL.h>
#include <SDL/SDL_syswm.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
namespace irr
{
@ -66,9 +66,6 @@ namespace irr
//! notifies the device that it should close itself
virtual void closeDevice() _IRR_OVERRIDE_;
//! \return Returns a pointer to a list with all video modes supported
virtual video::IVideoModeList* getVideoModeList() _IRR_OVERRIDE_;
//! Sets if the window should be resizable in windowed mode.
virtual void setResizable(bool resize=false) _IRR_OVERRIDE_;
@ -103,6 +100,8 @@ namespace irr
return EIDT_SDL;
}
void SwapWindow();
//! Implementation of the linux cursor control
class CCursorControl : public gui::ICursorControl
{
@ -152,7 +151,7 @@ namespace irr
//! Sets the new position of the cursor.
virtual void setPosition(s32 x, s32 y) _IRR_OVERRIDE_
{
SDL_WarpMouse( x, y );
SDL_WarpMouseInWindow(Device->Window, x, y);
}
//! Returns the current position of the mouse cursor.
@ -224,9 +223,9 @@ namespace irr
static EM_BOOL MouseEnterCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
static EM_BOOL MouseLeaveCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
// Check if it's a special key like left, right, up down and so on which shouldn't have a Unicode character.
bool isNoUnicodeKey(EKEY_CODE key) const;
#endif
// Check if it's a special key like left, right, up down and so on which shouldn't have a Unicode character.
bool isNoUnicodeKey(EKEY_CODE key) const;
//! create the driver
void createDriver();
@ -236,8 +235,8 @@ namespace irr
void createKeyMap();
void logAttributes();
SDL_Surface* Screen;
SDL_GLContext Context;
SDL_Window *Window;
int SDL_Flags;
#if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
core::array<SDL_Joystick*> Joysticks;

@ -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}

@ -36,13 +36,15 @@
#elif defined(_IRR_COMPILE_WITH_SDL_DEVICE_) && !defined(_IRR_COMPILE_WITH_X11_DEVICE_)
#if defined(_IRR_OPENGL_USE_EXTPOINTER_)
#define GL_GLEXT_LEGACY 1
#define GLX_GLXEXT_LEGACY 1
#else
#define GL_GLEXT_PROTOTYPES 1
#define GLX_GLXEXT_PROTOTYPES 1
#endif
#include <SDL/SDL_video.h>
#include <SDL/SDL_opengl.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_opengl.h>
#if defined(_IRR_OPENGL_USE_EXTPOINTER_)
// The SDL2 header doesn't cut it for extensions
#include <GL/glext.h>
#endif
#else
#if defined(_IRR_OPENGL_USE_EXTPOINTER_)
#define GL_GLEXT_LEGACY 1

@ -21,7 +21,7 @@
#include "mt_opengl.h"
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
#include <SDL/SDL.h>
#include "CIrrDeviceSDL.h"
#endif
namespace irr
@ -268,7 +268,7 @@ bool COpenGLDriver::beginScene(u16 clearFlag, SColor clearColor, f32 clearDepth,
ContextManager->activateContext(videoData, true);
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
if ( DeviceType == EIDT_SDL )
if (SDLDevice)
glFrontFace(GL_CW);
#endif
@ -289,9 +289,9 @@ bool COpenGLDriver::endScene()
status = ContextManager->swapBuffers();
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
if ( DeviceType == EIDT_SDL )
if (SDLDevice)
{
SDL_GL_SwapBuffers();
SDLDevice->SwapWindow();
status = true;
}
#endif

@ -8,7 +8,7 @@
#include "irrMath.h"
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
#include <SDL/SDL_endian.h>
#include <SDL2/SDL_endian.h>
#define bswap_16(X) SDL_Swap16(X)
#define bswap_32(X) SDL_Swap32(X)
#define bswap_64(X) SDL_Swap64(X)