Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ SDL_Jostick正在发送奇怪的信息_C++_Sdl_Sdl 2_Joystick - Fatal编程技术网

C++ SDL_Jostick正在发送奇怪的信息

C++ SDL_Jostick正在发送奇怪的信息,c++,sdl,sdl-2,joystick,C++,Sdl,Sdl 2,Joystick,在使用SDL(2.0.3版)处理SDL/OpenGL/C++程序中的多个游戏控制器时,我遇到了一些问题 我正在使用SDL_PollEvent(&m_events)循环更新事件,并使用开关(m_events.type)查找SDL_JOYBUTTONDOWN(或SDL_JOYBUTTONUP/SDL_JOYAXISMOTION/SDL_HATMOTION)事件。 我想要的是使用m_events.jbutton.which和m_events.jbutton.button的值(或轴和帽子的等效值)来更新

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

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

我正在使用Windows7 Pro 64位和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;
}
情景英语

#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
正如你所看到的,这似乎并不太好。有时我甚至会对同一个输入得到不同的值(就像这里按下第一个按钮),但我注意到“哪个”值之间的差异是256,所以它不是完全随机的。如果我重新启动程序或按两次相同的按钮,值基本相同。 它看起来像“event.jbutton.which”,包含按钮索引的信息(应该是Uint8)


我不知道我是否做错了,或者这是SDL2中的一个错误。

您的数字似乎是按位的,或者是各种标志字段,可能有一个值字段

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

1996554752 = 0x77010200

1996554496 = 0x77010100

1996555008 = 0x77010300
看起来您的“按钮编号”可能在15-8位,即值2、1、3


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

它在其他平台上工作吗?你试过最新的hg源代码吗?看起来源代码确实有问题,所以问题来自我的安装。文档上说SDL_JoyButtonEvent哪个字段应该是SDLJoystickID,而button值应该是存储在不同字段(即event.jbutton.button)中的Uint8。我的观点是“which”本身可能有子字段,其中不同的位有不同的用途。或者实际库和标头之间可能不兼容,例如不同的结构打包。第二个猜测确实正确(标头和库之间不兼容)。我重新安装了库的2.0版本(我在2.0.3之前使用过)现在它似乎起作用了(不过我必须进一步测试它)。我可能在某个时候更新库时出错了。
#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
};
#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;
}
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.
1996554752 = 0x77010200

1996554496 = 0x77010100

1996555008 = 0x77010300