From 678bd93c52df60468d1a09cb4f3e99c8a16ef63b Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Thu, 14 Jun 2018 22:37:44 -0400 Subject: [PATCH] Connects SDL joystick input to joystick machines. --- OSBindings/SDL/main.cpp | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/OSBindings/SDL/main.cpp b/OSBindings/SDL/main.cpp index 8fa5f9abb..8f23a0002 100644 --- a/OSBindings/SDL/main.cpp +++ b/OSBindings/SDL/main.cpp @@ -390,9 +390,9 @@ int main(int argc, char *argv[]) { int window_width, window_height; SDL_GetWindowSize(window, &window_width, &window_height); - // Establish user-friendly options by default. - Configurable::Device *configurable_device = machine->configurable_device(); + Configurable::Device *const configurable_device = machine->configurable_device(); if(configurable_device) { + // Establish user-friendly options by default. configurable_device->set_selections(configurable_device->get_user_friendly_selections()); // Consider transcoding any list selections that map to Boolean options. @@ -410,9 +410,21 @@ int main(int argc, char *argv[]) { } } } + + // Apply the user's actual selections to override the defaults. configurable_device->set_selections(arguments.selections); } + // If this is a joystick machine, check for and open attached joysticks. + std::vector> joysticks; + JoystickMachine::Machine *const joystick_machine = machine->joystick_machine(); + if(joystick_machine) { + SDL_InitSubSystem(SDL_INIT_JOYSTICK); + for(int c = 0; c < SDL_NumJoysticks(); ++c) { + joysticks.emplace_back(SDL_JoystickOpen(c), &SDL_JoystickClose); + } + } + // Run the main event loop until the OS tells us to quit. bool should_quit = false; Uint32 fullscreen_mode = 0; @@ -510,6 +522,27 @@ int main(int argc, char *argv[]) { } } + // Push new joystick state, if any. + JoystickMachine::Machine *const joystick_machine = machine->joystick_machine(); + if(joystick_machine) { + std::vector> &machine_joysticks = joystick_machine->get_joysticks(); + for(size_t c = 0; c < joysticks.size(); ++c) { + size_t target = c % machine_joysticks.size(); + + const float x_axis = static_cast(SDL_JoystickGetAxis(joysticks[c].get(), 0) + 32768) / 65536.0f; + const float y_axis = static_cast(SDL_JoystickGetAxis(joysticks[c].get(), 1) + 32768) / 65536.0f; + machine_joysticks[target]->set_input(Inputs::Joystick::Input(Inputs::Joystick::Input::Type::Horizontal), x_axis); + machine_joysticks[target]->set_input(Inputs::Joystick::Input(Inputs::Joystick::Input::Type::Vertical), y_axis); + + const int number_of_buttons = SDL_JoystickNumButtons(joysticks[c].get()); + for(int button = 0; button < number_of_buttons; ++button) { + machine_joysticks[target]->set_input( + Inputs::Joystick::Input(Inputs::Joystick::Input::Type::Fire, button), + SDL_JoystickGetButton(joysticks[c].get(), button) ? true : false); + } + } + } + // Display a new frame and wait for vsync. updater.update(); machine->crt_machine()->get_crt()->draw_frame(static_cast(window_width), static_cast(window_height), false); @@ -517,6 +550,7 @@ int main(int argc, char *argv[]) { } // Clean up. + joysticks.clear(); SDL_DestroyWindow( window ); SDL_Quit();