From f56abf05969cc5ecae8654ce0bcfa324d161084f Mon Sep 17 00:00:00 2001 From: Mariotaku Date: Sun, 17 Oct 2021 13:52:05 +0900 Subject: [PATCH] Compatibility and game controller patches (#42) * Compatibility for old CMake versions (3.0) Compatibility for SDL Mixer 2.0.1 Basic controller support: LB, RB for flippers, A for plunger, DPAD for table bump * Update SpaceCadetPinball/Sound.cpp Co-authored-by: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com> --- CMakeLists.txt | 13 +++++--- SpaceCadetPinball/Sound.cpp | 4 +++ SpaceCadetPinball/winmain.cpp | 63 +++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d729de1..a7bf214 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.0) project(SpaceCadetPinball) set(CMAKE_CXX_STANDARD 11) @@ -191,10 +191,13 @@ set_source_files_properties( SpaceCadetPinball/imgui_impl_sdl.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS 1 ) -target_precompile_headers(SpaceCadetPinball - PUBLIC - SpaceCadetPinball/pch.h -) + +if(${CMAKE_VERSION} VERSION_GREATER "3.16.0" OR ${CMAKE_VERSION} VERSION_EQUAL "3.16.0") + target_precompile_headers(SpaceCadetPinball + PUBLIC + SpaceCadetPinball/pch.h + ) +endif() target_link_libraries(SpaceCadetPinball ${SDL2_LIBRARY} ${SDL2_MIXER_LIBRARY}) diff --git a/SpaceCadetPinball/Sound.cpp b/SpaceCadetPinball/Sound.cpp index 902c042..d492459 100644 --- a/SpaceCadetPinball/Sound.cpp +++ b/SpaceCadetPinball/Sound.cpp @@ -8,7 +8,11 @@ int* Sound::TimeStamps = nullptr; bool Sound::Init(int channels, bool enableFlag) { +#if SDL_MIXER_VERSION_ATLEAST(2, 0, 3) Mix_Init(MIX_INIT_MID); +#else + Mix_Init(MIX_INIT_FLUIDSYNTH); +#endif auto result = Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024); SetChannels(channels); Enable(enableFlag); diff --git a/SpaceCadetPinball/winmain.cpp b/SpaceCadetPinball/winmain.cpp index f8951e8..eb154a2 100644 --- a/SpaceCadetPinball/winmain.cpp +++ b/SpaceCadetPinball/winmain.cpp @@ -741,6 +741,69 @@ int winmain::event_handler(const SDL_Event* event) default: ; } break; + case SDL_JOYDEVICEADDED: + if (SDL_IsGameController(event->jdevice.which)) + { + SDL_GameControllerOpen(event->jdevice.which); + } + break; + case SDL_JOYDEVICEREMOVED: + { + SDL_GameController *controller = SDL_GameControllerFromInstanceID(event->jdevice.which); + if (controller) + { + SDL_GameControllerClose(controller); + } + } + break; + case SDL_CONTROLLERBUTTONDOWN: + switch (event->cbutton.button) + { + case SDL_CONTROLLER_BUTTON_A: + pb::keydown(Options.Key.Plunger); + break; + case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: + pb::keydown(Options.Key.LeftFlipper); + break; + case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: + pb::keydown(Options.Key.RightFlipper); + break; + case SDL_CONTROLLER_BUTTON_DPAD_LEFT: + pb::keydown(Options.Key.LeftTableBump); + break; + case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: + pb::keydown(Options.Key.RightTableBump); + break; + case SDL_CONTROLLER_BUTTON_DPAD_DOWN: + pb::keydown(Options.Key.BottomTableBump); + break; + default: ; + } + break; + case SDL_CONTROLLERBUTTONUP: + switch (event->cbutton.button) + { + case SDL_CONTROLLER_BUTTON_A: + pb::keyup(Options.Key.Plunger); + break; + case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: + pb::keyup(Options.Key.LeftFlipper); + break; + case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: + pb::keyup(Options.Key.RightFlipper); + break; + case SDL_CONTROLLER_BUTTON_DPAD_LEFT: + pb::keyup(Options.Key.LeftTableBump); + break; + case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: + pb::keyup(Options.Key.RightTableBump); + break; + case SDL_CONTROLLER_BUTTON_DPAD_DOWN: + pb::keyup(Options.Key.BottomTableBump); + break; + default: ; + } + break; default: ; }