From 864c6b929d8155cb91733d96f5e0ba2c85af23ab Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Fri, 15 Aug 2025 22:58:53 +0200 Subject: [PATCH] - [NEW] Soport per a joystick tipo Sinclair --- gamepad.cpp | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++ gamepad.h | 16 ++++++ main.cpp | 10 ++++ 3 files changed, 182 insertions(+) create mode 100644 gamepad.cpp create mode 100644 gamepad.h diff --git a/gamepad.cpp b/gamepad.cpp new file mode 100644 index 0000000..49670a2 --- /dev/null +++ b/gamepad.cpp @@ -0,0 +1,156 @@ +#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 + +#define GAMEPAD_TYPE_ANY 0 +#define GAMEPAD_TYPE_SINCLAIR_1 1 +#define GAMEPAD_TYPE_SINCLAIR_2 2 +#define GAMEPAD_TYPE_KEMPSTON 3 +#define GAMEPAD_TYPE_FULLER 4 + +namespace gamepad +{ + void add(int32_t index, uint8_t type); + void remove(int32_t index); + void buttonDown(int32_t index, uint8_t button); + void buttonUp(int32_t index, uint8_t button); +} \ No newline at end of file diff --git a/main.cpp b/main.cpp index 97dbeb6..0ca485c 100644 --- a/main.cpp +++ b/main.cpp @@ -21,6 +21,7 @@ #include "ay-3-8912.h" #include "ay_viewer.h" #include "file.h" +#include "gamepad.h" uint32_t time = 0; uint32_t t_states = 0; @@ -173,11 +174,20 @@ int main(int argc, char *argv[]) if (e.type == SDL_QUIT) { zx_system::shutdown(); break; } if (e.type == SDL_JOYDEVICEADDED) { + gamepad::add(e.jdevice.which, GAMEPAD_TYPE_ANY); printf("JOYSTICK CONECTAT: %i\n", e.jdevice.which); + // [TODO] Mostrar dialeg de triar tipus de joystick } if (e.type == SDL_JOYDEVICEREMOVED) { + gamepad::remove(e.jdevice.which); printf("JOYSTICK DESCONECTAT: %i\n", e.jdevice.which); } + if (e.type == SDL_CONTROLLERBUTTONDOWN) { + gamepad::buttonDown(e.jbutton.which, e.jbutton.button); + } + if (e.type == SDL_CONTROLLERBUTTONUP) { + gamepad::buttonUp(e.jbutton.which, e.jbutton.button); + } if (e.type == SDL_MOUSEBUTTONDOWN) result = ui::window::sendEvent(e.button.windowID, &e); if (e.type == SDL_MOUSEBUTTONUP) result = ui::window::sendEvent(e.button.windowID, &e); if (e.type == SDL_MOUSEMOTION) result = ui::window::sendEvent(e.motion.windowID, &e);