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