diff --git a/.gitignore b/.gitignore index 262c8a3c9..8881ad2ab 100644 --- a/.gitignore +++ b/.gitignore @@ -46,6 +46,7 @@ vma-dumps/*.json *.ini *.csv *__pycache__* +.cache/ # auto generated documentations paths documentation/doxygen-output/ diff --git a/documentation/source/engine-overview/main.rst b/documentation/source/engine-overview/main.rst index c9c7ae97c..3214c54c9 100644 --- a/documentation/source/engine-overview/main.rst +++ b/documentation/source/engine-overview/main.rst @@ -119,7 +119,7 @@ Miscellaneous misc/gpu-selection misc/cameras - misc/keyboard-mouse-input + misc/input misc/make_info misc/representation misc/labelling diff --git a/documentation/source/engine-overview/misc/input.rst b/documentation/source/engine-overview/misc/input.rst new file mode 100644 index 000000000..c106912de --- /dev/null +++ b/documentation/source/engine-overview/misc/input.rst @@ -0,0 +1,29 @@ +Keyboard and Mouse Input +======================== + +We use `glfw3 `__ for window and input management with `callbacks `__ instead of `manual polling `__. This way, we ensure we are not missing a key event. For more information check out `glfw's input guide `__. We use a wrapper class for input named ``Input``. Currently, this supports keyboard, mouse, and one gamepad. + +.. note:: + We redirect input events to class methods which handle it. Because glfw is a C-style API, it is not possible to use class methods directly as callbacks for keyboard and mouse input data. To fix this, we set the glfw window user pointer to the class instance which contains the input callback methods. Then, we use a lambda to set up the class method as callback. All setups are done in ``Application::setup_window_and_input_callbacks``. For more information about this workaround, check out this `Stackoverflow issue `__. + +.. note:: + It's not possible handle glfw input data in a thread which is separate from the thread which created the corresponding window. For more information, check out this `glfw forum post `__. + +Keyboard and Mouse Input +------------------------ + +- Keyboard and mouse input data is managed in the ``KeyboardMouseInputData`` class. + +Gamepads +-------- + +- Gamepad input data is managed in the ``GamepadInputData`` class. + +.. note:: + We currently support only one gamepad. Refactoring of this is required. See `issue 612 `__. + +Joysticks +--------- + +.. note:: + We do not yet support `joysticks `__, but glfw allows us to support them in the future. See `issue 612 `__. diff --git a/documentation/source/engine-overview/misc/keyboard-mouse-input.rst b/documentation/source/engine-overview/misc/keyboard-mouse-input.rst deleted file mode 100644 index dfb33696e..000000000 --- a/documentation/source/engine-overview/misc/keyboard-mouse-input.rst +++ /dev/null @@ -1,39 +0,0 @@ -Keyboard and Mouse Input -======================== - -Inexor engine uses `glfw3 `__ for window management and for keyboard and mouse input. Inexor does not use `manual polling `__ of keyboard and mouse input data, but uses `callbacks `__ instead. This way, we ensure we are not missing a key event. For more information check out `glfw's input guide `__. Inexor uses a wrapper class for keyboard and mouse input data, called ``KeyboardMouseInputData``. This class offers an easy-to-use interface for setting and getting keyboard and mouse input. ``KeyboardMouseInputData`` is thread safe since `pull request 401. `__ - -.. note:: - Inexor redirects keyboard and mouse input events to class methods which handle it. Because glfw is a C-style API, it is not possible to use class methods directly as callbacks for keyboard and mouse input data. To fix this, we set the glfw window user pointer to the class instance which contains the input callback methods. Then, we use a lambda to set up the class method as callback. All setups are done in ``Application::setup_window_and_input_callbacks``. For more information about this workaround, check out this `Stackoverflow issue `__. - -.. note:: - It's not possible handle glfw input data in a thread which is separate from the thread which created the corresponding window. For more information, check out this `glfw forum post `__. - -Keyboard Input --------------- - -* We store the pressed keys as a ``std::array`` member in ``KeyboardMouseInputData`` -* The maximum number of keys is defined by ``GLFW_KEY_LAST`` -* If a key is pressed or released, we notify ``KeyboardMouseInputData`` by calling method ``press_key`` and ``release_key``, respectively -* Check if a key is currently pressed by calling method ``is_key_pressed`` -* Check if a key was pressed once by calling method ``was_key_pressed_once`` - -Mouse Input ------------ - -* We store the pressed mouse buttons as a ``std::array`` member in ``KeyboardMouseInputData`` -* The maximum number of mouse buttons is defined by ``GLFW_MOUSE_BUTTON_LAST``. -* If a mouse button is pressed or released, we notify ``KeyboardMouseInputData`` by calling method ``press_mouse_button`` and ``release_mouse_button``, respectively -* To update the current cursor position, we call ``set_cursor_pos`` -* To get the current cursor position, we call ``get_cursor_pos`` -* The change in cursor position can be queried with ``calculate_cursor_position_delta`` -* Check if a mouse button is pressed by calling method ``is_mouse_button_pressed`` -* Check if a mouse button was pressed once by calling method ``was_mouse_button_pressed_once`` - -Joysticks ---------- - -.. note:: - The support for joysticks is work in progress. For further information, read `GitHub pull request 518 `__. - -* We do not yet support `joysticks `__, but glfw allows us to support them in the future diff --git a/include/inexor/vulkan-renderer/application.hpp b/include/inexor/vulkan-renderer/application.hpp index 9542318e6..7d71c7e57 100644 --- a/include/inexor/vulkan-renderer/application.hpp +++ b/include/inexor/vulkan-renderer/application.hpp @@ -1,6 +1,6 @@ #pragma once -#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp" +#include "inexor/vulkan-renderer/input/input.hpp" #include "inexor/vulkan-renderer/renderer.hpp" #include "inexor/vulkan-renderer/world/collision_query.hpp" #include "inexor/vulkan-renderer/world/cube.hpp" @@ -18,7 +18,7 @@ class Application : public VulkanRenderer { std::vector m_texture_files; std::vector m_gltf_model_files; - std::unique_ptr m_input_data; + std::unique_ptr m_input; bool m_enable_validation_layers{true}; /// Inexor engine supports a variable number of octrees. @@ -42,38 +42,11 @@ class Application : public VulkanRenderer { void update_uniform_buffers(); /// Use the camera's position and view direction vector to check for ray-octree collisions with all octrees. void check_octree_collisions(); - void process_mouse_input(); + void process_input(); public: Application(int argc, char **argv); - /// @brief Call glfwSetKeyCallback. - /// @param window The window that received the event. - /// @param key The keyboard key that was pressed or released. - /// @param scancode The system-specific scancode of the key. - /// @param action GLFW_PRESS, GLFW_RELEASE or GLFW_REPEAT. - /// @param mods Bit field describing which modifier keys were held down. - void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods); - - /// @brief Call glfwSetCursorPosCallback. - /// @param window The window that received the event. - /// @param x_pos The new x-coordinate, in screen coordinates, of the cursor. - /// @param y_pos The new y-coordinate, in screen coordinates, of the cursor. - void cursor_position_callback(GLFWwindow *window, double x_pos, double y_pos); - - /// @brief Call glfwSetMouseButtonCallback. - /// @param window The window that received the event. - /// @param button The mouse button that was pressed or released. - /// @param action One of GLFW_PRESS or GLFW_RELEASE. - /// @param mods Bit field describing which modifier keys were held down. - void mouse_button_callback(GLFWwindow *window, int button, int action, int mods); - - /// @brief Call camera's process_mouse_scroll method. - /// @param window The window that received the event. - /// @param x_offset The change of x-offset of the mouse wheel. - /// @param y_offset The change of y-offset of the mouse wheel. - void mouse_scroll_callback(GLFWwindow *window, double x_offset, double y_offset); - void run(); }; diff --git a/include/inexor/vulkan-renderer/input/gamepad_data.hpp b/include/inexor/vulkan-renderer/input/gamepad_data.hpp new file mode 100644 index 000000000..37aae497e --- /dev/null +++ b/include/inexor/vulkan-renderer/input/gamepad_data.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include + +namespace inexor::vulkan_renderer::input { +/// @brief A wrapper for gamepad input data +class GamepadInputData { +private: + std::array m_current_joystick_axes{}; + std::array m_previous_joystick_axes{}; + std::array, GLFW_GAMEPAD_BUTTON_LAST> m_button_states{}; + bool m_joysticks_updated{false}; + bool m_buttons_updated{false}; + mutable std::shared_mutex m_input_mutex; + +public: + GamepadInputData() = default; + + void press_button(std::int32_t button, std::int32_t joystick = GLFW_JOYSTICK_1); + + void release_button(std::int32_t button, std::int32_t joystick = GLFW_JOYSTICK_1); + + [[nodiscard]] bool is_button_pressed(std::int32_t button, std::int32_t joystick = GLFW_JOYSTICK_1); + + [[nodiscard]] bool was_button_pressed_once(std::int32_t button, std::int32_t joystick = GLFW_JOYSTICK_1); + + void set_joystick_axis(std::int32_t axis, float state, std::int32_t joystick = GLFW_JOYSTICK_1); + + [[nodiscard]] glm::vec2 current_joystick_axes(std::int32_t joystick); + + [[nodiscard]] glm::vec2 calculate_joystick_axes_delta(std::int32_t joystick); +}; +} // namespace inexor::vulkan_renderer::input diff --git a/include/inexor/vulkan-renderer/input/input.hpp b/include/inexor/vulkan-renderer/input/input.hpp new file mode 100644 index 000000000..8be91c239 --- /dev/null +++ b/include/inexor/vulkan-renderer/input/input.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include "inexor/vulkan-renderer/input/gamepad_data.hpp" +#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp" + +#include + +namespace inexor::vulkan_renderer::input { +class Input { +private: + GamepadInputData m_gamepad_data; + KeyboardMouseInputData m_kbm_data; + +public: + Input() = default; + + GamepadInputData &gamepad_data() { + return m_gamepad_data; + } + KeyboardMouseInputData &kbm_data() { + return m_kbm_data; + } + + /// @brief Call glfwSetKeyCallback. + /// @param window The window that received the event. + /// @param key The keyboard key that was pressed or released. + /// @param scancode The system-specific scancode of the key. + /// @param action GLFW_PRESS, GLFW_RELEASE or GLFW_REPEAT. + /// @param mods Bit field describing which modifier keys were held down. + void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods); + + /// @brief Call glfwSetCursorPosCallback. + /// @param window The window that received the event. + /// @param x_pos The new x-coordinate, in screen coordinates, of the cursor. + /// @param y_pos The new y-coordinate, in screen coordinates, of the cursor. + void cursor_position_callback(GLFWwindow *window, double x_pos, double y_pos); + + /// @brief Call glfwSetMouseButtonCallback. + /// @param window The window that received the event. + /// @param button The mouse button that was pressed or released. + /// @param action One of GLFW_PRESS or GLFW_RELEASE. + /// @param mods Bit field describing which modifier keys were held down. + void mouse_button_callback(GLFWwindow *window, int button, int action, int mods); + + /// @brief Call camera's process_mouse_scroll method. + /// @param window The window that received the event. + /// @param x_offset The change of x-offset of the mouse wheel. + /// @param y_offset The change of y-offset of the mouse wheel. + void mouse_scroll_callback(GLFWwindow *window, double x_offset, double y_offset); + + void update_gamepad_data(); + + void update(); +}; +} // namespace inexor::vulkan_renderer::input diff --git a/include/inexor/vulkan-renderer/input/keyboard_mouse_data.hpp b/include/inexor/vulkan-renderer/input/keyboard_mouse_data.hpp index 161a8769f..d20e6c74e 100644 --- a/include/inexor/vulkan-renderer/input/keyboard_mouse_data.hpp +++ b/include/inexor/vulkan-renderer/input/keyboard_mouse_data.hpp @@ -1,31 +1,28 @@ #pragma once #include +#include #include #include +#include namespace inexor::vulkan_renderer::input { /// @brief A wrapper for keyboard and mouse input data. class KeyboardMouseInputData { private: - std::array m_previous_cursor_pos{0, 0}; - std::array m_current_cursor_pos{0, 0}; - std::array m_pressed_keys{false}; - std::array m_pressed_mouse_buttons{false}; + glm::ivec2 m_previous_cursor_pos{0, 0}; + glm::ivec2 m_current_cursor_pos{0, 0}; + std::array m_key_states{false}; + std::array m_mouse_button_states{false}; + double m_mouse_wheel_offset{}; bool m_keyboard_updated{false}; bool m_mouse_buttons_updated{false}; mutable std::shared_mutex m_input_mutex; public: KeyboardMouseInputData() = default; - KeyboardMouseInputData(const KeyboardMouseInputData &) = delete; - KeyboardMouseInputData(KeyboardMouseInputData &&) = delete; - ~KeyboardMouseInputData() = default; - - KeyboardMouseInputData &operator=(const KeyboardMouseInputData &) = delete; - KeyboardMouseInputData &operator=(KeyboardMouseInputData &&) = delete; /// @brief Change the key's state to pressed. /// @param key the key which was pressed and greater or equal to 0 @@ -75,12 +72,16 @@ class KeyboardMouseInputData { /// @param pos_y the current y-coordinate of the cursor void set_cursor_pos(double pos_x, double pos_y); - [[nodiscard]] std::array get_cursor_pos() const; + [[nodiscard]] glm::ivec2 get_cursor_pos() const; /// @brief Calculate the change in x- and y-position of the cursor. /// @return a std::array of size 2 which contains the change in x-position in index 0 and the change in y-position /// in index 1 - [[nodiscard]] std::array calculate_cursor_position_delta(); + [[nodiscard]] glm::dvec2 calculate_cursor_position_delta(); + + void set_mouse_wheel_offset(double y_offset); + + [[nodiscard]] double get_mouse_wheel_offset() const; }; } // namespace inexor::vulkan_renderer::input diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b6de5133d..c36ac4949 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,8 @@ set(INEXOR_SOURCE_FILES vulkan-renderer/renderer.cpp vulkan-renderer/time_step.cpp + vulkan-renderer/input/gamepad_data.cpp + vulkan-renderer/input/input.cpp vulkan-renderer/input/keyboard_mouse_data.cpp vulkan-renderer/io/byte_stream.cpp diff --git a/src/vulkan-renderer/application.cpp b/src/vulkan-renderer/application.cpp index 2fa189c52..616e71dd8 100644 --- a/src/vulkan-renderer/application.cpp +++ b/src/vulkan-renderer/application.cpp @@ -1,5 +1,6 @@ #include "inexor/vulkan-renderer/application.hpp" +#include "inexor/vulkan-renderer/camera.hpp" #include "inexor/vulkan-renderer/exception.hpp" #include "inexor/vulkan-renderer/meta.hpp" #include "inexor/vulkan-renderer/octree_gpu_vertex.hpp" @@ -11,6 +12,7 @@ #include "inexor/vulkan-renderer/wrapper/descriptor_builder.hpp" #include "inexor/vulkan-renderer/wrapper/instance.hpp" +#include #include #include @@ -19,48 +21,6 @@ namespace inexor::vulkan_renderer { -void Application::key_callback(GLFWwindow * /*window*/, int key, int, int action, int /*mods*/) { - if (key < 0 || key > GLFW_KEY_LAST) { - return; - } - - switch (action) { - case GLFW_PRESS: - m_input_data->press_key(key); - break; - case GLFW_RELEASE: - m_input_data->release_key(key); - break; - default: - break; - } -} - -void Application::cursor_position_callback(GLFWwindow * /*window*/, double x_pos, double y_pos) { - m_input_data->set_cursor_pos(x_pos, y_pos); -} - -void Application::mouse_button_callback(GLFWwindow * /*window*/, int button, int action, int /*mods*/) { - if (button < 0 || button > GLFW_MOUSE_BUTTON_LAST) { - return; - } - - switch (action) { - case GLFW_PRESS: - m_input_data->press_mouse_button(button); - break; - case GLFW_RELEASE: - m_input_data->release_mouse_button(button); - break; - default: - break; - } -} - -void Application::mouse_scroll_callback(GLFWwindow * /*window*/, double /*x_offset*/, double y_offset) { - m_camera->change_zoom(static_cast(y_offset)); -} - void Application::load_toml_configuration_file(const std::string &file_name) { spdlog::trace("Loading TOML configuration file: {}", file_name); @@ -231,7 +191,7 @@ void Application::setup_window_and_input_callbacks() { auto lambda_key_callback = [](GLFWwindow *window, int key, int scancode, int action, int mods) { auto *app = static_cast(glfwGetWindowUserPointer(window)); - app->key_callback(window, key, scancode, action, mods); + app->m_input->key_callback(window, key, scancode, action, mods); }; m_window->set_keyboard_button_callback(lambda_key_callback); @@ -240,7 +200,7 @@ void Application::setup_window_and_input_callbacks() { auto lambda_cursor_position_callback = [](GLFWwindow *window, double xpos, double ypos) { auto *app = static_cast(glfwGetWindowUserPointer(window)); - app->cursor_position_callback(window, xpos, ypos); + app->m_input->cursor_position_callback(window, xpos, ypos); }; m_window->set_cursor_position_callback(lambda_cursor_position_callback); @@ -249,7 +209,7 @@ void Application::setup_window_and_input_callbacks() { auto lambda_mouse_button_callback = [](GLFWwindow *window, int button, int action, int mods) { auto *app = static_cast(glfwGetWindowUserPointer(window)); - app->mouse_button_callback(window, button, action, mods); + app->m_input->mouse_button_callback(window, button, action, mods); }; m_window->set_mouse_button_callback(lambda_mouse_button_callback); @@ -258,7 +218,7 @@ void Application::setup_window_and_input_callbacks() { auto lambda_mouse_scroll_callback = [](GLFWwindow *window, double xoffset, double yoffset) { auto *app = static_cast(glfwGetWindowUserPointer(window)); - app->mouse_scroll_callback(window, xoffset, yoffset); + app->m_input->mouse_scroll_callback(window, xoffset, yoffset); }; m_window->set_mouse_scroll_callback(lambda_mouse_scroll_callback); @@ -376,7 +336,7 @@ Application::Application(int argc, char **argv) { VK_MAKE_API_VERSION(0, ENGINE_VERSION[0], ENGINE_VERSION[1], ENGINE_VERSION[2]), m_enable_validation_layers, enable_renderdoc_instance_layer); - m_input_data = std::make_unique(); + m_input = std::make_unique(); m_surface = std::make_unique(m_instance->instance(), m_window->get()); @@ -508,13 +468,13 @@ void Application::update_uniform_buffers() { } void Application::update_imgui_overlay() { - auto cursor_pos = m_input_data->get_cursor_pos(); + auto cursor_pos = m_input->kbm_data().get_cursor_pos(); ImGuiIO &io = ImGui::GetIO(); io.DeltaTime = m_time_passed; io.MousePos = ImVec2(static_cast(cursor_pos[0]), static_cast(cursor_pos[1])); - io.MouseDown[0] = m_input_data->is_mouse_button_pressed(GLFW_MOUSE_BUTTON_LEFT); - io.MouseDown[1] = m_input_data->is_mouse_button_pressed(GLFW_MOUSE_BUTTON_RIGHT); + io.MouseDown[0] = m_input->kbm_data().is_mouse_button_pressed(GLFW_MOUSE_BUTTON_LEFT); + io.MouseDown[1] = m_input->kbm_data().is_mouse_button_pressed(GLFW_MOUSE_BUTTON_RIGHT); io.DisplaySize = ImVec2(static_cast(m_swapchain->extent().width), static_cast(m_swapchain->extent().height)); @@ -551,17 +511,33 @@ void Application::update_imgui_overlay() { m_imgui_overlay->update(); } -void Application::process_mouse_input() { - const auto cursor_pos_delta = m_input_data->calculate_cursor_position_delta(); +void Application::process_input() { + const auto cursor_pos_delta = m_input->kbm_data().calculate_cursor_position_delta(); - if (m_camera->type() == CameraType::LOOK_AT && m_input_data->is_mouse_button_pressed(GLFW_MOUSE_BUTTON_LEFT)) { + auto deadzone_lambda = [](const float state) { return (glm::abs(state) < 0.2f) ? 0.0f : state; }; + + if (m_camera->type() == CameraType::LOOK_AT && + m_input->kbm_data().is_mouse_button_pressed(GLFW_MOUSE_BUTTON_LEFT)) { m_camera->rotate(static_cast(cursor_pos_delta[0]), -static_cast(cursor_pos_delta[1])); } + if (m_camera->type() == CameraType::LOOK_AT) { + m_camera->rotate(deadzone_lambda(m_input->gamepad_data().current_joystick_axes(1).x) * 5.f, + deadzone_lambda(m_input->gamepad_data().current_joystick_axes(1).y) * -5.f); + } - m_camera->set_movement_state(CameraMovement::FORWARD, m_input_data->is_key_pressed(GLFW_KEY_W)); - m_camera->set_movement_state(CameraMovement::LEFT, m_input_data->is_key_pressed(GLFW_KEY_A)); - m_camera->set_movement_state(CameraMovement::BACKWARD, m_input_data->is_key_pressed(GLFW_KEY_S)); - m_camera->set_movement_state(CameraMovement::RIGHT, m_input_data->is_key_pressed(GLFW_KEY_D)); + m_camera->set_movement_state(CameraMovement::FORWARD, + m_input->gamepad_data().current_joystick_axes(0)[GLFW_GAMEPAD_AXIS_LEFT_Y] <= -0.15); + m_camera->set_movement_state(CameraMovement::LEFT, + m_input->gamepad_data().current_joystick_axes(0)[GLFW_GAMEPAD_AXIS_LEFT_X] <= -0.15); + m_camera->set_movement_state(CameraMovement::BACKWARD, + m_input->gamepad_data().current_joystick_axes(0)[GLFW_GAMEPAD_AXIS_LEFT_Y] >= 0.15); + m_camera->set_movement_state(CameraMovement::RIGHT, + m_input->gamepad_data().current_joystick_axes(0)[GLFW_GAMEPAD_AXIS_LEFT_X] >= 0.15); + m_camera->update(m_time_passed); + m_camera->set_movement_state(CameraMovement::FORWARD, m_input->kbm_data().is_key_pressed(GLFW_KEY_W)); + m_camera->set_movement_state(CameraMovement::LEFT, m_input->kbm_data().is_key_pressed(GLFW_KEY_A)); + m_camera->set_movement_state(CameraMovement::BACKWARD, m_input->kbm_data().is_key_pressed(GLFW_KEY_S)); + m_camera->set_movement_state(CameraMovement::RIGHT, m_input->kbm_data().is_key_pressed(GLFW_KEY_D)); } void Application::check_octree_collisions() { @@ -590,11 +566,12 @@ void Application::run() { while (!m_window->should_close()) { m_window->poll(); + m_input->update_gamepad_data(); update_uniform_buffers(); update_imgui_overlay(); render_frame(); - process_mouse_input(); - if (m_input_data->was_key_pressed_once(GLFW_KEY_N)) { + process_input(); + if (m_input->kbm_data().was_key_pressed_once(GLFW_KEY_N)) { load_octree_geometry(false); generate_octree_indices(); m_index_buffer->upload_data(m_octree_indices); diff --git a/src/vulkan-renderer/input/gamepad_data.cpp b/src/vulkan-renderer/input/gamepad_data.cpp new file mode 100644 index 000000000..928855b42 --- /dev/null +++ b/src/vulkan-renderer/input/gamepad_data.cpp @@ -0,0 +1,47 @@ +#include "inexor/vulkan-renderer/input/gamepad_data.hpp" + +#include + +namespace inexor::vulkan_renderer::input { +void GamepadInputData::press_button(std::int32_t button, std::int32_t joystick) { + std::scoped_lock lock(m_input_mutex); + m_button_states.at(joystick).at(button) = true; + m_buttons_updated = true; +} + +void GamepadInputData::release_button(std::int32_t button, std::int32_t joystick) { + std::scoped_lock lock(m_input_mutex); + m_button_states.at(joystick).at(button) = false; + m_buttons_updated = true; +} + +[[nodiscard]] bool GamepadInputData::is_button_pressed(std::int32_t button, std::int32_t joystick) { + std::scoped_lock lock(m_input_mutex); + return m_button_states.at(joystick).at(button); +} + +[[nodiscard]] bool GamepadInputData::was_button_pressed_once(std::int32_t button, std::int32_t joystick) { + std::scoped_lock lock(m_input_mutex); + if (!m_button_states.at(joystick).at(button) || !m_buttons_updated) { + return false; + } + m_button_states.at(joystick).at(button) = false; + return true; +} + +void GamepadInputData::set_joystick_axis(std::int32_t axis, float state, std::int32_t joystick) { + std::scoped_lock lock(m_input_mutex); + m_current_joystick_axes[joystick][axis] = state; + m_joysticks_updated = true; +} + +[[nodiscard]] glm::vec2 GamepadInputData::current_joystick_axes(std::int32_t joystick) { + std::scoped_lock lock(m_input_mutex); + return m_current_joystick_axes.at(joystick); +} + +[[nodiscard]] glm::vec2 GamepadInputData::calculate_joystick_axes_delta(std::int32_t joystick) { + std::scoped_lock lock(m_input_mutex); + return {m_current_joystick_axes.at(joystick) - m_previous_joystick_axes.at(joystick)}; +} +} // namespace inexor::vulkan_renderer::input diff --git a/src/vulkan-renderer/input/input.cpp b/src/vulkan-renderer/input/input.cpp new file mode 100644 index 000000000..e68d23ef0 --- /dev/null +++ b/src/vulkan-renderer/input/input.cpp @@ -0,0 +1,73 @@ +#include "inexor/vulkan-renderer/input/input.hpp" + +#include +#include + +namespace inexor::vulkan_renderer::input { +void Input::key_callback(GLFWwindow * /*window*/, int key, int /*scancode*/, int action, int /*mods*/) { + if (key < 0 || key > GLFW_KEY_LAST) { + return; + } + + switch (action) { + case GLFW_PRESS: + m_kbm_data.press_key(key); + break; + case GLFW_RELEASE: + m_kbm_data.release_key(key); + break; + default: + break; + } +} + +void Input::cursor_position_callback(GLFWwindow * /*window*/, double x_pos, double y_pos) { + m_kbm_data.set_cursor_pos(x_pos, y_pos); +} + +void Input::mouse_button_callback(GLFWwindow * /*window*/, int button, int action, int /*mods*/) { + if (button < 0 || button > GLFW_MOUSE_BUTTON_LAST) { + return; + } + + switch (action) { + case GLFW_PRESS: + m_kbm_data.press_mouse_button(button); + break; + case GLFW_RELEASE: + m_kbm_data.release_mouse_button(button); + break; + default: + break; + } +} + +void Input::mouse_scroll_callback(GLFWwindow * /*window*/, double /*x_offset*/, double y_offset) { + m_kbm_data.set_mouse_wheel_offset(y_offset); +} + +void Input::update_gamepad_data() { + if (glfwJoystickIsGamepad(GLFW_JOYSTICK_1) != 1) { + return; + } + GLFWgamepadstate state; + if (glfwGetGamepadState(GLFW_JOYSTICK_1, &state) == 1) { + for (int i = 0; i < GLFW_GAMEPAD_BUTTON_LAST; i++) { + if (state.buttons[i] == 1) { + m_gamepad_data.press_button(i); + } else { + m_gamepad_data.release_button(i); + } + } + for (int i = 0; i < 2; i++) { + m_gamepad_data.set_joystick_axis(i, state.axes[i]); + m_gamepad_data.set_joystick_axis(i, state.axes[i + 2], 1); + } + } +} + +void Input::update() { + glfwPollEvents(); + update_gamepad_data(); +} +} // namespace inexor::vulkan_renderer::input diff --git a/src/vulkan-renderer/input/keyboard_mouse_data.cpp b/src/vulkan-renderer/input/keyboard_mouse_data.cpp index c42fe1b44..65bcdd09a 100644 --- a/src/vulkan-renderer/input/keyboard_mouse_data.cpp +++ b/src/vulkan-renderer/input/keyboard_mouse_data.cpp @@ -1,108 +1,92 @@ #include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp" +#include +#include + #include #include namespace inexor::vulkan_renderer::input { void KeyboardMouseInputData::press_key(const std::int32_t key) { - assert(key >= 0); - assert(key < GLFW_KEY_LAST); - std::scoped_lock lock(m_input_mutex); - m_pressed_keys[key] = true; + m_key_states.at(key) = true; m_keyboard_updated = true; } void KeyboardMouseInputData::release_key(const std::int32_t key) { - assert(key >= 0); - assert(key < GLFW_KEY_LAST); - std::scoped_lock lock(m_input_mutex); - m_pressed_keys[key] = false; + m_key_states.at(key) = false; m_keyboard_updated = true; } bool KeyboardMouseInputData::is_key_pressed(const std::int32_t key) const { - assert(key >= 0); - assert(key < GLFW_KEY_LAST); - - std::shared_lock lock(m_input_mutex); - return m_pressed_keys[key]; + std::scoped_lock lock(m_input_mutex); + return m_key_states.at(key); } bool KeyboardMouseInputData::was_key_pressed_once(const std::int32_t key) { - assert(key >= 0); - assert(key < GLFW_KEY_LAST); - std::scoped_lock lock(m_input_mutex); - if (!m_pressed_keys[key] || !m_keyboard_updated) { + if (!m_key_states.at(key) || !m_keyboard_updated) { return false; } - m_pressed_keys[key] = false; + m_key_states.at(key) = false; return true; } void KeyboardMouseInputData::press_mouse_button(const std::int32_t button) { - assert(button >= 0); - assert(button < GLFW_MOUSE_BUTTON_LAST); - std::scoped_lock lock(m_input_mutex); - m_pressed_mouse_buttons[button] = true; + m_mouse_button_states.at(button) = true; m_mouse_buttons_updated = true; } void KeyboardMouseInputData::release_mouse_button(const std::int32_t button) { - assert(button >= 0); - assert(button < GLFW_MOUSE_BUTTON_LAST); - std::scoped_lock lock(m_input_mutex); - m_pressed_mouse_buttons[button] = false; + m_mouse_button_states.at(button) = false; m_mouse_buttons_updated = true; } bool KeyboardMouseInputData::is_mouse_button_pressed(const std::int32_t button) const { - assert(button >= 0); - assert(button < GLFW_MOUSE_BUTTON_LAST); - - std::shared_lock lock(m_input_mutex); - return m_pressed_mouse_buttons[button]; + std::scoped_lock lock(m_input_mutex); + return m_mouse_button_states.at(button); } bool KeyboardMouseInputData::was_mouse_button_pressed_once(const std::int32_t button) { - assert(button >= 0); - assert(button < GLFW_MOUSE_BUTTON_LAST); - std::scoped_lock lock(m_input_mutex); - if (!m_pressed_mouse_buttons[button] || !m_mouse_buttons_updated) { + if (!m_mouse_button_states.at(button) || !m_mouse_buttons_updated) { return false; } - m_pressed_mouse_buttons[button] = false; + m_mouse_button_states.at(button) = false; return true; } void KeyboardMouseInputData::set_cursor_pos(const double pos_x, const double pos_y) { std::scoped_lock lock(m_input_mutex); - m_current_cursor_pos[0] = static_cast(pos_x); - m_current_cursor_pos[1] = static_cast(pos_y); + m_current_cursor_pos = {pos_x, pos_y}; } -std::array KeyboardMouseInputData::get_cursor_pos() const { - std::shared_lock lock(m_input_mutex); +glm::ivec2 KeyboardMouseInputData::get_cursor_pos() const { + std::scoped_lock lock(m_input_mutex); return m_current_cursor_pos; } -std::array KeyboardMouseInputData::calculate_cursor_position_delta() { +glm::dvec2 KeyboardMouseInputData::calculate_cursor_position_delta() { std::scoped_lock lock(m_input_mutex); // Calculate the change in cursor position in x- and y-axis. - const std::array m_cursor_pos_delta{ - static_cast(m_current_cursor_pos[0]) - static_cast(m_previous_cursor_pos[0]), - static_cast(m_current_cursor_pos[1]) - static_cast(m_previous_cursor_pos[1])}; + auto m_cursor_pos_delta = m_current_cursor_pos - m_previous_cursor_pos; m_previous_cursor_pos = m_current_cursor_pos; return m_cursor_pos_delta; } +void KeyboardMouseInputData::set_mouse_wheel_offset(double y_offset) { + std::scoped_lock lock(m_input_mutex); + m_mouse_wheel_offset = y_offset; +} +[[nodiscard]] double KeyboardMouseInputData::get_mouse_wheel_offset() const { + std::scoped_lock lock(m_input_mutex); + return m_mouse_wheel_offset; +} } // namespace inexor::vulkan_renderer::input diff --git a/src/vulkan-renderer/wrapper/window.cpp b/src/vulkan-renderer/wrapper/window.cpp index 25ef7115c..0993218f8 100644 --- a/src/vulkan-renderer/wrapper/window.cpp +++ b/src/vulkan-renderer/wrapper/window.cpp @@ -1,5 +1,6 @@ #include "inexor/vulkan-renderer/wrapper/window.hpp" +#include #include #include