【问题标题】:How to use a digital pad of a joystick with SDL 2.0 C++如何在 SDL 2.0 C++ 中使用操纵杆的数字键盘
【发布时间】:2015-02-10 16:26:32
【问题描述】:

我尝试使用 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_Joystick* 而 m_buttonStates 是一个向量。 不幸的是,这个功能没有映射数字键盘的 4 按钮,我现在不知道为什么...... 这是我的游戏手柄: http://www.trust.com/it-it/all-products/17518-gxt-530-dual-stick-gamepad 谢谢你,对不起我的英语不太好

【问题讨论】:

    标签: c++ sdl-2 joypad


    【解决方案1】:

    谢谢 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
    

    };

    【讨论】:

      猜你喜欢
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多