MBC7 mouse control in SDL

This commit is contained in:
Lior Halphon 2022-06-10 23:51:06 +03:00
parent 979d32faed
commit 24796acccf
3 changed files with 53 additions and 7 deletions

View File

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

View File

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

View File

@ -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: {