![[]](/images/special/trans.gif)
LibN2L-4 Library Code ReferenceClassesCompounds Files Members Method Index Full Reference cJoystick.cppGo to the documentation of this file.00001 #include "events/cJoystick.h" 00002 #include "events/constants.h" 00003 00004 /******************************************************************************/ 00005 namespace n2l { 00006 00007 /**************************************************************************/ 00008 cJoystick::cJoystick( const tUint iSDLJoyIndex ) 00009 { 00010 requireSDLSubSystem(SDLSubSystemInitFlag_Joystick); 00011 00012 if (iSDLJoyIndex>=tUint(SDL_NumJoysticks())) 00013 throw cOutOfBoundsException("cJoystick::cJoystick", 00014 "Joystick out of range, how the hell did that happen?"); 00015 00016 mSDLJoyIndex = iSDLJoyIndex; 00017 00018 mJoy = SDL_JoystickOpen( mSDLJoyIndex ); 00019 if (!mJoy) 00020 throw cSDLException( "cJoystick::cJoystick", 00021 "Failed to open requested Joystick: "+asString(iSDLJoyIndex) ); 00022 00023 mName = SDL_JoystickName( mSDLJoyIndex ); 00024 00025 mNumAxes = SDL_JoystickNumAxes(mJoy); 00026 mNumBalls = SDL_JoystickNumBalls(mJoy); 00027 mNumHats = SDL_JoystickNumHats(mJoy); 00028 mNumButtons = SDL_JoystickNumButtons(mJoy); 00029 00030 mAxisValue = new tFloat[mNumAxes]; 00031 mAxisInvert = new tBool[mNumAxes]; 00032 for (tUint i=0; i<mNumAxes; ++i) 00033 mAxisInvert[i] = false; 00034 } 00035 00036 /**************************************************************************/ 00037 cJoystick::~cJoystick() 00038 { 00039 SDL_JoystickClose( mJoy ); 00040 releaseSDLSubSystem(SDLSubSystemInitFlag_Joystick); 00041 } 00042 00043 /**************************************************************************/ 00044 const tFloat &cJoystick::axis( const tUint iNum ) const 00045 { 00046 if (iNum>=mNumAxes) 00047 throw cOutOfBoundsException( "cJoystick::axis", 00048 "No such joystick axis on "+mName, asString(iNum) ); 00049 const tFloat Val = SDL_JoystickGetAxis(mJoy, iNum); 00050 if (mAxisInvert[iNum]) 00051 mAxisValue[iNum] = Val/JoystickAxisInverseMagBound; 00052 else 00053 mAxisValue[iNum] = Val/JoystickAxisMagBound; 00054 return mAxisValue[iNum]; 00055 } 00056 00057 /**************************************************************************/ 00058 const tBool cJoystick::button( const tUint iNum ) const 00059 { 00060 if (iNum>=mNumButtons) 00061 throw cOutOfBoundsException( "cJoystick::button", 00062 "No such joystick button on "+mName, asString(iNum) ); 00063 00064 return SDL_JoystickGetButton(mJoy,iNum); 00065 } 00066 00067 /**************************************************************************/ 00068 const tUint cJoystick::hat( const tUint iNum ) const 00069 { 00070 if (iNum>=mNumHats) 00071 throw cOutOfBoundsException( "cJoystick::hat", 00072 "No such joystick hat on "+mName, asString(iNum) ); 00073 00074 return SDL_JoystickGetHat(mJoy,iNum); 00075 } 00076 00077 /**************************************************************************/ 00078 void cJoystick::inverted( const tUint iAxis, const tBool iValue ) 00079 { 00080 if (iAxis>=mNumAxes) 00081 throw cOutOfBoundsException( "cJoystick::inverted", 00082 "No such joystick axis on "+mName, asString(iAxis) ); 00083 mAxisInvert[iAxis] = iValue; 00084 } 00085 00086 } |