From bd35dfbce38ea9f4ff1a0243b5588466d8e10f99 Mon Sep 17 00:00:00 2001 From: Adam Pooley Date: Tue, 7 Jul 2020 10:50:57 +0100 Subject: [PATCH 1/2] DeadZone float saving/loading to input mapping cfg files. --- core/cfg/ini.cpp | 26 ++++++++++++++++++++++++++ core/cfg/ini.h | 3 +++ core/input/gamepad_device.cpp | 2 +- core/input/gamepad_device.h | 2 +- core/input/mapping.cpp | 4 +++- core/input/mapping.h | 4 ++++ 6 files changed, 38 insertions(+), 3 deletions(-) diff --git a/core/cfg/ini.cpp b/core/cfg/ini.cpp index e698231c4..eb9e97140 100644 --- a/core/cfg/ini.cpp +++ b/core/cfg/ini.cpp @@ -39,6 +39,12 @@ bool ConfigEntry::get_bool() } } +float ConfigEntry::get_float() +{ + return strtof(this->value.c_str(), NULL); + //return atof(this->value.c_str()); +} + /* ConfigSection */ bool ConfigSection::has_entry(const std::string& name) @@ -158,6 +164,19 @@ int ConfigFile::get_int(const std::string& section_name, const std::string& entr } } +float ConfigFile::get_float(const std::string& section_name, const std::string& entry_name, float default_value) +{ + ConfigEntry* entry = this->get_entry(section_name, entry_name); + if (entry == NULL) + { + return default_value; + } + else + { + return entry->get_float(); + } +} + bool ConfigFile::get_bool(const std::string& section_name, const std::string& entry_name, bool default_value) { ConfigEntry* entry = this->get_entry(section_name, entry_name); @@ -188,6 +207,13 @@ void ConfigFile::set_int(const std::string& section_name, const std::string& ent this->set(section_name, entry_name, str_value.str(), is_virtual); } +void ConfigFile::set_float(const std::string& section_name, const std::string& entry_name, float value, bool is_virtual) +{ + std::stringstream str_value; + str_value << value; + this->set(section_name, entry_name, str_value.str(), is_virtual); +} + void ConfigFile::set_bool(const std::string& section_name, const std::string& entry_name, bool value, bool is_virtual) { std::string str_value = (value ? "yes" : "no"); diff --git a/core/cfg/ini.h b/core/cfg/ini.h index e9951723d..385d4aeef 100644 --- a/core/cfg/ini.h +++ b/core/cfg/ini.h @@ -8,6 +8,7 @@ struct ConfigEntry { std::string value; const std::string& get_string() const; int get_int(); + float get_float(); bool get_bool(); }; @@ -38,10 +39,12 @@ struct ConfigFile { /* getting values */ std::string get(const std::string& section_name, const std::string& entry_name, const std::string& default_value = ""); int get_int(const std::string& section_name, const std::string& entry_name, int default_value = 0); + float get_float(const std::string& section_name, const std::string& entry_name, float default_value = 0.0f); bool get_bool(const std::string& section_name, const std::string& entry_name, bool default_value = false); /* setting values */ void set(const std::string& section_name, const std::string& entry_name, const std::string& value, bool is_virtual = false); void set_int(const std::string& section_name, const std::string& entry_name, int value, bool is_virtual = false); + void set_float(const std::string& section_name, const std::string& entry_name, float value, bool is_virtual = false); void set_bool(const std::string& section_name, const std::string& entry_name, bool value, bool is_virtual = false); void delete_section(const std::string& section_name); diff --git a/core/input/gamepad_device.cpp b/core/input/gamepad_device.cpp index e1bf9eafa..717fd616d 100644 --- a/core/input/gamepad_device.cpp +++ b/core/input/gamepad_device.cpp @@ -218,7 +218,7 @@ bool GamepadDevice::gamepad_axis_input(u32 code, int value) } // Radial dead zone // FIXME compute both axes at the same time - if ((float)(v * v + *other_axis * *other_axis) < _dead_zone * _dead_zone * 128.f * 128.f) + if ((float)(v * v + *other_axis * *other_axis) < input_mapper->dead_zone * input_mapper->dead_zone * 128.f * 128.f) { *this_axis = 0; *other_axis = 0; diff --git a/core/input/gamepad_device.h b/core/input/gamepad_device.h index 34b9a38a7..113dfecaa 100644 --- a/core/input/gamepad_device.h +++ b/core/input/gamepad_device.h @@ -89,7 +89,7 @@ private: double _detection_start_time = 0.0; input_detected_cb _input_detected; bool _remappable; - float _dead_zone = 0.1f; + //float _dead_zone = 0.1f; static std::vector> _gamepads; static std::mutex _gamepads_mutex; diff --git a/core/input/mapping.cpp b/core/input/mapping.cpp index d2e7144cd..31e828b86 100644 --- a/core/input/mapping.cpp +++ b/core/input/mapping.cpp @@ -67,7 +67,7 @@ axis_list[] = { { DC_AXIS_X, "dreamcast", "axis_x", "compat", "axis_x_inverted" }, { DC_AXIS_Y, "dreamcast", "axis_y", "compat", "axis_y_inverted" }, - { DC_AXIS_LT, "dreamcast", "axis_trigger_left", "compat", "axis_trigger_left_inverted" }, + { DC_AXIS_LT, "dreamcast", "axis_trigger_left", "compat", "axis_trigger_left_inverted" }, { DC_AXIS_RT, "dreamcast", "axis_trigger_right", "compat", "axis_trigger_right_inverted" }, { DC_AXIS_X2, "dreamcast", "axis_right_x", "compat", "axis_right_x_inverted" }, { DC_AXIS_Y2, "dreamcast", "axis_right_y", "compat", "axis_right_y_inverted" }, @@ -136,6 +136,7 @@ void InputMapping::load(FILE* fp) mf.parse(fp); this->name = mf.get("emulator", "mapping_name", ""); + this->dead_zone = mf.get_float("emulator", "dead_zone", 0.0f); for (u32 i = 0; i < ARRAY_SIZE(button_list); i++) { @@ -212,6 +213,7 @@ bool InputMapping::save(const char *name) ConfigFile mf; mf.set("emulator", "mapping_name", this->name); + mf.set_float("emulator", "dead_zone", this->dead_zone); for (u32 i = 0; i < ARRAY_SIZE(button_list); i++) { diff --git a/core/input/mapping.h b/core/input/mapping.h index 86833d4e5..a77585705 100644 --- a/core/input/mapping.h +++ b/core/input/mapping.h @@ -30,12 +30,14 @@ public: InputMapping() {} InputMapping(const InputMapping& other) { name = other.name; + dead_zone = other.dead_zone; buttons = other.buttons; axes = other.axes; axes_inverted = other.axes_inverted; } std::string name; + float dead_zone; DreamcastKey get_button_id(u32 code) { @@ -91,6 +93,8 @@ class IdentityInputMapping : public InputMapping public: IdentityInputMapping() { name = "Default"; + dead_zone = 0.1f; + for (int i = 0; i < 32; i++) set_button((DreamcastKey)(1 << i), 1 << i); set_axis(DC_AXIS_X, DC_AXIS_X, false); From a1f0053be638be1dd52c8cb755706a2466f02a6a Mon Sep 17 00:00:00 2001 From: Adam Pooley Date: Tue, 7 Jul 2020 12:30:55 +0100 Subject: [PATCH 2/2] Converting dead_zone from float to int storage in cfg file with min/max range of 0-100. --- core/cfg/ini.cpp | 26 -------------------------- core/cfg/ini.h | 3 --- core/input/gamepad_device.h | 1 - core/input/mapping.cpp | 9 +++++++-- 4 files changed, 7 insertions(+), 32 deletions(-) diff --git a/core/cfg/ini.cpp b/core/cfg/ini.cpp index eb9e97140..e698231c4 100644 --- a/core/cfg/ini.cpp +++ b/core/cfg/ini.cpp @@ -39,12 +39,6 @@ bool ConfigEntry::get_bool() } } -float ConfigEntry::get_float() -{ - return strtof(this->value.c_str(), NULL); - //return atof(this->value.c_str()); -} - /* ConfigSection */ bool ConfigSection::has_entry(const std::string& name) @@ -164,19 +158,6 @@ int ConfigFile::get_int(const std::string& section_name, const std::string& entr } } -float ConfigFile::get_float(const std::string& section_name, const std::string& entry_name, float default_value) -{ - ConfigEntry* entry = this->get_entry(section_name, entry_name); - if (entry == NULL) - { - return default_value; - } - else - { - return entry->get_float(); - } -} - bool ConfigFile::get_bool(const std::string& section_name, const std::string& entry_name, bool default_value) { ConfigEntry* entry = this->get_entry(section_name, entry_name); @@ -207,13 +188,6 @@ void ConfigFile::set_int(const std::string& section_name, const std::string& ent this->set(section_name, entry_name, str_value.str(), is_virtual); } -void ConfigFile::set_float(const std::string& section_name, const std::string& entry_name, float value, bool is_virtual) -{ - std::stringstream str_value; - str_value << value; - this->set(section_name, entry_name, str_value.str(), is_virtual); -} - void ConfigFile::set_bool(const std::string& section_name, const std::string& entry_name, bool value, bool is_virtual) { std::string str_value = (value ? "yes" : "no"); diff --git a/core/cfg/ini.h b/core/cfg/ini.h index 385d4aeef..e9951723d 100644 --- a/core/cfg/ini.h +++ b/core/cfg/ini.h @@ -8,7 +8,6 @@ struct ConfigEntry { std::string value; const std::string& get_string() const; int get_int(); - float get_float(); bool get_bool(); }; @@ -39,12 +38,10 @@ struct ConfigFile { /* getting values */ std::string get(const std::string& section_name, const std::string& entry_name, const std::string& default_value = ""); int get_int(const std::string& section_name, const std::string& entry_name, int default_value = 0); - float get_float(const std::string& section_name, const std::string& entry_name, float default_value = 0.0f); bool get_bool(const std::string& section_name, const std::string& entry_name, bool default_value = false); /* setting values */ void set(const std::string& section_name, const std::string& entry_name, const std::string& value, bool is_virtual = false); void set_int(const std::string& section_name, const std::string& entry_name, int value, bool is_virtual = false); - void set_float(const std::string& section_name, const std::string& entry_name, float value, bool is_virtual = false); void set_bool(const std::string& section_name, const std::string& entry_name, bool value, bool is_virtual = false); void delete_section(const std::string& section_name); diff --git a/core/input/gamepad_device.h b/core/input/gamepad_device.h index 113dfecaa..2b6880184 100644 --- a/core/input/gamepad_device.h +++ b/core/input/gamepad_device.h @@ -89,7 +89,6 @@ private: double _detection_start_time = 0.0; input_detected_cb _input_detected; bool _remappable; - //float _dead_zone = 0.1f; static std::vector> _gamepads; static std::mutex _gamepads_mutex; diff --git a/core/input/mapping.cpp b/core/input/mapping.cpp index 31e828b86..23a9e0690 100644 --- a/core/input/mapping.cpp +++ b/core/input/mapping.cpp @@ -136,7 +136,12 @@ void InputMapping::load(FILE* fp) mf.parse(fp); this->name = mf.get("emulator", "mapping_name", ""); - this->dead_zone = mf.get_float("emulator", "dead_zone", 0.0f); + + int dz = mf.get_int("emulator", "dead_zone", 10); + dz = std::min(dz, 100); + dz = std::max(dz, 0); + + this->dead_zone = ((float) dz) / 100; for (u32 i = 0; i < ARRAY_SIZE(button_list); i++) { @@ -213,7 +218,7 @@ bool InputMapping::save(const char *name) ConfigFile mf; mf.set("emulator", "mapping_name", this->name); - mf.set_float("emulator", "dead_zone", this->dead_zone); + mf.set_int("emulator", "dead_zone", this->dead_zone * 100); for (u32 i = 0; i < ARRAY_SIZE(button_list); i++) {