Win32 2Joystick 8cpp source


SFML - Simple and Fast Multimedia Library Main Page Namespaces Classes Files File List Joystick.cpp00001 00002 // 00003 // SFML - Simple and Fast Multimedia Library 00004 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) 00005 // 00006 // This software is provided 'as-is', without any express or implied warranty. 00007 // In no event will the authors be held liable for any damages arising from the use of this software. 00008 // 00009 // Permission is granted to anyone to use this software for any purpose, 00010 // including commercial applications, and to alter it and redistribute it freely, 00011 // subject to the following restrictions: 00012 // 00013 // 1. The origin of this software must not be misrepresented; 00014 // you must not claim that you wrote the original software. 00015 // If you use this software in a product, an acknowledgment 00016 // in the product documentation would be appreciated but is not required. 00017 // 00018 // 2. Altered source versions must be plainly marked as such, 00019 // and must not be misrepresented as being the original software. 00020 // 00021 // 3. This notice may not be removed or altered from any source distribution. 00022 // 00024 00026 // Headers 00028 #define _WIN32_WINDOWS 0x0501 00029 #define _WIN32_WINNT 0x0501 00030 #include <SFML/Window/Joystick.hpp> 00031 #include <windows.h> 00032 #include <mmsystem.h> 00033 00034 00035 namespace sf 00036 { 00037 namespace priv 00038 { 00042 void Joystick::Initialize(unsigned int Index) 00043 { 00044 // Reset state 00045 myIndex = JOYSTICKID1; 00046 myNbButtons = 0; 00047 myIsConnected = false; 00048 myHasContinuousPOV = false; 00049 for (int i = 0; i < Joy::AxisCount; ++i) 00050 myAxes[i] = false; 00051 00052 // Get the Index-th connected joystick 00053 MMRESULT Error; 00054 JOYINFOEX JoyInfo; 00055 JoyInfo.dwSize = sizeof(JoyInfo); 00056 JoyInfo.dwFlags = JOY_RETURNALL; 00057 for (unsigned int NbFound = 0; (Error = joyGetPosEx(myIndex, &JoyInfo)) != JOYERR_PARMS; myIndex++) 00058 { 00059 // Check if the current joystick is connected 00060 if (Error == JOYERR_NOERROR) 00061 { 00062 // Check if it's the required index 00063 if (NbFound == Index) 00064 { 00065 // Ok : store its parameters and return 00066 myIsConnected = true; 00067 JOYCAPS Caps; 00068 joyGetDevCaps(myIndex, &Caps, sizeof(Caps)); 00069 myNbButtons = Caps.wNumButtons; 00070 if (myNbButtons > Joy::ButtonCount) 00071 myNbButtons = Joy::ButtonCount; 00072 00073 myAxes[Joy::AxisX] = true; 00074 myAxes[Joy::AxisY] = true; 00075 myAxes[Joy::AxisZ] = (Caps.wCaps & JOYCAPS_HASZ) != 0; 00076 myAxes[Joy::AxisR] = (Caps.wCaps & JOYCAPS_HASR) != 0; 00077 myAxes[Joy::AxisU] = (Caps.wCaps & JOYCAPS_HASU) != 0; 00078 myAxes[Joy::AxisV] = (Caps.wCaps & JOYCAPS_HASV) != 0; 00079 myAxes[Joy::AxisPOV] = (Caps.wCaps & JOYCAPS_HASPOV) != 0; 00080 myHasContinuousPOV = (Caps.wCaps & JOYCAPS_POVCTS) != 0; 00081 00082 return; 00083 } 00084 00085 // Go to the next valid joystick 00086 ++NbFound; 00087 } 00088 } 00089 } 00090 00091 00095 JoystickState Joystick::UpdateState() 00096 { 00097 JoystickState State; 00098 00099 if (myIsConnected) 00100 { 00101 // Get the joystick caps (for range conversions) 00102 JOYCAPS Caps; 00103 if (joyGetDevCaps(myIndex, &Caps, sizeof(Caps)) == JOYERR_NOERROR) 00104 { 00105 // Get the current joystick state 00106 JOYINFOEX Pos; 00107 Pos.dwFlags = JOY_RETURNX | JOY_RETURNY | JOY_RETURNZ | JOY_RETURNR | JOY_RETURNU | JOY_RETURNV | JOY_RETURNBUTTONS; 00108 Pos.dwFlags |= myHasContinuousPOV ? JOY_RETURNPOVCTS : JOY_RETURNPOV; 00109 Pos.dwSize = sizeof(JOYINFOEX); 00110 if (joyGetPosEx(myIndex, &Pos) == JOYERR_NOERROR) 00111 { 00112 // Axes 00113 State.Axis[Joy::AxisX] = (Pos.dwXpos - (Caps.wXmax + Caps.wXmin) / 2.f) * 200.f / (Caps.wXmax - Caps.wXmin); 00114 State.Axis[Joy::AxisY] = (Pos.dwYpos - (Caps.wYmax + Caps.wYmin) / 2.f) * 200.f / (Caps.wYmax - Caps.wYmin); 00115 State.Axis[Joy::AxisZ] = (Pos.dwZpos - (Caps.wZmax + Caps.wZmin) / 2.f) * 200.f / (Caps.wZmax - Caps.wZmin); 00116 State.Axis[Joy::AxisR] = (Pos.dwRpos - (Caps.wRmax + Caps.wRmin) / 2.f) * 200.f / (Caps.wRmax - Caps.wRmin); 00117 State.Axis[Joy::AxisU] = (Pos.dwUpos - (Caps.wUmax + Caps.wUmin) / 2.f) * 200.f / (Caps.wUmax - Caps.wUmin); 00118 State.Axis[Joy::AxisV] = (Pos.dwVpos - (Caps.wVmax + Caps.wVmin) / 2.f) * 200.f / (Caps.wVmax - Caps.wVmin); 00119 00120 // POV 00121 if (Pos.dwPOV != 0xFFFF) 00122 State.Axis[Joy::AxisPOV] = Pos.dwPOV / 100.f; 00123 else 00124 State.Axis[Joy::AxisPOV] = -1.f; 00125 00126 // Buttons 00127 for (unsigned int i = 0; i < GetButtonsCount(); ++i) 00128 State.Buttons[i] = (Pos.dwButtons & (1 << i)) != 0; 00129 } 00130 } 00131 } 00132 00133 return State; 00134 } 00135 00136 00140 bool Joystick::HasAxis(Joy::Axis Axis) const 00141 { 00142 return myAxes[Axis]; 00143 } 00144 00145 00149 unsigned int Joystick::GetButtonsCount() const 00150 { 00151 return myNbButtons; 00152 } 00153 00154 00155 } // namespace priv 00156 00157 } // namespace sf  ::  Copyright © 2007-2008 Laurent Gomila, all rights reserved  ::  Documentation generated by doxygen 1.5.2  :: 

Wyszukiwarka