start with input
This commit is contained in:
@@ -23,7 +23,7 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/lib)
|
||||
|
||||
add_library(fever_core
|
||||
src/components/transform.cpp
|
||||
src/core/application.cpp
|
||||
# src/core/application.cpp
|
||||
src/core/camera.cpp
|
||||
src/core/glad.cpp
|
||||
src/core/graphics/framebuffer.cpp
|
||||
|
||||
5
apps/flecs-test/CMakeLists.txt
Normal file
5
apps/flecs-test/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
add_executable(flecs-test
|
||||
main.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(flecs-test PRIVATE fever_core)
|
||||
38
apps/flecs-test/main.cpp
Normal file
38
apps/flecs-test/main.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <input/input.h>
|
||||
#include <window/window.h>
|
||||
#include <flecs.h>
|
||||
#include <iostream>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
glfwInit();
|
||||
|
||||
flecs::world world;
|
||||
flecs::entity window_entity = world.entity();
|
||||
window_entity.add<Input::State<Input::KeyCode>>();
|
||||
Window window(&world, window_entity);
|
||||
|
||||
window_entity.observe<Input::KeyInput>([window_entity](Input::KeyInput& key_input) {
|
||||
auto* key_state = window_entity.get_mut<Input::State<Input::KeyCode>>();
|
||||
|
||||
if (key_input.action == static_cast<Input::Action>(GLFW_PRESS)) {
|
||||
key_state->press(key_input.key_code);
|
||||
} else if (key_input.action == static_cast<Input::Action>(GLFW_RELEASE)) {
|
||||
key_state->release(key_input.key_code);
|
||||
}
|
||||
});
|
||||
|
||||
while (glfwWindowShouldClose(&window.handle()) == GLFW_FALSE) {
|
||||
glfwPollEvents();
|
||||
|
||||
// X just pressed
|
||||
auto* keycode= window_entity.get<Input::State<Input::KeyCode>>();
|
||||
if (keycode->just_pressed(Input::KeyCode{GLFW_KEY_X}))
|
||||
std::cout << "X was just pressed!\n";
|
||||
|
||||
Input::State<Input::KeyCode>::update_state(window_entity);
|
||||
glfwSwapBuffers(&window.handle());
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ namespace FeverCore {
|
||||
Application::~Application() = default;
|
||||
|
||||
Application::Application() :
|
||||
game_window(std::make_shared<Window>(event_dispatcher)),
|
||||
game_window(std::make_shared<Window>(&_world)),
|
||||
post_processing_framebuffer(game_window->physical_dimensions()),
|
||||
key_listener{.registry = entt_registry},
|
||||
cursor_listener{.registry = entt_registry},
|
||||
@@ -68,7 +68,7 @@ void Application::run()
|
||||
|
||||
this->update();
|
||||
|
||||
Input::State<Input::KeyCode>::update_state(entt_registry);
|
||||
// Input::State<Input::KeyCode>::update_state(entt_registry);
|
||||
Input::reset_mouse_motion(entt_registry);
|
||||
|
||||
// --- Render and buffer swap ---
|
||||
|
||||
@@ -2,12 +2,11 @@
|
||||
|
||||
#include "core/graphics/framebuffer.h"
|
||||
#include "core/shader.h"
|
||||
#include "entt/entity/fwd.hpp"
|
||||
#include "entt/signal/fwd.hpp"
|
||||
#include "input/input.h"
|
||||
#include "scene/gltf_loader.h"
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
#include <flecs.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
|
||||
@@ -30,8 +29,8 @@ public:
|
||||
auto registry() -> entt::registry& { return entt_registry; }
|
||||
auto registry() const -> entt::registry const& { return entt_registry; }
|
||||
|
||||
auto dispatcher() -> entt::dispatcher& { return event_dispatcher; }
|
||||
auto dispatcher() const -> entt::dispatcher const& { return event_dispatcher; }
|
||||
auto world() -> flecs::world& { return _world; }
|
||||
auto world() const -> flecs::world const& { return _world; }
|
||||
|
||||
void run();
|
||||
|
||||
@@ -47,6 +46,7 @@ protected:
|
||||
Framebuffer post_processing_framebuffer;
|
||||
|
||||
entt::registry entt_registry;
|
||||
flecs::world _world;
|
||||
|
||||
entt::dispatcher event_dispatcher{};
|
||||
Input::KeyListener key_listener;
|
||||
|
||||
@@ -4,22 +4,22 @@
|
||||
|
||||
namespace Input {
|
||||
|
||||
void KeyListener::key_event(KeyInput const& key_input_event)
|
||||
{
|
||||
auto& key_state = registry.ctx().get<State<KeyCode>>();
|
||||
// void KeyListener::key_event(KeyInput const& key_input_event)
|
||||
// {
|
||||
// auto& key_state = registry.ctx().get<State<KeyCode>>();
|
||||
|
||||
if (key_input_event.action == static_cast<Action>(GLFW_PRESS)) {
|
||||
key_state.press(key_input_event.key_code);
|
||||
} else if (key_input_event.action == static_cast<Action>(GLFW_RELEASE)) {
|
||||
key_state.release(key_input_event.key_code);
|
||||
}
|
||||
}
|
||||
// if (key_input_event.action == static_cast<Action>(GLFW_PRESS)) {
|
||||
// key_state.press(key_input_event.key_code);
|
||||
// } else if (key_input_event.action == static_cast<Action>(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<MouseMotion>();
|
||||
mouse_motion.delta += mouse_motion_event.delta;
|
||||
}
|
||||
// void CursorListener::cursor_event(MouseMotion const& mouse_motion_event)
|
||||
// {
|
||||
// auto& mouse_motion = registry.ctx().get<MouseMotion>();
|
||||
// mouse_motion.delta += mouse_motion_event.delta;
|
||||
// }
|
||||
|
||||
void reset_mouse_motion(entt::registry& registry)
|
||||
{
|
||||
@@ -27,11 +27,11 @@ void reset_mouse_motion(entt::registry& registry)
|
||||
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>>();
|
||||
state.just_pressed_keys.clear();
|
||||
state.just_released_keys.clear();
|
||||
auto* state = window_entity.get_mut<State<T>>();
|
||||
state->just_pressed_keys.clear();
|
||||
state->just_released_keys.clear();
|
||||
}
|
||||
|
||||
template class State<KeyCode>;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "entt/entity/fwd.hpp"
|
||||
#include <entt/entt.hpp>
|
||||
#include <flecs.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
@@ -34,9 +35,9 @@ public:
|
||||
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(entt::registry& registry);
|
||||
static void update_state(flecs::entity window_entity);
|
||||
|
||||
private:
|
||||
// private:
|
||||
void press(T input)
|
||||
{
|
||||
if (pressed_keys.insert(input).second) {
|
||||
@@ -59,17 +60,17 @@ private:
|
||||
friend class CursorListener;
|
||||
};
|
||||
|
||||
struct KeyListener
|
||||
{
|
||||
entt::registry& registry;
|
||||
void key_event(KeyInput const& key_input_event);
|
||||
};
|
||||
// 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);
|
||||
};
|
||||
// struct CursorListener
|
||||
// {
|
||||
// entt::registry& registry;
|
||||
// void cursor_event(MouseMotion const& mouse_motion_event);
|
||||
// };
|
||||
|
||||
void reset_mouse_motion(entt::registry& registry);
|
||||
|
||||
|
||||
@@ -5,12 +5,14 @@
|
||||
#include <glad/gl.h>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
static constexpr unsigned INIT_WINDOW_WIDTH = 1280;
|
||||
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_MINOR, 3);
|
||||
@@ -60,7 +62,8 @@ void Window::framebuffer_size_callback(GLFWwindow* glfw_window, int width, int h
|
||||
auto& window = *static_cast<Window*>(glfwGetWindowUserPointer(glfw_window));
|
||||
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,
|
||||
@@ -71,28 +74,29 @@ void Window::key_callback(GLFWwindow* 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),
|
||||
.action = static_cast<Input::Action>(action)});
|
||||
}
|
||||
|
||||
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};
|
||||
delta *= MOUSE_SENSITIVITY;
|
||||
// window.last_cursor_pos = {xpos, ypos};
|
||||
// delta *= MOUSE_SENSITIVITY;
|
||||
|
||||
// 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() ||
|
||||
delta.y >= std::numeric_limits<double>::epsilon())) {
|
||||
window.first_mouse_input = false;
|
||||
return;
|
||||
}
|
||||
// // 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() ||
|
||||
// delta.y >= std::numeric_limits<double>::epsilon())) {
|
||||
// window.first_mouse_input = false;
|
||||
// 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
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
#include <flecs.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
class GLFWwindow;
|
||||
|
||||
@@ -25,7 +24,7 @@ public:
|
||||
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 physical_dimensions() const -> glm::u32vec2;
|
||||
@@ -43,9 +42,10 @@ private:
|
||||
|
||||
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{};
|
||||
bool first_mouse_input = true;
|
||||
|
||||
@@ -21,5 +21,5 @@
|
||||
"fx-gltf",
|
||||
"flecs"
|
||||
],
|
||||
"builtin-baseline": "c14d62387153eaa2d720113542dbde2e9754ee71"
|
||||
"builtin-baseline": "bc994510d2eb11aac7b43b03f67a7751d5bfe0e4"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user