Win32/Joystick.cpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
26 // Headers
28 #define _WIN32_WINDOWS 0x0501
29 #define _WIN32_WINNT 0x0501
30 #include <SFML/Window/Joystick.hpp>
31 #include <windows.h>
32 #include <mmsystem.h>
33 
34 
35 namespace sf
36 {
37 namespace priv
38 {
42 void Joystick::Initialize(unsigned int Index)
43 {
44  // Reset state
45  myIndex = JOYSTICKID1;
46  myNbButtons = 0;
47  myIsConnected = false;
48  myHasContinuousPOV = false;
49  for (int i = 0; i < Joy::AxisCount; ++i)
50  myAxes[i] = false;
51 
52  // Get the Index-th connected joystick
53  MMRESULT Error;
54  JOYINFOEX JoyInfo;
55  JoyInfo.dwSize = sizeof(JoyInfo);
56  JoyInfo.dwFlags = JOY_RETURNALL;
57  for (unsigned int NbFound = 0; (Error = joyGetPosEx(myIndex, &JoyInfo)) != JOYERR_PARMS; myIndex++)
58  {
59  // Check if the current joystick is connected
60  if (Error == JOYERR_NOERROR)
61  {
62  // Check if it's the required index
63  if (NbFound == Index)
64  {
65  // Ok : store its parameters and return
66  myIsConnected = true;
67  JOYCAPS Caps;
68  joyGetDevCaps(myIndex, &Caps, sizeof(Caps));
69  myNbButtons = Caps.wNumButtons;
70  if (myNbButtons > Joy::ButtonCount)
71  myNbButtons = Joy::ButtonCount;
72 
73  myAxes[Joy::AxisX] = true;
74  myAxes[Joy::AxisY] = true;
75  myAxes[Joy::AxisZ] = (Caps.wCaps & JOYCAPS_HASZ) != 0;
76  myAxes[Joy::AxisR] = (Caps.wCaps & JOYCAPS_HASR) != 0;
77  myAxes[Joy::AxisU] = (Caps.wCaps & JOYCAPS_HASU) != 0;
78  myAxes[Joy::AxisV] = (Caps.wCaps & JOYCAPS_HASV) != 0;
79  myAxes[Joy::AxisPOV] = (Caps.wCaps & JOYCAPS_HASPOV) != 0;
80  myHasContinuousPOV = (Caps.wCaps & JOYCAPS_POVCTS) != 0;
81 
82  return;
83  }
84 
85  // Go to the next valid joystick
86  ++NbFound;
87  }
88  }
89 }
90 
91 
95 JoystickState Joystick::UpdateState()
96 {
97  JoystickState State;
98 
99  if (myIsConnected)
100  {
101  // Get the joystick caps (for range conversions)
102  JOYCAPS Caps;
103  if (joyGetDevCaps(myIndex, &Caps, sizeof(Caps)) == JOYERR_NOERROR)
104  {
105  // Get the current joystick state
106  JOYINFOEX Pos;
107  Pos.dwFlags = JOY_RETURNX | JOY_RETURNY | JOY_RETURNZ | JOY_RETURNR | JOY_RETURNU | JOY_RETURNV | JOY_RETURNBUTTONS;
108  Pos.dwFlags |= myHasContinuousPOV ? JOY_RETURNPOVCTS : JOY_RETURNPOV;
109  Pos.dwSize = sizeof(JOYINFOEX);
110  if (joyGetPosEx(myIndex, &Pos) == JOYERR_NOERROR)
111  {
112  // Axes
113  State.Axis[Joy::AxisX] = (Pos.dwXpos - (Caps.wXmax + Caps.wXmin) / 2.f) * 200.f / (Caps.wXmax - Caps.wXmin);
114  State.Axis[Joy::AxisY] = (Pos.dwYpos - (Caps.wYmax + Caps.wYmin) / 2.f) * 200.f / (Caps.wYmax - Caps.wYmin);
115  State.Axis[Joy::AxisZ] = (Pos.dwZpos - (Caps.wZmax + Caps.wZmin) / 2.f) * 200.f / (Caps.wZmax - Caps.wZmin);
116  State.Axis[Joy::AxisR] = (Pos.dwRpos - (Caps.wRmax + Caps.wRmin) / 2.f) * 200.f / (Caps.wRmax - Caps.wRmin);
117  State.Axis[Joy::AxisU] = (Pos.dwUpos - (Caps.wUmax + Caps.wUmin) / 2.f) * 200.f / (Caps.wUmax - Caps.wUmin);
118  State.Axis[Joy::AxisV] = (Pos.dwVpos - (Caps.wVmax + Caps.wVmin) / 2.f) * 200.f / (Caps.wVmax - Caps.wVmin);
119 
120  // POV
121  if (Pos.dwPOV != 0xFFFF)
122  State.Axis[Joy::AxisPOV] = Pos.dwPOV / 100.f;
123  else
124  State.Axis[Joy::AxisPOV] = -1.f;
125 
126  // Buttons
127  for (unsigned int i = 0; i < GetButtonsCount(); ++i)
128  State.Buttons[i] = (Pos.dwButtons & (1 << i)) != 0;
129  }
130  }
131  }
132 
133  return State;
134 }
135 
136 
140 bool Joystick::HasAxis(Joy::Axis Axis) const
141 {
142  return myAxes[Axis];
143 }
144 
145 
149 unsigned int Joystick::GetButtonsCount() const
150 {
151  return myNbButtons;
152 }
153 
154 
155 } // namespace priv
156 
157 } // namespace sf
Total number of supported joystick buttons.
Definition: Event.hpp:189