From 8201d51668c8a0a379bb171c6355da2331b55d92 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Sat, 16 Aug 2025 10:09:42 +0200 Subject: [PATCH] - [NEW] Dialeg per a configurar els joysticks --- gamepad.cpp | 38 +++++++++++++++++++--- gamepad.h | 8 ++++- main.cpp | 38 ++++++++-------------- ui.cpp | 24 ++++++++++++-- ui.h | 5 ++- ui_menu.cpp | 11 ++++--- zx_dialog_joystick.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++ zx_dialog_joystick.h | 11 +++++++ zx_screen.cpp | 1 + 9 files changed, 170 insertions(+), 38 deletions(-) create mode 100644 zx_dialog_joystick.cpp create mode 100644 zx_dialog_joystick.h diff --git a/gamepad.cpp b/gamepad.cpp index 49670a2..e91b84b 100644 --- a/gamepad.cpp +++ b/gamepad.cpp @@ -34,7 +34,7 @@ namespace gamepad { found = false; next_type++; - if (next_type > GAMEPAD_TYPE_FULLER) return GAMEPAD_TYPE_SINCLAIR_1; + if (next_type == GAMEPAD_TYPE_CUSTOM) return next_type; break; } } @@ -42,7 +42,7 @@ namespace gamepad return next_type; } - void add(int32_t index, uint8_t type) + uint8_t add(int32_t index, uint8_t type) { if (SDL_IsGameController(index)) { gamepad_t gamepad; @@ -52,6 +52,19 @@ namespace gamepad gamepad.type = ( (type == GAMEPAD_TYPE_ANY) ? getNextType() : type ); gamepads.push_back(gamepad); } + return gamepads.size()-1; + } + + void modify(int32_t index, uint8_t type) + { + for (auto& gamepad : gamepads) + { + if (gamepad.index == index) + { + gamepad.type = type; + return; + } + } } void remove(int32_t index) @@ -67,7 +80,7 @@ namespace gamepad void buttonDown(int32_t index, uint8_t button) { SDL_GameController *game_controller = SDL_GameControllerFromInstanceID(index); - for (auto gamepad : gamepads) + for (auto& gamepad : gamepads) { if (gamepad.gamepad == game_controller) { @@ -111,7 +124,7 @@ namespace gamepad void buttonUp(int32_t index, uint8_t button) { SDL_GameController *game_controller = SDL_GameControllerFromInstanceID(index); - for (auto gamepad : gamepads) + for (auto& gamepad : gamepads) { if (gamepad.gamepad == game_controller) { @@ -153,4 +166,21 @@ namespace gamepad } + uint8_t getNumGamepads() + { + return gamepads.size(); + } + + uint8_t getGamepadType(uint8_t index) + { + if (gamepads.size()>index) return gamepads[index].type; + return 0; + } + + void setGamepadType(uint8_t index, uint8_t type) + { + if (gamepads.size()>index) gamepads[index].type = type; + } + + } \ No newline at end of file diff --git a/gamepad.h b/gamepad.h index 6a11347..3fd8bf6 100644 --- a/gamepad.h +++ b/gamepad.h @@ -6,11 +6,17 @@ #define GAMEPAD_TYPE_SINCLAIR_2 2 #define GAMEPAD_TYPE_KEMPSTON 3 #define GAMEPAD_TYPE_FULLER 4 +#define GAMEPAD_TYPE_CUSTOM 5 namespace gamepad { - void add(int32_t index, uint8_t type); + uint8_t add(int32_t index, uint8_t type); + void modify(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); + + uint8_t getNumGamepads(); + uint8_t getGamepadType(uint8_t index); + void setGamepadType(uint8_t index, uint8_t type); } \ No newline at end of file diff --git a/main.cpp b/main.cpp index 0ca485c..f2a86ff 100644 --- a/main.cpp +++ b/main.cpp @@ -22,6 +22,7 @@ #include "ay_viewer.h" #include "file.h" #include "gamepad.h" +#include "zx_dialog_joystick.h" uint32_t time = 0; uint32_t t_states = 0; @@ -165,6 +166,7 @@ int main(int argc, char *argv[]) time = SDL_GetTicks(); t_states = 0; + bool first_time = true; while (!zx_system::shuttingDown()) { @@ -174,8 +176,10 @@ 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); + const uint8_t index = gamepad::add(e.jdevice.which, GAMEPAD_TYPE_ANY); printf("JOYSTICK CONECTAT: %i\n", e.jdevice.which); + //if (!first_time) + dialogs::joysticks::init(index); // [TODO] Mostrar dialeg de triar tipus de joystick } if (e.type == SDL_JOYDEVICEREMOVED) { @@ -253,26 +257,8 @@ int main(int argc, char *argv[]) zx_system::shutdown(); break; } + first_time = false; if (!z80debug::debugging() && !z80debug::paused()) { -/* - //if (z80::getPC()==0x05C8) zx_tape::go_berserk(); - bool fastload=false; - if (zx_tape::getplaying() && zx_tape::getOption(ZXTAPE_OPTION_FAST_LOAD)) { fastload=true; time = SDL_GetTicks(); } - while (zx_tape::getplaying() && zx_tape::getOption(ZXTAPE_OPTION_FAST_LOAD)) - { -// zx_tape::update(z80::step()); - uint8_t dt = z80::step(); - t_states += dt; - zx_tape::update(dt); - if (SDL_GetTicks()-time>=1000) { - time = SDL_GetTicks(); - zx_tape::report(); - } - //speaker::update(dt); - //zxscreen::refresh(dt); - } - if (fastload) { printf("%i\n", SDL_GetTicks()-time); t_states=0; } -*/ // En cada bucle fem 10 pasos de la CPU, sino s'ofega (jo en veig 5) for (int i=0;i<5;++i) { @@ -284,9 +270,6 @@ int main(int argc, char *argv[]) uint8_t dt = z80::step(); t_states += dt; zx_system::update(dt); - //zx_tape::update(dt); - //audio::update(dt); - //speaker::update(dt); zxscreen::refresh(dt); if (z80debug::debugging()) break; } @@ -306,9 +289,14 @@ int main(int argc, char *argv[]) z80analyze::refresh(true); - } else if (/*!z80debug::debugging() &&*/ z80debug::paused()) { + } else if (z80debug::paused()) { zxscreen::redraw(false); - ui::menu::show(); + + if (ui::hasDialog()) + ui::callDialog(); + else + ui::menu::show(); + zxscreen::present(); } ui::setClicked(false); diff --git a/ui.cpp b/ui.cpp index 20788ec..06044ae 100644 --- a/ui.cpp +++ b/ui.cpp @@ -59,6 +59,26 @@ namespace ui offset_y += y; } + uint8_t getOffsetX() + { + return offset_x; + } + + uint8_t getOffsetY() + { + return offset_y; + } + + bool mouseInside(int x, int y, int w, int h) + { + int mx, my; + Uint32 mb = SDL_GetMouseState(&mx, &my); + mx=mx/CHR_W; my=my/CHR_H; + mx -= offset_x; + my -= offset_y; + return (mx >= x) && (mx < x+w) && (my >= y) && (my < y+h); + } + void panel(int x, int y, int w, int h, const char *title) { ui::setoffset(x, y); @@ -133,9 +153,9 @@ namespace ui return clicked; } - void setDialog(void(*dialog)(void)) + void setDialog(void(*new_dialog)(void)) { - ui:dialog = dialog; + ui:dialog = new_dialog; } bool hasDialog() diff --git a/ui.h b/ui.h index 6d522c2..fb4e6d1 100644 --- a/ui.h +++ b/ui.h @@ -27,6 +27,9 @@ namespace ui void setrenderer(SDL_Renderer *renderer, SDL_Texture *texture); void setoffset(uint8_t x, uint8_t y); void incoffset(uint8_t x, uint8_t y); + uint8_t getOffsetX(); + uint8_t getOffsetY(); + bool mouseInside(int x, int y, int w, int h); void panel(int x, int y, int w, int h, const char *title); void box(int x, int y, int w, int h, uint8_t color); @@ -41,7 +44,7 @@ namespace ui void setClicked(const bool value); const bool getClicked(); - void setDialog(void(*dialog)(void)); + void setDialog(void(*new_dialog)(void)); bool hasDialog(); void callDialog(); } diff --git a/ui_menu.cpp b/ui_menu.cpp index 6d64194..a67c57b 100644 --- a/ui_menu.cpp +++ b/ui_menu.cpp @@ -39,6 +39,7 @@ namespace ui void show() { + ui::setoffset(0,0); int mx, my; Uint32 mb = SDL_GetMouseState(&mx, &my); mx=mx/CHR_W; my=my/CHR_H; @@ -52,8 +53,8 @@ namespace ui uint8_t text_color = COLOR_WHITE; if (my<1 && mx>=opt_pos && mx=menu_x && mx=menu_x && mx + +namespace dialogs +{ + namespace joysticks + { + void init(int32_t index); + void show(); + } +} diff --git a/zx_screen.cpp b/zx_screen.cpp index 7f456c4..3160d1b 100644 --- a/zx_screen.cpp +++ b/zx_screen.cpp @@ -140,6 +140,7 @@ namespace zxscreen if (z80debug::paused()) { if (e->type == SDL_KEYDOWN) { if (e->key.keysym.scancode==SDL_SCANCODE_ESCAPE) { + ui::setDialog(nullptr); const uint8_t dt = z80::step(); z80debug::cont(); zxscreen::refresh(dt);