Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[input] Added controller support #518

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
212 changes: 212 additions & 0 deletions compile_commands.json

Large diffs are not rendered by default.

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 @@ -26,7 +26,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 @@ -50,38 +50,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
48 changes: 48 additions & 0 deletions include/inexor/vulkan-renderer/input/gamepad_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

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

#define GLM_PRECISION_HIGHP_DOUBLE
#define GLM_PRECISION_HIGHP_INT
Comment on lines +7 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define GLM_PRECISION_HIGHP_DOUBLE
#define GLM_PRECISION_HIGHP_INT

These don't do anything.

#include <glm/glm.hpp>

#include <array>
#include <shared_mutex>
Comment on lines +3 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "glm/detail/qualifier.hpp"
#include <GLFW/glfw3.h>
#include <vector>
#define GLM_PRECISION_HIGHP_DOUBLE
#define GLM_PRECISION_HIGHP_INT
#include <glm/glm.hpp>
#include <array>
#include <shared_mutex>
#include <glm/detail/qualifier.hpp>
#include <GLFW/glfw3.h>
#define GLM_PRECISION_HIGHP_DOUBLE
#define GLM_PRECISION_HIGHP_INT
#include <glm/glm.hpp>
#include <array>
#include <shared_mutex>
#include <vector>

Copy link
Member

@IAmNotHanni IAmNotHanni Dec 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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_GAMEPAD_BUTTON_LAST + 1>, GLFW_GAMEPAD_BUTTON_LAST> m_button_states{};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the outer array size should be GLFW_JOYSTICK_LAST + 1?

bool m_joysticks_updated{false};
bool m_buttons_updated{false};
mutable std::shared_mutex m_input_mutex;

public:
GamepadInputData() = default;
GamepadInputData(const GamepadInputData &) = delete;
GamepadInputData(GamepadInputData &&) = delete;
~GamepadInputData() = default;

GamepadInputData &operator=(const GamepadInputData &) = delete;
GamepadInputData &operator=(GamepadInputData &&) = delete;

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

#include <GLFW/glfw3.h>

#include "inexor/vulkan-renderer/input/gamepad_data.hpp"
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp"
Comment on lines +3 to +6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <GLFW/glfw3.h>
#include "inexor/vulkan-renderer/input/gamepad_data.hpp"
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp"
#include "inexor/vulkan-renderer/input/gamepad_data.hpp"
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp"
#include <GLFW/glfw3.h>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


namespace inexor::vulkan_renderer::input {
class Input {
private:
GamepadInputData m_gamepad_data;
KeyboardMouseInputData m_kbm_data;

public:
Input() = default;
Input(const Input &) = delete;
Input(Input &&) = delete;
~Input() = default;

Input &operator=(const Input &) = delete;
Input &operator=(Input &&) = delete;

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
22 changes: 16 additions & 6 deletions include/inexor/vulkan-renderer/input/keyboard_mouse_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

#include <GLFW/glfw3.h>

#define GLM_PRECISION_HIGHP_DOUBLE
#define GLM_PRECISION_HIGHP_INT
#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;
Expand Down Expand Up @@ -75,12 +81,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();
Comment on lines +84 to +89
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use glm types now everywhere or do we abstract them away to keep independent?
@IAmNotHanni @yeetari

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using glm here is ok.


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 @@ -10,6 +10,8 @@ set(INEXOR_SOURCE_FILES
vulkan-renderer/time_step.cpp

vulkan-renderer/input/keyboard_mouse_data.cpp
vulkan-renderer/input/gamepad_data.cpp
vulkan-renderer/input/input.cpp
Comment on lines 12 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
vulkan-renderer/input/keyboard_mouse_data.cpp
vulkan-renderer/input/gamepad_data.cpp
vulkan-renderer/input/input.cpp
vulkan-renderer/input/gamepad_data.cpp
vulkan-renderer/input/input.cpp
vulkan-renderer/input/keyboard_mouse_data.cpp

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes: keep in alphabetical order


vulkan-renderer/io/byte_stream.cpp
vulkan-renderer/io/nxoc_parser.cpp
Expand Down
Loading