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
6 #ifdef _IRR_COMPILE_WITH_WINDOWS_DEVICE_
\r
8 #if defined (__STRICT_ANSI__)
\r
9 #error Compiling with __STRICT_ANSI__ not supported. g++ does set this when compiling with -std=c++11 or -std=c++0x. Use instead -std=gnu++11 or -std=gnu++0x. Or use -U__STRICT_ANSI__ to disable strict ansi.
\r
12 #include "CIrrDeviceWin32.h"
\r
13 #include "IEventReceiver.h"
\r
17 #include "irrString.h"
\r
18 #include "COSOperator.h"
\r
19 #include "dimension2d.h"
\r
20 #include "IGUISpriteBank.h"
\r
21 #include <winuser.h>
\r
22 #include "SExposedVideoData.h"
\r
24 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
\r
25 #include <mmsystem.h>
\r
27 #ifdef _IRR_COMPILE_WITH_DIRECTINPUT_JOYSTICK_
\r
28 #define DIRECTINPUT_VERSION 0x0800
\r
33 #if defined(_IRR_COMPILE_WITH_OGLES1_) || defined(_IRR_COMPILE_WITH_OGLES2_)
\r
34 #include "CEGLManager.h"
\r
37 #if defined(_IRR_COMPILE_WITH_OPENGL_)
\r
38 #include "CWGLManager.h"
\r
45 #ifdef _IRR_COMPILE_WITH_DIRECT3D_9_
\r
46 IVideoDriver* createDirectX9Driver(const irr::SIrrlichtCreationParameters& params, io::IFileSystem* io, HWND window);
\r
49 #ifdef _IRR_COMPILE_WITH_OPENGL_
\r
50 IVideoDriver* createOpenGLDriver(const irr::SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);
\r
53 #ifdef _IRR_COMPILE_WITH_OGLES1_
\r
54 IVideoDriver* createOGLES1Driver(const irr::SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);
\r
57 #ifdef _IRR_COMPILE_WITH_OGLES2_
\r
58 IVideoDriver* createOGLES2Driver(const irr::SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);
\r
61 } // end namespace irr
\r
65 struct SJoystickWin32Control
\r
67 CIrrDeviceWin32* Device;
\r
69 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_) && defined(_IRR_COMPILE_WITH_DIRECTINPUT_JOYSTICK_)
\r
70 IDirectInput8* DirectInputDevice;
\r
72 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
\r
76 #ifdef _IRR_COMPILE_WITH_DIRECTINPUT_JOYSTICK_
\r
79 LPDIRECTINPUTDEVICE8 lpdijoy;
\r
86 core::array<JoystickInfo> ActiveJoysticks;
\r
89 SJoystickWin32Control(CIrrDeviceWin32* dev);
\r
90 ~SJoystickWin32Control();
\r
92 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_) && defined(_IRR_COMPILE_WITH_DIRECTINPUT_JOYSTICK_)
\r
93 static BOOL CALLBACK EnumJoysticks(LPCDIDEVICEINSTANCE lpddi, LPVOID cp);
\r
94 void directInputAddJoystick(LPCDIDEVICEINSTANCE lpddi);
\r
97 void pollJoysticks();
\r
98 bool activateJoysticks(core::array<SJoystickInfo> & joystickInfo);
\r
99 irr::core::stringc findJoystickName(int index, const JOYCAPS &caps) const;
\r
103 SJoystickWin32Control::SJoystickWin32Control(CIrrDeviceWin32* dev) : Device(dev)
\r
105 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_) && defined(_IRR_COMPILE_WITH_DIRECTINPUT_JOYSTICK_)
\r
106 DirectInputDevice=0;
\r
107 if (DI_OK != (DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&DirectInputDevice, NULL)))
\r
109 os::Printer::log("Could not create DirectInput8 Object", ELL_WARNING);
\r
115 SJoystickWin32Control::~SJoystickWin32Control()
\r
117 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_) && defined(_IRR_COMPILE_WITH_DIRECTINPUT_JOYSTICK_)
\r
118 for(u32 joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
\r
120 LPDIRECTINPUTDEVICE8 dev = ActiveJoysticks[joystick].lpdijoy;
\r
128 if (DirectInputDevice)
\r
129 DirectInputDevice->Release();
\r
133 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_) && defined(_IRR_COMPILE_WITH_DIRECTINPUT_JOYSTICK_)
\r
134 BOOL CALLBACK SJoystickWin32Control::EnumJoysticks(LPCDIDEVICEINSTANCE lpddi, LPVOID cp)
\r
136 SJoystickWin32Control* p=(SJoystickWin32Control*)cp;
\r
137 p->directInputAddJoystick(lpddi);
\r
138 return DIENUM_CONTINUE;
\r
140 void SJoystickWin32Control::directInputAddJoystick(LPCDIDEVICEINSTANCE lpddi)
\r
142 //Get the GUID of the joystuck
\r
143 const GUID guid = lpddi->guidInstance;
\r
145 JoystickInfo activeJoystick;
\r
146 activeJoystick.Index=ActiveJoysticks.size();
\r
147 activeJoystick.guid=guid;
\r
148 activeJoystick.Name=lpddi->tszProductName;
\r
149 if (FAILED(DirectInputDevice->CreateDevice(guid, &activeJoystick.lpdijoy, NULL)))
\r
151 os::Printer::log("Could not create DirectInput device", ELL_WARNING);
\r
155 activeJoystick.devcaps.dwSize=sizeof(activeJoystick.devcaps);
\r
156 if (FAILED(activeJoystick.lpdijoy->GetCapabilities(&activeJoystick.devcaps)))
\r
158 os::Printer::log("Could not create DirectInput device", ELL_WARNING);
\r
162 if (FAILED(activeJoystick.lpdijoy->SetCooperativeLevel(Device->HWnd, DISCL_BACKGROUND | DISCL_EXCLUSIVE)))
\r
164 os::Printer::log("Could not set DirectInput device cooperative level", ELL_WARNING);
\r
168 if (FAILED(activeJoystick.lpdijoy->SetDataFormat(&c_dfDIJoystick2)))
\r
170 os::Printer::log("Could not set DirectInput device data format", ELL_WARNING);
\r
174 if (FAILED(activeJoystick.lpdijoy->Acquire()))
\r
176 os::Printer::log("Could not set DirectInput cooperative level", ELL_WARNING);
\r
181 if (FAILED(activeJoystick.lpdijoy->GetDeviceState(sizeof(info),&info)))
\r
183 os::Printer::log("Could not read DirectInput device state", ELL_WARNING);
\r
187 ZeroMemory(activeJoystick.axisValid,sizeof(activeJoystick.axisValid));
\r
188 activeJoystick.axisValid[0]= (info.lX!=0) ? 1 : 0;
\r
189 activeJoystick.axisValid[1]= (info.lY!=0) ? 1 : 0;
\r
190 activeJoystick.axisValid[2]= (info.lZ!=0) ? 1 : 0;
\r
191 activeJoystick.axisValid[3]= (info.lRx!=0) ? 1 : 0;
\r
192 activeJoystick.axisValid[4]= (info.lRy!=0) ? 1 : 0;
\r
193 activeJoystick.axisValid[5]= (info.lRz!=0) ? 1 : 0;
\r
196 for (u8 i=0; i<6; i++)
\r
198 if (activeJoystick.axisValid[i])
\r
202 for (u8 i=0; i<(activeJoystick.devcaps.dwAxes)-caxis; i++)
\r
205 activeJoystick.axisValid[i+caxis]=1;
\r
208 ActiveJoysticks.push_back(activeJoystick);
\r
212 void SJoystickWin32Control::pollJoysticks()
\r
214 #if defined _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
\r
215 #ifdef _IRR_COMPILE_WITH_DIRECTINPUT_JOYSTICK_
\r
216 if(0 == ActiveJoysticks.size())
\r
222 for(joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
\r
224 // needs to be reset for each joystick
\r
225 // request ALL values and POV as continuous if possible
\r
227 const DIDEVCAPS & caps = ActiveJoysticks[joystick].devcaps;
\r
228 // if no POV is available don't ask for POV values
\r
230 if (!FAILED(ActiveJoysticks[joystick].lpdijoy->GetDeviceState(sizeof(info),&info)))
\r
234 event.EventType = irr::EET_JOYSTICK_INPUT_EVENT;
\r
235 event.JoystickEvent.Joystick = (u8)joystick;
\r
237 event.JoystickEvent.POV = (u16)info.rgdwPOV[0];
\r
238 // set to undefined if no POV value was returned or the value
\r
240 if ((caps.dwPOVs==0) || (event.JoystickEvent.POV > 35900))
\r
241 event.JoystickEvent.POV = 65535;
\r
243 for(int axis = 0; axis < SEvent::SJoystickEvent::NUMBER_OF_AXES; ++axis)
\r
244 event.JoystickEvent.Axis[axis] = 0;
\r
249 while (dxAxis < 6 && irrAxis <caps.dwAxes)
\r
266 axisValue=info.lRx;
\r
269 axisValue=info.lRy;
\r
272 axisValue=info.lRz;
\r
275 axisValue=info.rglSlider[0];
\r
278 axisValue=info.rglSlider[1];
\r
284 if (ActiveJoysticks[joystick].axisValid[dxAxis]>0)
\r
289 s32 val=axisValue - 32768;
\r
291 if (val <-32767) val=-32767;
\r
292 if (val > 32767) val=32767;
\r
293 event.JoystickEvent.Axis[irrAxis]=(s16)(val);
\r
301 BYTE* bytebuttons=info.rgbButtons;
\r
302 for (u16 i=0; i<32; i++)
\r
304 if (bytebuttons[i] >0)
\r
306 buttons |= (1 << i);
\r
309 event.JoystickEvent.ButtonStates = buttons;
\r
311 (void)Device->postEventFromUser(event);
\r
315 if (0 == ActiveJoysticks.size())
\r
321 for(joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
\r
323 // needs to be reset for each joystick
\r
324 // request ALL values and POV as continuous if possible
\r
325 info.dwSize = sizeof(info);
\r
326 info.dwFlags = JOY_RETURNALL|JOY_RETURNPOVCTS;
\r
327 const JOYCAPS & caps = ActiveJoysticks[joystick].Caps;
\r
328 // if no POV is available don't ask for POV values
\r
329 if (!(caps.wCaps & JOYCAPS_HASPOV))
\r
330 info.dwFlags &= ~(JOY_RETURNPOV|JOY_RETURNPOVCTS);
\r
331 if(JOYERR_NOERROR == joyGetPosEx(ActiveJoysticks[joystick].Index, &info))
\r
335 event.EventType = irr::EET_JOYSTICK_INPUT_EVENT;
\r
336 event.JoystickEvent.Joystick = (u8)joystick;
\r
338 event.JoystickEvent.POV = (u16)info.dwPOV;
\r
339 // set to undefined if no POV value was returned or the value
\r
341 if (!(info.dwFlags & JOY_RETURNPOV) || (event.JoystickEvent.POV > 35900))
\r
342 event.JoystickEvent.POV = 65535;
\r
344 for(int axis = 0; axis < SEvent::SJoystickEvent::NUMBER_OF_AXES; ++axis)
\r
345 event.JoystickEvent.Axis[axis] = 0;
\r
347 event.JoystickEvent.ButtonStates = info.dwButtons;
\r
349 switch(caps.wNumAxes)
\r
353 event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_V] =
\r
354 (s16)((65535 * (info.dwVpos - caps.wVmin)) / (caps.wVmax - caps.wVmin) - 32768);
\r
357 event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_U] =
\r
358 (s16)((65535 * (info.dwUpos - caps.wUmin)) / (caps.wUmax - caps.wUmin) - 32768);
\r
361 event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_R] =
\r
362 (s16)((65535 * (info.dwRpos - caps.wRmin)) / (caps.wRmax - caps.wRmin) - 32768);
\r
365 event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Z] =
\r
366 (s16)((65535 * (info.dwZpos - caps.wZmin)) / (caps.wZmax - caps.wZmin) - 32768);
\r
369 event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Y] =
\r
370 (s16)((65535 * (info.dwYpos - caps.wYmin)) / (caps.wYmax - caps.wYmin) - 32768);
\r
373 event.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_X] =
\r
374 (s16)((65535 * (info.dwXpos - caps.wXmin)) / (caps.wXmax - caps.wXmin) - 32768);
\r
377 (void)Device->postEventFromUser(event);
\r
381 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
\r
384 /** This function is ported from SDL and released under zlib-license:
\r
385 * Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org> */
\r
386 irr::core::stringc SJoystickWin32Control::findJoystickName(int index, const JOYCAPS &caps) const
\r
388 #if defined _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
\r
390 // As a default use the name given in the joystick structure.
\r
391 // It is always the same name, independent of joystick.
\r
392 irr::core::stringc result(caps.szPname);
\r
394 core::stringc key = core::stringc(REGSTR_PATH_JOYCONFIG)+ "\\" + caps.szRegKey + "\\" + REGSTR_KEY_JOYCURR;
\r
395 HKEY hTopKey = HKEY_LOCAL_MACHINE;
\r
397 long regresult = RegOpenKeyExA(hTopKey, key.c_str(), 0, KEY_READ, &hKey);
\r
398 if (regresult != ERROR_SUCCESS)
\r
400 hTopKey = HKEY_CURRENT_USER;
\r
401 regresult = RegOpenKeyExA(hTopKey, key.c_str(), 0, KEY_READ, &hKey);
\r
403 if (regresult != ERROR_SUCCESS)
\r
406 /* find the registry key name for the joystick's properties */
\r
408 DWORD regsize = sizeof(regname);
\r
409 core::stringc regvalue = core::stringc("Joystick")+core::stringc(index+1) + REGSTR_VAL_JOYOEMNAME;
\r
410 regresult = RegQueryValueExA(hKey, regvalue.c_str(), 0, 0, (LPBYTE)regname, ®size);
\r
412 if (regresult != ERROR_SUCCESS)
\r
415 /* open that registry key */
\r
416 core::stringc regkey = core::stringc(REGSTR_PATH_JOYOEM) + "\\" + regname;
\r
417 regresult = RegOpenKeyExA(hTopKey, regkey.c_str(), 0, KEY_READ, &hKey);
\r
418 if (regresult != ERROR_SUCCESS)
\r
421 /* find the size for the OEM name text */
\r
422 regsize = sizeof(regvalue);
\r
423 regresult = RegQueryValueEx(hKey, REGSTR_VAL_JOYOEMNAME, 0, 0,
\r
425 if (regresult == ERROR_SUCCESS)
\r
428 /* allocate enough memory for the OEM name text ... */
\r
429 name = new char[regsize];
\r
432 /* ... and read it from the registry */
\r
433 regresult = RegQueryValueEx(hKey, REGSTR_VAL_JOYOEMNAME, 0, 0,
\r
434 (LPBYTE)name, ®size );
\r
446 bool SJoystickWin32Control::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
\r
448 #if defined _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
\r
449 joystickInfo.clear();
\r
450 ActiveJoysticks.clear();
\r
451 #ifdef _IRR_COMPILE_WITH_DIRECTINPUT_JOYSTICK_
\r
452 if (!DirectInputDevice || (DirectInputDevice->EnumDevices(DI8DEVCLASS_GAMECTRL, SJoystickWin32Control::EnumJoysticks, this, DIEDFL_ATTACHEDONLY )))
\r
454 os::Printer::log("Could not enum DirectInput8 controllers", ELL_WARNING);
\r
458 for(u32 joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
\r
460 JoystickInfo& activeJoystick = ActiveJoysticks[joystick];
\r
461 SJoystickInfo info;
\r
462 info.Axes=activeJoystick.devcaps.dwAxes;
\r
463 info.Buttons=activeJoystick.devcaps.dwButtons;
\r
464 info.Name=activeJoystick.Name;
\r
465 info.PovHat = (activeJoystick.devcaps.dwPOVs != 0)
\r
466 ? SJoystickInfo::POV_HAT_PRESENT : SJoystickInfo::POV_HAT_ABSENT;
\r
467 joystickInfo.push_back(info);
\r
471 const u32 numberOfJoysticks = ::joyGetNumDevs();
\r
473 info.dwSize = sizeof(info);
\r
474 info.dwFlags = JOY_RETURNALL;
\r
476 JoystickInfo activeJoystick;
\r
477 SJoystickInfo returnInfo;
\r
479 joystickInfo.reallocate(numberOfJoysticks);
\r
480 ActiveJoysticks.reallocate(numberOfJoysticks);
\r
483 for(; joystick < numberOfJoysticks; ++joystick)
\r
485 if(JOYERR_NOERROR == joyGetPosEx(joystick, &info)
\r
487 JOYERR_NOERROR == joyGetDevCaps(joystick,
\r
488 &activeJoystick.Caps,
\r
489 sizeof(activeJoystick.Caps)))
\r
491 activeJoystick.Index = joystick;
\r
492 ActiveJoysticks.push_back(activeJoystick);
\r
494 returnInfo.Joystick = (u8)joystick;
\r
495 returnInfo.Axes = activeJoystick.Caps.wNumAxes;
\r
496 returnInfo.Buttons = activeJoystick.Caps.wNumButtons;
\r
497 returnInfo.Name = findJoystickName(joystick, activeJoystick.Caps);
\r
498 returnInfo.PovHat = ((activeJoystick.Caps.wCaps & JOYCAPS_HASPOV) == JOYCAPS_HASPOV)
\r
499 ? SJoystickInfo::POV_HAT_PRESENT : SJoystickInfo::POV_HAT_ABSENT;
\r
501 joystickInfo.push_back(returnInfo);
\r
505 for(joystick = 0; joystick < joystickInfo.size(); ++joystick)
\r
507 char logString[256];
\r
508 snprintf_irr(logString, sizeof(logString), "Found joystick %d, %d axes, %d buttons '%s'",
\r
509 joystick, joystickInfo[joystick].Axes,
\r
510 joystickInfo[joystick].Buttons, joystickInfo[joystick].Name.c_str());
\r
511 os::Printer::log(logString, ELL_INFORMATION);
\r
518 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
\r
520 } // end namespace irr
\r
527 irr::CIrrDeviceWin32* irrDev;
\r
529 // NOTE: This is global. We can have more than one Irrlicht Device at same time.
\r
530 irr::core::array<SEnvMapper> EnvMap;
\r
532 HKL KEYBOARD_INPUT_HKL=0;
\r
535 irr::CIrrDeviceWin32* getDeviceFromHWnd(HWND hWnd)
\r
537 const irr::u32 end = EnvMap.size();
\r
538 for ( irr::u32 i=0; i < end; ++i )
\r
540 const SEnvMapper& env = EnvMap[i];
\r
541 if ( env.hWnd == hWnd )
\r
549 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
\r
551 #ifndef WHEEL_DELTA
\r
552 #define WHEEL_DELTA 120
\r
555 irr::CIrrDeviceWin32* dev = 0;
\r
558 static irr::s32 ClickCount=0;
\r
559 if (GetCapture() != hWnd && ClickCount > 0)
\r
567 irr::s32 irrMessage;
\r
570 static messageMap mouseMap[] =
\r
572 {0, WM_LBUTTONDOWN, irr::EMIE_LMOUSE_PRESSED_DOWN},
\r
573 {1, WM_LBUTTONUP, irr::EMIE_LMOUSE_LEFT_UP},
\r
574 {0, WM_RBUTTONDOWN, irr::EMIE_RMOUSE_PRESSED_DOWN},
\r
575 {1, WM_RBUTTONUP, irr::EMIE_RMOUSE_LEFT_UP},
\r
576 {0, WM_MBUTTONDOWN, irr::EMIE_MMOUSE_PRESSED_DOWN},
\r
577 {1, WM_MBUTTONUP, irr::EMIE_MMOUSE_LEFT_UP},
\r
578 {2, WM_MOUSEMOVE, irr::EMIE_MOUSE_MOVED},
\r
579 {3, WM_MOUSEWHEEL, irr::EMIE_MOUSE_WHEEL},
\r
583 // handle grouped events
\r
584 messageMap * m = mouseMap;
\r
585 while ( m->group >=0 && m->winMessage != message )
\r
588 if ( m->group >= 0 )
\r
590 if ( m->group == 0 ) // down
\r
596 if ( m->group == 1 ) // up
\r
606 event.EventType = irr::EET_MOUSE_INPUT_EVENT;
\r
607 event.MouseInput.Event = (irr::EMOUSE_INPUT_EVENT) m->irrMessage;
\r
608 event.MouseInput.X = (short)LOWORD(lParam);
\r
609 event.MouseInput.Y = (short)HIWORD(lParam);
\r
610 event.MouseInput.Shift = ((LOWORD(wParam) & MK_SHIFT) != 0);
\r
611 event.MouseInput.Control = ((LOWORD(wParam) & MK_CONTROL) != 0);
\r
612 // left and right mouse buttons
\r
613 event.MouseInput.ButtonStates = wParam & ( MK_LBUTTON | MK_RBUTTON);
\r
614 // middle and extra buttons
\r
615 if (wParam & MK_MBUTTON)
\r
616 event.MouseInput.ButtonStates |= irr::EMBSM_MIDDLE;
\r
617 if (wParam & MK_XBUTTON1)
\r
618 event.MouseInput.ButtonStates |= irr::EMBSM_EXTRA1;
\r
619 if (wParam & MK_XBUTTON2)
\r
620 event.MouseInput.ButtonStates |= irr::EMBSM_EXTRA2;
\r
621 event.MouseInput.Wheel = 0.f;
\r
624 if ( m->group == 3 )
\r
626 POINT p; // fixed by jox
\r
628 ClientToScreen(hWnd, &p);
\r
629 event.MouseInput.X -= p.x;
\r
630 event.MouseInput.Y -= p.y;
\r
631 event.MouseInput.Wheel = ((irr::f32)((short)HIWORD(wParam))) / (irr::f32)WHEEL_DELTA;
\r
634 dev = getDeviceFromHWnd(hWnd);
\r
637 dev->postEventFromUser(event);
\r
639 if ( event.MouseInput.Event >= irr::EMIE_LMOUSE_PRESSED_DOWN && event.MouseInput.Event <= irr::EMIE_MMOUSE_PRESSED_DOWN )
\r
641 irr::u32 clicks = dev->checkSuccessiveClicks(event.MouseInput.X, event.MouseInput.Y, event.MouseInput.Event);
\r
644 event.MouseInput.Event = (irr::EMOUSE_INPUT_EVENT)(irr::EMIE_LMOUSE_DOUBLE_CLICK + event.MouseInput.Event-irr::EMIE_LMOUSE_PRESSED_DOWN);
\r
645 dev->postEventFromUser(event);
\r
647 else if ( clicks == 3 )
\r
649 event.MouseInput.Event = (irr::EMOUSE_INPUT_EVENT)(irr::EMIE_LMOUSE_TRIPLE_CLICK + event.MouseInput.Event-irr::EMIE_LMOUSE_PRESSED_DOWN);
\r
650 dev->postEventFromUser(event);
\r
662 BeginPaint(hWnd, &ps);
\r
663 EndPaint(hWnd, &ps);
\r
667 case WM_ERASEBKGND:
\r
670 case WM_SYSKEYDOWN:
\r
677 event.EventType = irr::EET_KEY_INPUT_EVENT;
\r
678 event.KeyInput.Key = (irr::EKEY_CODE)wParam;
\r
679 event.KeyInput.PressedDown = (message==WM_KEYDOWN || message == WM_SYSKEYDOWN);
\r
681 if ( event.KeyInput.Key == irr::KEY_SHIFT )
\r
683 event.KeyInput.Key = (irr::EKEY_CODE)MapVirtualKey( ((lParam>>16) & 255), MAPVK_VSC_TO_VK_EX );
\r
685 if ( event.KeyInput.Key == irr::KEY_CONTROL )
\r
687 event.KeyInput.Key = (irr::EKEY_CODE)MapVirtualKey( ((lParam>>16) & 255), MAPVK_VSC_TO_VK_EX );
\r
688 // some keyboards will just return LEFT for both - left and right keys. So also check extend bit.
\r
689 if (lParam & 0x1000000)
\r
690 event.KeyInput.Key = irr::KEY_RCONTROL;
\r
692 if ( event.KeyInput.Key == irr::KEY_MENU )
\r
694 event.KeyInput.Key = (irr::EKEY_CODE)MapVirtualKey( ((lParam>>16) & 255), MAPVK_VSC_TO_VK_EX );
\r
695 if (lParam & 0x1000000)
\r
696 event.KeyInput.Key = irr::KEY_RMENU;
\r
699 GetKeyboardState(allKeys);
\r
701 event.KeyInput.Shift = ((allKeys[VK_SHIFT] & 0x80)!=0);
\r
702 event.KeyInput.Control = ((allKeys[VK_CONTROL] & 0x80)!=0);
\r
704 // Handle unicode and deadkeys
\r
706 UINT scanCode = HIWORD(lParam);
\r
707 int conversionResult = ToUnicodeEx(static_cast<UINT>(wParam),scanCode,allKeys,keyChars,2,0,KEYBOARD_INPUT_HKL);
\r
708 if (conversionResult == 1)
\r
709 event.KeyInput.Char = keyChars[0];
\r
711 event.KeyInput.Char = 0;
\r
713 // allow composing characters like '@' with Alt Gr on non-US keyboards
\r
714 if ((allKeys[VK_MENU] & 0x80) != 0)
\r
715 event.KeyInput.Control = 0;
\r
717 dev = getDeviceFromHWnd(hWnd);
\r
719 dev->postEventFromUser(event);
\r
721 if (message == WM_SYSKEYDOWN || message == WM_SYSKEYUP)
\r
722 return DefWindowProcW(hWnd, message, wParam, lParam);
\r
730 dev = getDeviceFromHWnd(hWnd);
\r
737 PostQuitMessage(0);
\r
740 case WM_SYSCOMMAND:
\r
741 // prevent screensaver or monitor powersave mode from starting
\r
742 if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
\r
743 (wParam & 0xFFF0) == SC_MONITORPOWER ||
\r
744 (wParam & 0xFFF0) == SC_KEYMENU
\r
751 event.EventType = irr::EET_USER_EVENT;
\r
752 event.UserEvent.UserData1 = static_cast<size_t>(wParam);
\r
753 event.UserEvent.UserData2 = static_cast<size_t>(lParam);
\r
754 dev = getDeviceFromHWnd(hWnd);
\r
757 dev->postEventFromUser(event);
\r
762 // because Windows forgot about that in the meantime
\r
763 dev = getDeviceFromHWnd(hWnd);
\r
766 dev->getCursorControl()->setActiveIcon( dev->getCursorControl()->getActiveIcon() );
\r
767 dev->getCursorControl()->setVisible( dev->getCursorControl()->isVisible() );
\r
771 case WM_INPUTLANGCHANGE:
\r
772 // get the new codepage used for keyboard input
\r
773 KEYBOARD_INPUT_HKL = GetKeyboardLayout(0);
\r
776 return DefWindowProcW(hWnd, message, wParam, lParam);
\r
784 CIrrDeviceWin32::CIrrDeviceWin32(const SIrrlichtCreationParameters& params)
\r
785 : CIrrDeviceStub(params), HWnd(0), Resized(false),
\r
786 ExternalWindow(false), Win32CursorControl(0), JoyControl(0),
\r
787 WindowMaximized(params.WindowMaximized)
\r
790 setDebugName("CIrrDeviceWin32");
\r
793 // get windows version and create OS operator
\r
794 core::stringc winversion;
\r
795 getWindowsVersion(winversion);
\r
796 Operator = new COSOperator(winversion);
\r
797 os::Printer::log(winversion.c_str(), ELL_INFORMATION);
\r
799 // get handle to exe file
\r
800 HINSTANCE hInstance = GetModuleHandle(0);
\r
802 // create the window if we need to and we do not use the null device
\r
803 if (!CreationParams.WindowId && CreationParams.DriverType != video::EDT_NULL)
\r
805 const wchar_t* ClassName = L"CIrrDeviceWin32";
\r
809 wcex.cbSize = sizeof(WNDCLASSEXW);
\r
810 wcex.style = CS_HREDRAW | CS_VREDRAW;
\r
811 wcex.lpfnWndProc = WndProc;
\r
812 wcex.cbClsExtra = 0;
\r
813 wcex.cbWndExtra = 0;
\r
814 wcex.hInstance = hInstance;
\r
816 wcex.hCursor = 0; // LoadCursor(NULL, IDC_ARROW);
\r
817 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
\r
818 wcex.lpszMenuName = 0;
\r
819 wcex.lpszClassName = ClassName;
\r
822 // if there is an icon, load it
\r
823 wcex.hIcon = (HICON)LoadImageW(hInstance, L"irrlicht.ico", IMAGE_ICON, 0,0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
\r
825 RegisterClassExW(&wcex);
\r
827 // calculate client size
\r
830 clientSize.top = 0;
\r
831 clientSize.left = 0;
\r
832 clientSize.right = CreationParams.WindowSize.Width;
\r
833 clientSize.bottom = CreationParams.WindowSize.Height;
\r
835 DWORD style = getWindowStyle(CreationParams.Fullscreen, CreationParams.WindowResizable > 0 ? true : false);
\r
836 AdjustWindowRect(&clientSize, style, FALSE);
\r
838 const s32 realWidth = clientSize.right - clientSize.left;
\r
839 const s32 realHeight = clientSize.bottom - clientSize.top;
\r
841 s32 windowLeft = (CreationParams.WindowPosition.X == -1 ?
\r
842 (GetSystemMetrics(SM_CXSCREEN) - realWidth) / 2 :
\r
843 CreationParams.WindowPosition.X);
\r
844 s32 windowTop = (CreationParams.WindowPosition.Y == -1 ?
\r
845 (GetSystemMetrics(SM_CYSCREEN) - realHeight) / 2 :
\r
846 CreationParams.WindowPosition.Y);
\r
848 if ( windowLeft < 0 )
\r
850 if ( windowTop < 0 )
\r
851 windowTop = 0; // make sure window menus are in screen on creation
\r
853 if (CreationParams.Fullscreen)
\r
860 HWnd = CreateWindowW( ClassName, L"", style, windowLeft, windowTop,
\r
861 realWidth, realHeight, NULL, NULL, hInstance, NULL);
\r
864 os::Printer::log("Window could not be created.", ELL_ERROR);
\r
867 CreationParams.WindowId = HWnd;
\r
868 // CreationParams.WindowSize.Width = realWidth;
\r
869 // CreationParams.WindowSize.Height = realHeight;
\r
871 ShowWindow(HWnd, SW_SHOWNORMAL);
\r
872 UpdateWindow(HWnd);
\r
874 // fix ugly ATI driver bugs. Thanks to ariaci
\r
875 MoveWindow(HWnd, windowLeft, windowTop, realWidth, realHeight, TRUE);
\r
877 // make sure everything gets updated to the real sizes
\r
880 else if (CreationParams.WindowId)
\r
882 // attach external window
\r
883 HWnd = static_cast<HWND>(CreationParams.WindowId);
\r
885 GetWindowRect(HWnd, &r);
\r
886 CreationParams.WindowSize.Width = r.right - r.left;
\r
887 CreationParams.WindowSize.Height = r.bottom - r.top;
\r
888 CreationParams.Fullscreen = false;
\r
889 ExternalWindow = true;
\r
892 // create cursor control
\r
894 Win32CursorControl = new CCursorControl(this, CreationParams.WindowSize, HWnd, CreationParams.Fullscreen);
\r
895 CursorControl = Win32CursorControl;
\r
896 JoyControl = new SJoystickWin32Control(this);
\r
898 // initialize doubleclicks with system values
\r
899 MouseMultiClicks.DoubleClickTime = GetDoubleClickTime();
\r
906 createGUIAndScene();
\r
908 // register environment
\r
913 EnvMap.push_back(em);
\r
915 // set this as active window
\r
916 if (!ExternalWindow)
\r
918 SetActiveWindow(HWnd);
\r
919 SetForegroundWindow(HWnd);
\r
922 KEYBOARD_INPUT_HKL = GetKeyboardLayout(0);
\r
924 // inform driver about the window size etc.
\r
925 resizeIfNecessary();
\r
927 if (params.WindowMaximized)
\r
933 CIrrDeviceWin32::~CIrrDeviceWin32()
\r
937 // unregister environment
\r
938 for (u32 i=0; i< EnvMap.size(); ++i)
\r
940 if (EnvMap[i].hWnd == HWnd)
\r
949 //! create the driver
\r
950 void CIrrDeviceWin32::createDriver()
\r
952 switch(CreationParams.DriverType)
\r
954 case video::DEPRECATED_EDT_DIRECT3D8_NO_LONGER_EXISTS:
\r
955 os::Printer::log("DIRECT3D8 Driver is no longer supported in Irrlicht. Try another one.", ELL_ERROR);
\r
957 case video::EDT_DIRECT3D9:
\r
958 #ifdef _IRR_COMPILE_WITH_DIRECT3D_9_
\r
959 VideoDriver = video::createDirectX9Driver(CreationParams, FileSystem, HWnd);
\r
962 os::Printer::log("Could not create DIRECT3D9 Driver.", ELL_ERROR);
\r
964 os::Printer::log("DIRECT3D9 Driver was not compiled into this dll. Try another one.", ELL_ERROR);
\r
967 case video::EDT_OPENGL:
\r
968 #ifdef _IRR_COMPILE_WITH_OPENGL_
\r
969 switchToFullScreen();
\r
971 ContextManager = new video::CWGLManager();
\r
972 ContextManager->initialize(CreationParams, video::SExposedVideoData(HWnd));
\r
974 VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem, ContextManager);
\r
977 os::Printer::log("Could not create OpenGL driver.", ELL_ERROR);
\r
979 os::Printer::log("OpenGL driver was not compiled in.", ELL_ERROR);
\r
982 case video::EDT_OGLES1:
\r
983 #ifdef _IRR_COMPILE_WITH_OGLES1_
\r
984 switchToFullScreen();
\r
986 ContextManager = new video::CEGLManager();
\r
987 ContextManager->initialize(CreationParams, video::SExposedVideoData(HWnd));
\r
989 VideoDriver = video::createOGLES1Driver(CreationParams, FileSystem, ContextManager);
\r
992 os::Printer::log("Could not create OpenGL-ES1 driver.", ELL_ERROR);
\r
994 os::Printer::log("OpenGL-ES1 driver was not compiled in.", ELL_ERROR);
\r
997 case video::EDT_OGLES2:
\r
998 #ifdef _IRR_COMPILE_WITH_OGLES2_
\r
999 switchToFullScreen();
\r
1001 ContextManager = new video::CEGLManager();
\r
1002 ContextManager->initialize(CreationParams, video::SExposedVideoData(HWnd));
\r
1004 VideoDriver = video::createOGLES2Driver(CreationParams, FileSystem, ContextManager);
\r
1007 os::Printer::log("Could not create OpenGL-ES2 driver.", ELL_ERROR);
\r
1009 os::Printer::log("OpenGL-ES2 driver was not compiled in.", ELL_ERROR);
\r
1012 case video::EDT_WEBGL1:
\r
1013 os::Printer::log("WebGL1 driver not supported on Win32 device.", ELL_ERROR);
\r
1015 case video::EDT_NULL:
\r
1016 VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);
\r
1019 os::Printer::log("Unable to create video driver of unknown type.", ELL_ERROR);
\r
1025 //! runs the device. Returns false if device wants to be deleted
\r
1026 bool CIrrDeviceWin32::run()
\r
1028 os::Timer::tick();
\r
1030 static_cast<CCursorControl*>(CursorControl)->update();
\r
1032 handleSystemMessages();
\r
1035 resizeIfNecessary();
\r
1037 if(!Close && JoyControl)
\r
1038 JoyControl->pollJoysticks();
\r
1044 //! Pause the current process for the minimum time allowed only to allow other processes to execute
\r
1045 void CIrrDeviceWin32::yield()
\r
1050 //! Pause execution and let other processes to run for a specified amount of time.
\r
1051 void CIrrDeviceWin32::sleep(u32 timeMs, bool pauseTimer)
\r
1053 const bool wasStopped = Timer ? Timer->isStopped() : true;
\r
1054 if (pauseTimer && !wasStopped)
\r
1059 if (pauseTimer && !wasStopped)
\r
1064 void CIrrDeviceWin32::resizeIfNecessary()
\r
1066 if (!Resized || !getVideoDriver())
\r
1070 GetClientRect(HWnd, &r);
\r
1074 if (r.right < 2 || r.bottom < 2)
\r
1076 snprintf_irr(tmp, sizeof(tmp), "Ignoring resize operation to (%ld %ld)", r.right, r.bottom);
\r
1077 os::Printer::log(tmp);
\r
1081 snprintf_irr(tmp, sizeof(tmp), "Resizing window (%ld %ld)", r.right, r.bottom);
\r
1082 os::Printer::log(tmp);
\r
1084 getVideoDriver()->OnResize(irr::core::dimension2du((u32)r.right, (u32)r.bottom));
\r
1085 getWin32CursorControl()->OnResize(getVideoDriver()->getScreenSize());
\r
1092 DWORD CIrrDeviceWin32::getWindowStyle(bool fullscreen, bool resizable) const
\r
1098 return WS_THICKFRAME | WS_SYSMENU | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
\r
1100 return WS_BORDER | WS_SYSMENU | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
\r
1103 //! sets the caption of the window
\r
1104 void CIrrDeviceWin32::setWindowCaption(const wchar_t* text)
\r
1106 // We use SendMessage instead of SetText to ensure proper
\r
1107 // function even in cases where the HWND was created in a different thread
\r
1108 DWORD_PTR dwResult;
\r
1109 SendMessageTimeoutW(HWnd, WM_SETTEXT, 0,
\r
1110 reinterpret_cast<LPARAM>(text),
\r
1111 SMTO_ABORTIFHUNG, 2000, &dwResult);
\r
1115 //! notifies the device that it should close itself
\r
1116 void CIrrDeviceWin32::closeDevice()
\r
1118 if (!ExternalWindow)
\r
1121 PeekMessage(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE);
\r
1122 PostQuitMessage(0);
\r
1123 PeekMessage(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE);
\r
1124 DestroyWindow(HWnd);
\r
1125 const wchar_t* ClassName = L"CIrrDeviceWin32";
\r
1126 HINSTANCE hInstance = GetModuleHandle(0);
\r
1127 UnregisterClassW(ClassName, hInstance);
\r
1133 //! returns if window is active. if not, nothing needs to be drawn
\r
1134 bool CIrrDeviceWin32::isWindowActive() const
\r
1136 return (GetActiveWindow() == HWnd);
\r
1140 //! returns if window has focus
\r
1141 bool CIrrDeviceWin32::isWindowFocused() const
\r
1143 bool ret = (GetFocus() == HWnd);
\r
1148 //! returns if window is minimized
\r
1149 bool CIrrDeviceWin32::isWindowMinimized() const
\r
1151 WINDOWPLACEMENT plc;
\r
1152 plc.length=sizeof(WINDOWPLACEMENT);
\r
1154 if (GetWindowPlacement(HWnd,&plc))
\r
1155 ret = plc.showCmd == SW_SHOWMINIMIZED;
\r
1160 //! returns last state from maximizeWindow() and restoreWindow()
\r
1161 bool CIrrDeviceWin32::isWindowMaximized() const
\r
1163 return WindowMaximized;
\r
1167 //! switches to fullscreen
\r
1168 bool CIrrDeviceWin32::switchToFullScreen()
\r
1170 if (!CreationParams.Fullscreen)
\r
1173 // No border, title bar, etc. is already set up through getWindowStyle()
\r
1174 // We only set the window size to match the monitor.
\r
1177 mi.cbSize = sizeof(mi);
\r
1178 if (GetMonitorInfo(MonitorFromWindow(HWnd,MONITOR_DEFAULTTOPRIMARY),&mi))
\r
1180 UINT flags = SWP_NOCOPYBITS|SWP_NOOWNERZORDER|SWP_FRAMECHANGED;
\r
1181 SetWindowPos(HWnd, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top,
\r
1182 mi.rcMonitor.right - mi.rcMonitor.left,
\r
1183 mi.rcMonitor.bottom - mi.rcMonitor.top, flags);
\r
1187 CreationParams.Fullscreen = false;
\r
1190 return CreationParams.Fullscreen;
\r
1194 //! returns the win32 cursor control
\r
1195 CIrrDeviceWin32::CCursorControl* CIrrDeviceWin32::getWin32CursorControl()
\r
1197 return Win32CursorControl;
\r
1200 void CIrrDeviceWin32::getWindowsVersion(core::stringc& out)
\r
1202 OSVERSIONINFO osvi;
\r
1204 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
\r
1205 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
\r
1206 GetVersionEx(&osvi);
\r
1209 snprintf(tmp, sizeof(tmp), "Microsoft Windows %lu.%lu %s", osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.szCSDVersion);
\r
1213 //! Notifies the device, that it has been resized
\r
1214 void CIrrDeviceWin32::OnResized()
\r
1219 //! Resize the render window.
\r
1220 void CIrrDeviceWin32::setWindowSize(const irr::core::dimension2d<u32>& size)
\r
1222 if (ExternalWindow || !getVideoDriver() || CreationParams.Fullscreen)
\r
1225 // get size of the window for the give size of the client area
\r
1226 DWORD style = static_cast<DWORD>(GetWindowLongPtr(HWnd, GWL_STYLE));
\r
1227 DWORD exStyle = static_cast<DWORD>(GetWindowLongPtr(HWnd, GWL_EXSTYLE));
\r
1229 clientSize.top = 0;
\r
1230 clientSize.left = 0;
\r
1231 clientSize.right = size.Width;
\r
1232 clientSize.bottom = size.Height;
\r
1233 AdjustWindowRectEx(&clientSize, style, false, exStyle);
\r
1234 const s32 realWidth = clientSize.right - clientSize.left;
\r
1235 const s32 realHeight = clientSize.bottom - clientSize.top;
\r
1237 UINT flags = SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER;
\r
1238 SetWindowPos(HWnd, HWND_TOP, 0, 0, realWidth, realHeight, flags);
\r
1241 //! Sets if the window should be resizable in windowed mode.
\r
1242 void CIrrDeviceWin32::setResizable(bool resize)
\r
1244 if (ExternalWindow || !getVideoDriver() || CreationParams.Fullscreen)
\r
1247 LONG_PTR style = (LONG_PTR)getWindowStyle(false, resize);
\r
1248 if (!SetWindowLongPtr(HWnd, GWL_STYLE, style))
\r
1249 os::Printer::log("Could not change window style.");
\r
1252 clientSize.top = 0;
\r
1253 clientSize.left = 0;
\r
1254 clientSize.right = getVideoDriver()->getScreenSize().Width;
\r
1255 clientSize.bottom = getVideoDriver()->getScreenSize().Height;
\r
1257 AdjustWindowRect(&clientSize, static_cast<DWORD>(style), FALSE);
\r
1259 const s32 realWidth = clientSize.right - clientSize.left;
\r
1260 const s32 realHeight = clientSize.bottom - clientSize.top;
\r
1262 const s32 windowLeft = (GetSystemMetrics(SM_CXSCREEN) - realWidth) / 2;
\r
1263 const s32 windowTop = (GetSystemMetrics(SM_CYSCREEN) - realHeight) / 2;
\r
1265 SetWindowPos(HWnd, HWND_TOP, windowLeft, windowTop, realWidth, realHeight,
\r
1266 SWP_FRAMECHANGED | SWP_NOMOVE | SWP_SHOWWINDOW);
\r
1268 static_cast<CCursorControl*>(CursorControl)->updateBorderSize(CreationParams.Fullscreen, resize);
\r
1272 //! Minimizes the window.
\r
1273 void CIrrDeviceWin32::minimizeWindow()
\r
1275 WINDOWPLACEMENT wndpl;
\r
1276 wndpl.length = sizeof(WINDOWPLACEMENT);
\r
1277 GetWindowPlacement(HWnd, &wndpl);
\r
1278 wndpl.showCmd = SW_SHOWMINNOACTIVE;
\r
1279 SetWindowPlacement(HWnd, &wndpl);
\r
1283 //! Maximizes the window.
\r
1284 void CIrrDeviceWin32::maximizeWindow()
\r
1286 WINDOWPLACEMENT wndpl;
\r
1287 wndpl.length = sizeof(WINDOWPLACEMENT);
\r
1288 GetWindowPlacement(HWnd, &wndpl);
\r
1289 wndpl.showCmd = SW_SHOWMAXIMIZED;
\r
1290 SetWindowPlacement(HWnd, &wndpl);
\r
1292 WindowMaximized = true;
\r
1296 //! Restores the window to its original size.
\r
1297 void CIrrDeviceWin32::restoreWindow()
\r
1299 WINDOWPLACEMENT wndpl;
\r
1300 wndpl.length = sizeof(WINDOWPLACEMENT);
\r
1301 GetWindowPlacement(HWnd, &wndpl);
\r
1302 wndpl.showCmd = SW_SHOWNORMAL;
\r
1303 SetWindowPlacement(HWnd, &wndpl);
\r
1305 WindowMaximized = false;
\r
1308 core::position2di CIrrDeviceWin32::getWindowPosition()
\r
1310 WINDOWPLACEMENT wndpl;
\r
1311 wndpl.length = sizeof(WINDOWPLACEMENT);
\r
1312 if (GetWindowPlacement(HWnd, &wndpl))
\r
1314 return core::position2di((int)wndpl.rcNormalPosition.left,
\r
1315 (int)wndpl.rcNormalPosition.top);
\r
1319 // No reason for this to happen
\r
1320 os::Printer::log("Failed to retrieve window location", ELL_ERROR);
\r
1321 return core::position2di(-1, -1);
\r
1325 bool CIrrDeviceWin32::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
\r
1328 return JoyControl->activateJoysticks(joystickInfo);
\r
1334 //! Process system events
\r
1335 void CIrrDeviceWin32::handleSystemMessages()
\r
1339 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
\r
1341 if (ExternalWindow && msg.hwnd == HWnd)
\r
1343 if (msg.hwnd == HWnd)
\r
1345 WndProc(HWnd, msg.message, msg.wParam, msg.lParam);
\r
1349 TranslateMessage(&msg);
\r
1350 DispatchMessage(&msg);
\r
1355 // No message translation because we don't use WM_CHAR and it would conflict with our
\r
1356 // deadkey handling.
\r
1357 DispatchMessage(&msg);
\r
1360 if (msg.message == WM_QUIT)
\r
1366 //! Remove all messages pending in the system message loop
\r
1367 void CIrrDeviceWin32::clearSystemMessages()
\r
1370 while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE))
\r
1372 while (PeekMessage(&msg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE))
\r
1376 // Convert an Irrlicht texture to a Windows cursor
\r
1377 // Based on http://www.codeguru.com/cpp/w-p/win32/cursors/article.php/c4529/
\r
1378 HCURSOR CIrrDeviceWin32::TextureToCursor(HWND hwnd, irr::video::ITexture * tex, const core::rect<s32>& sourceRect, const core::position2d<s32> &hotspot)
\r
1381 // create the bitmaps needed for cursors from the texture
\r
1383 HDC dc = GetDC(hwnd);
\r
1384 HDC andDc = CreateCompatibleDC(dc);
\r
1385 HDC xorDc = CreateCompatibleDC(dc);
\r
1386 HBITMAP andBitmap = CreateCompatibleBitmap(dc, sourceRect.getWidth(), sourceRect.getHeight());
\r
1387 HBITMAP xorBitmap = CreateCompatibleBitmap(dc, sourceRect.getWidth(), sourceRect.getHeight());
\r
1389 HBITMAP oldAndBitmap = (HBITMAP)SelectObject(andDc, andBitmap);
\r
1390 HBITMAP oldXorBitmap = (HBITMAP)SelectObject(xorDc, xorBitmap);
\r
1393 video::ECOLOR_FORMAT format = tex->getColorFormat();
\r
1394 u32 bytesPerPixel = video::IImage::getBitsPerPixelFromFormat(format) / 8;
\r
1395 u32 bytesLeftGap = sourceRect.UpperLeftCorner.X * bytesPerPixel;
\r
1396 u32 bytesRightGap = tex->getPitch() - sourceRect.LowerRightCorner.X * bytesPerPixel;
\r
1397 const u8* data = (const u8*)tex->lock(video::ETLM_READ_ONLY, 0);
\r
1398 data += sourceRect.UpperLeftCorner.Y*tex->getPitch();
\r
1399 for ( s32 y = 0; y < sourceRect.getHeight(); ++y )
\r
1401 data += bytesLeftGap;
\r
1402 for ( s32 x = 0; x < sourceRect.getWidth(); ++x )
\r
1404 video::SColor pixelCol;
\r
1405 pixelCol.setData((const void*)data, format);
\r
1406 data += bytesPerPixel;
\r
1408 if ( pixelCol.getAlpha() == 0 ) // transparent
\r
1410 SetPixel(andDc, x, y, RGB(255,255,255));
\r
1411 SetPixel(xorDc, x, y, RGB(0,0,0));
\r
1415 SetPixel(andDc, x, y, RGB(0,0,0));
\r
1416 SetPixel(xorDc, x, y, RGB(pixelCol.getRed(), pixelCol.getGreen(), pixelCol.getBlue()));
\r
1419 data += bytesRightGap;
\r
1423 SelectObject(andDc, oldAndBitmap);
\r
1424 SelectObject(xorDc, oldXorBitmap);
\r
1429 ReleaseDC(hwnd, dc);
\r
1431 // create the cursor
\r
1433 ICONINFO iconinfo;
\r
1434 iconinfo.fIcon = false; // type is cursor not icon
\r
1435 iconinfo.xHotspot = hotspot.X;
\r
1436 iconinfo.yHotspot = hotspot.Y;
\r
1437 iconinfo.hbmMask = andBitmap;
\r
1438 iconinfo.hbmColor = xorBitmap;
\r
1440 HCURSOR cursor = CreateIconIndirect(&iconinfo);
\r
1442 DeleteObject(andBitmap);
\r
1443 DeleteObject(xorBitmap);
\r
1449 CIrrDeviceWin32::CCursorControl::CCursorControl(CIrrDeviceWin32* device, const core::dimension2d<u32>& wsize, HWND hwnd, bool fullscreen)
\r
1450 : Device(device), WindowSize(wsize), InvWindowSize(0.0f, 0.0f),
\r
1451 HWnd(hwnd), BorderX(0), BorderY(0),
\r
1452 UseReferenceRect(false), IsVisible(true)
\r
1453 , ActiveIcon(gui::ECI_NORMAL), ActiveIconStartTime(0)
\r
1455 if (WindowSize.Width!=0)
\r
1456 InvWindowSize.Width = 1.0f / WindowSize.Width;
\r
1458 if (WindowSize.Height!=0)
\r
1459 InvWindowSize.Height = 1.0f / WindowSize.Height;
\r
1461 updateBorderSize(fullscreen, false);
\r
1465 CIrrDeviceWin32::CCursorControl::~CCursorControl()
\r
1467 for ( u32 i=0; i < Cursors.size(); ++i )
\r
1469 for ( u32 f=0; f < Cursors[i].Frames.size(); ++f )
\r
1471 DestroyCursor(Cursors[i].Frames[f].IconHW);
\r
1477 void CIrrDeviceWin32::CCursorControl::initCursors()
\r
1479 Cursors.push_back( CursorW32(LoadCursor(NULL, IDC_ARROW)) );
\r
1480 Cursors.push_back( CursorW32(LoadCursor(NULL, IDC_CROSS)) );
\r
1481 Cursors.push_back( CursorW32(LoadCursor(NULL, IDC_HAND)) );
\r
1482 Cursors.push_back( CursorW32(LoadCursor(NULL, IDC_HELP)) );
\r
1483 Cursors.push_back( CursorW32(LoadCursor(NULL, IDC_IBEAM)) );
\r
1484 Cursors.push_back( CursorW32(LoadCursor(NULL, IDC_NO)) );
\r
1485 Cursors.push_back( CursorW32(LoadCursor(NULL, IDC_WAIT)) );
\r
1486 Cursors.push_back( CursorW32(LoadCursor(NULL, IDC_SIZEALL)) );
\r
1487 Cursors.push_back( CursorW32(LoadCursor(NULL, IDC_SIZENESW)) );
\r
1488 Cursors.push_back( CursorW32(LoadCursor(NULL, IDC_SIZENWSE)) );
\r
1489 Cursors.push_back( CursorW32(LoadCursor(NULL, IDC_SIZENS)) );
\r
1490 Cursors.push_back( CursorW32(LoadCursor(NULL, IDC_SIZEWE)) );
\r
1491 Cursors.push_back( CursorW32(LoadCursor(NULL, IDC_UPARROW)) );
\r
1495 void CIrrDeviceWin32::CCursorControl::update()
\r
1497 if ( !Cursors[ActiveIcon].Frames.empty() && Cursors[ActiveIcon].FrameTime )
\r
1499 // update animated cursors. This could also be done by X11 in case someone wants to figure that out (this way was just easier to implement)
\r
1500 u32 now = Device->getTimer()->getRealTime();
\r
1501 u32 frame = ((now - ActiveIconStartTime) / Cursors[ActiveIcon].FrameTime) % Cursors[ActiveIcon].Frames.size();
\r
1502 SetCursor( Cursors[ActiveIcon].Frames[frame].IconHW );
\r
1506 //! Sets the active cursor icon
\r
1507 void CIrrDeviceWin32::CCursorControl::setActiveIcon(gui::ECURSOR_ICON iconId)
\r
1509 if ( iconId >= (s32)Cursors.size() )
\r
1512 ActiveIcon = iconId;
\r
1513 ActiveIconStartTime = Device->getTimer()->getRealTime();
\r
1514 if ( Cursors[ActiveIcon].Frames.size() )
\r
1515 SetCursor( Cursors[ActiveIcon].Frames[0].IconHW );
\r
1519 //! Add a custom sprite as cursor icon.
\r
1520 gui::ECURSOR_ICON CIrrDeviceWin32::CCursorControl::addIcon(const gui::SCursorSprite& icon)
\r
1522 if ( icon.SpriteId >= 0 )
\r
1525 cW32.FrameTime = icon.SpriteBank->getSprites()[icon.SpriteId].frameTime;
\r
1527 for ( u32 i=0; i < icon.SpriteBank->getSprites()[icon.SpriteId].Frames.size(); ++i )
\r
1529 irr::u32 texId = icon.SpriteBank->getSprites()[icon.SpriteId].Frames[i].textureNumber;
\r
1530 irr::u32 rectId = icon.SpriteBank->getSprites()[icon.SpriteId].Frames[i].rectNumber;
\r
1531 irr::core::rect<s32> rectIcon = icon.SpriteBank->getPositions()[rectId];
\r
1533 HCURSOR hc = Device->TextureToCursor(HWnd, icon.SpriteBank->getTexture(texId), rectIcon, icon.HotSpot);
\r
1534 cW32.Frames.push_back( CursorFrameW32(hc) );
\r
1537 Cursors.push_back( cW32 );
\r
1538 return (gui::ECURSOR_ICON)(Cursors.size() - 1);
\r
1540 return gui::ECI_NORMAL;
\r
1544 //! replace the given cursor icon.
\r
1545 void CIrrDeviceWin32::CCursorControl::changeIcon(gui::ECURSOR_ICON iconId, const gui::SCursorSprite& icon)
\r
1547 if ( iconId >= (s32)Cursors.size() )
\r
1550 for ( u32 i=0; i < Cursors[iconId].Frames.size(); ++i )
\r
1551 DestroyCursor(Cursors[iconId].Frames[i].IconHW);
\r
1553 if ( icon.SpriteId >= 0 )
\r
1556 cW32.FrameTime = icon.SpriteBank->getSprites()[icon.SpriteId].frameTime;
\r
1557 for ( u32 i=0; i < icon.SpriteBank->getSprites()[icon.SpriteId].Frames.size(); ++i )
\r
1559 irr::u32 texId = icon.SpriteBank->getSprites()[icon.SpriteId].Frames[i].textureNumber;
\r
1560 irr::u32 rectId = icon.SpriteBank->getSprites()[icon.SpriteId].Frames[i].rectNumber;
\r
1561 irr::core::rect<s32> rectIcon = icon.SpriteBank->getPositions()[rectId];
\r
1563 HCURSOR hc = Device->TextureToCursor(HWnd, icon.SpriteBank->getTexture(texId), rectIcon, icon.HotSpot);
\r
1564 cW32.Frames.push_back( CursorFrameW32(hc) );
\r
1567 Cursors[iconId] = cW32;
\r
1572 //! Return a system-specific size which is supported for cursors. Larger icons will fail, smaller icons might work.
\r
1573 core::dimension2di CIrrDeviceWin32::CCursorControl::getSupportedIconSize() const
\r
1575 core::dimension2di result;
\r
1577 result.Width = GetSystemMetrics(SM_CXCURSOR);
\r
1578 result.Height = GetSystemMetrics(SM_CYCURSOR);
\r
1585 } // end namespace
\r
1587 #endif // _IRR_COMPILE_WITH_WINDOWS_DEVICE_
\r