Implement GameController through SDL2

This commit is contained in:
Adrian Graber
2022-10-27 20:02:02 +09:00
committed by kichikuou
parent 65aa76a337
commit 034ab8dd03
3 changed files with 31 additions and 0 deletions
+14
View File
@@ -104,6 +104,20 @@ NACT::NACT(int sys_ver, uint32 crc32_a, uint32 crc32_b, const Config& config)
}
#endif
#ifdef USE_JOY_SDL
SDL_Init(SDL_INIT_GAMECONTROLLER);
for (int i = 0; i < SDL_NumJoysticks(); ++i) {
if (SDL_IsGameController(i)) {
sdl_gamecontroller = SDL_GameControllerOpen(i);
if (sdl_gamecontroller) {
break;
} else {
WARNING("Could not open gamecontroller %i: %s\n", i, SDL_GetError());
}
}
}
#endif
terminate = false;
restart_after_terminate = false;
}
+4
View File
@@ -235,6 +235,10 @@ protected:
int joy_num;
JOYCAPS joycaps;
#endif
#ifdef USE_JOY_SDL
SDL_GameController *sdl_gamecontroller = NULL;
#endif
int mouse_x = 0, mouse_y = 0;
static uint32 calc_crc32(const char* file_name, const std::string& game_id);
+13
View File
@@ -102,6 +102,19 @@ uint8 NACT::get_key()
}
#endif
#ifdef USE_JOY_SDL
if(sdl_gamecontroller) {
if(SDL_GameControllerGetButton(sdl_gamecontroller, SDL_CONTROLLER_BUTTON_DPAD_UP) || SDL_GameControllerGetAxis(sdl_gamecontroller, SDL_CONTROLLER_AXIS_LEFTY) <= -8000) val |= 0x01;
if(SDL_GameControllerGetButton(sdl_gamecontroller, SDL_CONTROLLER_BUTTON_DPAD_DOWN) || SDL_GameControllerGetAxis(sdl_gamecontroller, SDL_CONTROLLER_AXIS_LEFTY) >= 8000) val |= 0x02;
if(SDL_GameControllerGetButton(sdl_gamecontroller, SDL_CONTROLLER_BUTTON_DPAD_LEFT) || SDL_GameControllerGetAxis(sdl_gamecontroller, SDL_CONTROLLER_AXIS_LEFTX) <= -8000) val |= 0x04;
if(SDL_GameControllerGetButton(sdl_gamecontroller, SDL_CONTROLLER_BUTTON_DPAD_RIGHT) || SDL_GameControllerGetAxis(sdl_gamecontroller, SDL_CONTROLLER_AXIS_LEFTX) >= 8000) val |= 0x08;
if(SDL_GameControllerGetButton(sdl_gamecontroller, SDL_CONTROLLER_BUTTON_A)) val |= 0x10;
if(SDL_GameControllerGetButton(sdl_gamecontroller, SDL_CONTROLLER_BUTTON_B)) val |= 0x20;
if(SDL_GameControllerGetButton(sdl_gamecontroller, SDL_CONTROLLER_BUTTON_X)) val |= 0x40;
if(SDL_GameControllerGetButton(sdl_gamecontroller, SDL_CONTROLLER_BUTTON_Y)) val |= 0x80;
}
#endif
return val;
}