00001 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 myNbAxes = 0; 00047 myNbButtons = 0; 00048 00049 // Get the Index-th connected joystick 00050 MMRESULT Error; 00051 JOYINFOEX JoyInfo; 00052 JoyInfo.dwSize = sizeof(JoyInfo); 00053 JoyInfo.dwFlags = JOY_RETURNALL; 00054 for (unsigned int NbFound = 0; (Error = joyGetPosEx(myIndex, &JoyInfo)) != JOYERR_PARMS; myIndex++) 00055 { 00056 // Check if the current joystick is connected 00057 if (Error == JOYERR_NOERROR) 00058 { 00059 // Check if it's the required index 00060 if (NbFound == Index) 00061 { 00062 // Ok : store its parameters and return 00063 JOYCAPS Caps; 00064 joyGetDevCaps(myIndex, &Caps, sizeof(Caps)); 00065 myNbAxes = Caps.wNumAxes; 00066 myNbButtons = Caps.wNumButtons; 00067 if (myNbButtons > JoystickState::MaxButtons) 00068 myNbButtons = JoystickState::MaxButtons; 00069 00070 return; 00071 } 00072 00073 // Go to the next valid joystick 00074 ++NbFound; 00075 } 00076 } 00077 } 00078 00079 00083 JoystickState Joystick::UpdateState() 00084 { 00085 JoystickState State = {0}; 00086 00087 // Get the joystick caps (for range conversions) 00088 JOYCAPS Caps; 00089 if (joyGetDevCaps(myIndex, &Caps, sizeof(Caps)) == JOYERR_NOERROR) 00090 { 00091 // Get the current joystick state 00092 JOYINFOEX Pos; 00093 Pos.dwFlags = JOY_RETURNALL; 00094 Pos.dwSize = sizeof(JOYINFOEX); 00095 if (joyGetPosEx(myIndex, &Pos) == JOYERR_NOERROR) 00096 { 00097 // Axes 00098 State.Axis[Joy::AxisX] = (Pos.dwXpos - (Caps.wXmax + Caps.wXmin) / 2.f) * 200.f / (Caps.wXmax - Caps.wXmin); 00099 State.Axis[Joy::AxisY] = (Pos.dwYpos - (Caps.wYmax + Caps.wYmin) / 2.f) * 200.f / (Caps.wYmax - Caps.wYmin); 00100 State.Axis[Joy::AxisZ] = (Pos.dwZpos - (Caps.wZmax + Caps.wZmin) / 2.f) * 200.f / (Caps.wZmax - Caps.wZmin); 00101 State.Axis[Joy::AxisR] = (Pos.dwRpos - (Caps.wRmax + Caps.wRmin) / 2.f) * 200.f / (Caps.wRmax - Caps.wRmin); 00102 State.Axis[Joy::AxisU] = (Pos.dwUpos - (Caps.wUmax + Caps.wUmin) / 2.f) * 200.f / (Caps.wUmax - Caps.wUmin); 00103 State.Axis[Joy::AxisV] = (Pos.dwVpos - (Caps.wVmax + Caps.wVmin) / 2.f) * 200.f / (Caps.wVmax - Caps.wVmin); 00104 00105 // POV 00106 State.Axis[Joy::AxisPOV] = Pos.dwPOV / 100.f; 00107 00108 // Buttons 00109 for (unsigned int i = 0; i < GetButtonsCount(); ++i) 00110 State.Buttons[i] = (Pos.dwButtons & (1 << i)) != 0; 00111 } 00112 } 00113 00114 return State; 00115 } 00116 00117 00121 unsigned int Joystick::GetAxesCount() const 00122 { 00123 return myNbAxes; 00124 } 00125 00126 00130 unsigned int Joystick::GetButtonsCount() const 00131 { 00132 return myNbButtons; 00133 } 00134 00135 00136 } // namespace priv 00137 00138 } // namespace sf
:: Copyright © 2007-2008 Laurent Gomila, all rights reserved :: Documentation generated by doxygen 1.5.2 ::