C++ SDL2操纵杆事件未触发
所以我得到了这个代码:C++ SDL2操纵杆事件未触发,c++,events,sdl-2,joystick,C++,Events,Sdl 2,Joystick,所以我得到了这个代码: void Engine::Run() { // initialize all components if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK)) throw Exception("couldn't initialize SDL\n" + string(SDL_GetError()), 1); // other code run = true; SDL_Even
void Engine::Run() {
// initialize all components
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK))
throw Exception("couldn't initialize SDL\n" + string(SDL_GetError()), 1);
// other code
run = true;
SDL_Event event;
while (run) {
// other code
uint32 timeout = SDL_GetTicks() + 50;
while (SDL_PollEvent(&event) && timeout - SDL_GetTicks() > 0)
HandleEvent(event);
}
}
void Engine::HandleEvent(const SDL_Event& event) {
switch (event.type) {
case SDL_KEYDOWN:
inputSys->KeyDownEvent(event.key);
break;
case SDL_KEYUP:
inputSys->KeyUpEvent(event.key);
break;
case SDL_JOYBUTTONDOWN:
cout << "button" << endl;
break;
case SDL_JOYHATMOTION:
cout << "hat" << endl;
break;
case SDL_JOYAXISMOTION:
cout << "axis" << endl;
break;
case SDL_JOYDEVICEADDED: case SDL_JOYDEVICEREMOVED:
inputSys->UpdateControllers();
break;
// other code
}
}
void引擎::Run(){
//初始化所有组件
if(SDL_Init(SDL_Init_视频| SDL_Init_操纵杆))
抛出异常(“无法初始化SDL\n”+字符串(SDL\u GetError()),1);
//其他代码
run=true;
SDL_事件;
while(运行){
//其他代码
uint32 timeout=SDL_GetTicks()+50;
while(SDL_polleevent(&event)&&timeout-SDL_GetTicks()>0)
HandleEvent(事件);
}
}
无效引擎::HandleEvent(常量SDL_事件和事件){
开关(事件类型){
案例SDL_按键关闭:
inputSys->KeyDownEvent(event.key);
打破
案例SDL_密钥更新:
inputSys->KeyUpEvent(event.key);
打破
案例SDL_JOYBUTTONDOWN:
cout你必须打电话获取这些事件。下面是它们的示例:
SDL_Joystick *joy;
// Initialize the joystick subsystem
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
// Check for joystick
if (SDL_NumJoysticks() > 0) {
// Open joystick
joy = SDL_JoystickOpen(0);
if (joy) {
printf("Opened Joystick 0\n");
printf("Name: %s\n", SDL_JoystickNameForIndex(0));
printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joy));
printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joy));
printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joy));
} else {
printf("Couldn't open Joystick 0\n");
}
// Close if opened
if (SDL_JoystickGetAttached(joy)) {
SDL_JoystickClose(joy);
}
}
你必须打电话获取这些事件。以下是它们的示例:
SDL_Joystick *joy;
// Initialize the joystick subsystem
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
// Check for joystick
if (SDL_NumJoysticks() > 0) {
// Open joystick
joy = SDL_JoystickOpen(0);
if (joy) {
printf("Opened Joystick 0\n");
printf("Name: %s\n", SDL_JoystickNameForIndex(0));
printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joy));
printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joy));
printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joy));
} else {
printf("Couldn't open Joystick 0\n");
}
// Close if opened
if (SDL_JoystickGetAttached(joy)) {
SDL_JoystickClose(joy);
}
}