start with input

This commit is contained in:
2025-04-13 21:16:10 +02:00
parent ecad49ee32
commit baca1b2f97
8 changed files with 63 additions and 58 deletions

View File

@@ -23,7 +23,7 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/lib)
add_library(fever_core add_library(fever_core
src/components/transform.cpp src/components/transform.cpp
src/core/application.cpp # src/core/application.cpp
src/core/camera.cpp src/core/camera.cpp
src/core/glad.cpp src/core/glad.cpp
src/core/graphics/framebuffer.cpp src/core/graphics/framebuffer.cpp

View File

@@ -20,7 +20,7 @@ namespace FeverCore {
Application::~Application() = default; Application::~Application() = default;
Application::Application() : Application::Application() :
game_window(std::make_shared<Window>(event_dispatcher)), game_window(std::make_shared<Window>(&_world)),
post_processing_framebuffer(game_window->physical_dimensions()), post_processing_framebuffer(game_window->physical_dimensions()),
key_listener{.registry = entt_registry}, key_listener{.registry = entt_registry},
cursor_listener{.registry = entt_registry}, cursor_listener{.registry = entt_registry},
@@ -68,7 +68,7 @@ void Application::run()
this->update(); this->update();
Input::State<Input::KeyCode>::update_state(entt_registry); // Input::State<Input::KeyCode>::update_state(entt_registry);
Input::reset_mouse_motion(entt_registry); Input::reset_mouse_motion(entt_registry);
// --- Render and buffer swap --- // --- Render and buffer swap ---

View File

@@ -2,12 +2,11 @@
#include "core/graphics/framebuffer.h" #include "core/graphics/framebuffer.h"
#include "core/shader.h" #include "core/shader.h"
#include "entt/entity/fwd.hpp"
#include "entt/signal/fwd.hpp"
#include "input/input.h" #include "input/input.h"
#include "scene/gltf_loader.h" #include "scene/gltf_loader.h"
#include <entt/entt.hpp> #include <entt/entt.hpp>
#include <flecs.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <memory> #include <memory>
@@ -30,8 +29,8 @@ public:
auto registry() -> entt::registry& { return entt_registry; } auto registry() -> entt::registry& { return entt_registry; }
auto registry() const -> entt::registry const& { return entt_registry; } auto registry() const -> entt::registry const& { return entt_registry; }
auto dispatcher() -> entt::dispatcher& { return event_dispatcher; } auto world() -> flecs::world& { return _world; }
auto dispatcher() const -> entt::dispatcher const& { return event_dispatcher; } auto world() const -> flecs::world const& { return _world; }
void run(); void run();
@@ -47,6 +46,7 @@ protected:
Framebuffer post_processing_framebuffer; Framebuffer post_processing_framebuffer;
entt::registry entt_registry; entt::registry entt_registry;
flecs::world _world;
entt::dispatcher event_dispatcher{}; entt::dispatcher event_dispatcher{};
Input::KeyListener key_listener; Input::KeyListener key_listener;

View File

@@ -4,22 +4,22 @@
namespace Input { namespace Input {
void KeyListener::key_event(KeyInput const& key_input_event) // void KeyListener::key_event(KeyInput const& key_input_event)
{ // {
auto& key_state = registry.ctx().get<State<KeyCode>>(); // auto& key_state = registry.ctx().get<State<KeyCode>>();
if (key_input_event.action == static_cast<Action>(GLFW_PRESS)) { // if (key_input_event.action == static_cast<Action>(GLFW_PRESS)) {
key_state.press(key_input_event.key_code); // key_state.press(key_input_event.key_code);
} else if (key_input_event.action == static_cast<Action>(GLFW_RELEASE)) { // } else if (key_input_event.action == static_cast<Action>(GLFW_RELEASE)) {
key_state.release(key_input_event.key_code); // key_state.release(key_input_event.key_code);
} // }
} // }
void CursorListener::cursor_event(MouseMotion const& mouse_motion_event) // void CursorListener::cursor_event(MouseMotion const& mouse_motion_event)
{ // {
auto& mouse_motion = registry.ctx().get<MouseMotion>(); // auto& mouse_motion = registry.ctx().get<MouseMotion>();
mouse_motion.delta += mouse_motion_event.delta; // mouse_motion.delta += mouse_motion_event.delta;
} // }
void reset_mouse_motion(entt::registry& registry) void reset_mouse_motion(entt::registry& registry)
{ {
@@ -27,11 +27,11 @@ void reset_mouse_motion(entt::registry& registry)
mouse_motion = {}; mouse_motion = {};
} }
template <typename T> void State<T>::update_state(entt::registry& registry) template <typename T> void State<T>::update_state(flecs::entity window_entity)
{ {
auto& state = registry.ctx().get<State<T>>(); auto* state = window_entity.get_mut<State<T>>();
state.just_pressed_keys.clear(); state->just_pressed_keys.clear();
state.just_released_keys.clear(); state->just_released_keys.clear();
} }
template class State<KeyCode>; template class State<KeyCode>;

View File

@@ -2,6 +2,7 @@
#include "entt/entity/fwd.hpp" #include "entt/entity/fwd.hpp"
#include <entt/entt.hpp> #include <entt/entt.hpp>
#include <flecs.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <set> #include <set>
#include <utility> #include <utility>
@@ -34,9 +35,9 @@ public:
auto just_pressed(T input) const -> bool { return just_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); } auto just_released(T input) const -> bool { return just_pressed_keys.contains(input); }
static void update_state(entt::registry& registry); static void update_state(flecs::entity window_entity);
private: // private:
void press(T input) void press(T input)
{ {
if (pressed_keys.insert(input).second) { if (pressed_keys.insert(input).second) {
@@ -59,17 +60,17 @@ private:
friend class CursorListener; friend class CursorListener;
}; };
struct KeyListener // struct KeyListener
{ // {
entt::registry& registry; // entt::registry& registry;
void key_event(KeyInput const& key_input_event); // void key_event(KeyInput const& key_input_event);
}; // };
struct CursorListener // struct CursorListener
{ // {
entt::registry& registry; // entt::registry& registry;
void cursor_event(MouseMotion const& mouse_motion_event); // void cursor_event(MouseMotion const& mouse_motion_event);
}; // };
void reset_mouse_motion(entt::registry& registry); void reset_mouse_motion(entt::registry& registry);

View File

@@ -5,12 +5,14 @@
#include <glad/gl.h> #include <glad/gl.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <iostream>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
static constexpr unsigned INIT_WINDOW_WIDTH = 1280; static constexpr unsigned INIT_WINDOW_WIDTH = 1280;
static constexpr unsigned INIT_WINDOW_HEIGHT = 720; static constexpr unsigned INIT_WINDOW_HEIGHT = 720;
Window::Window(entt::dispatcher& event_dispatcher) : event_dispatcher(event_dispatcher) Window::Window(flecs::world* world, flecs::entity window_entity) :
world(world), window_entity(window_entity)
{ {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
@@ -42,7 +44,7 @@ Window::Window(entt::dispatcher& event_dispatcher) : event_dispatcher(event_disp
glfwSetFramebufferSizeCallback(glfw_window.get(), framebuffer_size_callback); glfwSetFramebufferSizeCallback(glfw_window.get(), framebuffer_size_callback);
init_glad(); init_glad();
{ {
int width, height; int width, height;
glfwGetFramebufferSize(glfw_window.get(), &width, &height); glfwGetFramebufferSize(glfw_window.get(), &width, &height);
@@ -60,7 +62,8 @@ void Window::framebuffer_size_callback(GLFWwindow* glfw_window, int width, int h
auto& window = *static_cast<Window*>(glfwGetWindowUserPointer(glfw_window)); auto& window = *static_cast<Window*>(glfwGetWindowUserPointer(glfw_window));
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
window.event_dispatcher.enqueue<ResizeEvent>(); // world->event<ResizeEvent>().emit();
// window.event_dispatcher.enqueue<ResizeEvent>();
} }
void Window::key_callback(GLFWwindow* glfw_window, void Window::key_callback(GLFWwindow* glfw_window,
@@ -71,28 +74,29 @@ void Window::key_callback(GLFWwindow* glfw_window,
{ {
auto& window = *static_cast<Window*>(glfwGetWindowUserPointer(glfw_window)); auto& window = *static_cast<Window*>(glfwGetWindowUserPointer(glfw_window));
window.event_dispatcher.enqueue<Input::KeyInput>( window.window_entity.emit<Input::KeyInput>(
Input::KeyInput{.key_code = static_cast<Input::KeyCode>(key), Input::KeyInput{.key_code = static_cast<Input::KeyCode>(key),
.action = static_cast<Input::Action>(action)}); .action = static_cast<Input::Action>(action)});
} }
void Window::mouse_cursor_callback(GLFWwindow* glfw_window, double xpos, double ypos) void Window::mouse_cursor_callback(GLFWwindow* glfw_window, double xpos, double ypos)
{ {
auto& window = *static_cast<Window*>(glfwGetWindowUserPointer(glfw_window)); // auto* world = static_cast<flecs::world*>(glfwGetWindowUserPointer(glfw_window));
glm::vec2 delta{xpos - window.last_cursor_pos.x, ypos - window.last_cursor_pos.y}; // glm::vec2 delta{xpos - window.last_cursor_pos.x, ypos - window.last_cursor_pos.y};
window.last_cursor_pos = {xpos, ypos}; // window.last_cursor_pos = {xpos, ypos};
delta *= MOUSE_SENSITIVITY; // delta *= MOUSE_SENSITIVITY;
// Check if this is the first VALID mouse event after window being resized // // Check if this is the first VALID mouse event after window being resized
if (window.first_mouse_input && (delta.x >= std::numeric_limits<double>::epsilon() || // if (window.first_mouse_input && (delta.x >= std::numeric_limits<double>::epsilon() ||
delta.y >= std::numeric_limits<double>::epsilon())) { // delta.y >= std::numeric_limits<double>::epsilon())) {
window.first_mouse_input = false; // window.first_mouse_input = false;
return; // return;
} // }
window.event_dispatcher.enqueue<Input::MouseMotion>(Input::MouseMotion{.delta = delta}); // TODO remove with flecs
// window.event_dispatcher.enqueue<Input::MouseMotion>(Input::MouseMotion{.delta = delta});
} }
auto Window::logical_dimensions() const -> glm::u32vec2 auto Window::logical_dimensions() const -> glm::u32vec2

View File

@@ -1,10 +1,9 @@
#pragma once #pragma once
#include <entt/entt.hpp> #include <entt/entt.hpp>
#include <flecs.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <memory> #include <memory>
#include <unordered_map>
#include <utility>
class GLFWwindow; class GLFWwindow;
@@ -25,7 +24,7 @@ public:
struct ResizeEvent struct ResizeEvent
{}; {};
Window(entt::dispatcher& event_dispatcher); Window(flecs::world* world, flecs::entity window_entity); // TODO: not like this...
[[nodiscard]] auto handle() -> GLFWwindow& { return *glfw_window; } [[nodiscard]] auto handle() -> GLFWwindow& { return *glfw_window; }
[[nodiscard]] auto physical_dimensions() const -> glm::u32vec2; [[nodiscard]] auto physical_dimensions() const -> glm::u32vec2;
@@ -43,9 +42,10 @@ private:
static constexpr float MOUSE_SENSITIVITY = 0.15F; static constexpr float MOUSE_SENSITIVITY = 0.15F;
std::shared_ptr<GLFWwindow> glfw_window; flecs::world* world;
flecs::entity window_entity;
entt::dispatcher& event_dispatcher; std::shared_ptr<GLFWwindow> glfw_window;
glm::vec2 last_cursor_pos{}; glm::vec2 last_cursor_pos{};
bool first_mouse_input = true; bool first_mouse_input = true;

View File

@@ -21,5 +21,5 @@
"fx-gltf", "fx-gltf",
"flecs" "flecs"
], ],
"builtin-baseline": "c14d62387153eaa2d720113542dbde2e9754ee71" "builtin-baseline": "bc994510d2eb11aac7b43b03f67a7751d5bfe0e4"
} }