Skip to content

Commit 86cc88b

Browse files
authored
Merge pull request #278 from inexorgame/hanni/glfw_input_callback
Use glfw callbacks instead of polling events manually.
2 parents c1b5666 + fa9683f commit 86cc88b

File tree

7 files changed

+274
-81
lines changed

7 files changed

+274
-81
lines changed

include/inexor/vulkan-renderer/application.hpp

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "inexor/vulkan-renderer/input/keyboard_mouse_data.hpp"
34
#include "inexor/vulkan-renderer/renderer.hpp"
45

56
#include <GLFW/glfw3.h>
@@ -10,39 +11,72 @@
1011
#include <string>
1112
#include <vector>
1213

14+
// forward declarations
15+
namespace inexor::vulkan_renderer::input {
16+
class KeyboardMouseInputData;
17+
}
18+
1319
namespace inexor::vulkan_renderer {
1420

1521
class Application : public VulkanRenderer {
1622
std::string m_application_name;
1723
std::string m_engine_name;
1824

19-
std::uint32_t m_application_version{};
20-
std::uint32_t m_engine_version{};
25+
std::uint32_t m_application_version;
26+
std::uint32_t m_engine_version;
2127

2228
std::vector<std::string> m_vertex_shader_files;
2329
std::vector<std::string> m_fragment_shader_files;
2430
std::vector<std::string> m_texture_files;
2531
std::vector<std::string> m_shader_files;
2632
std::vector<std::string> m_gltf_model_files;
2733

34+
std::unique_ptr<input::KeyboardMouseInputData> m_input_data;
35+
2836
/// @brief Load the configuration of the renderer from a TOML configuration file.
2937
/// @brief file_name The TOML configuration file.
3038
/// @note It was collectively decided not to use JSON for configuration files.
3139
void load_toml_configuration_file(const std::string &file_name);
3240
void load_textures();
3341
void load_shaders();
3442
void load_octree_geometry();
43+
void setup_window_and_input_callbacks();
3544
void update_imgui_overlay();
3645
void check_application_specific_features();
3746
void update_uniform_buffers();
38-
void update_mouse_input();
39-
40-
// TODO: Refactor!
41-
double m_cursor_x, m_cursor_y;
47+
void process_mouse_input();
48+
// TODO: Implement a method for processing keyboard input.
4249

4350
public:
4451
Application(int argc, char **argv);
4552

53+
/// @brief Call glfwSetFramebufferSizeCallback.
54+
/// @param window The window whose framebuffer was resized.
55+
/// @param width The new width, in pixels, of the framebuffer.
56+
/// @param height The new height, in pixels, of the framebuffer.
57+
void frame_buffer_resize_callback(GLFWwindow *window, int width, int height);
58+
59+
/// @brief Call glfwSetKeyCallback.
60+
/// @param window The window that received the event.
61+
/// @param key The keyboard key that was pressed or released.
62+
/// @param scancode The system-specific scancode of the key.
63+
/// @param action GLFW_PRESS, GLFW_RELEASE or GLFW_REPEAT.
64+
/// @param mods Bit field describing which modifier keys were held down.
65+
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods);
66+
67+
/// @brief Call glfwSetCursorPosCallback.
68+
/// @param window The window that received the event.
69+
/// @param xpos The new x-coordinate, in screen coordinates, of the cursor.
70+
/// @param ypos The new y-coordinate, in screen coordinates, of the cursor.
71+
void cursor_position_callback(GLFWwindow *window, double xpos, double ypos);
72+
73+
/// @brief Call glfwSetMouseButtonCallback.
74+
/// @param window The window that received the event.
75+
/// @param button The mouse button that was pressed or released.
76+
/// @param action One of GLFW_PRESS or GLFW_RELEASE.
77+
/// @param mods Bit field describing which modifier keys were held down.
78+
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods);
79+
4680
void run();
4781
};
4882

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#pragma once
2+
3+
#include <array>
4+
#include <unordered_map>
5+
6+
namespace inexor::vulkan_renderer::input {
7+
8+
/// @brief A wrapper for keyboard and mouse input data.
9+
/// @warning Not thread safe!
10+
class KeyboardMouseInputData {
11+
std::array<std::int64_t, 2> m_previous_cursor_pos{0, 0};
12+
std::array<std::int64_t, 2> m_current_cursor_pos{0, 0};
13+
std::unordered_map<std::int32_t, bool> m_pressed_keys;
14+
std::unordered_map<std::int32_t, bool> m_pressed_mouse_buttons;
15+
16+
public:
17+
KeyboardMouseInputData();
18+
KeyboardMouseInputData(const KeyboardMouseInputData &) = delete;
19+
KeyboardMouseInputData(KeyboardMouseInputData &&) = delete;
20+
21+
~KeyboardMouseInputData() = default;
22+
23+
KeyboardMouseInputData &operator=(const KeyboardMouseInputData &) = delete;
24+
KeyboardMouseInputData &operator=(KeyboardMouseInputData &&) = delete;
25+
26+
/// @brief Change the key's state to pressed.
27+
/// @param button The key which was pressed.
28+
void press_key(std::int32_t key);
29+
30+
/// @brief Change the key's state to unpressed.
31+
/// @param button The key which was released.
32+
void release_key(std::int32_t button);
33+
34+
/// @brief Change the mouse button's state to pressed.
35+
/// @param button The mouse button which was pressed.
36+
void press_mouse_button(std::int32_t button);
37+
38+
/// @brief Change the mouse button's state to unpressed.
39+
/// @param button The mouse button which was released.
40+
void release_mouse_button(std::int32_t button);
41+
42+
/// @brief Set the current cursor position.
43+
/// @param cursor_pos_x The current x-coordinate of the cursor.
44+
/// @param cursor_pos_y The current y-coordinate of the cursor.
45+
void set_cursor_pos(double cursor_pos_x, double coursor_pos_y);
46+
47+
/// @return Return a std::array of size 2 which contains the x-position in index 0 and y-position in index 1.
48+
[[nodiscard]] std::array<std::int64_t, 2> get_cursor_pos();
49+
50+
/// @brief Check if the given mouse button is currently pressed.
51+
/// @param mouse_button The mouse button index.
52+
/// @returm True if the mouse button is pressed, false otherwise.
53+
[[nodiscard]] bool is_mouse_button_pressed(std::int32_t mouse_button);
54+
55+
/// @brief Check if the given key is currently pressed.
56+
/// @param key The key index.
57+
/// @retur True if the key is pressed, false otherwise.
58+
[[nodiscard]] bool is_key_pressed(std::int32_t key);
59+
60+
/// @brief Calculate the change in x- and y-position of the cursor.
61+
/// @return A std::array of size 2 which contains the change in x-position in index 0
62+
/// and the change in y-position in index 1.
63+
[[nodiscard]] std::array<double, 2> calculate_cursor_position_delta();
64+
};
65+
66+
} // namespace inexor::vulkan_renderer::input

include/inexor/vulkan-renderer/wrapper/window.hpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace inexor::vulkan_renderer::wrapper {
1111
/// @brief RAII wrapper class for GLFW windows.
1212
class Window {
1313
GLFWwindow *m_window;
14-
std::uint32_t m_width{0};
15-
std::uint32_t m_height{0};
14+
std::uint32_t m_width;
15+
std::uint32_t m_height;
1616

1717
public:
1818
/// @brief Default constructor.
@@ -22,10 +22,8 @@ class Window {
2222
/// @param visible True if the window is visible after creation, false otherwise.
2323
/// @param resizable True if the window should be resizable, false otherwise.
2424
Window(const std::string &title, std::uint32_t width, std::uint32_t height, bool visible, bool resizable);
25-
2625
Window(const Window &) = delete;
2726
Window(Window &&) noexcept;
28-
2927
~Window();
3028

3129
Window &operator=(const Window &) = delete;
@@ -47,21 +45,24 @@ class Window {
4745
/// @param frame_buffer_resize_callback The window resize callback.
4846
void set_resize_callback(GLFWframebuffersizefun frame_buffer_resize_callback);
4947

48+
/// @brief Call glfwSetKeyCallback.
49+
/// @param key_input_callback The keyboard input callback.
50+
void set_keyboard_button_callback(GLFWkeyfun keyboard_button_callback);
51+
52+
/// @brief Call glfwSetCursorPosCallback.
53+
/// @param cursor_pos_callback They cursor position callback.
54+
void set_cursor_position_callback(GLFWcursorposfun cursor_pos_callback);
55+
56+
/// @brief Call glfwSetMouseButtonCallback.
57+
/// @param mouse_button_callback The mouse button callback.
58+
void set_mouse_button_callback(GLFWmousebuttonfun mouse_button_callback);
59+
5060
/// @brief Call glfwShowWindow.
5161
void show();
5262

5363
/// @brief Call glfwHideWindow.
5464
void hide();
5565

56-
/// @brief Returns the cursor position.
57-
std::array<double, 2> cursor_pos();
58-
59-
/// @brief Check if a specifiy button is pressed.
60-
/// @param button The button to check.
61-
/// @return ``true`` if the button is pressed.
62-
/// @todo: Use a callback instead!
63-
bool is_button_pressed(int button);
64-
6566
/// @brief Call glfwPollEvents.
6667
void poll();
6768

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ set(INEXOR_SOURCE_FILES
1212
vulkan-renderer/settings_decision_maker.cpp
1313
vulkan-renderer/time_step.cpp
1414

15+
vulkan-renderer/input/keyboard_mouse_data.cpp
16+
1517
vulkan-renderer/io/byte_stream.cpp
1618
vulkan-renderer/io/octree_parser.cpp
1719

0 commit comments

Comments
 (0)