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 // This device code is based on the original SDL device implementation
\r
5 // contributed by Shane Parker (sirshane).
\r
7 #ifndef __C_IRR_DEVICE_SDL_H_INCLUDED__
\r
8 #define __C_IRR_DEVICE_SDL_H_INCLUDED__
\r
10 #include "IrrCompileConfig.h"
\r
12 #ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
\r
14 #include "IrrlichtDevice.h"
\r
15 #include "CIrrDeviceStub.h"
\r
16 #include "IImagePresenter.h"
\r
17 #include "ICursorControl.h"
\r
19 #ifdef _IRR_EMSCRIPTEN_PLATFORM_
\r
20 #include <emscripten/html5.h>
\r
23 #include <SDL2/SDL.h>
\r
24 #include <SDL2/SDL_syswm.h>
\r
29 class CIrrDeviceSDL : public CIrrDeviceStub, video::IImagePresenter
\r
34 CIrrDeviceSDL(const SIrrlichtCreationParameters& param);
\r
37 virtual ~CIrrDeviceSDL();
\r
39 //! runs the device. Returns false if device wants to be deleted
\r
40 virtual bool run() _IRR_OVERRIDE_;
\r
42 //! pause execution temporarily
\r
43 virtual void yield() _IRR_OVERRIDE_;
\r
45 //! pause execution for a specified time
\r
46 virtual void sleep(u32 timeMs, bool pauseTimer) _IRR_OVERRIDE_;
\r
48 //! sets the caption of the window
\r
49 virtual void setWindowCaption(const wchar_t* text) _IRR_OVERRIDE_;
\r
51 //! returns if window is active. if not, nothing need to be drawn
\r
52 virtual bool isWindowActive() const _IRR_OVERRIDE_;
\r
54 //! returns if window has focus.
\r
55 bool isWindowFocused() const _IRR_OVERRIDE_;
\r
57 //! returns if window is minimized.
\r
58 bool isWindowMinimized() const _IRR_OVERRIDE_;
\r
60 //! returns color format of the window.
\r
61 video::ECOLOR_FORMAT getColorFormat() const _IRR_OVERRIDE_;
\r
63 //! presents a surface in the client area
\r
64 virtual bool present(video::IImage* surface, void* windowId=0, core::rect<s32>* src=0) _IRR_OVERRIDE_;
\r
66 //! notifies the device that it should close itself
\r
67 virtual void closeDevice() _IRR_OVERRIDE_;
\r
69 //! Sets if the window should be resizable in windowed mode.
\r
70 virtual void setResizable(bool resize=false) _IRR_OVERRIDE_;
\r
72 //! Minimizes the window.
\r
73 virtual void minimizeWindow() _IRR_OVERRIDE_;
\r
75 //! Maximizes the window.
\r
76 virtual void maximizeWindow() _IRR_OVERRIDE_;
\r
78 //! Restores the window size.
\r
79 virtual void restoreWindow() _IRR_OVERRIDE_;
\r
81 //! Checks if the Irrlicht window is running in fullscreen mode
\r
82 /** \return True if window is fullscreen. */
\r
83 virtual bool isFullscreen() const _IRR_OVERRIDE_;
\r
85 //! Get the position of this window on screen
\r
86 virtual core::position2di getWindowPosition() _IRR_OVERRIDE_;
\r
88 //! Activate any joysticks, and generate events for them.
\r
89 virtual bool activateJoysticks(core::array<SJoystickInfo> & joystickInfo) _IRR_OVERRIDE_;
\r
91 //! Set the current Gamma Value for the Display
\r
92 virtual bool setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast ) _IRR_OVERRIDE_;
\r
94 //! Get the current Gamma Value for the Display
\r
95 virtual bool getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightness, f32 &contrast ) _IRR_OVERRIDE_;
\r
97 //! Get the device type
\r
98 virtual E_DEVICE_TYPE getType() const _IRR_OVERRIDE_
\r
105 //! Implementation of the linux cursor control
\r
106 class CCursorControl : public gui::ICursorControl
\r
110 CCursorControl(CIrrDeviceSDL* dev)
\r
111 : Device(dev), IsVisible(true)
\r
115 //! Changes the visible state of the mouse cursor.
\r
116 virtual void setVisible(bool visible) _IRR_OVERRIDE_
\r
118 IsVisible = visible;
\r
120 SDL_ShowCursor( SDL_ENABLE );
\r
123 SDL_ShowCursor( SDL_DISABLE );
\r
127 //! Returns if the cursor is currently visible.
\r
128 virtual bool isVisible() const _IRR_OVERRIDE_
\r
133 //! Sets the new position of the cursor.
\r
134 virtual void setPosition(const core::position2d<f32> &pos) _IRR_OVERRIDE_
\r
136 setPosition(pos.X, pos.Y);
\r
139 //! Sets the new position of the cursor.
\r
140 virtual void setPosition(f32 x, f32 y) _IRR_OVERRIDE_
\r
142 setPosition((s32)(x*Device->Width), (s32)(y*Device->Height));
\r
145 //! Sets the new position of the cursor.
\r
146 virtual void setPosition(const core::position2d<s32> &pos) _IRR_OVERRIDE_
\r
148 setPosition(pos.X, pos.Y);
\r
151 //! Sets the new position of the cursor.
\r
152 virtual void setPosition(s32 x, s32 y) _IRR_OVERRIDE_
\r
154 SDL_WarpMouseInWindow(Device->Window, x, y);
\r
157 //! Returns the current position of the mouse cursor.
\r
158 virtual const core::position2d<s32>& getPosition(bool updateCursor) _IRR_OVERRIDE_
\r
160 if ( updateCursor )
\r
165 //! Returns the current position of the mouse cursor.
\r
166 virtual core::position2d<f32> getRelativePosition(bool updateCursor) _IRR_OVERRIDE_
\r
168 if ( updateCursor )
\r
170 return core::position2d<f32>(CursorPos.X / (f32)Device->Width,
\r
171 CursorPos.Y / (f32)Device->Height);
\r
174 virtual void setReferenceRect(core::rect<s32>* rect=0) _IRR_OVERRIDE_
\r
180 void updateCursorPos()
\r
182 #ifdef _IRR_EMSCRIPTEN_PLATFORM_
\r
183 EmscriptenPointerlockChangeEvent pointerlockStatus; // let's hope that test is not expensive ...
\r
184 if ( emscripten_get_pointerlock_status(&pointerlockStatus) == EMSCRIPTEN_RESULT_SUCCESS )
\r
186 if ( pointerlockStatus.isActive )
\r
188 CursorPos.X += Device->MouseXRel;
\r
189 CursorPos.Y += Device->MouseYRel;
\r
190 Device->MouseXRel = 0;
\r
191 Device->MouseYRel = 0;
\r
195 CursorPos.X = Device->MouseX;
\r
196 CursorPos.Y = Device->MouseY;
\r
200 CursorPos.X = Device->MouseX;
\r
201 CursorPos.Y = Device->MouseY;
\r
203 if (CursorPos.X < 0)
\r
205 if (CursorPos.X > (s32)Device->Width)
\r
206 CursorPos.X = Device->Width;
\r
207 if (CursorPos.Y < 0)
\r
209 if (CursorPos.Y > (s32)Device->Height)
\r
210 CursorPos.Y = Device->Height;
\r
214 CIrrDeviceSDL* Device;
\r
215 core::position2d<s32> CursorPos;
\r
221 #ifdef _IRR_EMSCRIPTEN_PLATFORM_
\r
222 static EM_BOOL MouseUpDownCallback(int eventType, const EmscriptenMouseEvent * event, void* userData);
\r
223 static EM_BOOL MouseEnterCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
\r
224 static EM_BOOL MouseLeaveCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
\r
227 // Check if it's a special key like left, right, up down and so on which shouldn't have a Unicode character.
\r
228 bool isNoUnicodeKey(EKEY_CODE key) const;
\r
230 //! create the driver
\r
231 void createDriver();
\r
233 bool createWindow();
\r
235 void createKeyMap();
\r
237 void logAttributes();
\r
238 SDL_GLContext Context;
\r
239 SDL_Window *Window;
\r
241 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
\r
242 core::array<SDL_Joystick*> Joysticks;
\r
245 s32 MouseX, MouseY;
\r
246 s32 MouseXRel, MouseYRel;
\r
247 u32 MouseButtonStates;
\r
252 bool WindowMinimized;
\r
257 SKeyMap(s32 x11, s32 win32)
\r
258 : SDLKey(x11), Win32Key(win32)
\r
265 bool operator<(const SKeyMap& o) const
\r
267 return SDLKey<o.SDLKey;
\r
271 core::array<SKeyMap> KeyMap;
\r
272 SDL_SysWMinfo Info;
\r
275 } // end namespace irr
\r
277 #endif // _IRR_COMPILE_WITH_SDL_DEVICE_
\r
278 #endif // __C_IRR_DEVICE_SDL_H_INCLUDED__
\r