SDL_Jostick正在发送奇怪的整数

时间:2014-10-13 16:51:07

标签: c++ sdl sdl-2 joystick

我在尝试使用SDL(版本2.0.3)处理SDL / OpenGL / C ++程序中的多个游戏控制器时遇到了一些问题。

我正在使用SDL_PollEvent(& m_events)循环更新事件,并使用switch(m_events.type)查找SDL_JOYBUTTONDOWN(或SDL_JOYBUTTONUP / SDL_JOYAXISMOTION / SDL_HATMOTION)事件。 我想要的是使用m_events.jbutton.which和m_events.jbutton.button(或轴和帽子的等价物)的值来更新包含我的控制器状态的数组。

我正在使用Windows 7 Pro 64bit和Code :: Blocks 13.12,但它不应该改变任何东西。

这是我的(调试)代码,我试图保留相关部分:

的main.cpp

#include "SceneOpenGL.h"
int main(int argc, char *argv[])
{
  SceneOpenGL scene(/* some args for window size */);

  if(!scene.initWindow())
    return -1;

  scene.mainLoop();

  return 0;
}

SceneOpenGL.h

#ifndef SCENEOPENGL_H_INCLUDED
#define SCENEOPENGL_H_INCLUDED
#include <iostream>
#include "Input.h"
#include <SDL2/SDL.h>

class SceneOpenGL
{
public:
  SceneOpenGL(/* some args for window size */);
  ~SceneOpenGL();
  bool initWindow();
  void mainLoop();

private:
  SDL_Window* m_window;
  SDL_Event m_event;
  Input m_input;
  bool m_useJoysticks;
};
#endif

SceneOpenGL.cpp

SceneOpenGL::SceneOpenGL(/* some args for window size */) :
  m_window(0), m_input()
{
  if(SDL_NumJoysticks())
    m_useJoysticks = true;
}

bool SceneOpenGL::initWindow()
{
  if(SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) == -1)
  {
    std::cout << "Error while initializing SDL : " << SDL_GetError() << std::endl;
    SDL_Quit();
    return false;
  }
  /*
  **** Creation of SDL Window and GLContext ****
  */
  return true;
}

void SceneOpenGL::mainLoop()
{
  if(m_useJoysticks)
  {
    m_input.openJoysticks();
    SDL_JoystickEventState(SDL_ENABLE);
  }

  while(!m_input.terminate()) // m_input.terminate() sends true when SDL receives SDL_WINDOWEVENT_CLOSE event
  {
    m_input.updateEvents(); // this is the other loop where I update the joystick, keyboard & mouse state

    /* ...
    **** moving opengl objects & render ****
    */
  }
}

Input.h

#ifndef INPUT_H_INCLUDED
#define INPUT_H_INCLUDED
#include <SDL2/SDL.h>
#include <iostream>

class Input
{
public:
  Input();
  ~Input();
  void updateEvents();
  bool terminate() const;

  void openJoysticks();

private:
  SDL_Event m_events;

  int m_numJoysticks;
  SDL_Joysticks* m_joysticks[4]; // Array containing the first 4 joysicks

  bool m_terminate; // used for ending program
};

Input.cpp

#include "Input.h"

Input::Input() :
  m_numJoysticks(0), m_terminate(false)
{}

Input::~Input()
{
  for(int i(0); i < SDL_NumJoysticks(); i++)
    SDL_JoystickClose(m_joysticks[i]); // Closes joysticks before exiting
}

void Input::openJoysticks()
{
  m_numJoysticks = SDL_NumJoysticks; // Counts the connected joysticks
  if(m_numJoysticks > 4)
    m_numJoysticks = 4; // Sets maximum joysticks to 4

  for(int i(0); i < m_numJoysticks; i++)
  {
    m_joysticks[i] = SDL_JoystickOpen(0) // Open existing joysticks
    std::cout << "Joystick #" << i << " OK" << std::endl;
  }
}

void Input::updateEvents()
{
  while(SDL_PollEvent(&m_events))
  {
    switch(m_events.type)
    {
    /* ... Keyboard and mouse events, no problem there */
    case SDL_JOYBUTTONDOWN:
      std::cout << "JOYBUTTONDOWN" << std::endl;
      std::cout << "type : " << m_events.jbutton.type << std::endl;
      std::cout << "which : " << m_events.jbutton.which << std::endl;
      std::cout << "button : " << m_events.jbutton.button << std::endl;
      std::cout << "state : " << m_events.jbutton.state << std:: endl << std::endl;
      break;

    /* ... Same thing for SDL_JOYBUTTONUP, SDL_JOYAXISMOTION, SDL_JOYHATMOTION  */

    case SDL_WINDOWEVENT:
      if(m_events.window.event == SDL_WINDOWEVENT_CLOSE)
        m_terminate = true; // end program
      break;

    default:
      break;
    }
  }
}

bool Input::terminate() const
{
  return m_terminate;
}

所以这是我在x360控制器上按A,B,X,Y时的控制台输出(=按钮0,1,2和3):

Joystick #0 OK
JOYBUTTONDOWN
type : 1539
which : 65536
button : §
state : Ý

JOYBUTTONDOWN
type : 1539
which : 1996554496
button :
state :

JOYBUTTONDOWN
type : 1539
which : 1996554752
button :
state :

JOYBUTTONDOWN
type : 1539
which : 1996555008
button :
state :

Process returned 0 (0x0)   execution time : 7.437 s
Press any key to continue.

你可以看到它看起来不太好。我甚至有时会为相同的输入获得不同的值(就像这里按下第一个按钮),但我注意到“which”值之间的差异是256,所以它不是完全随机的。如果我重新启动程序或按两次相同的按钮,则值大致相同。 好吧,似乎“event.jbutton.which”包含按钮索引的信息(应该是Uint8)

我不知道我做错了还是SDL2中的错误。

1 个答案:

答案 0 :(得分:1)

您的数字似乎是各种标志字段的按位OR,可能带有值字段。

用十六进制检查它们会使这一点显而易见:

1996554752 = 0x77010200

1996554496 = 0x77010100

1996555008 = 0x77010300

看起来您的“按钮编号”可能位于第15-8位,即值2,1,3

这几乎肯定包含在相应的文档中