diff --git a/apps/fall-fever/flycam.cpp b/apps/fall-fever/flycam.cpp index d7a7943..3ba5d54 100644 --- a/apps/fall-fever/flycam.cpp +++ b/apps/fall-fever/flycam.cpp @@ -17,7 +17,7 @@ void Flycam::keyboard_movement(entt::registry& registry) }; auto& movement_context = registry.ctx().emplace(); - auto const& key_state = registry.ctx().get>(); + auto const& key_state = registry.ctx().get>(); auto const& delta_time = registry.ctx().get(); auto camera_view = diff --git a/apps/flecs-test/main.cpp b/apps/flecs-test/main.cpp index 9588a1a..b8bae60 100644 --- a/apps/flecs-test/main.cpp +++ b/apps/flecs-test/main.cpp @@ -1,38 +1,25 @@ -#include -#include #include +#include #include +#include #include int main() { flecs::world world; - - world.import(); - spawn_window(world); - auto window = world.singleton(); + + world.import (); + auto window = Window::spawn_window(world); auto glfw_window = window.get>(); - window.observe([window](Input::KeyInput& key_input) { - auto* key_state = window.get_mut>(); - - if (key_input.action == static_cast(GLFW_PRESS)) { - key_state->press(key_input.key_code); - } else if (key_input.action == static_cast(GLFW_RELEASE)) { - key_state->release(key_input.key_code); - } - }); + world.import (); while (glfwWindowShouldClose(glfw_window->get()) == GLFW_FALSE) { glfwPollEvents(); - // X just pressed - auto* keycode= window.get>(); - if (keycode->just_pressed(Input::KeyCode{GLFW_KEY_X})) - std::cout << "X was just pressed!\n"; + world.progress(); - Input::State::update_state(window); glfwSwapBuffers(glfw_window->get()); } -} \ No newline at end of file +} diff --git a/src/core/camera.cpp b/src/core/camera.cpp index 83227d1..87d2ac2 100644 --- a/src/core/camera.cpp +++ b/src/core/camera.cpp @@ -32,13 +32,13 @@ auto Camera::front_vector(GlobalTransform const& transform) -> glm::vec3 void Camera::aspect_ratio_update(entt::registry& registry) { - float aspect_ratio = registry.ctx().get().aspect_ratio; + // float aspect_ratio = registry.ctx().get().aspect_ratio; - auto camera_view = registry.view(); + // auto camera_view = registry.view(); - for (auto [entity, camera] : camera_view.each()) { - // Orthographic projection currently unsupported - auto& camera_perspective = std::get(camera.projection); - camera_perspective.aspect_ratio = aspect_ratio; - } + // for (auto [entity, camera] : camera_view.each()) { + // // Orthographic projection currently unsupported + // auto& camera_perspective = std::get(camera.projection); + // camera_perspective.aspect_ratio = aspect_ratio; + // } } diff --git a/src/input/input.cpp b/src/input/input.cpp index 79c8e36..5bea100 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -4,36 +4,40 @@ namespace Input { -// void KeyListener::key_event(KeyInput const& key_input_event) -// { -// auto& key_state = registry.ctx().get>(); +static void keyboard_input_observer(KeyboardInput const& keyboard_input); -// if (key_input_event.action == static_cast(GLFW_PRESS)) { -// key_state.press(key_input_event.key_code); -// } else if (key_input_event.action == static_cast(GLFW_RELEASE)) { -// key_state.release(key_input_event.key_code); -// } -// } - -// void CursorListener::cursor_event(MouseMotion const& mouse_motion_event) -// { -// auto& mouse_motion = registry.ctx().get(); -// mouse_motion.delta += mouse_motion_event.delta; -// } - -void reset_mouse_motion(entt::registry& registry) +InputModule::InputModule(flecs::world& world) { - auto& mouse_motion = registry.ctx().get(); - mouse_motion = {}; + world.component(); + world.component(); + world.component(); + + world.system>("ClearButtonInput") + .kind(flecs::PostUpdate) + .each(ButtonInput::clear_button_input); + + world.observer("KeyboardInputObserver") + .event(flecs::OnSet) + .each(keyboard_input_observer); } -template void State::update_state(flecs::entity window_entity) +void keyboard_input_observer(KeyboardInput const& keyboard_input) { - auto* state = window_entity.get_mut>(); - state->just_pressed_keys.clear(); - state->just_released_keys.clear(); + auto* key_state = keyboard_input.window.get_mut>(); + + if (keyboard_input.action == static_cast(GLFW_PRESS)) { + key_state->press(keyboard_input.key_code); + } else if (keyboard_input.action == static_cast(GLFW_RELEASE)) { + key_state->release(keyboard_input.key_code); + } } -template class State; +template void ButtonInput::clear_button_input(ButtonInput& state) +{ + state.just_pressed_keys.clear(); + state.just_released_keys.clear(); +} + +template class ButtonInput; } // namespace Input diff --git a/src/input/input.h b/src/input/input.h index 47a09ef..844fb2b 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -1,14 +1,16 @@ #pragma once -#include "entt/entity/fwd.hpp" -#include #include #include #include -#include namespace Input { +struct InputModule +{ + InputModule(flecs::world& world); +}; + enum class KeyCode : int { }; @@ -17,10 +19,18 @@ enum class Action : int { }; -struct KeyInput +struct KeyboardInput { KeyCode key_code; Action action; + flecs::entity window; +}; + +struct MouseButtonInput +{ + KeyCode key_code; + Action action; + flecs::entity window; }; struct MouseMotion @@ -28,16 +38,16 @@ struct MouseMotion glm::vec2 delta{}; }; -template class State +template class ButtonInput { public: auto pressed(T input) const -> bool { return pressed_keys.contains(input); } auto just_pressed(T input) const -> bool { return just_pressed_keys.contains(input); } auto just_released(T input) const -> bool { return just_pressed_keys.contains(input); } - static void update_state(flecs::entity window_entity); + static void clear_button_input(ButtonInput& state); -// private: + // private: void press(T input) { if (pressed_keys.insert(input).second) { @@ -55,23 +65,6 @@ public: std::set pressed_keys; std::set just_pressed_keys; std::set just_released_keys; - - friend class KeyListener; - friend class CursorListener; }; -// struct KeyListener -// { -// entt::registry& registry; -// void key_event(KeyInput const& key_input_event); -// }; - -// struct CursorListener -// { -// entt::registry& registry; -// void cursor_event(MouseMotion const& mouse_motion_event); -// }; - -void reset_mouse_motion(entt::registry& registry); - }; // namespace Input diff --git a/src/window/window.cpp b/src/window/window.cpp index e7843d0..a2241e3 100644 --- a/src/window/window.cpp +++ b/src/window/window.cpp @@ -5,14 +5,17 @@ #include #include -#include #include +namespace Window { + static constexpr unsigned INIT_WINDOW_WIDTH = 1280; static constexpr unsigned INIT_WINDOW_HEIGHT = 720; +static constexpr double MOUSE_SENSITIVITY = 0.15; static void glfw_error_callback(int error, char const* description); static void framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height); +static void window_size_callback(GLFWwindow* glfw_window, int width, int height); static void key_callback(GLFWwindow* glfw_window, int key, int scancode, int action, int mods); static void mouse_cursor_callback(GLFWwindow* glfw_window, double xpos, double ypos); @@ -21,9 +24,12 @@ WindowModule::WindowModule(flecs::world& world) glfwInit(); world.component(); world.component>(); + world.component>(); + world.component(); + world.component(); } -void spawn_window(flecs::world& world) +flecs::entity spawn_window(flecs::world& world) { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); @@ -52,17 +58,26 @@ void spawn_window(flecs::world& world) glfwSetWindowUserPointer(glfw_window.get(), &world); glfwSetKeyCallback(glfw_window.get(), key_callback); glfwSetCursorPosCallback(glfw_window.get(), mouse_cursor_callback); + glfwSetWindowSizeCallback(glfw_window.get(), window_size_callback); glfwSetFramebufferSizeCallback(glfw_window.get(), framebuffer_size_callback); init_glad(); - int width, height; - glfwGetFramebufferSize(glfw_window.get(), &width, &height); - glViewport(0, 0, width, height); + int window_width, window_height; + glfwGetWindowSize(glfw_window.get(), &window_width, &window_height); + + int framebuffer_width, framebuffer_height; + glfwGetFramebufferSize(glfw_window.get(), &framebuffer_width, &framebuffer_height); + glViewport(0, 0, framebuffer_width, framebuffer_height); flecs::entity window = world.singleton(); - window.add>(); + window.add>(); + window.add>(); window.set>(glfw_window); + window.set({window_width, window_height}); + window.set({framebuffer_width, framebuffer_height}); + + return window; } static void glfw_error_callback(int error, char const* description) @@ -70,13 +85,22 @@ static void glfw_error_callback(int error, char const* description) spdlog::warn("GLFW [{:d}]: {:s}\n", error, description); } +void window_size_callback(GLFWwindow* glfw_window, int width, int height) +{ + auto* world = static_cast(glfwGetWindowUserPointer(glfw_window)); + flecs::entity window = world->singleton(); + + window.set({width, height}); +} + void framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height) { - // auto& window = *static_cast(glfwGetWindowUserPointer(glfw_window)); - // glViewport(0, 0, width, height); + auto* world = static_cast(glfwGetWindowUserPointer(glfw_window)); + flecs::entity window = world->singleton(); - // world->event().emit(); - // window.event_dispatcher.enqueue(); + window.set({width, height}); + + glViewport(0, 0, width, height); } void key_callback(GLFWwindow* glfw_window, @@ -86,77 +110,56 @@ void key_callback(GLFWwindow* glfw_window, [[maybe_unused]] int mods) { auto* world = static_cast(glfwGetWindowUserPointer(glfw_window)); - flecs::entity window = world->component(); + flecs::entity window = world->singleton(); - window.emit(Input::KeyInput{.key_code = static_cast(key), - .action = static_cast(action)}); + world->component().set( + {.key_code = static_cast(key), + .action = static_cast(action), + .window = window}); } void mouse_cursor_callback(GLFWwindow* glfw_window, double xpos, double ypos) { - // auto* world = static_cast(glfwGetWindowUserPointer(glfw_window)); + auto* world = static_cast(glfwGetWindowUserPointer(glfw_window)); + flecs::entity window = world->singleton(); - // glm::vec2 delta{xpos - window.last_cursor_pos.x, ypos - window.last_cursor_pos.y}; + auto cursor_position = window.get_mut>(); + bool had_value = cursor_position->has_value(); - // window.last_cursor_pos = {xpos, ypos}; - // delta *= MOUSE_SENSITIVITY; + *cursor_position = {xpos, ypos}; - // // Check if this is the first VALID mouse event after window being resized - // if (window.first_mouse_input && (delta.x >= std::numeric_limits::epsilon() || - // delta.y >= std::numeric_limits::epsilon())) { - // window.first_mouse_input = false; - // return; - // } + // Prevent invalid delta events when the cursor position wasn't valid + if (!had_value) + return; - // TODO remove with flecs - // window.event_dispatcher.enqueue(Input::MouseMotion{.delta = delta}); + glm::vec2 delta{xpos - cursor_position->value().x, ypos - cursor_position->value().y}; + + delta *= MOUSE_SENSITIVITY; + + world->component().set({delta}); } -auto WindowDeprecated::logical_dimensions() const -> glm::u32vec2 -{ - int width{}; - int height{}; - glfwGetWindowSize(glfw_window.get(), &width, &height); - return {width, height}; -} +} // namespace Window -auto WindowDeprecated::physical_dimensions() const -> glm::u32vec2 -{ - int width{}; - int height{}; - glfwGetFramebufferSize(glfw_window.get(), &width, &height); - return {width, height}; -} +// void WindowDeprecated::mouse_catching(entt::registry& registry) const +// { +// auto& mouse_catched = registry.ctx().emplace(MouseCatched{.catched = false}); +// auto const& key_state = registry.ctx().get>(); -void WindowDeprecated::mouse_catching(entt::registry& registry) const -{ - auto& mouse_catched = registry.ctx().emplace(MouseCatched{.catched = false}); - auto const& key_state = registry.ctx().get>(); +// if (key_state.just_pressed(Input::KeyCode{GLFW_KEY_LEFT_CONTROL})) { +// mouse_catched.catched = !mouse_catched.catched; - if (key_state.just_pressed(Input::KeyCode{GLFW_KEY_LEFT_CONTROL})) { - mouse_catched.catched = !mouse_catched.catched; +// glfwSetInputMode(glfw_window.get(), +// GLFW_CURSOR, +// mouse_catched.catched ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL); +// } +// } - glfwSetInputMode(glfw_window.get(), - GLFW_CURSOR, - mouse_catched.catched ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL); - } -} +// void WindowDeprecated::close_on_esc(entt::registry& registry) const +// { +// auto const& key_state = registry.ctx().get>(); -void WindowDeprecated::close_on_esc(entt::registry& registry) const -{ - auto const& key_state = registry.ctx().get>(); - - if (key_state.just_pressed(Input::KeyCode{GLFW_KEY_ESCAPE})) { - glfwSetWindowShouldClose(glfw_window.get(), GLFW_TRUE); - } -} - -void WindowDeprecated::update_descriptor(entt::registry& registry) const -{ - auto dimensions = logical_dimensions(); - - registry.ctx().erase(); - registry.ctx().emplace(Descriptor{ - .logical_dimensions = dimensions, - .aspect_ratio = static_cast(dimensions.x) / static_cast(dimensions.y)}); -} +// if (key_state.just_pressed(Input::KeyCode{GLFW_KEY_ESCAPE})) { +// glfwSetWindowShouldClose(glfw_window.get(), GLFW_TRUE); +// } +// } diff --git a/src/window/window.h b/src/window/window.h index 17518a3..4bfe6d3 100644 --- a/src/window/window.h +++ b/src/window/window.h @@ -8,39 +8,46 @@ class GLFWwindow; +namespace Window { + void glfw_init(); -struct Window{}; +struct Window +{}; + +struct CursorPosition +{ + double x; + double y; +}; + +struct LogicalSize +{ + int width; + int height; +}; + +struct PhysicalSize +{ + int width; + int height; +}; struct WindowModule { WindowModule(flecs::world& world); }; -void spawn_window(flecs::world& world); +flecs::entity spawn_window(flecs::world& world); + +} // namespace Window class WindowDeprecated { public: - struct MouseCatched - { - bool catched = true; - }; - - struct Descriptor - { - glm::u32vec2 logical_dimensions; - float aspect_ratio{}; - }; - - struct ResizeEvent - {}; [[nodiscard]] auto handle() -> GLFWwindow& { return *glfw_window; } - [[nodiscard]] auto physical_dimensions() const -> glm::u32vec2; - [[nodiscard]] auto logical_dimensions() const -> glm::u32vec2; - void update_descriptor(entt::registry& registry) const; void mouse_catching(entt::registry& registry) const; void close_on_esc(entt::registry& registry) const; @@ -51,7 +58,4 @@ private: flecs::entity window_entity; std::shared_ptr glfw_window; - - glm::vec2 last_cursor_pos{}; - bool first_mouse_input = true; }; diff --git a/vcpkg.json b/vcpkg.json index cbe0582..21ac891 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -21,5 +21,5 @@ "fx-gltf", "flecs" ], - "builtin-baseline": "bc994510d2eb11aac7b43b03f67a7751d5bfe0e4" + "builtin-baseline": "ce613c41372b23b1f51333815feb3edd87ef8a8b" }