SFML logo
  • Main Page
  • Namespaces
  • Classes
  • Files
  • File List

Joystick.cpp

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 {
00040 void Joystick::Initialize(unsigned int index)
00041 {
00042     // Reset state
00043     myIndex            = JOYSTICKID1;
00044     myNbButtons        = 0;
00045     myIsConnected      = false;
00046     myHasContinuousPOV = false;
00047     for (int i = 0; i < Joy::AxisCount; ++i)
00048         myAxes[i] = false;
00049 
00050     // Get the Index-th connected joystick
00051     MMRESULT error;
00052     JOYINFOEX joyInfos;
00053     joyInfos.dwSize = sizeof(joyInfos);
00054     joyInfos.dwFlags = JOY_RETURNALL;
00055     for (unsigned int found = 0; (error = joyGetPosEx(myIndex, &joyInfos)) != JOYERR_PARMS; myIndex++)
00056     {
00057         // Check if the current joystick is connected
00058         if (error == JOYERR_NOERROR)
00059         {
00060             // Check if it's the required index
00061             if (found == index)
00062             {
00063                 // Ok : store its parameters and return
00064                 myIsConnected = true;
00065                 JOYCAPS caps;
00066                 joyGetDevCaps(myIndex, &caps, sizeof(caps));
00067                 myNbButtons = caps.wNumButtons;
00068                 if (myNbButtons > Joy::ButtonCount)
00069                     myNbButtons = Joy::ButtonCount;
00070 
00071                 myAxes[Joy::AxisX]   = true;
00072                 myAxes[Joy::AxisY]   = true;
00073                 myAxes[Joy::AxisZ]   = (caps.wCaps & JOYCAPS_HASZ) != 0;
00074                 myAxes[Joy::AxisR]   = (caps.wCaps & JOYCAPS_HASR) != 0;
00075                 myAxes[Joy::AxisU]   = (caps.wCaps & JOYCAPS_HASU) != 0;
00076                 myAxes[Joy::AxisV]   = (caps.wCaps & JOYCAPS_HASV) != 0;
00077                 myAxes[Joy::AxisPOV] = (caps.wCaps & JOYCAPS_HASPOV) != 0;
00078                 myHasContinuousPOV   = (caps.wCaps & JOYCAPS_POVCTS) != 0;
00079 
00080                 return;
00081             }
00082 
00083             // Go to the next valid joystick
00084             ++found;
00085         }
00086     }
00087 }
00088 
00089 
00091 JoystickState Joystick::UpdateState()
00092 {
00093     JoystickState state;
00094 
00095     if (myIsConnected)
00096     {
00097         // Get the joystick caps (for range conversions)
00098         JOYCAPS caps;
00099         if (joyGetDevCaps(myIndex, &caps, sizeof(caps)) == JOYERR_NOERROR)
00100         {
00101             // Get the current joystick state
00102             JOYINFOEX pos;
00103             pos.dwFlags  = JOY_RETURNX | JOY_RETURNY | JOY_RETURNZ | JOY_RETURNR | JOY_RETURNU | JOY_RETURNV | JOY_RETURNBUTTONS;
00104             pos.dwFlags |= myHasContinuousPOV ? JOY_RETURNPOVCTS : JOY_RETURNPOV;
00105             pos.dwSize   = sizeof(JOYINFOEX);
00106             if (joyGetPosEx(myIndex, &pos) == JOYERR_NOERROR)
00107             {
00108                 // Axes
00109                 state.Axis[Joy::AxisX] = (pos.dwXpos - (caps.wXmax + caps.wXmin) / 2.f) * 200.f / (caps.wXmax - caps.wXmin);
00110                 state.Axis[Joy::AxisY] = (pos.dwYpos - (caps.wYmax + caps.wYmin) / 2.f) * 200.f / (caps.wYmax - caps.wYmin);
00111                 state.Axis[Joy::AxisZ] = (pos.dwZpos - (caps.wZmax + caps.wZmin) / 2.f) * 200.f / (caps.wZmax - caps.wZmin);
00112                 state.Axis[Joy::AxisR] = (pos.dwRpos - (caps.wRmax + caps.wRmin) / 2.f) * 200.f / (caps.wRmax - caps.wRmin);
00113                 state.Axis[Joy::AxisU] = (pos.dwUpos - (caps.wUmax + caps.wUmin) / 2.f) * 200.f / (caps.wUmax - caps.wUmin);
00114                 state.Axis[Joy::AxisV] = (pos.dwVpos - (caps.wVmax + caps.wVmin) / 2.f) * 200.f / (caps.wVmax - caps.wVmin);
00115 
00116                 // POV
00117                 if (pos.dwPOV != 0xFFFF)
00118                     state.Axis[Joy::AxisPOV] = pos.dwPOV / 100.f;
00119                 else
00120                     state.Axis[Joy::AxisPOV] = -1.f;
00121 
00122                 // Buttons
00123                 for (unsigned int i = 0; i < GetButtonsCount(); ++i)
00124                     state.Buttons[i] = (pos.dwButtons & (1 << i)) != 0;
00125             }
00126         }
00127     }
00128 
00129     return state;
00130 }
00131 
00132 
00134 bool Joystick::HasAxis(Joy::Axis Axis) const
00135 {
00136     return myAxes[Axis];
00137 }
00138 
00139 
00141 unsigned int Joystick::GetButtonsCount() const
00142 {
00143     return myNbButtons;
00144 }
00145 
00146 
00147 } // namespace priv
00148 
00149 } // namespace sf

 ::  Copyright © 2007-2008 Laurent Gomila, all rights reserved  ::  Documentation generated by doxygen 1.5.2  ::