]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/CIrrDeviceSDL.cpp
Merge branch 'master' into opengl3
[irrlicht.git] / source / Irrlicht / CIrrDeviceSDL.cpp
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 \r
6 #ifdef _IRR_COMPILE_WITH_SDL_DEVICE_\r
7 \r
8 #include "CIrrDeviceSDL.h"\r
9 #include "IEventReceiver.h"\r
10 #include "IGUIElement.h"\r
11 #include "IGUIEnvironment.h"\r
12 #include "os.h"\r
13 #include "CTimer.h"\r
14 #include "irrString.h"\r
15 #include "Keycodes.h"\r
16 #include "COSOperator.h"\r
17 #include <stdio.h>\r
18 #include <stdlib.h>\r
19 #include "SIrrCreationParameters.h"\r
20 #include <SDL_video.h>\r
21 \r
22 #ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
23 #include <emscripten.h>\r
24 #endif\r
25 \r
26 #include "CSDLManager.h"\r
27 \r
28 static int SDLDeviceInstances = 0;\r
29 \r
30 namespace irr\r
31 {\r
32         namespace video\r
33         {\r
34                 #ifdef _IRR_COMPILE_WITH_OPENGL_\r
35                 IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);\r
36                 #else\r
37                 static IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager)\r
38                 {\r
39                         os::Printer::log("No OpenGL support compiled in.", ELL_ERROR);\r
40                         return nullptr;\r
41                 }\r
42                 #endif\r
43 \r
44                 #ifdef ENABLE_OPENGL3\r
45                 IVideoDriver* createOpenGL3Driver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);\r
46                 #else\r
47                 static IVideoDriver* createOpenGL3Driver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager)\r
48                 {\r
49                         os::Printer::log("No OpenGL 3 support compiled in.", ELL_ERROR);\r
50                         return nullptr;\r
51                 }\r
52                 #endif\r
53 \r
54                 #ifdef _IRR_COMPILE_WITH_OGLES2_\r
55                 IVideoDriver* createOGLES2Driver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);\r
56                 #else\r
57                 static IVideoDriver* createOGLES2Driver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager)\r
58                 {\r
59                         os::Printer::log("No OpenGL ES 2 support compiled in.", ELL_ERROR);\r
60                         return nullptr;\r
61                 }\r
62                 #endif\r
63 \r
64                 #ifdef _IRR_COMPILE_WITH_WEBGL1_\r
65                 IVideoDriver* createWebGL1Driver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);\r
66                 #else\r
67                 static IVideoDriver* createWebGL1Driver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager)\r
68                 {\r
69                         os::Printer::log("No WebGL 1 support compiled in.", ELL_ERROR);\r
70                         return nullptr;\r
71                 }\r
72                 #endif\r
73         } // end namespace video\r
74 \r
75 } // end namespace irr\r
76 \r
77 \r
78 namespace irr\r
79 {\r
80 #ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
81 EM_BOOL CIrrDeviceSDL::MouseUpDownCallback(int eventType, const EmscriptenMouseEvent * event, void* userData)\r
82 {\r
83         // We need this callback so far only because otherwise "emscripten_request_pointerlock" calls will\r
84         // fail as their request are infinitely deferred.\r
85         // Not exactly certain why, maybe SDL does catch those mouse-events otherwise and not pass them on.\r
86         return EM_FALSE;\r
87 }\r
88 \r
89 EM_BOOL CIrrDeviceSDL::MouseEnterCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)\r
90 {\r
91         CIrrDeviceSDL * This = static_cast<CIrrDeviceSDL*>(userData);\r
92 \r
93         SEvent irrevent;\r
94 \r
95         irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;\r
96         irrevent.MouseInput.Event = irr::EMIE_MOUSE_ENTER_CANVAS;\r
97         This->MouseX = irrevent.MouseInput.X = mouseEvent->canvasX;\r
98         This->MouseY = irrevent.MouseInput.Y = mouseEvent->canvasY;\r
99         This->MouseXRel = mouseEvent->movementX; // should be 0 I guess? Or can it enter while pointer is locked()?\r
100         This->MouseYRel = mouseEvent->movementY;\r
101         irrevent.MouseInput.ButtonStates = This->MouseButtonStates;     // TODO: not correct, but couldn't figure out the bitset of mouseEvent->buttons yet.\r
102         irrevent.MouseInput.Shift = mouseEvent->shiftKey;\r
103         irrevent.MouseInput.Control = mouseEvent->ctrlKey;\r
104 \r
105         This->postEventFromUser(irrevent);\r
106 \r
107         return EM_FALSE;\r
108 }\r
109 \r
110 EM_BOOL CIrrDeviceSDL::MouseLeaveCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)\r
111 {\r
112         CIrrDeviceSDL * This = static_cast<CIrrDeviceSDL*>(userData);\r
113 \r
114         SEvent irrevent;\r
115 \r
116         irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;\r
117         irrevent.MouseInput.Event = irr::EMIE_MOUSE_LEAVE_CANVAS;\r
118         This->MouseX = irrevent.MouseInput.X = mouseEvent->canvasX;\r
119         This->MouseY = irrevent.MouseInput.Y = mouseEvent->canvasY;\r
120         This->MouseXRel = mouseEvent->movementX; // should be 0 I guess? Or can it enter while pointer is locked()?\r
121         This->MouseYRel = mouseEvent->movementY;\r
122         irrevent.MouseInput.ButtonStates = This->MouseButtonStates;     // TODO: not correct, but couldn't figure out the bitset of mouseEvent->buttons yet.\r
123         irrevent.MouseInput.Shift = mouseEvent->shiftKey;\r
124         irrevent.MouseInput.Control = mouseEvent->ctrlKey;\r
125 \r
126         This->postEventFromUser(irrevent);\r
127 \r
128         return EM_FALSE;\r
129 }\r
130 #endif\r
131 \r
132 \r
133 bool CIrrDeviceSDL::keyIsKnownSpecial(EKEY_CODE key)\r
134 {\r
135         switch ( key )\r
136         {\r
137                 // keys which are known to have safe special character interpretation\r
138                 // could need changes over time (removals and additions!)\r
139                 case KEY_RETURN:\r
140                 case KEY_PAUSE:\r
141                 case KEY_ESCAPE:\r
142                 case KEY_PRIOR:\r
143                 case KEY_NEXT:\r
144                 case KEY_HOME:\r
145                 case KEY_END:\r
146                 case KEY_LEFT:\r
147                 case KEY_UP:\r
148                 case KEY_RIGHT:\r
149                 case KEY_DOWN:\r
150                 case KEY_TAB:\r
151                 case KEY_PRINT:\r
152                 case KEY_SNAPSHOT:\r
153                 case KEY_INSERT:\r
154                 case KEY_BACK:\r
155                 case KEY_DELETE:\r
156                 case KEY_HELP:\r
157                 case KEY_APPS:\r
158                 case KEY_SLEEP:\r
159                 case KEY_F1:\r
160                 case KEY_F2:\r
161                 case KEY_F3:\r
162                 case KEY_F4:\r
163                 case KEY_F5:\r
164                 case KEY_F6:\r
165                 case KEY_F7:\r
166                 case KEY_F8:\r
167                 case KEY_F9:\r
168                 case KEY_F10:\r
169                 case KEY_F11:\r
170                 case KEY_F12:\r
171                 case KEY_F13:\r
172                 case KEY_F14:\r
173                 case KEY_F15:\r
174                 case KEY_F16:\r
175                 case KEY_F17:\r
176                 case KEY_F18:\r
177                 case KEY_F19:\r
178                 case KEY_F20:\r
179                 case KEY_F21:\r
180                 case KEY_F22:\r
181                 case KEY_F23:\r
182                 case KEY_F24:\r
183                 case KEY_NUMLOCK:\r
184                 case KEY_SCROLL:\r
185                 case KEY_LCONTROL:\r
186                 case KEY_RCONTROL:\r
187                         return true;\r
188 \r
189                 default:\r
190                         return false;\r
191         }\r
192 }\r
193 \r
194 int CIrrDeviceSDL::findCharToPassToIrrlicht(int assumedChar, EKEY_CODE key) {\r
195         // SDL in-place ORs values with no character representation with 1<<30\r
196         // https://wiki.libsdl.org/SDL2/SDLKeycodeLookup\r
197         if (assumedChar & (1<<30))\r
198                 return 0;\r
199 \r
200         switch (key) {\r
201                 case KEY_PRIOR:\r
202                 case KEY_NEXT:\r
203                 case KEY_HOME:\r
204                 case KEY_END:\r
205                 case KEY_LEFT:\r
206                 case KEY_UP:\r
207                 case KEY_RIGHT:\r
208                 case KEY_DOWN:\r
209                 case KEY_NUMLOCK:\r
210                         return 0;\r
211                 default:\r
212                         return assumedChar;\r
213         }\r
214 }\r
215 \r
216 void CIrrDeviceSDL::resetReceiveTextInputEvents() {\r
217         gui::IGUIElement *elem = GUIEnvironment->getFocus();\r
218         if (elem && elem->acceptsIME())\r
219                 SDL_StartTextInput();\r
220         else\r
221                 SDL_StopTextInput();\r
222 }\r
223 \r
224 //! constructor\r
225 CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)\r
226         : CIrrDeviceStub(param),\r
227         Window((SDL_Window*)param.WindowId), SDL_Flags(0),\r
228         MouseX(0), MouseY(0), MouseXRel(0), MouseYRel(0), MouseButtonStates(0),\r
229         Width(param.WindowSize.Width), Height(param.WindowSize.Height),\r
230         Resizable(param.WindowResizable == 1 ? true : false)\r
231 {\r
232         #ifdef _DEBUG\r
233         setDebugName("CIrrDeviceSDL");\r
234         #endif\r
235 \r
236         if ( ++SDLDeviceInstances == 1 )\r
237         {\r
238                 u32 flags = SDL_INIT_TIMER | SDL_INIT_EVENTS;\r
239                 if (CreationParams.DriverType != video::EDT_NULL)\r
240                         flags |= SDL_INIT_VIDEO;\r
241 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)\r
242                 flags |= SDL_INIT_JOYSTICK;\r
243 #endif\r
244                 if (SDL_Init(flags) < 0)\r
245                 {\r
246                         os::Printer::log( "Unable to initialize SDL!", SDL_GetError());\r
247                         Close = true;\r
248                 }\r
249                 else\r
250                 {\r
251                         os::Printer::log("SDL initialized", ELL_INFORMATION);\r
252                 }\r
253         }\r
254 \r
255         // create keymap\r
256         createKeyMap();\r
257 \r
258         // create window\r
259         if (CreationParams.DriverType != video::EDT_NULL)\r
260         {\r
261                 // create the window, only if we do not use the null device\r
262                 createWindow();\r
263         }\r
264 \r
265 \r
266         SDL_VERSION(&Info.version);\r
267 \r
268 #ifndef _IRR_EMSCRIPTEN_PLATFORM_\r
269         SDL_GetWindowWMInfo(Window,&Info);\r
270 #endif //_IRR_EMSCRIPTEN_PLATFORM_\r
271         core::stringc sdlversion = "SDL Version ";\r
272         sdlversion += Info.version.major;\r
273         sdlversion += ".";\r
274         sdlversion += Info.version.minor;\r
275         sdlversion += ".";\r
276         sdlversion += Info.version.patch;\r
277 \r
278         Operator = new COSOperator(sdlversion);\r
279         if (SDLDeviceInstances == 1) {\r
280                 os::Printer::log(sdlversion.c_str(), ELL_INFORMATION);\r
281         }\r
282 \r
283         // create cursor control\r
284         CursorControl = new CCursorControl(this);\r
285 \r
286         // create driver\r
287         createDriver();\r
288 \r
289         if (VideoDriver) {\r
290                 createGUIAndScene();\r
291                 VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));\r
292         }\r
293 }\r
294 \r
295 \r
296 //! destructor\r
297 CIrrDeviceSDL::~CIrrDeviceSDL()\r
298 {\r
299         if ( --SDLDeviceInstances == 0 )\r
300         {\r
301 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)\r
302                 const u32 numJoysticks = Joysticks.size();\r
303                 for (u32 i=0; i<numJoysticks; ++i)\r
304                         SDL_JoystickClose(Joysticks[i]);\r
305 #endif\r
306                 if (Window)\r
307                 {\r
308                         SDL_GL_MakeCurrent(Window, NULL);\r
309                         SDL_GL_DeleteContext(Context);\r
310                         SDL_DestroyWindow(Window);\r
311                 }\r
312                 SDL_Quit();\r
313 \r
314                 os::Printer::log("Quit SDL", ELL_INFORMATION);\r
315         }\r
316 }\r
317 \r
318 void CIrrDeviceSDL::logAttributes()\r
319 {\r
320         core::stringc sdl_attr("SDL attribs:");\r
321         int value = 0;\r
322         if ( SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value ) == 0 )\r
323                 sdl_attr += core::stringc(" r:") + core::stringc(value);\r
324         if ( SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value ) == 0 )\r
325                 sdl_attr += core::stringc(" g:") + core::stringc(value);\r
326         if ( SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value ) == 0 )\r
327                 sdl_attr += core::stringc(" b:") + core::stringc(value);\r
328         if ( SDL_GL_GetAttribute( SDL_GL_ALPHA_SIZE, &value ) == 0 )\r
329                 sdl_attr += core::stringc(" a:") + core::stringc(value);\r
330 \r
331         if ( SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value) == 0 )\r
332                 sdl_attr += core::stringc(" depth:") + core::stringc(value);\r
333         if ( SDL_GL_GetAttribute( SDL_GL_STENCIL_SIZE, &value ) == 0 )\r
334                 sdl_attr += core::stringc(" stencil:") + core::stringc(value);\r
335         if ( SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value ) == 0 )\r
336                 sdl_attr += core::stringc(" doublebuf:") + core::stringc(value);\r
337         if ( SDL_GL_GetAttribute( SDL_GL_MULTISAMPLEBUFFERS, &value ) == 0 )\r
338                 sdl_attr += core::stringc(" aa:") + core::stringc(value);\r
339         if ( SDL_GL_GetAttribute( SDL_GL_MULTISAMPLESAMPLES, &value ) == 0 )\r
340                 sdl_attr += core::stringc(" aa-samples:") + core::stringc(value);\r
341 \r
342         os::Printer::log(sdl_attr.c_str());\r
343 }\r
344 \r
345 bool CIrrDeviceSDL::createWindow()\r
346 {\r
347         if (CreationParams.Fullscreen) {\r
348 #ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
349                 SDL_Flags |= SDL_WINDOW_FULLSCREEN;\r
350 #else\r
351                 SDL_Flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;\r
352 #endif\r
353         }\r
354         if (Resizable)\r
355                 SDL_Flags |= SDL_WINDOW_RESIZABLE;\r
356         if (CreationParams.WindowMaximized)\r
357                 SDL_Flags |= SDL_WINDOW_MAXIMIZED;\r
358         SDL_Flags |= SDL_WINDOW_OPENGL;\r
359 \r
360 #ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
361         if ( Width != 0 || Height != 0 )\r
362                 emscripten_set_canvas_size( Width, Height);\r
363         else\r
364         {\r
365                 int w, h, fs;\r
366                 emscripten_get_canvas_size(&w, &h, &fs);\r
367                 Width = w;\r
368                 Height = h;\r
369         }\r
370 \r
371         SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );\r
372         SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );\r
373         SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );\r
374         SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, CreationParams.WithAlphaChannel?8:0 );\r
375 \r
376         SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, CreationParams.ZBufferBits);\r
377         SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, CreationParams.Stencilbuffer ? 8 : 0);\r
378         SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, CreationParams.Doublebuffer ? 1 : 0);\r
379 \r
380         if (CreationParams.AntiAlias>1)\r
381         {\r
382                 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );\r
383                 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, CreationParams.AntiAlias );\r
384         }\r
385         else\r
386         {\r
387                 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);\r
388                 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);\r
389         }\r
390 \r
391         SDL_CreateWindowAndRenderer(0, 0, SDL_Flags, &Window, &Renderer); // 0,0 will use the canvas size\r
392 \r
393         logAttributes();\r
394 \r
395         // "#canvas" is for the opengl context\r
396         emscripten_set_mousedown_callback("#canvas", (void*)this, true, MouseUpDownCallback);\r
397     emscripten_set_mouseup_callback("#canvas", (void*)this, true, MouseUpDownCallback);\r
398     emscripten_set_mouseenter_callback("#canvas", (void*)this, false, MouseEnterCallback);\r
399     emscripten_set_mouseleave_callback("#canvas", (void*)this, false, MouseLeaveCallback);\r
400 \r
401         return true;\r
402 #else // !_IRR_EMSCRIPTEN_PLATFORM_\r
403         if ( Close )\r
404                 return false;\r
405 \r
406         switch (CreationParams.DriverType) {\r
407                 case video::EDT_OPENGL:\r
408                         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);\r
409                         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);\r
410                         break;\r
411                 case video::EDT_OPENGL3:\r
412                         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);\r
413                         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);\r
414                         SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);\r
415                         break;\r
416                 case video::EDT_OGLES1:\r
417                         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);\r
418                         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);\r
419                         SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);\r
420                         break;\r
421                 case video::EDT_OGLES2:\r
422                 case video::EDT_WEBGL1:\r
423                         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);\r
424                         SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);\r
425                         SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);\r
426                         break;\r
427                 default:;\r
428         }\r
429 \r
430 #ifdef _DEBUG\r
431         SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG | SDL_GL_CONTEXT_ROBUST_ACCESS_FLAG);\r
432 #endif\r
433 \r
434         if (CreationParams.Bits == 16) {\r
435                 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);\r
436                 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);\r
437                 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);\r
438                 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, CreationParams.WithAlphaChannel ? 1 : 0);\r
439         } else {\r
440                 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);\r
441                 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);\r
442                 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);\r
443                 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, CreationParams.WithAlphaChannel ? 8 : 0);\r
444         }\r
445         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, CreationParams.ZBufferBits);\r
446         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, CreationParams.Doublebuffer);\r
447         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, CreationParams.Stencilbuffer ? 8 : 0);\r
448         SDL_GL_SetAttribute(SDL_GL_STEREO, CreationParams.Stereobuffer);\r
449         if (CreationParams.AntiAlias > 1) {\r
450                 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);\r
451                 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, CreationParams.AntiAlias);\r
452         }\r
453         if (!Window)\r
454                 Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);\r
455         if (!Window) {\r
456                 os::Printer::log("Could not create window...", SDL_GetError(), ELL_WARNING);\r
457         }\r
458         if (!Window && CreationParams.AntiAlias > 1) {\r
459                 while (--CreationParams.AntiAlias > 1) {\r
460                         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, CreationParams.AntiAlias);\r
461                         Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);\r
462                         if (Window)\r
463                                 break;\r
464                 }\r
465                 if (!Window) {\r
466                         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);\r
467                         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);\r
468                         Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);\r
469                         if (Window)\r
470                                 os::Printer::log("AntiAliasing disabled due to lack of support!", ELL_WARNING);\r
471                 }\r
472         }\r
473 \r
474         if ( !Window && CreationParams.Doublebuffer)\r
475         {\r
476                 // Try single buffer\r
477                 if (CreationParams.DriverType == video::EDT_OPENGL)\r
478                         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);\r
479                 Window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_Flags);\r
480         }\r
481         if ( !Window )\r
482         {\r
483                 os::Printer::log("Could not initialize display", SDL_GetError(), ELL_ERROR);\r
484                 return false;\r
485         }\r
486 \r
487         Context = SDL_GL_CreateContext(Window);\r
488         if (!Context) {\r
489                 os::Printer::log("Could not initialize context", SDL_GetError(), ELL_ERROR);\r
490                 SDL_DestroyWindow(Window);\r
491                 return false;\r
492         }\r
493 \r
494         return true;\r
495 #endif // !_IRR_EMSCRIPTEN_PLATFORM_\r
496 }\r
497 \r
498 \r
499 //! create the driver\r
500 void CIrrDeviceSDL::createDriver()\r
501 {\r
502         if (CreationParams.DriverType == video::EDT_NULL) {\r
503                 VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);\r
504                 return;\r
505         }\r
506 \r
507         ContextManager = new video::CSDLManager(this);\r
508         switch(CreationParams.DriverType)\r
509         {\r
510         case video::EDT_OPENGL: VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem, ContextManager); break;\r
511         case video::EDT_OPENGL3: VideoDriver = video::createOpenGL3Driver(CreationParams, FileSystem, ContextManager); break;\r
512         case video::EDT_OGLES2: VideoDriver = video::createOGLES2Driver(CreationParams, FileSystem, ContextManager); break;\r
513         case video::EDT_WEBGL1: VideoDriver = video::createWebGL1Driver(CreationParams, FileSystem, ContextManager); break;\r
514         default:;\r
515         }\r
516         if (!VideoDriver)\r
517                 os::Printer::log("Could not create video driver", ELL_ERROR);\r
518 }\r
519 \r
520 \r
521 //! runs the device. Returns false if device wants to be deleted\r
522 bool CIrrDeviceSDL::run()\r
523 {\r
524         os::Timer::tick();\r
525 \r
526         SEvent irrevent;\r
527         SDL_Event SDL_event;\r
528 \r
529         while ( !Close && SDL_PollEvent( &SDL_event ) )\r
530         {\r
531                 // os::Printer::log("event: ", core::stringc((int)SDL_event.type).c_str(),   ELL_INFORMATION);  // just for debugging\r
532 \r
533                 switch ( SDL_event.type )\r
534                 {\r
535                 case SDL_MOUSEMOTION: {\r
536                         SDL_Keymod keymod = SDL_GetModState();\r
537 \r
538                         irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;\r
539                         irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;\r
540                         MouseX = irrevent.MouseInput.X = SDL_event.motion.x;\r
541                         MouseY = irrevent.MouseInput.Y = SDL_event.motion.y;\r
542                         MouseXRel = SDL_event.motion.xrel;\r
543                         MouseYRel = SDL_event.motion.yrel;\r
544                         irrevent.MouseInput.ButtonStates = MouseButtonStates;\r
545                         irrevent.MouseInput.Shift = (keymod & KMOD_SHIFT) != 0;\r
546                         irrevent.MouseInput.Control = (keymod & KMOD_CTRL) != 0;\r
547 \r
548                         postEventFromUser(irrevent);\r
549                         break;\r
550                 }\r
551                 case SDL_MOUSEWHEEL: {\r
552                         SDL_Keymod keymod = SDL_GetModState();\r
553 \r
554                         irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;\r
555                         irrevent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;\r
556                         irrevent.MouseInput.Wheel = static_cast<float>(SDL_event.wheel.y);\r
557                         irrevent.MouseInput.Shift = (keymod & KMOD_SHIFT) != 0;\r
558                         irrevent.MouseInput.Control = (keymod & KMOD_CTRL) != 0;\r
559                         irrevent.MouseInput.X = MouseX;\r
560                         irrevent.MouseInput.Y = MouseY;\r
561 \r
562                         postEventFromUser(irrevent);\r
563                         break;\r
564                 }\r
565                 case SDL_MOUSEBUTTONDOWN:\r
566                 case SDL_MOUSEBUTTONUP: {\r
567                         SDL_Keymod keymod = SDL_GetModState();\r
568 \r
569                         irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;\r
570                         irrevent.MouseInput.X = SDL_event.button.x;\r
571                         irrevent.MouseInput.Y = SDL_event.button.y;\r
572                         irrevent.MouseInput.Shift = (keymod & KMOD_SHIFT) != 0;\r
573                         irrevent.MouseInput.Control = (keymod & KMOD_CTRL) != 0;\r
574 \r
575                         irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;\r
576 \r
577 \r
578 #ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
579                         // Handle mouselocking in emscripten in Windowed mode.\r
580                         // In fullscreen SDL will handle it.\r
581                         // The behavior we want windowed is - when the canvas was clicked then\r
582                         // we will lock the mouse-pointer if it should be invisible.\r
583                         // For security reasons this will be delayed until the next mouse-up event.\r
584                         // We do not pass on this event as we don't want the activation click to do anything.\r
585                         if ( SDL_event.type == SDL_MOUSEBUTTONDOWN && !isFullscreen() )\r
586                         {\r
587                                 EmscriptenPointerlockChangeEvent pointerlockStatus; // let's hope that test is not expensive ...\r
588                                 if ( emscripten_get_pointerlock_status(&pointerlockStatus) == EMSCRIPTEN_RESULT_SUCCESS )\r
589                                 {\r
590                                         if ( CursorControl->isVisible() && pointerlockStatus.isActive )\r
591                                         {\r
592                                                 emscripten_exit_pointerlock();\r
593                                                 return !Close;\r
594                                         }\r
595                                         else if ( !CursorControl->isVisible() && !pointerlockStatus.isActive )\r
596                                         {\r
597                                                 emscripten_request_pointerlock(0, true);\r
598                                                 return !Close;\r
599                                         }\r
600                                 }\r
601                         }\r
602 #endif\r
603 \r
604                         switch(SDL_event.button.button)\r
605                         {\r
606                         case SDL_BUTTON_LEFT:\r
607                                 if (SDL_event.type == SDL_MOUSEBUTTONDOWN)\r
608                                 {\r
609                                         irrevent.MouseInput.Event = irr::EMIE_LMOUSE_PRESSED_DOWN;\r
610                                         MouseButtonStates |= irr::EMBSM_LEFT;\r
611                                 }\r
612                                 else\r
613                                 {\r
614                                         irrevent.MouseInput.Event = irr::EMIE_LMOUSE_LEFT_UP;\r
615                                         MouseButtonStates &= ~irr::EMBSM_LEFT;\r
616                                 }\r
617                                 break;\r
618 \r
619                         case SDL_BUTTON_RIGHT:\r
620                                 if (SDL_event.type == SDL_MOUSEBUTTONDOWN)\r
621                                 {\r
622                                         irrevent.MouseInput.Event = irr::EMIE_RMOUSE_PRESSED_DOWN;\r
623                                         MouseButtonStates |= irr::EMBSM_RIGHT;\r
624                                 }\r
625                                 else\r
626                                 {\r
627                                         irrevent.MouseInput.Event = irr::EMIE_RMOUSE_LEFT_UP;\r
628                                         MouseButtonStates &= ~irr::EMBSM_RIGHT;\r
629                                 }\r
630                                 break;\r
631 \r
632                         case SDL_BUTTON_MIDDLE:\r
633                                 if (SDL_event.type == SDL_MOUSEBUTTONDOWN)\r
634                                 {\r
635                                         irrevent.MouseInput.Event = irr::EMIE_MMOUSE_PRESSED_DOWN;\r
636                                         MouseButtonStates |= irr::EMBSM_MIDDLE;\r
637                                 }\r
638                                 else\r
639                                 {\r
640                                         irrevent.MouseInput.Event = irr::EMIE_MMOUSE_LEFT_UP;\r
641                                         MouseButtonStates &= ~irr::EMBSM_MIDDLE;\r
642                                 }\r
643                                 break;\r
644                         }\r
645 \r
646                         irrevent.MouseInput.ButtonStates = MouseButtonStates;\r
647 \r
648                         if (irrevent.MouseInput.Event != irr::EMIE_MOUSE_MOVED)\r
649                         {\r
650                                 postEventFromUser(irrevent);\r
651 \r
652                                 if ( irrevent.MouseInput.Event >= EMIE_LMOUSE_PRESSED_DOWN && irrevent.MouseInput.Event <= EMIE_MMOUSE_PRESSED_DOWN )\r
653                                 {\r
654                                         u32 clicks = checkSuccessiveClicks(irrevent.MouseInput.X, irrevent.MouseInput.Y, irrevent.MouseInput.Event);\r
655                                         if ( clicks == 2 )\r
656                                         {\r
657                                                 irrevent.MouseInput.Event = (EMOUSE_INPUT_EVENT)(EMIE_LMOUSE_DOUBLE_CLICK + irrevent.MouseInput.Event-EMIE_LMOUSE_PRESSED_DOWN);\r
658                                                 postEventFromUser(irrevent);\r
659                                         }\r
660                                         else if ( clicks == 3 )\r
661                                         {\r
662                                                 irrevent.MouseInput.Event = (EMOUSE_INPUT_EVENT)(EMIE_LMOUSE_TRIPLE_CLICK + irrevent.MouseInput.Event-EMIE_LMOUSE_PRESSED_DOWN);\r
663                                                 postEventFromUser(irrevent);\r
664                                         }\r
665                                 }\r
666                         }\r
667                         break;\r
668                 }\r
669 \r
670                 case SDL_TEXTINPUT:\r
671                         {\r
672                                 irrevent.EventType = irr::EET_STRING_INPUT_EVENT;\r
673                                 irrevent.StringInput.Str = new core::stringw();\r
674                                 irr::core::multibyteToWString(*irrevent.StringInput.Str, SDL_event.text.text);\r
675                                 postEventFromUser(irrevent);\r
676                                 delete irrevent.StringInput.Str;\r
677                                 irrevent.StringInput.Str = NULL;\r
678                         }\r
679                         break;\r
680 \r
681                 case SDL_KEYDOWN:\r
682                 case SDL_KEYUP:\r
683                         {\r
684                                 SKeyMap mp;\r
685                                 mp.SDLKey = SDL_event.key.keysym.sym;\r
686                                 s32 idx = KeyMap.binary_search(mp);\r
687 \r
688                                 EKEY_CODE key;\r
689                                 if (idx == -1)\r
690                                         key = (EKEY_CODE)0;\r
691                                 else\r
692                                         key = (EKEY_CODE)KeyMap[idx].Win32Key;\r
693 \r
694                                 // Make sure to only input special characters if something is in focus, as SDL_TEXTINPUT handles normal unicode already\r
695                                 if (SDL_IsTextInputActive() && !keyIsKnownSpecial(key) && (SDL_event.key.keysym.mod & KMOD_CTRL) == 0)\r
696                                         break;\r
697 \r
698 #ifdef _IRR_WINDOWS_API_\r
699                                 // handle alt+f4 in Windows, because SDL seems not to\r
700                                 if ( (SDL_event.key.keysym.mod & KMOD_LALT) && key == KEY_F4)\r
701                                 {\r
702                                         Close = true;\r
703                                         break;\r
704                                 }\r
705 #endif\r
706                                 irrevent.EventType = irr::EET_KEY_INPUT_EVENT;\r
707                                 irrevent.KeyInput.Key = key;\r
708                                 irrevent.KeyInput.PressedDown = (SDL_event.type == SDL_KEYDOWN);\r
709                                 irrevent.KeyInput.Shift = (SDL_event.key.keysym.mod & KMOD_SHIFT) != 0;\r
710                                 irrevent.KeyInput.Control = (SDL_event.key.keysym.mod & KMOD_CTRL ) != 0;\r
711                                 irrevent.KeyInput.Char = findCharToPassToIrrlicht(mp.SDLKey, key);\r
712                                 postEventFromUser(irrevent);\r
713                         }\r
714                         break;\r
715 \r
716                 case SDL_QUIT:\r
717                         Close = true;\r
718                         break;\r
719 \r
720                 case SDL_WINDOWEVENT:\r
721                         switch (SDL_event.window.event)\r
722                         {\r
723                         case SDL_WINDOWEVENT_RESIZED:\r
724                                 if ((SDL_event.window.data1 != (int)Width) || (SDL_event.window.data2 != (int)Height))\r
725                                 {\r
726                                         Width = SDL_event.window.data1;\r
727                                         Height = SDL_event.window.data2;\r
728                                         if (VideoDriver)\r
729                                                 VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));\r
730                                 }\r
731                                 break;\r
732                         }\r
733 \r
734                 case SDL_USEREVENT:\r
735                         irrevent.EventType = irr::EET_USER_EVENT;\r
736                         irrevent.UserEvent.UserData1 = reinterpret_cast<uintptr_t>(SDL_event.user.data1);\r
737                         irrevent.UserEvent.UserData2 = reinterpret_cast<uintptr_t>(SDL_event.user.data2);\r
738 \r
739                         postEventFromUser(irrevent);\r
740                         break;\r
741 \r
742                 default:\r
743                         break;\r
744                 } // end switch\r
745         resetReceiveTextInputEvents();\r
746         } // end while\r
747 \r
748 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)\r
749         // TODO: Check if the multiple open/close calls are too expensive, then\r
750         // open/close in the constructor/destructor instead\r
751 \r
752         // update joystick states manually\r
753         SDL_JoystickUpdate();\r
754         // we'll always send joystick input events...\r
755         SEvent joyevent;\r
756         joyevent.EventType = EET_JOYSTICK_INPUT_EVENT;\r
757         for (u32 i=0; i<Joysticks.size(); ++i)\r
758         {\r
759                 SDL_Joystick* joystick = Joysticks[i];\r
760                 if (joystick)\r
761                 {\r
762                         int j;\r
763                         // query all buttons\r
764                         const int numButtons = core::min_(SDL_JoystickNumButtons(joystick), 32);\r
765                         joyevent.JoystickEvent.ButtonStates=0;\r
766                         for (j=0; j<numButtons; ++j)\r
767                                 joyevent.JoystickEvent.ButtonStates |= (SDL_JoystickGetButton(joystick, j)<<j);\r
768 \r
769                         // query all axes, already in correct range\r
770                         const int numAxes = core::min_(SDL_JoystickNumAxes(joystick), (int)SEvent::SJoystickEvent::NUMBER_OF_AXES);\r
771                         joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_X]=0;\r
772                         joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Y]=0;\r
773                         joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Z]=0;\r
774                         joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_R]=0;\r
775                         joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_U]=0;\r
776                         joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_V]=0;\r
777                         for (j=0; j<numAxes; ++j)\r
778                                 joyevent.JoystickEvent.Axis[j] = SDL_JoystickGetAxis(joystick, j);\r
779 \r
780                         // we can only query one hat, SDL only supports 8 directions\r
781                         if (SDL_JoystickNumHats(joystick)>0)\r
782                         {\r
783                                 switch (SDL_JoystickGetHat(joystick, 0))\r
784                                 {\r
785                                         case SDL_HAT_UP:\r
786                                                 joyevent.JoystickEvent.POV=0;\r
787                                                 break;\r
788                                         case SDL_HAT_RIGHTUP:\r
789                                                 joyevent.JoystickEvent.POV=4500;\r
790                                                 break;\r
791                                         case SDL_HAT_RIGHT:\r
792                                                 joyevent.JoystickEvent.POV=9000;\r
793                                                 break;\r
794                                         case SDL_HAT_RIGHTDOWN:\r
795                                                 joyevent.JoystickEvent.POV=13500;\r
796                                                 break;\r
797                                         case SDL_HAT_DOWN:\r
798                                                 joyevent.JoystickEvent.POV=18000;\r
799                                                 break;\r
800                                         case SDL_HAT_LEFTDOWN:\r
801                                                 joyevent.JoystickEvent.POV=22500;\r
802                                                 break;\r
803                                         case SDL_HAT_LEFT:\r
804                                                 joyevent.JoystickEvent.POV=27000;\r
805                                                 break;\r
806                                         case SDL_HAT_LEFTUP:\r
807                                                 joyevent.JoystickEvent.POV=31500;\r
808                                                 break;\r
809                                         case SDL_HAT_CENTERED:\r
810                                         default:\r
811                                                 joyevent.JoystickEvent.POV=65535;\r
812                                                 break;\r
813                                 }\r
814                         }\r
815                         else\r
816                         {\r
817                                 joyevent.JoystickEvent.POV=65535;\r
818                         }\r
819 \r
820                         // we map the number directly\r
821                         joyevent.JoystickEvent.Joystick=static_cast<u8>(i);\r
822                         // now post the event\r
823                         postEventFromUser(joyevent);\r
824                         // and close the joystick\r
825                 }\r
826         }\r
827 #endif\r
828         return !Close;\r
829 }\r
830 \r
831 //! Activate any joysticks, and generate events for them.\r
832 bool CIrrDeviceSDL::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)\r
833 {\r
834 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)\r
835         joystickInfo.clear();\r
836 \r
837         // we can name up to 256 different joysticks\r
838         const int numJoysticks = core::min_(SDL_NumJoysticks(), 256);\r
839         Joysticks.reallocate(numJoysticks);\r
840         joystickInfo.reallocate(numJoysticks);\r
841 \r
842         int joystick = 0;\r
843         for (; joystick<numJoysticks; ++joystick)\r
844         {\r
845                 Joysticks.push_back( SDL_JoystickOpen(joystick));\r
846                 SJoystickInfo info;\r
847 \r
848                 info.Joystick = joystick;\r
849                 info.Axes = SDL_JoystickNumAxes(Joysticks[joystick]);\r
850                 info.Buttons = SDL_JoystickNumButtons(Joysticks[joystick]);\r
851                 info.Name = SDL_JoystickName(Joysticks[joystick]);\r
852                 info.PovHat = (SDL_JoystickNumHats(Joysticks[joystick]) > 0)\r
853                                                 ? SJoystickInfo::POV_HAT_PRESENT : SJoystickInfo::POV_HAT_ABSENT;\r
854 \r
855                 joystickInfo.push_back(info);\r
856         }\r
857 \r
858         for(joystick = 0; joystick < (int)joystickInfo.size(); ++joystick)\r
859         {\r
860                 char logString[256];\r
861                 snprintf_irr(logString, sizeof(logString), "Found joystick %d, %d axes, %d buttons '%s'",\r
862                         joystick, joystickInfo[joystick].Axes,\r
863                         joystickInfo[joystick].Buttons, joystickInfo[joystick].Name.c_str());\r
864                 os::Printer::log(logString, ELL_INFORMATION);\r
865         }\r
866 \r
867         return true;\r
868 \r
869 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_\r
870 \r
871         return false;\r
872 }\r
873 \r
874 void CIrrDeviceSDL::SwapWindow()\r
875 {\r
876         SDL_GL_SwapWindow(Window);\r
877 }\r
878 \r
879 \r
880 \r
881 //! pause execution temporarily\r
882 void CIrrDeviceSDL::yield()\r
883 {\r
884         SDL_Delay(0);\r
885 }\r
886 \r
887 \r
888 //! pause execution for a specified time\r
889 void CIrrDeviceSDL::sleep(u32 timeMs, bool pauseTimer)\r
890 {\r
891         const bool wasStopped = Timer ? Timer->isStopped() : true;\r
892         if (pauseTimer && !wasStopped)\r
893                 Timer->stop();\r
894 \r
895         SDL_Delay(timeMs);\r
896 \r
897         if (pauseTimer && !wasStopped)\r
898                 Timer->start();\r
899 }\r
900 \r
901 \r
902 //! sets the caption of the window\r
903 void CIrrDeviceSDL::setWindowCaption(const wchar_t* text)\r
904 {\r
905         core::stringc textc;\r
906         core::wStringToMultibyte(textc, text);\r
907         SDL_SetWindowTitle(Window, textc.c_str());\r
908 }\r
909 \r
910 \r
911 //! notifies the device that it should close itself\r
912 void CIrrDeviceSDL::closeDevice()\r
913 {\r
914         Close = true;\r
915 }\r
916 \r
917 \r
918 //! Sets if the window should be resizable in windowed mode.\r
919 void CIrrDeviceSDL::setResizable(bool resize)\r
920 {\r
921 #ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
922         os::Printer::log("Resizable not available on the web." , ELL_WARNING);\r
923         return;\r
924 #else // !_IRR_EMSCRIPTEN_PLATFORM_\r
925         if (resize != Resizable) {\r
926                 if (resize)\r
927                         SDL_Flags |= SDL_WINDOW_RESIZABLE;\r
928                 else\r
929                         SDL_Flags &= ~SDL_WINDOW_RESIZABLE;\r
930 \r
931                 if (Window) {\r
932                         SDL_SetWindowResizable(Window, (SDL_bool)resize);\r
933                 }\r
934                 Resizable = resize;\r
935         }\r
936 #endif // !_IRR_EMSCRIPTEN_PLATFORM_\r
937 }\r
938 \r
939 \r
940 //! Minimizes window if possible\r
941 void CIrrDeviceSDL::minimizeWindow()\r
942 {\r
943         if (Window)\r
944                 SDL_MinimizeWindow(Window);\r
945 }\r
946 \r
947 \r
948 //! Maximize window\r
949 void CIrrDeviceSDL::maximizeWindow()\r
950 {\r
951         if (Window)\r
952                 SDL_MaximizeWindow(Window);\r
953 }\r
954 \r
955 //! Get the position of this window on screen\r
956 core::position2di CIrrDeviceSDL::getWindowPosition()\r
957 {\r
958     return core::position2di(-1, -1);\r
959 }\r
960 \r
961 \r
962 //! Restore original window size\r
963 void CIrrDeviceSDL::restoreWindow()\r
964 {\r
965         if (Window)\r
966                 SDL_RestoreWindow(Window);\r
967 }\r
968 \r
969 bool CIrrDeviceSDL::isWindowMaximized() const\r
970 {\r
971         return Window && (SDL_GetWindowFlags(Window) & SDL_WINDOW_MAXIMIZED) != 0;\r
972 }\r
973 \r
974 bool CIrrDeviceSDL::isFullscreen() const\r
975 {\r
976 #ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
977         return SDL_GetWindowFlags(0) == SDL_WINDOW_FULLSCREEN;\r
978 #else\r
979 \r
980         return CIrrDeviceStub::isFullscreen();\r
981 #endif\r
982 }\r
983 \r
984 \r
985 //! returns if window is active. if not, nothing need to be drawn\r
986 bool CIrrDeviceSDL::isWindowActive() const\r
987 {\r
988 #ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
989         // Hidden test only does something in some browsers (when tab in background or window is minimized)\r
990         // In other browsers code automatically doesn't seem to be called anymore.\r
991         EmscriptenVisibilityChangeEvent emVisibility;\r
992         if ( emscripten_get_visibility_status(&emVisibility) == EMSCRIPTEN_RESULT_SUCCESS)\r
993         {\r
994                 if ( emVisibility.hidden )\r
995                         return false;\r
996         }\r
997 #endif\r
998         const u32 windowFlags = SDL_GetWindowFlags(Window);\r
999         return windowFlags & SDL_WINDOW_SHOWN && windowFlags & SDL_WINDOW_INPUT_FOCUS;\r
1000 }\r
1001 \r
1002 \r
1003 //! returns if window has focus.\r
1004 bool CIrrDeviceSDL::isWindowFocused() const\r
1005 {\r
1006         return Window && (SDL_GetWindowFlags(Window) & SDL_WINDOW_INPUT_FOCUS) != 0;\r
1007 }\r
1008 \r
1009 \r
1010 //! returns if window is minimized.\r
1011 bool CIrrDeviceSDL::isWindowMinimized() const\r
1012 {\r
1013         return Window && (SDL_GetWindowFlags(Window) & SDL_WINDOW_MINIMIZED) != 0;\r
1014 }\r
1015 \r
1016 \r
1017 //! returns color format of the window.\r
1018 video::ECOLOR_FORMAT CIrrDeviceSDL::getColorFormat() const\r
1019 {\r
1020         if (Window)\r
1021         {\r
1022                 SDL_Surface *surface = SDL_GetWindowSurface(Window);\r
1023                 if (surface->format->BitsPerPixel == 16)\r
1024                 {\r
1025                         if (surface->format->Amask != 0)\r
1026                                 return video::ECF_A1R5G5B5;\r
1027                         else\r
1028                                 return video::ECF_R5G6B5;\r
1029                 }\r
1030                 else\r
1031                 {\r
1032                         if (surface->format->Amask != 0)\r
1033                                 return video::ECF_A8R8G8B8;\r
1034                         else\r
1035                                 return video::ECF_R8G8B8;\r
1036                 }\r
1037         }\r
1038         else\r
1039                 return CIrrDeviceStub::getColorFormat();\r
1040 }\r
1041 \r
1042 \r
1043 void CIrrDeviceSDL::createKeyMap()\r
1044 {\r
1045         // I don't know if this is the best method  to create\r
1046         // the lookuptable, but I'll leave it like that until\r
1047         // I find a better version.\r
1048 \r
1049         KeyMap.reallocate(105);\r
1050 \r
1051         // buttons missing\r
1052 \r
1053         KeyMap.push_back(SKeyMap(SDLK_BACKSPACE, KEY_BACK));\r
1054         KeyMap.push_back(SKeyMap(SDLK_TAB, KEY_TAB));\r
1055         KeyMap.push_back(SKeyMap(SDLK_CLEAR, KEY_CLEAR));\r
1056         KeyMap.push_back(SKeyMap(SDLK_RETURN, KEY_RETURN));\r
1057 \r
1058         // combined modifiers missing\r
1059 \r
1060         KeyMap.push_back(SKeyMap(SDLK_PAUSE, KEY_PAUSE));\r
1061         KeyMap.push_back(SKeyMap(SDLK_CAPSLOCK, KEY_CAPITAL));\r
1062 \r
1063         // asian letter keys missing\r
1064 \r
1065         KeyMap.push_back(SKeyMap(SDLK_ESCAPE, KEY_ESCAPE));\r
1066 \r
1067         // asian letter keys missing\r
1068 \r
1069         KeyMap.push_back(SKeyMap(SDLK_SPACE, KEY_SPACE));\r
1070         KeyMap.push_back(SKeyMap(SDLK_PAGEUP, KEY_PRIOR));\r
1071         KeyMap.push_back(SKeyMap(SDLK_PAGEDOWN, KEY_NEXT));\r
1072         KeyMap.push_back(SKeyMap(SDLK_END, KEY_END));\r
1073         KeyMap.push_back(SKeyMap(SDLK_HOME, KEY_HOME));\r
1074         KeyMap.push_back(SKeyMap(SDLK_LEFT, KEY_LEFT));\r
1075         KeyMap.push_back(SKeyMap(SDLK_UP, KEY_UP));\r
1076         KeyMap.push_back(SKeyMap(SDLK_RIGHT, KEY_RIGHT));\r
1077         KeyMap.push_back(SKeyMap(SDLK_DOWN, KEY_DOWN));\r
1078 \r
1079         // select missing\r
1080         KeyMap.push_back(SKeyMap(SDLK_PRINTSCREEN, KEY_PRINT));\r
1081         // execute missing\r
1082         KeyMap.push_back(SKeyMap(SDLK_PRINTSCREEN, KEY_SNAPSHOT));\r
1083 \r
1084         KeyMap.push_back(SKeyMap(SDLK_INSERT, KEY_INSERT));\r
1085         KeyMap.push_back(SKeyMap(SDLK_DELETE, KEY_DELETE));\r
1086         KeyMap.push_back(SKeyMap(SDLK_HELP, KEY_HELP));\r
1087 \r
1088         KeyMap.push_back(SKeyMap(SDLK_0, KEY_KEY_0));\r
1089         KeyMap.push_back(SKeyMap(SDLK_1, KEY_KEY_1));\r
1090         KeyMap.push_back(SKeyMap(SDLK_2, KEY_KEY_2));\r
1091         KeyMap.push_back(SKeyMap(SDLK_3, KEY_KEY_3));\r
1092         KeyMap.push_back(SKeyMap(SDLK_4, KEY_KEY_4));\r
1093         KeyMap.push_back(SKeyMap(SDLK_5, KEY_KEY_5));\r
1094         KeyMap.push_back(SKeyMap(SDLK_6, KEY_KEY_6));\r
1095         KeyMap.push_back(SKeyMap(SDLK_7, KEY_KEY_7));\r
1096         KeyMap.push_back(SKeyMap(SDLK_8, KEY_KEY_8));\r
1097         KeyMap.push_back(SKeyMap(SDLK_9, KEY_KEY_9));\r
1098 \r
1099         KeyMap.push_back(SKeyMap(SDLK_a, KEY_KEY_A));\r
1100         KeyMap.push_back(SKeyMap(SDLK_b, KEY_KEY_B));\r
1101         KeyMap.push_back(SKeyMap(SDLK_c, KEY_KEY_C));\r
1102         KeyMap.push_back(SKeyMap(SDLK_d, KEY_KEY_D));\r
1103         KeyMap.push_back(SKeyMap(SDLK_e, KEY_KEY_E));\r
1104         KeyMap.push_back(SKeyMap(SDLK_f, KEY_KEY_F));\r
1105         KeyMap.push_back(SKeyMap(SDLK_g, KEY_KEY_G));\r
1106         KeyMap.push_back(SKeyMap(SDLK_h, KEY_KEY_H));\r
1107         KeyMap.push_back(SKeyMap(SDLK_i, KEY_KEY_I));\r
1108         KeyMap.push_back(SKeyMap(SDLK_j, KEY_KEY_J));\r
1109         KeyMap.push_back(SKeyMap(SDLK_k, KEY_KEY_K));\r
1110         KeyMap.push_back(SKeyMap(SDLK_l, KEY_KEY_L));\r
1111         KeyMap.push_back(SKeyMap(SDLK_m, KEY_KEY_M));\r
1112         KeyMap.push_back(SKeyMap(SDLK_n, KEY_KEY_N));\r
1113         KeyMap.push_back(SKeyMap(SDLK_o, KEY_KEY_O));\r
1114         KeyMap.push_back(SKeyMap(SDLK_p, KEY_KEY_P));\r
1115         KeyMap.push_back(SKeyMap(SDLK_q, KEY_KEY_Q));\r
1116         KeyMap.push_back(SKeyMap(SDLK_r, KEY_KEY_R));\r
1117         KeyMap.push_back(SKeyMap(SDLK_s, KEY_KEY_S));\r
1118         KeyMap.push_back(SKeyMap(SDLK_t, KEY_KEY_T));\r
1119         KeyMap.push_back(SKeyMap(SDLK_u, KEY_KEY_U));\r
1120         KeyMap.push_back(SKeyMap(SDLK_v, KEY_KEY_V));\r
1121         KeyMap.push_back(SKeyMap(SDLK_w, KEY_KEY_W));\r
1122         KeyMap.push_back(SKeyMap(SDLK_x, KEY_KEY_X));\r
1123         KeyMap.push_back(SKeyMap(SDLK_y, KEY_KEY_Y));\r
1124         KeyMap.push_back(SKeyMap(SDLK_z, KEY_KEY_Z));\r
1125 \r
1126         KeyMap.push_back(SKeyMap(SDLK_LGUI, KEY_LWIN));\r
1127         KeyMap.push_back(SKeyMap(SDLK_RGUI, KEY_RWIN));\r
1128         // apps missing\r
1129         KeyMap.push_back(SKeyMap(SDLK_POWER, KEY_SLEEP)); //??\r
1130 \r
1131         KeyMap.push_back(SKeyMap(SDLK_KP_0, KEY_NUMPAD0));\r
1132         KeyMap.push_back(SKeyMap(SDLK_KP_1, KEY_NUMPAD1));\r
1133         KeyMap.push_back(SKeyMap(SDLK_KP_2, KEY_NUMPAD2));\r
1134         KeyMap.push_back(SKeyMap(SDLK_KP_3, KEY_NUMPAD3));\r
1135         KeyMap.push_back(SKeyMap(SDLK_KP_4, KEY_NUMPAD4));\r
1136         KeyMap.push_back(SKeyMap(SDLK_KP_5, KEY_NUMPAD5));\r
1137         KeyMap.push_back(SKeyMap(SDLK_KP_6, KEY_NUMPAD6));\r
1138         KeyMap.push_back(SKeyMap(SDLK_KP_7, KEY_NUMPAD7));\r
1139         KeyMap.push_back(SKeyMap(SDLK_KP_8, KEY_NUMPAD8));\r
1140         KeyMap.push_back(SKeyMap(SDLK_KP_9, KEY_NUMPAD9));\r
1141         KeyMap.push_back(SKeyMap(SDLK_KP_MULTIPLY, KEY_MULTIPLY));\r
1142         KeyMap.push_back(SKeyMap(SDLK_KP_PLUS, KEY_ADD));\r
1143 //      KeyMap.push_back(SKeyMap(SDLK_KP_, KEY_SEPARATOR));\r
1144         KeyMap.push_back(SKeyMap(SDLK_KP_MINUS, KEY_SUBTRACT));\r
1145         KeyMap.push_back(SKeyMap(SDLK_KP_PERIOD, KEY_DECIMAL));\r
1146         KeyMap.push_back(SKeyMap(SDLK_KP_DIVIDE, KEY_DIVIDE));\r
1147 \r
1148         KeyMap.push_back(SKeyMap(SDLK_F1,  KEY_F1));\r
1149         KeyMap.push_back(SKeyMap(SDLK_F2,  KEY_F2));\r
1150         KeyMap.push_back(SKeyMap(SDLK_F3,  KEY_F3));\r
1151         KeyMap.push_back(SKeyMap(SDLK_F4,  KEY_F4));\r
1152         KeyMap.push_back(SKeyMap(SDLK_F5,  KEY_F5));\r
1153         KeyMap.push_back(SKeyMap(SDLK_F6,  KEY_F6));\r
1154         KeyMap.push_back(SKeyMap(SDLK_F7,  KEY_F7));\r
1155         KeyMap.push_back(SKeyMap(SDLK_F8,  KEY_F8));\r
1156         KeyMap.push_back(SKeyMap(SDLK_F9,  KEY_F9));\r
1157         KeyMap.push_back(SKeyMap(SDLK_F10, KEY_F10));\r
1158         KeyMap.push_back(SKeyMap(SDLK_F11, KEY_F11));\r
1159         KeyMap.push_back(SKeyMap(SDLK_F12, KEY_F12));\r
1160         KeyMap.push_back(SKeyMap(SDLK_F13, KEY_F13));\r
1161         KeyMap.push_back(SKeyMap(SDLK_F14, KEY_F14));\r
1162         KeyMap.push_back(SKeyMap(SDLK_F15, KEY_F15));\r
1163         // no higher F-keys\r
1164 \r
1165         KeyMap.push_back(SKeyMap(SDLK_NUMLOCKCLEAR, KEY_NUMLOCK));\r
1166         KeyMap.push_back(SKeyMap(SDLK_SCROLLLOCK, KEY_SCROLL));\r
1167         KeyMap.push_back(SKeyMap(SDLK_LSHIFT, KEY_LSHIFT));\r
1168         KeyMap.push_back(SKeyMap(SDLK_RSHIFT, KEY_RSHIFT));\r
1169         KeyMap.push_back(SKeyMap(SDLK_LCTRL,  KEY_LCONTROL));\r
1170         KeyMap.push_back(SKeyMap(SDLK_RCTRL,  KEY_RCONTROL));\r
1171         KeyMap.push_back(SKeyMap(SDLK_LALT,  KEY_LMENU));\r
1172         KeyMap.push_back(SKeyMap(SDLK_RALT,  KEY_RMENU));\r
1173 \r
1174         KeyMap.push_back(SKeyMap(SDLK_PLUS,   KEY_PLUS));\r
1175         KeyMap.push_back(SKeyMap(SDLK_COMMA,  KEY_COMMA));\r
1176         KeyMap.push_back(SKeyMap(SDLK_MINUS,  KEY_MINUS));\r
1177         KeyMap.push_back(SKeyMap(SDLK_PERIOD, KEY_PERIOD));\r
1178 \r
1179         // some special keys missing\r
1180 \r
1181         KeyMap.sort();\r
1182 }\r
1183 \r
1184 void CIrrDeviceSDL::CCursorControl::initCursors()\r
1185 {\r
1186         Cursors.reserve(gui::ECI_COUNT);\r
1187 \r
1188         Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW));     // ECI_NORMAL\r
1189         Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR)); // ECI_CROSS\r
1190         Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND));      // ECI_HAND\r
1191         Cursors.emplace_back(nullptr);                                             // ECI_HELP\r
1192         Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM));     // ECI_IBEAM\r
1193         Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO));        // ECI_NO\r
1194         Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAIT));      // ECI_WAIT\r
1195         Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL));   // ECI_SIZEALL\r
1196         Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW));  // ECI_SIZENESW\r
1197         Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE));  // ECI_SIZENWSE\r
1198         Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS));    // ECI_SIZENS\r
1199         Cursors.emplace_back(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE));    // ECI_SIZEWE\r
1200         Cursors.emplace_back(nullptr);                                             // ECI_UP\r
1201 }\r
1202 \r
1203 } // end namespace irr\r
1204 \r
1205 #endif // _IRR_COMPILE_WITH_SDL_DEVICE_\r
1206 \r