From d0fbd68055e3f4a796330cc8acc6c0954b5327ff Mon Sep 17 00:00:00 2001 From: untodesu Date: Thu, 11 Sep 2025 15:48:53 +0500 Subject: Run clang-format across the project --- game/client/io/gamepad.cc | 366 +++++++++++++++++++++++----------------------- game/client/io/gamepad.hh | 100 ++++++------- game/client/io/glfw.hh | 80 +++++----- 3 files changed, 273 insertions(+), 273 deletions(-) (limited to 'game/client/io') diff --git a/game/client/io/gamepad.cc b/game/client/io/gamepad.cc index 3a71920..1407996 100644 --- a/game/client/io/gamepad.cc +++ b/game/client/io/gamepad.cc @@ -1,183 +1,183 @@ -#include "client/pch.hh" - -#include "client/io/gamepad.hh" - -#include "core/config/boolean.hh" -#include "core/config/number.hh" -#include "core/io/cmdline.hh" -#include "core/io/config_map.hh" -#include "core/math/constexpr.hh" - -#include "client/gui/settings.hh" -#include "client/io/glfw.hh" - -#include "client/globals.hh" -#include "client/toggles.hh" - -constexpr static int INVALID_GAMEPAD_ID = INT_MAX; -constexpr static std::size_t NUM_AXES = static_cast(GLFW_GAMEPAD_AXIS_LAST + 1); -constexpr static std::size_t NUM_BUTTONS = static_cast(GLFW_GAMEPAD_BUTTON_LAST + 1); -constexpr static float GAMEPAD_AXIS_EVENT_THRESHOLD = 0.5f; - -static int active_gamepad_id; - -bool io::gamepad::available = false; -config::Float io::gamepad::deadzone(0.00f, 0.00f, 0.66f); -config::Boolean io::gamepad::active(false); -GLFWgamepadstate io::gamepad::state; -GLFWgamepadstate io::gamepad::last_state; - -static void on_toggle_enable(const ToggleEnabledEvent& event) -{ - if(event.type == TOGGLE_USE_GAMEPAD) { - io::gamepad::active.set_value(true); - return; - } -} - -static void on_toggle_disable(const ToggleDisabledEvent& event) -{ - if(event.type == TOGGLE_USE_GAMEPAD) { - io::gamepad::active.set_value(false); - return; - } -} - -static void on_glfw_joystick_event(const io::GlfwJoystickEvent& event) -{ - if((event.event_type == GLFW_CONNECTED) && glfwJoystickIsGamepad(event.joystick_id) && (active_gamepad_id == INVALID_GAMEPAD_ID)) { - io::gamepad::available = true; - - active_gamepad_id = event.joystick_id; - - for(int i = 0; i < NUM_AXES; io::gamepad::last_state.axes[i++] = 0.0f) { - // empty - } - - for(int i = 0; i < NUM_BUTTONS; io::gamepad::last_state.buttons[i++] = GLFW_RELEASE) { - // empty - } - - spdlog::info("gamepad: detected gamepad: {}", glfwGetGamepadName(event.joystick_id)); - - return; - } - - if((event.event_type == GLFW_DISCONNECTED) && (active_gamepad_id == event.joystick_id)) { - io::gamepad::available = false; - - active_gamepad_id = INVALID_GAMEPAD_ID; - - for(int i = 0; i < NUM_AXES; io::gamepad::last_state.axes[i++] = 0.0f) { - // empty - } - - for(int i = 0; i < NUM_BUTTONS; io::gamepad::last_state.buttons[i++] = GLFW_RELEASE) { - // empty - } - - spdlog::warn("gamepad: disconnected"); - - return; - } -} - -void io::gamepad::init(void) -{ - io::gamepad::available = false; - - active_gamepad_id = INVALID_GAMEPAD_ID; - - globals::client_config.add_value("gamepad.deadzone", io::gamepad::deadzone); - globals::client_config.add_value("gamepad.active", io::gamepad::active); - - settings::add_checkbox(0, io::gamepad::active, settings_location::GAMEPAD, "gamepad.active", true); - settings::add_slider(1, io::gamepad::deadzone, settings_location::GAMEPAD, "gamepad.deadzone", true, "%.03f"); - - auto mappings_path = io::cmdline::get_cstr("gpmap", "misc/gamecontrollerdb.txt"); - auto mappings_file = PHYSFS_openRead(mappings_path); - - if(mappings_file) { - spdlog::info("gamepad: using mappings from {}", mappings_path); - auto mappings_string = std::string(PHYSFS_fileLength(mappings_file), char(0x00)); - PHYSFS_readBytes(mappings_file, mappings_string.data(), mappings_string.size()); - glfwUpdateGamepadMappings(mappings_string.c_str()); - PHYSFS_close(mappings_file); - } - - for(int joystick = 0; joystick <= GLFW_JOYSTICK_LAST; joystick += 1) { - if(glfwJoystickIsGamepad(joystick)) { - io::gamepad::available = true; - - active_gamepad_id = joystick; - - for(int i = 0; i < NUM_AXES; io::gamepad::last_state.axes[i++] = 0.0f) { - // empty - } - - for(int i = 0; i < NUM_BUTTONS; io::gamepad::last_state.buttons[i++] = GLFW_RELEASE) { - // empty - } - - spdlog::info("gamepad: detected gamepad: {}", glfwGetGamepadName(joystick)); - - break; - } - } - - for(int i = 0; i < NUM_AXES; io::gamepad::state.axes[i++] = 0.0f) { - // empty - } - - for(int i = 0; i < NUM_BUTTONS; io::gamepad::state.buttons[i++] = GLFW_RELEASE) { - // empty - } - - globals::dispatcher.sink().connect<&on_toggle_enable>(); - globals::dispatcher.sink().connect<&on_toggle_disable>(); - globals::dispatcher.sink().connect<&on_glfw_joystick_event>(); -} - -void io::gamepad::update_late(void) -{ - if(active_gamepad_id == INVALID_GAMEPAD_ID) { - // No active gamepad found - return; - } - - if(glfwGetGamepadState(active_gamepad_id, &io::gamepad::state)) { - for(int i = 0; i < NUM_AXES; ++i) { - if((math::abs(io::gamepad::state.axes[i]) > GAMEPAD_AXIS_EVENT_THRESHOLD) - && (math::abs(io::gamepad::last_state.axes[i]) <= GAMEPAD_AXIS_EVENT_THRESHOLD)) { - GamepadAxisEvent event; - event.action = GLFW_PRESS; - event.axis = i; - globals::dispatcher.enqueue(event); - continue; - } - - if((math::abs(io::gamepad::state.axes[i]) <= GAMEPAD_AXIS_EVENT_THRESHOLD) - && (math::abs(io::gamepad::last_state.axes[i]) > GAMEPAD_AXIS_EVENT_THRESHOLD)) { - GamepadAxisEvent event; - event.action = GLFW_RELEASE; - event.axis = i; - globals::dispatcher.enqueue(event); - continue; - } - } - - for(int i = 0; i < NUM_BUTTONS; ++i) { - if(io::gamepad::state.buttons[i] == io::gamepad::last_state.buttons[i]) { - // Nothing happens - continue; - } - - GamepadButtonEvent event; - event.action = io::gamepad::state.buttons[i]; - event.button = i; - globals::dispatcher.enqueue(event); - } - } - - io::gamepad::last_state = io::gamepad::state; -} +#include "client/pch.hh" + +#include "client/io/gamepad.hh" + +#include "core/config/boolean.hh" +#include "core/config/number.hh" +#include "core/io/cmdline.hh" +#include "core/io/config_map.hh" +#include "core/math/constexpr.hh" + +#include "client/gui/settings.hh" +#include "client/io/glfw.hh" + +#include "client/globals.hh" +#include "client/toggles.hh" + +constexpr static int INVALID_GAMEPAD_ID = INT_MAX; +constexpr static std::size_t NUM_AXES = static_cast(GLFW_GAMEPAD_AXIS_LAST + 1); +constexpr static std::size_t NUM_BUTTONS = static_cast(GLFW_GAMEPAD_BUTTON_LAST + 1); +constexpr static float GAMEPAD_AXIS_EVENT_THRESHOLD = 0.5f; + +static int active_gamepad_id; + +bool io::gamepad::available = false; +config::Float io::gamepad::deadzone(0.00f, 0.00f, 0.66f); +config::Boolean io::gamepad::active(false); +GLFWgamepadstate io::gamepad::state; +GLFWgamepadstate io::gamepad::last_state; + +static void on_toggle_enable(const ToggleEnabledEvent& event) +{ + if(event.type == TOGGLE_USE_GAMEPAD) { + io::gamepad::active.set_value(true); + return; + } +} + +static void on_toggle_disable(const ToggleDisabledEvent& event) +{ + if(event.type == TOGGLE_USE_GAMEPAD) { + io::gamepad::active.set_value(false); + return; + } +} + +static void on_glfw_joystick_event(const io::GlfwJoystickEvent& event) +{ + if((event.event_type == GLFW_CONNECTED) && glfwJoystickIsGamepad(event.joystick_id) && (active_gamepad_id == INVALID_GAMEPAD_ID)) { + io::gamepad::available = true; + + active_gamepad_id = event.joystick_id; + + for(int i = 0; i < NUM_AXES; io::gamepad::last_state.axes[i++] = 0.0f) { + // empty + } + + for(int i = 0; i < NUM_BUTTONS; io::gamepad::last_state.buttons[i++] = GLFW_RELEASE) { + // empty + } + + spdlog::info("gamepad: detected gamepad: {}", glfwGetGamepadName(event.joystick_id)); + + return; + } + + if((event.event_type == GLFW_DISCONNECTED) && (active_gamepad_id == event.joystick_id)) { + io::gamepad::available = false; + + active_gamepad_id = INVALID_GAMEPAD_ID; + + for(int i = 0; i < NUM_AXES; io::gamepad::last_state.axes[i++] = 0.0f) { + // empty + } + + for(int i = 0; i < NUM_BUTTONS; io::gamepad::last_state.buttons[i++] = GLFW_RELEASE) { + // empty + } + + spdlog::warn("gamepad: disconnected"); + + return; + } +} + +void io::gamepad::init(void) +{ + io::gamepad::available = false; + + active_gamepad_id = INVALID_GAMEPAD_ID; + + globals::client_config.add_value("gamepad.deadzone", io::gamepad::deadzone); + globals::client_config.add_value("gamepad.active", io::gamepad::active); + + settings::add_checkbox(0, io::gamepad::active, settings_location::GAMEPAD, "gamepad.active", true); + settings::add_slider(1, io::gamepad::deadzone, settings_location::GAMEPAD, "gamepad.deadzone", true, "%.03f"); + + auto mappings_path = io::cmdline::get_cstr("gpmap", "misc/gamecontrollerdb.txt"); + auto mappings_file = PHYSFS_openRead(mappings_path); + + if(mappings_file) { + spdlog::info("gamepad: using mappings from {}", mappings_path); + auto mappings_string = std::string(PHYSFS_fileLength(mappings_file), char(0x00)); + PHYSFS_readBytes(mappings_file, mappings_string.data(), mappings_string.size()); + glfwUpdateGamepadMappings(mappings_string.c_str()); + PHYSFS_close(mappings_file); + } + + for(int joystick = 0; joystick <= GLFW_JOYSTICK_LAST; joystick += 1) { + if(glfwJoystickIsGamepad(joystick)) { + io::gamepad::available = true; + + active_gamepad_id = joystick; + + for(int i = 0; i < NUM_AXES; io::gamepad::last_state.axes[i++] = 0.0f) { + // empty + } + + for(int i = 0; i < NUM_BUTTONS; io::gamepad::last_state.buttons[i++] = GLFW_RELEASE) { + // empty + } + + spdlog::info("gamepad: detected gamepad: {}", glfwGetGamepadName(joystick)); + + break; + } + } + + for(int i = 0; i < NUM_AXES; io::gamepad::state.axes[i++] = 0.0f) { + // empty + } + + for(int i = 0; i < NUM_BUTTONS; io::gamepad::state.buttons[i++] = GLFW_RELEASE) { + // empty + } + + globals::dispatcher.sink().connect<&on_toggle_enable>(); + globals::dispatcher.sink().connect<&on_toggle_disable>(); + globals::dispatcher.sink().connect<&on_glfw_joystick_event>(); +} + +void io::gamepad::update_late(void) +{ + if(active_gamepad_id == INVALID_GAMEPAD_ID) { + // No active gamepad found + return; + } + + if(glfwGetGamepadState(active_gamepad_id, &io::gamepad::state)) { + for(int i = 0; i < NUM_AXES; ++i) { + if((math::abs(io::gamepad::state.axes[i]) > GAMEPAD_AXIS_EVENT_THRESHOLD) + && (math::abs(io::gamepad::last_state.axes[i]) <= GAMEPAD_AXIS_EVENT_THRESHOLD)) { + GamepadAxisEvent event; + event.action = GLFW_PRESS; + event.axis = i; + globals::dispatcher.enqueue(event); + continue; + } + + if((math::abs(io::gamepad::state.axes[i]) <= GAMEPAD_AXIS_EVENT_THRESHOLD) + && (math::abs(io::gamepad::last_state.axes[i]) > GAMEPAD_AXIS_EVENT_THRESHOLD)) { + GamepadAxisEvent event; + event.action = GLFW_RELEASE; + event.axis = i; + globals::dispatcher.enqueue(event); + continue; + } + } + + for(int i = 0; i < NUM_BUTTONS; ++i) { + if(io::gamepad::state.buttons[i] == io::gamepad::last_state.buttons[i]) { + // Nothing happens + continue; + } + + GamepadButtonEvent event; + event.action = io::gamepad::state.buttons[i]; + event.button = i; + globals::dispatcher.enqueue(event); + } + } + + io::gamepad::last_state = io::gamepad::state; +} diff --git a/game/client/io/gamepad.hh b/game/client/io/gamepad.hh index 9c56894..ff38127 100644 --- a/game/client/io/gamepad.hh +++ b/game/client/io/gamepad.hh @@ -1,50 +1,50 @@ -#pragma once - -namespace io -{ -constexpr static int INVALID_GAMEPAD_AXIS = INT_MAX; -constexpr static int INVALID_GAMEPAD_BUTTON = INT_MAX; -} // namespace io - -namespace config -{ -class Boolean; -class Float; -} // namespace config - -struct GLFWgamepadstate; - -namespace io::gamepad -{ -extern bool available; -extern config::Float deadzone; -extern config::Boolean active; -extern GLFWgamepadstate state; -extern GLFWgamepadstate last_state; -} // namespace io::gamepad - -namespace io::gamepad -{ -void init(void); -void update_late(void); -} // namespace io::gamepad - -namespace io -{ -// This simulates buttons using axes. When an axis -// value exceeds 1.5 times the deadzone, the event is -// queued with a GLFW_PRESS action, when it falls back -// below the threshold, the event is queued with GLFW_RELEASE action -struct GamepadAxisEvent final { - int action; - int axis; -}; - -// This smears GLFW event sugar over gamepad polling -// system. Whenever it detects a state change, the event -// is queued with an appropriate action -struct GamepadButtonEvent final { - int action; - int button; -}; -} // namespace io +#pragma once + +namespace io +{ +constexpr static int INVALID_GAMEPAD_AXIS = INT_MAX; +constexpr static int INVALID_GAMEPAD_BUTTON = INT_MAX; +} // namespace io + +namespace config +{ +class Boolean; +class Float; +} // namespace config + +struct GLFWgamepadstate; + +namespace io::gamepad +{ +extern bool available; +extern config::Float deadzone; +extern config::Boolean active; +extern GLFWgamepadstate state; +extern GLFWgamepadstate last_state; +} // namespace io::gamepad + +namespace io::gamepad +{ +void init(void); +void update_late(void); +} // namespace io::gamepad + +namespace io +{ +// This simulates buttons using axes. When an axis +// value exceeds 1.5 times the deadzone, the event is +// queued with a GLFW_PRESS action, when it falls back +// below the threshold, the event is queued with GLFW_RELEASE action +struct GamepadAxisEvent final { + int action; + int axis; +}; + +// This smears GLFW event sugar over gamepad polling +// system. Whenever it detects a state change, the event +// is queued with an appropriate action +struct GamepadButtonEvent final { + int action; + int button; +}; +} // namespace io diff --git a/game/client/io/glfw.hh b/game/client/io/glfw.hh index ea3f4a7..bbd767a 100644 --- a/game/client/io/glfw.hh +++ b/game/client/io/glfw.hh @@ -1,40 +1,40 @@ -#ifndef CLIENTFW -#define CLIENTFW 1 -#pragma once - -namespace io -{ -struct GlfwCursorPosEvent final { - glm::fvec2 pos; -}; - -struct GlfwFramebufferSizeEvent final { - glm::ivec2 size; - float aspect; -}; - -struct GlfwJoystickEvent final { - int joystick_id; - int event_type; -}; - -struct GlfwKeyEvent final { - int key { GLFW_KEY_UNKNOWN }; - int scancode; - int action; - int mods; -}; - -struct GlfwMouseButtonEvent final { - int button { GLFW_KEY_UNKNOWN }; - int action; - int mods; -}; - -struct GlfwScrollEvent final { - float dx; - float dy; -}; -} // namespace io - -#endif // CLIENTFW +#ifndef CLIENTFW +#define CLIENTFW 1 +#pragma once + +namespace io +{ +struct GlfwCursorPosEvent final { + glm::fvec2 pos; +}; + +struct GlfwFramebufferSizeEvent final { + glm::ivec2 size; + float aspect; +}; + +struct GlfwJoystickEvent final { + int joystick_id; + int event_type; +}; + +struct GlfwKeyEvent final { + int key { GLFW_KEY_UNKNOWN }; + int scancode; + int action; + int mods; +}; + +struct GlfwMouseButtonEvent final { + int button { GLFW_KEY_UNKNOWN }; + int action; + int mods; +}; + +struct GlfwScrollEvent final { + float dx; + float dy; +}; +} // namespace io + +#endif // CLIENTFW -- cgit