mirror of
https://github.com/TomHarte/CLK.git
synced 2024-11-22 12:33:29 +00:00
Connects SDL joystick input to joystick machines.
This commit is contained in:
parent
1bf0c1891a
commit
678bd93c52
@ -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<std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)>> 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<std::unique_ptr<Inputs::Joystick>> &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<float>(SDL_JoystickGetAxis(joysticks[c].get(), 0) + 32768) / 65536.0f;
|
||||
const float y_axis = static_cast<float>(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<unsigned int>(window_width), static_cast<unsigned int>(window_height), false);
|
||||
@ -517,6 +550,7 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
// Clean up.
|
||||
joysticks.clear();
|
||||
SDL_DestroyWindow( window );
|
||||
SDL_Quit();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user