From 10eeeda581e280bd84af1c39449abd197e6ab6f2 Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Sat, 19 Dec 2020 14:26:48 +0000 Subject: [PATCH] Add support for the RetroPad and disable keyboard. How they can coexist is still not clear to me. Signed-off-by: Andrea Odetti --- linux.md | 2 +- source/frontends/retro/CMakeLists.txt | 1 + source/frontends/retro/environment.h | 2 +- source/frontends/retro/joypad.cpp | 39 +++++++++++++++++++++++++++ source/frontends/retro/joypad.h | 20 ++++++++++++++ source/frontends/retro/libretro.cpp | 30 ++++++++++++--------- 6 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 source/frontends/retro/joypad.cpp create mode 100644 source/frontends/retro/joypad.h diff --git a/linux.md b/linux.md index b6902fee..07df0105 100644 --- a/linux.md +++ b/linux.md @@ -95,7 +95,7 @@ See [sa2](source/frontends/sa2/README.md). There is an initial [libretro](https://docs.libretro.com/development/cores/developing-cores/) core. -Keyboard works, but a lot of keys overlap with RetroArch shortcuts. +Keyboard works, but a lot of keys overlap with RetroArch shortcuts. In the latest version the keyboard has been disabled and only the retro joypad works. Video works, but the vertical flip is done in software. diff --git a/source/frontends/retro/CMakeLists.txt b/source/frontends/retro/CMakeLists.txt index c2cf3072..0ca5ef1c 100644 --- a/source/frontends/retro/CMakeLists.txt +++ b/source/frontends/retro/CMakeLists.txt @@ -12,6 +12,7 @@ if (IS_DIRECTORY ${LIBRETRO_PATH}) rdirectsound.cpp interface.cpp game.cpp + joypad.cpp ) target_include_directories(ra2 PRIVATE diff --git a/source/frontends/retro/environment.h b/source/frontends/retro/environment.h index da36b741..9843004b 100644 --- a/source/frontends/retro/environment.h +++ b/source/frontends/retro/environment.h @@ -14,6 +14,6 @@ extern retro_audio_sample_batch_t audio_batch_cb; extern std::string retro_base_directory; -#define RETRO_DEVICES 5 +#define RETRO_DEVICES 1 extern unsigned int retro_devices[RETRO_DEVICES]; diff --git a/source/frontends/retro/joypad.cpp b/source/frontends/retro/joypad.cpp new file mode 100644 index 00000000..230cc780 --- /dev/null +++ b/source/frontends/retro/joypad.cpp @@ -0,0 +1,39 @@ +#include "frontends/retro/joypad.h" +#include "frontends/retro/environment.h" + +#include "libretro.h" + + +Joypad::Joypad() + : myButtonCodes(2), myAxisCodes(2) +{ + myButtonCodes[0] = RETRO_DEVICE_ID_JOYPAD_A; + myButtonCodes[1] = RETRO_DEVICE_ID_JOYPAD_B; + myAxisCodes[0][RETRO_DEVICE_ID_JOYPAD_LEFT] = -1.0; + myAxisCodes[0][RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1.0; + myAxisCodes[1][RETRO_DEVICE_ID_JOYPAD_UP] = -1.0; + myAxisCodes[1][RETRO_DEVICE_ID_JOYPAD_DOWN] = 1.0; +} + +bool Joypad::getButton(int i) const +{ + const int value = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, myButtonCodes[i]); + // if (value) + // log_cb(RETRO_LOG_INFO, "Joypad button: %d.\n", value); + return value != 0; +} + +double Joypad::getAxis(int i) const +{ + for (const auto & axis : myAxisCodes[i]) + { + const int value = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, axis.first); + if (value) + { + // log_cb(RETRO_LOG_INFO, "Joypad axis: %d.\n", value); + return axis.second; + } + } + + return 0.0; +} diff --git a/source/frontends/retro/joypad.h b/source/frontends/retro/joypad.h new file mode 100644 index 00000000..e01144f5 --- /dev/null +++ b/source/frontends/retro/joypad.h @@ -0,0 +1,20 @@ +#pragma once + +#include "linux/paddle.h" + +#include +#include + + +class Joypad : public Paddle +{ +public: + Joypad(); + + virtual bool getButton(int i) const; + virtual double getAxis(int i) const; + +private: + std::vector myButtonCodes; + std::vector > myAxisCodes; +}; diff --git a/source/frontends/retro/libretro.cpp b/source/frontends/retro/libretro.cpp index 5e24da7f..66a2adf5 100644 --- a/source/frontends/retro/libretro.cpp +++ b/source/frontends/retro/libretro.cpp @@ -6,9 +6,11 @@ #include "Frame.h" #include "linux/version.h" +#include "linux/paddle.h" #include "frontends/retro/game.h" #include "frontends/retro/environment.h" +#include "frontends/retro/joypad.h" namespace { @@ -81,21 +83,20 @@ void retro_set_environment(retro_environment_t cb) log_cb = logging.log; else log_cb = fallback_log; - /* - static const retro_controller_description controllers[] = - { - { "Apple Keyboard", RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_KEYBOARD, 0) }, - }; - static const retro_controller_info ports[] = - { - { controllers, 1 }, - { NULL, 0 }, - }; + static const struct retro_controller_description controllers[] = { + { "Nintendo DS", RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0) }, + }; - cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);*/ - retro_keyboard_callback callback = {&Game::keyboardCallback}; - cb(RETRO_ENVIRONMENT_SET_KEYBOARD_CALLBACK, &callback); + static const struct retro_controller_info ports[] = { + { controllers, 1 }, + { NULL, 0 }, + }; + + cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports); + + // retro_keyboard_callback callback = {&Game::keyboardCallback}; + // cb(RETRO_ENVIRONMENT_SET_KEYBOARD_CALLBACK, &callback); } void retro_set_audio_sample(retro_audio_sample_t cb) @@ -153,6 +154,9 @@ bool retro_load_game(const retro_game_info *info) log_cb(RETRO_LOG_INFO, "Game path: %s:%d\n", info->path, ok); + Paddle::instance().reset(new Joypad); + Paddle::setSquaring(false); + return ok; } catch (const std::exception & e)