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