gpt4 book ai didi

c++ - SDL_Jostick 正在发送奇怪的整数

转载 作者:行者123 更新时间:2023-11-28 06:38:42 25 4
gpt4 key购买 nike

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

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

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

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

主要.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;
}

场景OpenGL.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

场景OpenGL.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 ****
*/
}
}

输入.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
};

输入.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 Controller 上按 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.

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

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

最佳答案

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

用十六进制检查它们会很明显:​​

1996554752 = 0x77010200

1996554496 = 0x77010100

1996555008 = 0x77010300

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

这几乎肯定包含在适当的文档中

关于c++ - SDL_Jostick 正在发送奇怪的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26345018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com