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