- [NEW] Dialeg per a configurar els joysticks

This commit is contained in:
2025-08-16 10:09:42 +02:00
parent 864c6b929d
commit 8201d51668
9 changed files with 170 additions and 38 deletions

View File

@@ -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;
}
}

View File

@@ -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);
}

View File

@@ -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);
if (ui::hasDialog())
ui::callDialog();
else
ui::menu::show();
zxscreen::present();
}
ui::setClicked(false);

24
ui.cpp
View File

@@ -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()

5
ui.h
View File

@@ -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();
}

View File

@@ -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<opt_pos+text_size)
{
ui::printrect(opt_pos-1, 0, text_size, 1, COLOR_DARK_BLUE);
text_color = COLOR_WHITE;
ui::printrect(opt_pos-1, 0, text_size, 1, COLOR_WHITE);
text_color = COLOR_BLACK;
visible_menu = index;
menu_x = opt_pos-1;
}
@@ -73,10 +74,10 @@ namespace ui
{
const int text_size = (option.label.size()+2);
uint8_t text_color = COLOR_WHITE;
if (my==opt_pos && mx>=menu_x && mx<menu_x+text_size)
if (my==opt_pos && mx>=menu_x && mx<menu_x+20)
{
ui::printrect(menu_x+1, opt_pos, 20, 1, COLOR_DARK_BLUE);
text_color = COLOR_WHITE;
ui::printrect(menu_x+1, opt_pos, 20, 1, COLOR_WHITE);
text_color = COLOR_BLACK;
if (ui::getClicked())
{
if (option.callback) option.value = option.callback(option.value);

72
zx_dialog_joystick.cpp Normal file
View File

@@ -0,0 +1,72 @@
#include "zx_dialog_joystick.h"
#include "ui.h"
#include "z80debug.h"
#include "gamepad.h"
namespace dialogs
{
namespace joysticks
{
int32_t selected = 0;
void init(int32_t index)
{
if (index != -1) selected = index;
ui::setDialog(dialogs::joysticks::show);
z80debug::pause();
}
void show()
{
uint8_t back_color = COLOR_DARK;
uint8_t front_color = COLOR_WHITE;
ui::setoffset(0,0);
ui::printrect(36, 15, 46, 12, COLOR_DARK);
ui::panel(36,15,46,12,"CONFIGURE JOYSTICKS:");
if (ui::mouseInside(34,9,7,1)) {
back_color = COLOR_WHITE;
front_color = COLOR_BLACK;
if (ui::getClicked()) { ui::setDialog(nullptr); z80debug::cont(); }
}
ui::printrect(34, 9, 7, 1, back_color);
ui::printtxt(35,9,"CLOSE", front_color);
ui::panel(38,17,20,8,"JOYSTICKS:");
const int num_gamepads = gamepad::getNumGamepads();
for (int i=0; i<num_gamepads; ++i)
{
char name[] = "JOYSTICK 0";
name[9] = 48+i;
if (selected==i) {
ui::printrect(0,i,18,1, COLOR_WHITE);
ui::printtxt(0,i,name, COLOR_BLACK);
} else {
ui::printtxt(0,i,name, COLOR_WHITE);
}
}
ui::panel(60,17,20,8,"TYPE:");
const int selected_type = gamepad::getGamepadType(selected);
const char* types[5] = {"SINCLAIR 1", "SINCLAIR 2", "KEMPSTON", "FULLER", "CUSTOM"};
for (int i=0;i<5;++i)
{
back_color = COLOR_DARK;
front_color = COLOR_WHITE;
if (ui::mouseInside(0,i,18,1)) {
back_color = COLOR_BLACK;
if (ui::getClicked()) {
gamepad::setGamepadType(selected, i+1);
}
}
if (i==selected_type-1) {
back_color = COLOR_WHITE;
front_color = COLOR_BLACK;
}
ui::printrect(0,i,18,1, back_color);
ui::printtxt(0,i,types[i],front_color);
}
}
}
}

11
zx_dialog_joystick.h Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#include <stdint.h>
namespace dialogs
{
namespace joysticks
{
void init(int32_t index);
void show();
}
}

View File

@@ -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);