diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CIrrDeviceWin32.h | 406 |
1 files changed, 406 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CIrrDeviceWin32.h b/libraries/irrlicht-1.8/source/Irrlicht/CIrrDeviceWin32.h new file mode 100644 index 0000000..16c5d54 --- /dev/null +++ b/libraries/irrlicht-1.8/source/Irrlicht/CIrrDeviceWin32.h | |||
@@ -0,0 +1,406 @@ | |||
1 | // Copyright (C) 2002-2012 Nikolaus Gebhardt | ||
2 | // This file is part of the "Irrlicht Engine". | ||
3 | // For conditions of distribution and use, see copyright notice in irrlicht.h | ||
4 | |||
5 | #ifndef __C_IRR_DEVICE_WIN32_H_INCLUDED__ | ||
6 | #define __C_IRR_DEVICE_WIN32_H_INCLUDED__ | ||
7 | |||
8 | #include "IrrCompileConfig.h" | ||
9 | #ifdef _IRR_COMPILE_WITH_WINDOWS_DEVICE_ | ||
10 | |||
11 | #include "CIrrDeviceStub.h" | ||
12 | #include "IrrlichtDevice.h" | ||
13 | #include "IImagePresenter.h" | ||
14 | |||
15 | #define WIN32_LEAN_AND_MEAN | ||
16 | #if !defined(_IRR_XBOX_PLATFORM_) | ||
17 | #include <windows.h> | ||
18 | #include <mmsystem.h> // For JOYCAPS | ||
19 | #include <windowsx.h> | ||
20 | #endif | ||
21 | #if !defined(GET_X_LPARAM) | ||
22 | #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp)) | ||
23 | #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp)) | ||
24 | #endif | ||
25 | |||
26 | namespace irr | ||
27 | { | ||
28 | struct SJoystickWin32Control; | ||
29 | |||
30 | class CIrrDeviceWin32 : public CIrrDeviceStub, video::IImagePresenter | ||
31 | { | ||
32 | friend struct SJoystickWin32Control; | ||
33 | public: | ||
34 | |||
35 | //! constructor | ||
36 | CIrrDeviceWin32(const SIrrlichtCreationParameters& params); | ||
37 | |||
38 | //! destructor | ||
39 | virtual ~CIrrDeviceWin32(); | ||
40 | |||
41 | //! runs the device. Returns false if device wants to be deleted | ||
42 | virtual bool run(); | ||
43 | |||
44 | //! Cause the device to temporarily pause execution and let other processes to run | ||
45 | // This should bring down processor usage without major performance loss for Irrlicht | ||
46 | virtual void yield(); | ||
47 | |||
48 | //! Pause execution and let other processes to run for a specified amount of time. | ||
49 | virtual void sleep(u32 timeMs, bool pauseTimer); | ||
50 | |||
51 | //! sets the caption of the window | ||
52 | virtual void setWindowCaption(const wchar_t* text); | ||
53 | |||
54 | //! returns if window is active. if not, nothing need to be drawn | ||
55 | virtual bool isWindowActive() const; | ||
56 | |||
57 | //! returns if window has focus | ||
58 | virtual bool isWindowFocused() const; | ||
59 | |||
60 | //! returns if window is minimized | ||
61 | virtual bool isWindowMinimized() const; | ||
62 | |||
63 | //! presents a surface in the client area | ||
64 | virtual bool present(video::IImage* surface, void* windowId=0, core::rect<s32>* src=0); | ||
65 | |||
66 | //! notifies the device that it should close itself | ||
67 | virtual void closeDevice(); | ||
68 | |||
69 | //! \return Returns a pointer to a list with all video modes | ||
70 | //! supported by the gfx adapter. | ||
71 | video::IVideoModeList* getVideoModeList(); | ||
72 | |||
73 | //! Notifies the device, that it has been resized | ||
74 | void OnResized(); | ||
75 | |||
76 | //! Sets if the window should be resizable in windowed mode. | ||
77 | virtual void setResizable(bool resize=false); | ||
78 | |||
79 | //! Minimizes the window. | ||
80 | virtual void minimizeWindow(); | ||
81 | |||
82 | //! Maximizes the window. | ||
83 | virtual void maximizeWindow(); | ||
84 | |||
85 | //! Restores the window size. | ||
86 | virtual void restoreWindow(); | ||
87 | |||
88 | //! Activate any joysticks, and generate events for them. | ||
89 | virtual bool activateJoysticks(core::array<SJoystickInfo> & joystickInfo); | ||
90 | |||
91 | //! Set the current Gamma Value for the Display | ||
92 | virtual bool setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast ); | ||
93 | |||
94 | //! Get the current Gamma Value for the Display | ||
95 | virtual bool getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightness, f32 &contrast ); | ||
96 | |||
97 | //! Remove all messages pending in the system message loop | ||
98 | virtual void clearSystemMessages(); | ||
99 | |||
100 | //! Get the device type | ||
101 | virtual E_DEVICE_TYPE getType() const | ||
102 | { | ||
103 | return EIDT_WIN32; | ||
104 | } | ||
105 | |||
106 | //! Compares to the last call of this function to return double and triple clicks. | ||
107 | //! \return Returns only 1,2 or 3. A 4th click will start with 1 again. | ||
108 | virtual u32 checkSuccessiveClicks(s32 mouseX, s32 mouseY, EMOUSE_INPUT_EVENT inputEvent ) | ||
109 | { | ||
110 | // we just have to make it public | ||
111 | return CIrrDeviceStub::checkSuccessiveClicks(mouseX, mouseY, inputEvent ); | ||
112 | } | ||
113 | |||
114 | //! switchs to fullscreen | ||
115 | bool switchToFullScreen(bool reset=false); | ||
116 | |||
117 | //! Check for and show last Windows API error to help internal debugging. | ||
118 | //! Does call GetLastError and on errors formats the errortext and displays it in a messagebox. | ||
119 | static void ReportLastWinApiError(); | ||
120 | |||
121 | // convert an Irrlicht texture to a windows cursor | ||
122 | HCURSOR TextureToCursor(HWND hwnd, irr::video::ITexture * tex, const core::rect<s32>& sourceRect, const core::position2d<s32> &hotspot); | ||
123 | |||
124 | //! Implementation of the win32 cursor control | ||
125 | class CCursorControl : public gui::ICursorControl | ||
126 | { | ||
127 | public: | ||
128 | |||
129 | CCursorControl(CIrrDeviceWin32* device, const core::dimension2d<u32>& wsize, HWND hwnd, bool fullscreen); | ||
130 | ~CCursorControl(); | ||
131 | |||
132 | //! Changes the visible state of the mouse cursor. | ||
133 | virtual void setVisible(bool visible) | ||
134 | { | ||
135 | CURSORINFO info; | ||
136 | info.cbSize = sizeof(CURSORINFO); | ||
137 | BOOL gotCursorInfo = GetCursorInfo(&info); | ||
138 | while ( gotCursorInfo ) | ||
139 | { | ||
140 | if ( (visible && info.flags == CURSOR_SHOWING) // visible | ||
141 | || (!visible && info.flags == 0 ) ) // hidden | ||
142 | { | ||
143 | break; | ||
144 | } | ||
145 | int showResult = ShowCursor(visible); // this only increases an internal display counter in windows, so it might have to be called some more | ||
146 | if ( showResult < 0 ) | ||
147 | { | ||
148 | break; | ||
149 | } | ||
150 | info.cbSize = sizeof(CURSORINFO); // yes, it really must be set each time | ||
151 | gotCursorInfo = GetCursorInfo(&info); | ||
152 | } | ||
153 | IsVisible = visible; | ||
154 | } | ||
155 | |||
156 | //! Returns if the cursor is currently visible. | ||
157 | virtual bool isVisible() const | ||
158 | { | ||
159 | _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; | ||
160 | return IsVisible; | ||
161 | } | ||
162 | |||
163 | //! Sets the new position of the cursor. | ||
164 | virtual void setPosition(const core::position2d<f32> &pos) | ||
165 | { | ||
166 | setPosition(pos.X, pos.Y); | ||
167 | } | ||
168 | |||
169 | //! Sets the new position of the cursor. | ||
170 | virtual void setPosition(f32 x, f32 y) | ||
171 | { | ||
172 | if (!UseReferenceRect) | ||
173 | setPosition(core::round32(x*WindowSize.Width), core::round32(y*WindowSize.Height)); | ||
174 | else | ||
175 | setPosition(core::round32(x*ReferenceRect.getWidth()), core::round32(y*ReferenceRect.getHeight())); | ||
176 | } | ||
177 | |||
178 | //! Sets the new position of the cursor. | ||
179 | virtual void setPosition(const core::position2d<s32> &pos) | ||
180 | { | ||
181 | setPosition(pos.X, pos.Y); | ||
182 | } | ||
183 | |||
184 | //! Sets the new position of the cursor. | ||
185 | virtual void setPosition(s32 x, s32 y) | ||
186 | { | ||
187 | if (UseReferenceRect) | ||
188 | { | ||
189 | SetCursorPos(ReferenceRect.UpperLeftCorner.X + x, | ||
190 | ReferenceRect.UpperLeftCorner.Y + y); | ||
191 | } | ||
192 | else | ||
193 | { | ||
194 | RECT rect; | ||
195 | if (GetWindowRect(HWnd, &rect)) | ||
196 | SetCursorPos(x + rect.left + BorderX, y + rect.top + BorderY); | ||
197 | } | ||
198 | |||
199 | CursorPos.X = x; | ||
200 | CursorPos.Y = y; | ||
201 | } | ||
202 | |||
203 | //! Returns the current position of the mouse cursor. | ||
204 | virtual const core::position2d<s32>& getPosition() | ||
205 | { | ||
206 | updateInternalCursorPosition(); | ||
207 | return CursorPos; | ||
208 | } | ||
209 | |||
210 | //! Returns the current position of the mouse cursor. | ||
211 | virtual core::position2d<f32> getRelativePosition() | ||
212 | { | ||
213 | updateInternalCursorPosition(); | ||
214 | |||
215 | if (!UseReferenceRect) | ||
216 | { | ||
217 | return core::position2d<f32>(CursorPos.X * InvWindowSize.Width, | ||
218 | CursorPos.Y * InvWindowSize.Height); | ||
219 | } | ||
220 | |||
221 | return core::position2d<f32>(CursorPos.X / (f32)ReferenceRect.getWidth(), | ||
222 | CursorPos.Y / (f32)ReferenceRect.getHeight()); | ||
223 | } | ||
224 | |||
225 | //! Sets an absolute reference rect for calculating the cursor position. | ||
226 | virtual void setReferenceRect(core::rect<s32>* rect=0) | ||
227 | { | ||
228 | if (rect) | ||
229 | { | ||
230 | ReferenceRect = *rect; | ||
231 | UseReferenceRect = true; | ||
232 | |||
233 | // prevent division through zero and uneven sizes | ||
234 | |||
235 | if (!ReferenceRect.getHeight() || ReferenceRect.getHeight()%2) | ||
236 | ReferenceRect.LowerRightCorner.Y += 1; | ||
237 | |||
238 | if (!ReferenceRect.getWidth() || ReferenceRect.getWidth()%2) | ||
239 | ReferenceRect.LowerRightCorner.X += 1; | ||
240 | } | ||
241 | else | ||
242 | UseReferenceRect = false; | ||
243 | } | ||
244 | |||
245 | /** Used to notify the cursor that the window was resized. */ | ||
246 | virtual void OnResize(const core::dimension2d<u32>& size) | ||
247 | { | ||
248 | WindowSize = size; | ||
249 | if (size.Width!=0) | ||
250 | InvWindowSize.Width = 1.0f / size.Width; | ||
251 | else | ||
252 | InvWindowSize.Width = 0.f; | ||
253 | |||
254 | if (size.Height!=0) | ||
255 | InvWindowSize.Height = 1.0f / size.Height; | ||
256 | else | ||
257 | InvWindowSize.Height = 0.f; | ||
258 | } | ||
259 | |||
260 | /** Used to notify the cursor that the window resizable settings changed. */ | ||
261 | void updateBorderSize(bool fullscreen, bool resizable) | ||
262 | { | ||
263 | if (!fullscreen) | ||
264 | { | ||
265 | if (resizable) | ||
266 | { | ||
267 | BorderX = GetSystemMetrics(SM_CXSIZEFRAME); | ||
268 | BorderY = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYSIZEFRAME); | ||
269 | } | ||
270 | else | ||
271 | { | ||
272 | BorderX = GetSystemMetrics(SM_CXDLGFRAME); | ||
273 | BorderY = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYDLGFRAME); | ||
274 | } | ||
275 | } | ||
276 | else | ||
277 | { | ||
278 | BorderX = BorderY = 0; | ||
279 | } | ||
280 | } | ||
281 | |||
282 | |||
283 | //! Sets the active cursor icon | ||
284 | virtual void setActiveIcon(gui::ECURSOR_ICON iconId); | ||
285 | |||
286 | //! Gets the currently active icon | ||
287 | virtual gui::ECURSOR_ICON getActiveIcon() const | ||
288 | { | ||
289 | return ActiveIcon; | ||
290 | } | ||
291 | |||
292 | //! Add a custom sprite as cursor icon. | ||
293 | virtual gui::ECURSOR_ICON addIcon(const gui::SCursorSprite& icon); | ||
294 | |||
295 | //! replace the given cursor icon. | ||
296 | virtual void changeIcon(gui::ECURSOR_ICON iconId, const gui::SCursorSprite& icon); | ||
297 | |||
298 | //! Return a system-specific size which is supported for cursors. Larger icons will fail, smaller icons might work. | ||
299 | virtual core::dimension2di getSupportedIconSize() const; | ||
300 | |||
301 | void update(); | ||
302 | |||
303 | private: | ||
304 | |||
305 | //! Updates the internal cursor position | ||
306 | void updateInternalCursorPosition() | ||
307 | { | ||
308 | POINT p; | ||
309 | if (!GetCursorPos(&p)) | ||
310 | { | ||
311 | DWORD xy = GetMessagePos(); | ||
312 | p.x = GET_X_LPARAM(xy); | ||
313 | p.y = GET_Y_LPARAM(xy); | ||
314 | } | ||
315 | |||
316 | if (UseReferenceRect) | ||
317 | { | ||
318 | CursorPos.X = p.x - ReferenceRect.UpperLeftCorner.X; | ||
319 | CursorPos.Y = p.y - ReferenceRect.UpperLeftCorner.Y; | ||
320 | } | ||
321 | else | ||
322 | { | ||
323 | RECT rect; | ||
324 | if (GetWindowRect(HWnd, &rect)) | ||
325 | { | ||
326 | CursorPos.X = p.x-rect.left-BorderX; | ||
327 | CursorPos.Y = p.y-rect.top-BorderY; | ||
328 | } | ||
329 | else | ||
330 | { | ||
331 | // window seems not to be existent, so set cursor to | ||
332 | // a negative value | ||
333 | CursorPos.X = -1; | ||
334 | CursorPos.Y = -1; | ||
335 | } | ||
336 | } | ||
337 | } | ||
338 | |||
339 | CIrrDeviceWin32* Device; | ||
340 | core::position2d<s32> CursorPos; | ||
341 | core::dimension2d<u32> WindowSize; | ||
342 | core::dimension2d<f32> InvWindowSize; | ||
343 | HWND HWnd; | ||
344 | |||
345 | s32 BorderX, BorderY; | ||
346 | core::rect<s32> ReferenceRect; | ||
347 | bool UseReferenceRect; | ||
348 | bool IsVisible; | ||
349 | |||
350 | |||
351 | struct CursorFrameW32 | ||
352 | { | ||
353 | CursorFrameW32() : IconHW(0) {} | ||
354 | CursorFrameW32(HCURSOR icon) : IconHW(icon) {} | ||
355 | |||
356 | HCURSOR IconHW; // hardware cursor | ||
357 | }; | ||
358 | |||
359 | struct CursorW32 | ||
360 | { | ||
361 | CursorW32() {} | ||
362 | explicit CursorW32(HCURSOR iconHw, u32 frameTime=0) : FrameTime(frameTime) | ||
363 | { | ||
364 | Frames.push_back( CursorFrameW32(iconHw) ); | ||
365 | } | ||
366 | core::array<CursorFrameW32> Frames; | ||
367 | u32 FrameTime; | ||
368 | }; | ||
369 | |||
370 | core::array<CursorW32> Cursors; | ||
371 | gui::ECURSOR_ICON ActiveIcon; | ||
372 | u32 ActiveIconStartTime; | ||
373 | |||
374 | void initCursors(); | ||
375 | }; | ||
376 | |||
377 | //! returns the win32 cursor control | ||
378 | CCursorControl* getWin32CursorControl(); | ||
379 | |||
380 | private: | ||
381 | |||
382 | //! create the driver | ||
383 | void createDriver(); | ||
384 | |||
385 | //! Process system events | ||
386 | void handleSystemMessages(); | ||
387 | |||
388 | void getWindowsVersion(core::stringc& version); | ||
389 | |||
390 | void resizeIfNecessary(); | ||
391 | |||
392 | HWND HWnd; | ||
393 | |||
394 | bool ChangedToFullScreen; | ||
395 | bool Resized; | ||
396 | bool ExternalWindow; | ||
397 | CCursorControl* Win32CursorControl; | ||
398 | DEVMODE DesktopMode; | ||
399 | |||
400 | SJoystickWin32Control* JoyControl; | ||
401 | }; | ||
402 | |||
403 | } // end namespace irr | ||
404 | |||
405 | #endif // _IRR_COMPILE_WITH_WINDOWS_DEVICE_ | ||
406 | #endif // __C_IRR_DEVICE_WIN32_H_INCLUDED__ | ||