From 24796acccf698aead888bc942dad155f67bc1f4a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 10 Jun 2022 23:51:06 +0300 Subject: [PATCH] MBC7 mouse control in SDL --- SDL/gui.c | 37 ++++++++++++++++++++++++++++++------- SDL/gui.h | 4 ++++ SDL/main.c | 19 +++++++++++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index e76177b..4b4ee7b 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -1155,9 +1155,20 @@ void connect_joypad(void) } } +static void toggle_mouse_control(unsigned index) +{ + configuration.allow_mouse_controls = !configuration.allow_mouse_controls; +} + +const char *mouse_control_string(unsigned index) +{ + return configuration.allow_mouse_controls? "Allow mouse control" : "Disallow mouse control"; +} + static const struct menu_item controls_menu[] = { {"Keyboard Options", enter_keyboard_menu}, {"Joypad Options", enter_joypad_menu}, + {"Motion-controlled games:", toggle_mouse_control, mouse_control_string, toggle_mouse_control}, {"Back", return_to_root_menu}, {NULL,} }; @@ -1234,6 +1245,22 @@ static void toggle_audio_recording(unsigned index) } } +void convert_mouse_coordinates(signed *x, signed *y) +{ + signed width = GB_get_screen_width(&gb); + signed height = GB_get_screen_height(&gb); + signed x_offset = (width - 160) / 2; + signed y_offset = (height - 144) / 2; + + *x = (signed)(*x - rect.x / factor) * width / (signed)(rect.w / factor) - x_offset; + *y = (signed)(*y - rect.y / factor) * height / (signed)(rect.h / factor) - y_offset; + + if (strcmp("CRT", configuration.filter) == 0) { + *y = *y * 8 / 7; + *y -= 144 / 16; + } +} + void run_gui(bool is_running) { SDL_ShowCursor(SDL_ENABLE); @@ -1296,13 +1323,9 @@ void run_gui(bool is_running) event.key.keysym.scancode = SDL_SCANCODE_ESCAPE; } else if (gui_state == SHOWING_MENU) { - signed x = (event.button.x - rect.x / factor) * width / (rect.w / factor) - x_offset; - signed y = (event.button.y - rect.y / factor) * height / (rect.h / factor) - y_offset; - - if (strcmp("CRT", configuration.filter) == 0) { - y = y * 8 / 7; - y -= 144 / 16; - } + signed x = event.button.x; + signed y = event.button.y; + convert_mouse_coordinates(&x, &y); y += scroll; if (x < 0 || x >= 160 || y < 24) { diff --git a/SDL/gui.h b/SDL/gui.h index 1fe8a54..81a13bb 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -123,6 +123,9 @@ typedef struct { /* v0.14.4 */ bool osd; + + /* v0.15 */ + bool allow_mouse_controls; } configuration_t; extern configuration_t configuration; @@ -149,5 +152,6 @@ void show_osd_text(const char *text); extern const char *osd_text; extern unsigned osd_countdown; extern unsigned osd_text_lines; +void convert_mouse_coordinates(signed *x, signed *y); #endif diff --git a/SDL/main.c b/SDL/main.c index aa95a07..da8c40e 100644 --- a/SDL/main.c +++ b/SDL/main.c @@ -242,6 +242,25 @@ static void handle_events(GB_gameboy_t *gb) } break; } + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: { + if (GB_has_accelerometer(gb) && configuration.allow_mouse_controls) { + GB_set_key_state(gb, GB_KEY_A, event.type == SDL_MOUSEBUTTONDOWN); + } + break; + } + + case SDL_MOUSEMOTION: { + if (GB_has_accelerometer(gb) && configuration.allow_mouse_controls) { + signed x = event.motion.x; + signed y = event.motion.y; + convert_mouse_coordinates(&x, &y); + x = SDL_max(SDL_min(x, 160), 0); + y = SDL_max(SDL_min(y, 144), 0); + GB_set_accelerometer_values(gb, (x - 80) / -80.0, (y - 72) / -72.0); + } + break; + } case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN: {