#include "gamepad.h" #include #include "zx_ula.h" #include #define GAMEPAD_BUTTON_LEFT 0 #define GAMEPAD_BUTTON_RIGHT 1 #define GAMEPAD_BUTTON_UP 2 #define GAMEPAD_BUTTON_DOWN 3 #define GAMEPAD_BUTTON_FIRE 4 namespace gamepad { struct gamepad_t { SDL_GameController *gamepad; int32_t index; uint8_t type; bool buttons[5] { false, false, false, false, false }; }; std::vector gamepads; uint8_t getNextType() { uint8_t next_type = GAMEPAD_TYPE_SINCLAIR_1; bool found = false; while (!found) { found = true; for (auto gamepad : gamepads) { if (gamepad.type == next_type) { found = false; next_type++; if (next_type > GAMEPAD_TYPE_FULLER) return GAMEPAD_TYPE_SINCLAIR_1; break; } } } return next_type; } void add(int32_t index, uint8_t type) { if (SDL_IsGameController(index)) { gamepad_t gamepad; gamepad.gamepad = SDL_GameControllerOpen(index); if (SDL_GameControllerGetAttached(gamepad.gamepad) == SDL_TRUE) SDL_GameControllerEventState(SDL_ENABLE); gamepad.index = index; gamepad.type = ( (type == GAMEPAD_TYPE_ANY) ? getNextType() : type ); gamepads.push_back(gamepad); } } void remove(int32_t index) { SDL_GameController *game_controller = SDL_GameControllerFromInstanceID(index); for (int i=0; i