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
5 #ifndef __C_IRR_DEVICE_WIN32_H_INCLUDED__
\r
6 #define __C_IRR_DEVICE_WIN32_H_INCLUDED__
\r
8 #include "IrrCompileConfig.h"
\r
9 #ifdef _IRR_COMPILE_WITH_WINDOWS_DEVICE_
\r
11 #include "CIrrDeviceStub.h"
\r
12 #include "IrrlichtDevice.h"
\r
13 #include "IImagePresenter.h"
\r
15 #define WIN32_LEAN_AND_MEAN
\r
16 #if !defined(_IRR_XBOX_PLATFORM_)
\r
17 #include <windows.h>
\r
18 #include <mmsystem.h> // For JOYCAPS
\r
19 #include <windowsx.h>
\r
21 #if !defined(GET_X_LPARAM)
\r
22 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
\r
23 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
\r
28 struct SJoystickWin32Control;
\r
30 class CIrrDeviceWin32 : public CIrrDeviceStub, video::IImagePresenter
\r
32 friend struct SJoystickWin32Control;
\r
36 CIrrDeviceWin32(const SIrrlichtCreationParameters& params);
\r
39 virtual ~CIrrDeviceWin32();
\r
41 //! runs the device. Returns false if device wants to be deleted
\r
42 virtual bool run() _IRR_OVERRIDE_;
\r
44 //! Cause the device to temporarily pause execution and let other processes to run
\r
45 // This should bring down processor usage without major performance loss for Irrlicht
\r
46 virtual void yield() _IRR_OVERRIDE_;
\r
48 //! Pause execution and let other processes to run for a specified amount of time.
\r
49 virtual void sleep(u32 timeMs, bool pauseTimer) _IRR_OVERRIDE_;
\r
51 //! sets the caption of the window
\r
52 virtual void setWindowCaption(const wchar_t* text) _IRR_OVERRIDE_;
\r
54 //! returns if window is active. if not, nothing need to be drawn
\r
55 virtual bool isWindowActive() const _IRR_OVERRIDE_;
\r
57 //! returns if window has focus
\r
58 virtual bool isWindowFocused() const _IRR_OVERRIDE_;
\r
60 //! returns if window is minimized
\r
61 virtual bool isWindowMinimized() 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 //! Notifies the device, that it has been resized
\r
70 /** Must be publis as it is called from free function (event handler) */
\r
73 //! Sets if the window should be resizable in windowed mode.
\r
74 virtual void setResizable(bool resize=false) _IRR_OVERRIDE_;
\r
76 //! Resize the render window.
\r
77 virtual void setWindowSize(const irr::core::dimension2d<u32>& size) _IRR_OVERRIDE_;
\r
79 //! Minimizes the window.
\r
80 virtual void minimizeWindow() _IRR_OVERRIDE_;
\r
82 //! Maximizes the window.
\r
83 virtual void maximizeWindow() _IRR_OVERRIDE_;
\r
85 //! Restores the window size.
\r
86 virtual void restoreWindow() _IRR_OVERRIDE_;
\r
88 //! Get the position of the window on screen
\r
89 virtual core::position2di getWindowPosition() _IRR_OVERRIDE_;
\r
91 //! Activate any joysticks, and generate events for them.
\r
92 virtual bool activateJoysticks(core::array<SJoystickInfo> & joystickInfo) _IRR_OVERRIDE_;
\r
94 //! Set the current Gamma Value for the Display
\r
95 virtual bool setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast ) _IRR_OVERRIDE_;
\r
97 //! Get the current Gamma Value for the Display
\r
98 virtual bool getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightness, f32 &contrast ) _IRR_OVERRIDE_;
\r
100 //! Remove all messages pending in the system message loop
\r
101 virtual void clearSystemMessages() _IRR_OVERRIDE_;
\r
103 //! Get the device type
\r
104 virtual E_DEVICE_TYPE getType() const _IRR_OVERRIDE_
\r
109 //! Compares to the last call of this function to return double and triple clicks.
\r
110 //! \return Returns only 1,2 or 3. A 4th click will start with 1 again.
\r
111 virtual u32 checkSuccessiveClicks(s32 mouseX, s32 mouseY, EMOUSE_INPUT_EVENT inputEvent ) _IRR_OVERRIDE_
\r
113 // we just have to make it public
\r
114 return CIrrDeviceStub::checkSuccessiveClicks(mouseX, mouseY, inputEvent );
\r
117 //! Switch to fullscreen
\r
118 bool switchToFullScreen();
\r
120 //! Check for and show last Windows API error to help internal debugging.
\r
121 //! Does call GetLastError and on errors formats the error text and displays it in a messagebox.
\r
122 static void ReportLastWinApiError();
\r
124 //! Same function Windows offers in VersionHelpers.h, but we can't use that as it's not available before SDK 8.1
\r
125 static bool isWindowsVistaOrGreater();
\r
127 // convert an Irrlicht texture to a windows cursor
\r
128 HCURSOR TextureToCursor(HWND hwnd, irr::video::ITexture * tex, const core::rect<s32>& sourceRect, const core::position2d<s32> &hotspot);
\r
130 //! Implementation of the win32 cursor control
\r
131 class CCursorControl : public gui::ICursorControl
\r
135 CCursorControl(CIrrDeviceWin32* device, const core::dimension2d<u32>& wsize, HWND hwnd, bool fullscreen);
\r
138 //! Changes the visible state of the mouse cursor.
\r
139 virtual void setVisible(bool visible) _IRR_OVERRIDE_
\r
142 info.cbSize = sizeof(CURSORINFO);
\r
143 BOOL gotCursorInfo = GetCursorInfo(&info);
\r
144 while ( gotCursorInfo )
\r
146 #ifdef CURSOR_SUPPRESSED
\r
147 // Since Windows 8 the cursor can be suppressed by a touch interface
\r
148 if (visible && info.flags == CURSOR_SUPPRESSED)
\r
153 if ( (visible && info.flags == CURSOR_SHOWING) || // visible
\r
154 (!visible && info.flags == 0 ) ) // hidden
\r
158 // this only increases an internal
\r
159 // display counter in windows, so it
\r
160 // might have to be called some more
\r
161 const int showResult = ShowCursor(visible);
\r
162 // if result has correct sign we can
\r
163 // stop here as well
\r
164 if (( !visible && showResult < 0 ) ||
\r
165 (visible && showResult >= 0))
\r
167 // yes, it really must be set each time
\r
168 info.cbSize = sizeof(CURSORINFO);
\r
169 gotCursorInfo = GetCursorInfo(&info);
\r
171 #ifdef CURSOR_SUPPRESSED
\r
172 // Not sure if a cursor which we tried to hide still can be suppressed.
\r
173 // I have no touch-display for testing this and MSDN doesn't describe it.
\r
174 // But adding this check shouldn't hurt and might prevent an endless loop.
\r
175 if (!visible && info.flags == CURSOR_SUPPRESSED)
\r
181 IsVisible = visible;
\r
184 //! Returns if the cursor is currently visible.
\r
185 virtual bool isVisible() const _IRR_OVERRIDE_
\r
190 //! Sets the new position of the cursor.
\r
191 virtual void setPosition(const core::position2d<f32> &pos) _IRR_OVERRIDE_
\r
193 setPosition(pos.X, pos.Y);
\r
196 //! Sets the new position of the cursor.
\r
197 virtual void setPosition(f32 x, f32 y) _IRR_OVERRIDE_
\r
199 if (!UseReferenceRect)
\r
200 setPosition(core::round32(x*WindowSize.Width), core::round32(y*WindowSize.Height));
\r
202 setPosition(core::round32(x*ReferenceRect.getWidth()), core::round32(y*ReferenceRect.getHeight()));
\r
205 //! Sets the new position of the cursor.
\r
206 virtual void setPosition(const core::position2d<s32> &pos) _IRR_OVERRIDE_
\r
208 setPosition(pos.X, pos.Y);
\r
211 //! Sets the new position of the cursor.
\r
212 virtual void setPosition(s32 x, s32 y) _IRR_OVERRIDE_
\r
214 if (UseReferenceRect)
\r
216 SetCursorPos(ReferenceRect.UpperLeftCorner.X + x,
\r
217 ReferenceRect.UpperLeftCorner.Y + y);
\r
222 if (GetWindowRect(HWnd, &rect))
\r
223 SetCursorPos(x + rect.left + BorderX, y + rect.top + BorderY);
\r
230 //! Returns the current position of the mouse cursor.
\r
231 virtual const core::position2d<s32>& getPosition(bool updateCursor) _IRR_OVERRIDE_
\r
233 if ( updateCursor )
\r
234 updateInternalCursorPosition();
\r
238 //! Returns the current position of the mouse cursor.
\r
239 virtual core::position2d<f32> getRelativePosition(bool updateCursor) _IRR_OVERRIDE_
\r
241 if ( updateCursor )
\r
242 updateInternalCursorPosition();
\r
244 if (!UseReferenceRect)
\r
246 return core::position2d<f32>(CursorPos.X * InvWindowSize.Width,
\r
247 CursorPos.Y * InvWindowSize.Height);
\r
250 return core::position2d<f32>(CursorPos.X / (f32)ReferenceRect.getWidth(),
\r
251 CursorPos.Y / (f32)ReferenceRect.getHeight());
\r
254 //! Sets an absolute reference rect for calculating the cursor position.
\r
255 virtual void setReferenceRect(core::rect<s32>* rect=0) _IRR_OVERRIDE_
\r
259 ReferenceRect = *rect;
\r
260 UseReferenceRect = true;
\r
262 // prevent division through zero and uneven sizes
\r
264 if (!ReferenceRect.getHeight() || ReferenceRect.getHeight()%2)
\r
265 ReferenceRect.LowerRightCorner.Y += 1;
\r
267 if (!ReferenceRect.getWidth() || ReferenceRect.getWidth()%2)
\r
268 ReferenceRect.LowerRightCorner.X += 1;
\r
271 UseReferenceRect = false;
\r
274 /** Used to notify the cursor that the window was resized. */
\r
275 void OnResize(const core::dimension2d<u32>& size)
\r
279 InvWindowSize.Width = 1.0f / size.Width;
\r
281 InvWindowSize.Width = 0.f;
\r
283 if (size.Height!=0)
\r
284 InvWindowSize.Height = 1.0f / size.Height;
\r
286 InvWindowSize.Height = 0.f;
\r
289 /** Used to notify the cursor that the window resizable settings changed. */
\r
290 void updateBorderSize(bool fullscreen, bool resizable)
\r
294 s32 paddingBorder = 0;
\r
295 #if defined (SM_CXPADDEDBORDER)
\r
296 if (CIrrDeviceWin32::isWindowsVistaOrGreater())
\r
297 paddingBorder = GetSystemMetrics(SM_CXPADDEDBORDER);
\r
302 BorderX = GetSystemMetrics(SM_CXSIZEFRAME) + paddingBorder;
\r
303 BorderY = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYSIZEFRAME) + paddingBorder;
\r
307 BorderX = GetSystemMetrics(SM_CXDLGFRAME) + paddingBorder;
\r
308 BorderY = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYDLGFRAME) + paddingBorder;
\r
313 BorderX = BorderY = 0;
\r
318 //! Sets the active cursor icon
\r
319 virtual void setActiveIcon(gui::ECURSOR_ICON iconId) _IRR_OVERRIDE_;
\r
321 //! Gets the currently active icon
\r
322 virtual gui::ECURSOR_ICON getActiveIcon() const _IRR_OVERRIDE_
\r
327 //! Add a custom sprite as cursor icon.
\r
328 virtual gui::ECURSOR_ICON addIcon(const gui::SCursorSprite& icon) _IRR_OVERRIDE_;
\r
330 //! replace the given cursor icon.
\r
331 virtual void changeIcon(gui::ECURSOR_ICON iconId, const gui::SCursorSprite& icon) _IRR_OVERRIDE_;
\r
333 //! Return a system-specific size which is supported for cursors. Larger icons will fail, smaller icons might work.
\r
334 virtual core::dimension2di getSupportedIconSize() const _IRR_OVERRIDE_;
\r
340 //! Updates the internal cursor position
\r
341 void updateInternalCursorPosition()
\r
344 if (!GetCursorPos(&p))
\r
346 DWORD xy = GetMessagePos();
\r
347 p.x = GET_X_LPARAM(xy);
\r
348 p.y = GET_Y_LPARAM(xy);
\r
351 if (UseReferenceRect)
\r
353 CursorPos.X = p.x - ReferenceRect.UpperLeftCorner.X;
\r
354 CursorPos.Y = p.y - ReferenceRect.UpperLeftCorner.Y;
\r
359 if (GetWindowRect(HWnd, &rect))
\r
361 CursorPos.X = p.x-rect.left-BorderX;
\r
362 CursorPos.Y = p.y-rect.top-BorderY;
\r
366 // window seems not to be existent, so set cursor to
\r
367 // a negative value
\r
374 CIrrDeviceWin32* Device;
\r
375 core::position2d<s32> CursorPos;
\r
376 core::dimension2d<u32> WindowSize;
\r
377 core::dimension2d<f32> InvWindowSize;
\r
380 s32 BorderX, BorderY;
\r
381 core::rect<s32> ReferenceRect;
\r
382 bool UseReferenceRect;
\r
386 struct CursorFrameW32
\r
388 CursorFrameW32() : IconHW(0) {}
\r
389 CursorFrameW32(HCURSOR icon) : IconHW(icon) {}
\r
391 HCURSOR IconHW; // hardware cursor
\r
397 explicit CursorW32(HCURSOR iconHw, u32 frameTime=0) : FrameTime(frameTime)
\r
399 Frames.push_back( CursorFrameW32(iconHw) );
\r
401 core::array<CursorFrameW32> Frames;
\r
405 core::array<CursorW32> Cursors;
\r
406 gui::ECURSOR_ICON ActiveIcon;
\r
407 u32 ActiveIconStartTime;
\r
409 void initCursors();
\r
412 //! returns the win32 cursor control
\r
413 CCursorControl* getWin32CursorControl();
\r
417 //! create the driver
\r
418 void createDriver();
\r
420 //! Process system events
\r
421 void handleSystemMessages();
\r
423 void getWindowsVersion(core::stringc& version);
\r
425 void resizeIfNecessary();
\r
427 DWORD getWindowStyle(bool fullscreen, bool resizable) const;
\r
432 bool ExternalWindow;
\r
433 CCursorControl* Win32CursorControl;
\r
435 SJoystickWin32Control* JoyControl;
\r
438 } // end namespace irr
\r
440 #endif // _IRR_COMPILE_WITH_WINDOWS_DEVICE_
\r
441 #endif // __C_IRR_DEVICE_WIN32_H_INCLUDED__
\r