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