如何在SDL 2.0 C++中使用操纵杆的数字键盘
How to use a digital pad of a joystick with SDL 2.0 C++
我尝试使用SDL来映射类似于xbox360游戏板的游戏板。现在使用按钮我使用这个代码:
std::vector<bool> tempButtons;
for (int j = 0; j < SDL_JoystickNumButtons(joy); j++)
{
tempButtons.push_back(false);
}
m_buttonStates.push_back(tempButtons);
joy是SDL_Joycle*,m_buttonStates是一个向量。不幸的是,这个功能不能映射数字键盘的4个按钮,我现在不知道为什么。。。这是我的游戏手柄:http://www.trust.com/it-it/all-products/17518-gxt-530-dual-stick-gamepad谢谢你,很抱歉我的英语不太好
谢谢lago Silva,但经过一些测试,我解决了这个代码的问题:
void InputHandler::Update() {
SDL_Event e;
while (SDL_PollEvent(&e)){
m_keyState = SDL_GetKeyboardState(NULL);
{
switch (e.type)
{
case SDL_QUIT:TheGame::Instance()->Quit(); break;
case SDL_KEYDOWN:KeyboardButtonDown(); break;
case SDL_KEYUP:KeyboardButtonUp(); break;
case SDL_JOYAXISMOTION:JoyAxisMotion(e); break;
case SDL_JOYBUTTONDOWN:JoyButtonDown(e); break;
case SDL_JOYBUTTONUP:JoyButtonUp(e); break;
case SDL_JOYHATMOTION:JoyHatMotion(e); break; //this is the code that I have implemented
case SDL_MOUSEBUTTONDOWN:MouseButtonDown(e); break;
case SDL_MOUSEBUTTONUP:MouseButtonUp(e); break;
case SDL_MOUSEMOTION:MouseMotion(e); break;
default:break;
}
}
}}
代码JoyHatMotion(e)调用这个函数:
void InputHandler::JoyHatMotion(SDL_Event e){
int whichOne = e.jaxis.which;
switch (e.jhat.value)
{
case SDL_HAT_DOWN:m_joyHatValues[whichOne]->Sety(1); m_joyHatValues[whichOne]->Setx(0); break;
case SDL_HAT_CENTERED:m_joyHatValues[whichOne]->Setx(0); m_joyHatValues[whichOne]->Sety(0); break;
case SDL_HAT_LEFTDOWN:m_joyHatValues[whichOne]->Setx(-1); m_joyHatValues[whichOne]->Sety(1); break;
case SDL_HAT_LEFT:m_joyHatValues[whichOne]->Setx(-1); m_joyHatValues[whichOne]->Sety(0); break;
case SDL_HAT_LEFTUP:m_joyHatValues[whichOne]->Setx(-1); m_joyHatValues[whichOne]->Sety(-1); break;
case SDL_HAT_UP:m_joyHatValues[whichOne]->Setx(0); m_joyHatValues[whichOne]->Sety(-1); break;
case SDL_HAT_RIGHTUP:m_joyHatValues[whichOne]->Setx(1); m_joyHatValues[whichOne]->Sety(-1); break;
case SDL_HAT_RIGHT:m_joyHatValues[whichOne]->Setx(1); m_joyHatValues[whichOne]->Sety(0); break;
case SDL_HAT_RIGHTDOWN:m_joyHatValues[whichOne]->Setx(1); m_joyHatValues[whichOne]->Sety(1); break;
}}
请注意,m_joyHatValues是Vector2D*
class Vector2D{private:
float x;
float y;
公共:Vector2D(float_x,float_y):x(_x),y(_y){}
float Getx(){ return x; }
float Gety(){ return y; }
void Setx(float _x){ this->x = _x; }
void Sety(float _y){ this->y = _y; }
float Length(){ return sqrt((x*x) + (y*y)); } //la distanza dall'origine
Vector2D operator+(const Vector2D&v2)const{ return Vector2D(this->x + v2.x, this->y + v2.y); }
friend Vector2D operator+=(Vector2D& v1, const Vector2D& v2){ v1.x += v2.x; v1.y += v2.y; return v1; }
Vector2D operator- (const Vector2D&v2)const{ return Vector2D(this->x - v2.x, this->y - v2.y); }
friend Vector2D operator-=(Vector2D& v1, const Vector2D&v2){ v1.x -= v2.x; v1.y -= v2.y; return v1; }
Vector2D operator*(float scala){ return Vector2D(this->x*scala, this->y*scala); }
Vector2D& operator*=(float scala){ this->x *= scala; this->y *= scala; return *this; }
Vector2D operator/(float scala){ return Vector2D(this->x / scala, this->y / scala); }
Vector2D& operator/=(float scala){ this->x /= scala; this->y /= scala; return *this; }
void Normalize(); //porta la distanza a 0 moltiplicanto il vector2D per il suo reciproco
};
相关文章:
- 物理键盘上的触发器按键
- 如何使用C/C++在MacOSX中获得键盘布局
- SDL 操纵杆按钮按下/C++
- 视窗键盘输入 c++
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- C ++异步键盘输入(标准方式)
- 键盘不起作用 - Arduino Uno - 与变量有关的多个错误
- 如何在 WindowProc 处理程序中区分箭头键和数字键盘?
- 覆盖应用程序的低级别键盘挂钩问题
- C++键盘挂钩 CTRL 键卡住
- 在 OpenGL 中使用键盘移动 3D 形状,C++
- SFML 键盘条件或
- 找不到键盘,即使我包括键盘。
- 在C++中隐藏键盘记录器的控制台窗口
- 盖奇不读取键盘输入
- 使用Boost async_read和POSIX::stream_descriptor从键盘读取
- 在 Linux 上C++:在作为 systemd 服务运行时侦听键盘输入
- 是否可以将键盘输入绑定到 Win32 中的单个应用程序?
- SDL2 无法捕获控制台键盘事件?
- 如何在SDL 2.0 C++中使用操纵杆的数字键盘