Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ vma-dumps/*.json
*.ini
*.csv
*__pycache__*
.cache/

# auto generated documentations paths
documentation/doxygen-output/
Expand Down
2 changes: 1 addition & 1 deletion documentation/source/engine-overview/main.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Miscellaneous

misc/gpu-selection
misc/cameras
misc/keyboard-mouse-input
misc/input
misc/make_info
misc/representation
misc/labelling
Expand Down
29 changes: 29 additions & 0 deletions documentation/source/engine-overview/misc/input.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Keyboard and Mouse Input
========================

We use `glfw3 <https://www.glfw.org/>`__ for window and input management with `callbacks <https://www.glfw.org/docs/3.3/input_guide.html#input_keyboard>`__ instead of `manual polling <https://www.glfw.org/docs/3.3/group__input.html#ga67ddd1b7dcbbaff03e4a76c0ea67103a>`__. This way, we ensure we are not missing a key event. For more information check out `glfw's input guide <https://www.glfw.org/docs/3.3/input_guide.html>`__. 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 <https://stackoverflow.com/questions/7676971/pointing-to-a-function-that-is-a-class-member-glfw-setkeycallback>`__.

.. 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 <https://discourse.glfw.org/t/multithreading-glfw/573>`__.

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 <https://github.com/inexorgame/vulkan-renderer/issues/612>`__.

Joysticks
---------

.. note::
We do not yet support `joysticks <https://www.glfw.org/docs/3.3/input_guide.html#joystick>`__, but glfw allows us to support them in the future. See `issue 612 <https://github.com/inexorgame/vulkan-renderer/issues/612>`__.
39 changes: 0 additions & 39 deletions documentation/source/engine-overview/misc/keyboard-mouse-input.rst

This file was deleted.

33 changes: 3 additions & 30 deletions include/inexor/vulkan-renderer/application.hpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -18,7 +18,7 @@ class Application : public VulkanRenderer {
std::vector<std::string> m_texture_files;
std::vector<std::string> m_gltf_model_files;

std::unique_ptr<input::KeyboardMouseInputData> m_input_data;
std::unique_ptr<input::Input> m_input;

bool m_enable_validation_layers{true};
/// Inexor engine supports a variable number of octrees.
Expand All @@ -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();
};

Expand Down
39 changes: 39 additions & 0 deletions include/inexor/vulkan-renderer/input/gamepad_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <GLFW/glfw3.h>
#include <glm/detail/qualifier.hpp>
#include <glm/glm.hpp>

#include <array>
#include <shared_mutex>
#include <vector>

namespace inexor::vulkan_renderer::input {
/// @brief A wrapper for gamepad input data
class GamepadInputData {
private:
std::array<glm::vec2, 2> m_current_joystick_axes{};
std::array<glm::vec2, 2> m_previous_joystick_axes{};
std::array<std::array<bool, GLFW_JOYSTICK_LAST + 1>, 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
55 changes: 55 additions & 0 deletions include/inexor/vulkan-renderer/input/input.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include "inexor/vulkan-renderer/input/gamepad_data.hpp"
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp"

#include <GLFW/glfw3.h>

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
25 changes: 13 additions & 12 deletions include/inexor/vulkan-renderer/input/keyboard_mouse_data.hpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
#pragma once

#include <GLFW/glfw3.h>
#include <glm/glm.hpp>

#include <array>
#include <shared_mutex>
#include <vector>

namespace inexor::vulkan_renderer::input {

/// @brief A wrapper for keyboard and mouse input data.
class KeyboardMouseInputData {
private:
std::array<std::int64_t, 2> m_previous_cursor_pos{0, 0};
std::array<std::int64_t, 2> m_current_cursor_pos{0, 0};
std::array<bool, GLFW_KEY_LAST> m_pressed_keys{false};
std::array<bool, GLFW_MOUSE_BUTTON_LAST> m_pressed_mouse_buttons{false};
glm::ivec2 m_previous_cursor_pos{0, 0};
glm::ivec2 m_current_cursor_pos{0, 0};
std::array<bool, GLFW_KEY_LAST> m_key_states{false};
std::array<bool, GLFW_MOUSE_BUTTON_LAST> 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
Expand Down Expand Up @@ -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<std::int64_t, 2> 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<double, 2> 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
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading