]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/CIrrDeviceSDL.h
Update CIrrDeviceSDL to support SDL2 (#85)
[irrlicht.git] / source / Irrlicht / CIrrDeviceSDL.h
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
6 \r
7 #ifndef __C_IRR_DEVICE_SDL_H_INCLUDED__\r
8 #define __C_IRR_DEVICE_SDL_H_INCLUDED__\r
9 \r
10 #include "IrrCompileConfig.h"\r
11 \r
12 #ifdef _IRR_COMPILE_WITH_SDL_DEVICE_\r
13 \r
14 #include "IrrlichtDevice.h"\r
15 #include "CIrrDeviceStub.h"\r
16 #include "IImagePresenter.h"\r
17 #include "ICursorControl.h"\r
18 \r
19 #ifdef _IRR_EMSCRIPTEN_PLATFORM_\r
20 #include <emscripten/html5.h>\r
21 #endif\r
22 \r
23 #include <SDL2/SDL.h>\r
24 #include <SDL2/SDL_syswm.h>\r
25 \r
26 namespace irr\r
27 {\r
28 \r
29         class CIrrDeviceSDL : public CIrrDeviceStub, video::IImagePresenter\r
30         {\r
31         public:\r
32 \r
33                 //! constructor\r
34                 CIrrDeviceSDL(const SIrrlichtCreationParameters& param);\r
35 \r
36                 //! destructor\r
37                 virtual ~CIrrDeviceSDL();\r
38 \r
39                 //! runs the device. Returns false if device wants to be deleted\r
40                 virtual bool run() _IRR_OVERRIDE_;\r
41 \r
42                 //! pause execution temporarily\r
43                 virtual void yield() _IRR_OVERRIDE_;\r
44 \r
45                 //! pause execution for a specified time\r
46                 virtual void sleep(u32 timeMs, bool pauseTimer) _IRR_OVERRIDE_;\r
47 \r
48                 //! sets the caption of the window\r
49                 virtual void setWindowCaption(const wchar_t* text) _IRR_OVERRIDE_;\r
50 \r
51                 //! returns if window is active. if not, nothing need to be drawn\r
52                 virtual bool isWindowActive() const _IRR_OVERRIDE_;\r
53 \r
54                 //! returns if window has focus.\r
55                 bool isWindowFocused() const _IRR_OVERRIDE_;\r
56 \r
57                 //! returns if window is minimized.\r
58                 bool isWindowMinimized() const _IRR_OVERRIDE_;\r
59 \r
60                 //! returns color format of the window.\r
61                 video::ECOLOR_FORMAT getColorFormat() const _IRR_OVERRIDE_;\r
62 \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
65 \r
66                 //! notifies the device that it should close itself\r
67                 virtual void closeDevice() _IRR_OVERRIDE_;\r
68 \r
69                 //! Sets if the window should be resizable in windowed mode.\r
70                 virtual void setResizable(bool resize=false) _IRR_OVERRIDE_;\r
71 \r
72                 //! Minimizes the window.\r
73                 virtual void minimizeWindow() _IRR_OVERRIDE_;\r
74 \r
75                 //! Maximizes the window.\r
76                 virtual void maximizeWindow() _IRR_OVERRIDE_;\r
77 \r
78                 //! Restores the window size.\r
79                 virtual void restoreWindow() _IRR_OVERRIDE_;\r
80 \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
84 \r
85                 //! Get the position of this window on screen\r
86                 virtual core::position2di getWindowPosition() _IRR_OVERRIDE_;\r
87 \r
88                 //! Activate any joysticks, and generate events for them.\r
89                 virtual bool activateJoysticks(core::array<SJoystickInfo> & joystickInfo) _IRR_OVERRIDE_;\r
90 \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
93 \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
96 \r
97                 //! Get the device type\r
98                 virtual E_DEVICE_TYPE getType() const _IRR_OVERRIDE_\r
99                 {\r
100                         return EIDT_SDL;\r
101                 }\r
102 \r
103                 void SwapWindow();\r
104 \r
105                 //! Implementation of the linux cursor control\r
106                 class CCursorControl : public gui::ICursorControl\r
107                 {\r
108                 public:\r
109 \r
110                         CCursorControl(CIrrDeviceSDL* dev)\r
111                                 : Device(dev), IsVisible(true)\r
112                         {\r
113                         }\r
114 \r
115                         //! Changes the visible state of the mouse cursor.\r
116                         virtual void setVisible(bool visible) _IRR_OVERRIDE_\r
117                         {\r
118                                 IsVisible = visible;\r
119                                 if ( visible )\r
120                                         SDL_ShowCursor( SDL_ENABLE );\r
121                                 else\r
122                                 {\r
123                                         SDL_ShowCursor( SDL_DISABLE );\r
124                                 }\r
125                         }\r
126 \r
127                         //! Returns if the cursor is currently visible.\r
128                         virtual bool isVisible() const _IRR_OVERRIDE_\r
129                         {\r
130                                 return IsVisible;\r
131                         }\r
132 \r
133                         //! Sets the new position of the cursor.\r
134                         virtual void setPosition(const core::position2d<f32> &pos) _IRR_OVERRIDE_\r
135                         {\r
136                                 setPosition(pos.X, pos.Y);\r
137                         }\r
138 \r
139                         //! Sets the new position of the cursor.\r
140                         virtual void setPosition(f32 x, f32 y) _IRR_OVERRIDE_\r
141                         {\r
142                                 setPosition((s32)(x*Device->Width), (s32)(y*Device->Height));\r
143                         }\r
144 \r
145                         //! Sets the new position of the cursor.\r
146                         virtual void setPosition(const core::position2d<s32> &pos) _IRR_OVERRIDE_\r
147                         {\r
148                                 setPosition(pos.X, pos.Y);\r
149                         }\r
150 \r
151                         //! Sets the new position of the cursor.\r
152                         virtual void setPosition(s32 x, s32 y) _IRR_OVERRIDE_\r
153                         {\r
154                                 SDL_WarpMouseInWindow(Device->Window, x, y);\r
155                         }\r
156 \r
157                         //! Returns the current position of the mouse cursor.\r
158                         virtual const core::position2d<s32>& getPosition(bool updateCursor) _IRR_OVERRIDE_\r
159                         {\r
160                                 if ( updateCursor )\r
161                                         updateCursorPos();\r
162                                 return CursorPos;\r
163                         }\r
164 \r
165                         //! Returns the current position of the mouse cursor.\r
166                         virtual core::position2d<f32> getRelativePosition(bool updateCursor) _IRR_OVERRIDE_\r
167                         {\r
168                                 if ( updateCursor )\r
169                                         updateCursorPos();\r
170                                 return core::position2d<f32>(CursorPos.X / (f32)Device->Width,\r
171                                         CursorPos.Y / (f32)Device->Height);\r
172                         }\r
173 \r
174                         virtual void setReferenceRect(core::rect<s32>* rect=0) _IRR_OVERRIDE_\r
175                         {\r
176                         }\r
177 \r
178                 private:\r
179 \r
180                         void updateCursorPos()\r
181                         {\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
185                                 {\r
186                                         if ( pointerlockStatus.isActive )\r
187                                         {\r
188                                                 CursorPos.X += Device->MouseXRel;\r
189                                                 CursorPos.Y += Device->MouseYRel;\r
190                                                 Device->MouseXRel = 0;\r
191                                                 Device->MouseYRel = 0;\r
192                                         }\r
193                                         else\r
194                                         {\r
195                                                 CursorPos.X = Device->MouseX;\r
196                                                 CursorPos.Y = Device->MouseY;\r
197                                         }\r
198                                 }\r
199 #else\r
200                                 CursorPos.X = Device->MouseX;\r
201                                 CursorPos.Y = Device->MouseY;\r
202 \r
203                                 if (CursorPos.X < 0)\r
204                                         CursorPos.X = 0;\r
205                                 if (CursorPos.X > (s32)Device->Width)\r
206                                         CursorPos.X = Device->Width;\r
207                                 if (CursorPos.Y < 0)\r
208                                         CursorPos.Y = 0;\r
209                                 if (CursorPos.Y > (s32)Device->Height)\r
210                                         CursorPos.Y = Device->Height;\r
211 #endif\r
212                         }\r
213 \r
214                         CIrrDeviceSDL* Device;\r
215                         core::position2d<s32> CursorPos;\r
216                         bool IsVisible;\r
217                 };\r
218 \r
219         private:\r
220 \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
225 \r
226 #endif\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
229 \r
230                 //! create the driver\r
231                 void createDriver();\r
232 \r
233                 bool createWindow();\r
234 \r
235                 void createKeyMap();\r
236 \r
237                 void logAttributes();\r
238                 SDL_GLContext Context;\r
239                 SDL_Window *Window;\r
240                 int SDL_Flags;\r
241 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)\r
242                 core::array<SDL_Joystick*> Joysticks;\r
243 #endif\r
244 \r
245                 s32 MouseX, MouseY;\r
246                 s32 MouseXRel, MouseYRel;\r
247                 u32 MouseButtonStates;\r
248 \r
249                 u32 Width, Height;\r
250 \r
251                 bool Resizable;\r
252                 bool WindowMinimized;\r
253 \r
254                 struct SKeyMap\r
255                 {\r
256                         SKeyMap() {}\r
257                         SKeyMap(s32 x11, s32 win32)\r
258                                 : SDLKey(x11), Win32Key(win32)\r
259                         {\r
260                         }\r
261 \r
262                         s32 SDLKey;\r
263                         s32 Win32Key;\r
264 \r
265                         bool operator<(const SKeyMap& o) const\r
266                         {\r
267                                 return SDLKey<o.SDLKey;\r
268                         }\r
269                 };\r
270 \r
271                 core::array<SKeyMap> KeyMap;\r
272                 SDL_SysWMinfo Info;\r
273         };\r
274 \r
275 } // end namespace irr\r
276 \r
277 #endif // _IRR_COMPILE_WITH_SDL_DEVICE_\r
278 #endif // __C_IRR_DEVICE_SDL_H_INCLUDED__\r
279 \r