如何将Joystick支持或Xbox 360支持集成到SDL中

时间:2013-07-13 05:54:39

标签: c++ controller sdl

我正在尝试找到将Xbox 360编码到我的项目中的示例或教程。到目前为止,我一直只使用SDL_keysym来使用箭头键和WASD键来移动我的精灵。想知道我如何整合Xbox 360(最好)或操纵杆支持。

以下是我目前使用箭头和WASD键移动我的精灵Spaceship1的代码。请原谅我,因为代码杂乱无章。

struct oSprite 
{
int m_nTotalFrames;
int m_nCurrentFrame;
int m_nFrameWidth;
int m_nFrameHeight;

fVector2 m_vPosition;

SDL_Surface* m_pImage;

bool        m_bIsHuman;
oControls   m_oControls;
oControls   m_oAltControls;
};

bool Update()
{
fVector2 vKeyPresses;

Uint8* paunKeyStates = SDL_GetKeyState(NULL);
float fMoveSpeed = 0.5f;

if(paunKeyStates[g_Spaceship1.m_oControls.m_nUpButton] || paunKeyStates[g_Spaceship1.m_oAltControls.m_nUpButton])
{
    vKeyPresses.y -= fMoveSpeed;
}

if(paunKeyStates[g_Spaceship1.m_oControls.m_nDownButton] || paunKeyStates[g_Spaceship1.m_oAltControls.m_nDownButton])
{
    vKeyPresses.y += fMoveSpeed;
}

if(paunKeyStates[g_Spaceship1.m_oControls.m_nLeftButton] || paunKeyStates[g_Spaceship1.m_oAltControls.m_nLeftButton])
{
    vKeyPresses.x -= fMoveSpeed;
}

if(paunKeyStates[g_Spaceship1.m_oControls.m_nRightButton] || paunKeyStates[g_Spaceship1.m_oAltControls.m_nRightButton])
{
    vKeyPresses.x += fMoveSpeed;
}
} 

1 个答案:

答案 0 :(得分:0)

在SDL的初始化试试这个

SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK)