Start ECSsing the window

This commit is contained in:
2025-04-14 23:15:00 +02:00
parent 82ddc88246
commit e590203234
6 changed files with 69 additions and 51 deletions

View File

@@ -58,7 +58,7 @@ void Controller::update()
{ {
Flycam::keyboard_movement(registry()); Flycam::keyboard_movement(registry());
if (registry().ctx().get<Window::MouseCatched>().catched) { if (registry().ctx().get<WindowDeprecated::MouseCatched>().catched) {
Flycam::mouse_orientation(registry()); Flycam::mouse_orientation(registry());
} }
} }

View File

@@ -7,15 +7,15 @@
int main() int main()
{ {
glfwInit();
flecs::world world; flecs::world world;
flecs::entity window_entity = world.entity();
window_entity.add<Input::State<Input::KeyCode>>(); world.import<WindowModule>();
Window window(&world, window_entity); spawn_window(world);
auto window = world.singleton<Window>();
auto glfw_window = window.get<std::shared_ptr<GLFWwindow>>();
window_entity.observe<Input::KeyInput>([window_entity](Input::KeyInput& key_input) { window.observe<Input::KeyInput>([window](Input::KeyInput& key_input) {
auto* key_state = window_entity.get_mut<Input::State<Input::KeyCode>>(); auto* key_state = window.get_mut<Input::State<Input::KeyCode>>();
if (key_input.action == static_cast<Input::Action>(GLFW_PRESS)) { if (key_input.action == static_cast<Input::Action>(GLFW_PRESS)) {
key_state->press(key_input.key_code); key_state->press(key_input.key_code);
@@ -24,15 +24,15 @@ int main()
} }
}); });
while (glfwWindowShouldClose(&window.handle()) == GLFW_FALSE) { while (glfwWindowShouldClose(glfw_window->get()) == GLFW_FALSE) {
glfwPollEvents(); glfwPollEvents();
// X just pressed // X just pressed
auto* keycode= window_entity.get<Input::State<Input::KeyCode>>(); auto* keycode= window.get<Input::State<Input::KeyCode>>();
if (keycode->just_pressed(Input::KeyCode{GLFW_KEY_X})) if (keycode->just_pressed(Input::KeyCode{GLFW_KEY_X}))
std::cout << "X was just pressed!\n"; std::cout << "X was just pressed!\n";
Input::State<Input::KeyCode>::update_state(window_entity); Input::State<Input::KeyCode>::update_state(window);
glfwSwapBuffers(&window.handle()); glfwSwapBuffers(glfw_window->get());
} }
} }

View File

@@ -11,7 +11,7 @@
#include <memory> #include <memory>
class Camera; class Camera;
class Window; class WindowDeprecated;
namespace FeverCore { namespace FeverCore {
@@ -40,7 +40,7 @@ protected:
virtual void register_context_variables(); virtual void register_context_variables();
void recreate_framebuffer(); void recreate_framebuffer();
std::shared_ptr<Window> game_window; std::shared_ptr<WindowDeprecated> game_window;
Shader post_processing_shader{"post_processing", "data/shaders"}; Shader post_processing_shader{"post_processing", "data/shaders"};
Framebuffer post_processing_framebuffer; Framebuffer post_processing_framebuffer;

View File

@@ -32,7 +32,7 @@ auto Camera::front_vector(GlobalTransform const& transform) -> glm::vec3
void Camera::aspect_ratio_update(entt::registry& registry) void Camera::aspect_ratio_update(entt::registry& registry)
{ {
float aspect_ratio = registry.ctx().get<Window::Descriptor>().aspect_ratio; float aspect_ratio = registry.ctx().get<WindowDeprecated::Descriptor>().aspect_ratio;
auto camera_view = registry.view<Camera>(); auto camera_view = registry.view<Camera>();

View File

@@ -11,8 +11,19 @@
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(flecs::world* world, flecs::entity window_entity) : static void glfw_error_callback(int error, char const* description);
world(world), window_entity(window_entity) static void framebuffer_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);
WindowModule::WindowModule(flecs::world& world)
{
glfwInit();
world.component<Window>();
world.component<std::shared_ptr<GLFWwindow>>();
}
void spawn_window(flecs::world& world)
{ {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
@@ -26,7 +37,7 @@ Window::Window(flecs::world* world, flecs::entity window_entity) :
glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE); glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
#endif #endif
glfw_window = std::shared_ptr<GLFWwindow>( auto glfw_window = std::shared_ptr<GLFWwindow>(
glfwCreateWindow(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT, "OpenGL", nullptr, nullptr), glfwCreateWindow(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT, "OpenGL", nullptr, nullptr),
[](GLFWwindow* window) { glfwDestroyWindow(window); }); [](GLFWwindow* window) { glfwDestroyWindow(window); });
@@ -38,48 +49,50 @@ Window::Window(flecs::world* world, flecs::entity window_entity) :
glfwMakeContextCurrent(glfw_window.get()); glfwMakeContextCurrent(glfw_window.get());
// Callbacks // Callbacks
glfwSetWindowUserPointer(glfw_window.get(), this); glfwSetWindowUserPointer(glfw_window.get(), &world);
glfwSetKeyCallback(glfw_window.get(), key_callback); glfwSetKeyCallback(glfw_window.get(), key_callback);
glfwSetCursorPosCallback(glfw_window.get(), mouse_cursor_callback); glfwSetCursorPosCallback(glfw_window.get(), mouse_cursor_callback);
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); glViewport(0, 0, width, height);
glViewport(0, 0, width, height);
} flecs::entity window = world.singleton<Window>();
window.add<Input::State<Input::KeyCode>>();
window.set<std::shared_ptr<GLFWwindow>>(glfw_window);
} }
void Window::glfw_error_callback(int error, char const* description) static void glfw_error_callback(int error, char const* description)
{ {
spdlog::warn("GLFW [{:d}]: {:s}\n", error, description); spdlog::warn("GLFW [{:d}]: {:s}\n", error, description);
} }
void Window::framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height) void framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height)
{ {
auto& window = *static_cast<Window*>(glfwGetWindowUserPointer(glfw_window)); // auto& window = *static_cast<WindowDeprecated*>(glfwGetWindowUserPointer(glfw_window));
glViewport(0, 0, width, height); // glViewport(0, 0, width, height);
// world->event<ResizeEvent>().emit(); // world->event<ResizeEvent>().emit();
// window.event_dispatcher.enqueue<ResizeEvent>(); // window.event_dispatcher.enqueue<ResizeEvent>();
} }
void Window::key_callback(GLFWwindow* glfw_window, void key_callback(GLFWwindow* glfw_window,
int key, int key,
[[maybe_unused]] int scancode, [[maybe_unused]] int scancode,
int action, int action,
[[maybe_unused]] int mods) [[maybe_unused]] int mods)
{ {
auto& window = *static_cast<Window*>(glfwGetWindowUserPointer(glfw_window)); auto* world = static_cast<flecs::world*>(glfwGetWindowUserPointer(glfw_window));
flecs::entity window = world->component<Window>();
window.window_entity.emit<Input::KeyInput>( window.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 mouse_cursor_callback(GLFWwindow* glfw_window, double xpos, double ypos)
{ {
// auto* world = static_cast<flecs::world*>(glfwGetWindowUserPointer(glfw_window)); // auto* world = static_cast<flecs::world*>(glfwGetWindowUserPointer(glfw_window));
@@ -99,7 +112,7 @@ void Window::mouse_cursor_callback(GLFWwindow* glfw_window, double xpos, double
// window.event_dispatcher.enqueue<Input::MouseMotion>(Input::MouseMotion{.delta = delta}); // window.event_dispatcher.enqueue<Input::MouseMotion>(Input::MouseMotion{.delta = delta});
} }
auto Window::logical_dimensions() const -> glm::u32vec2 auto WindowDeprecated::logical_dimensions() const -> glm::u32vec2
{ {
int width{}; int width{};
int height{}; int height{};
@@ -107,7 +120,7 @@ auto Window::logical_dimensions() const -> glm::u32vec2
return {width, height}; return {width, height};
} }
auto Window::physical_dimensions() const -> glm::u32vec2 auto WindowDeprecated::physical_dimensions() const -> glm::u32vec2
{ {
int width{}; int width{};
int height{}; int height{};
@@ -115,7 +128,7 @@ auto Window::physical_dimensions() const -> glm::u32vec2
return {width, height}; return {width, height};
} }
void Window::mouse_catching(entt::registry& registry) const void WindowDeprecated::mouse_catching(entt::registry& registry) const
{ {
auto& mouse_catched = registry.ctx().emplace<MouseCatched>(MouseCatched{.catched = false}); auto& mouse_catched = registry.ctx().emplace<MouseCatched>(MouseCatched{.catched = false});
auto const& key_state = registry.ctx().get<Input::State<Input::KeyCode>>(); auto const& key_state = registry.ctx().get<Input::State<Input::KeyCode>>();
@@ -129,7 +142,7 @@ void Window::mouse_catching(entt::registry& registry) const
} }
} }
void Window::close_on_esc(entt::registry& registry) const void WindowDeprecated::close_on_esc(entt::registry& registry) const
{ {
auto const& key_state = registry.ctx().get<Input::State<Input::KeyCode>>(); auto const& key_state = registry.ctx().get<Input::State<Input::KeyCode>>();
@@ -138,7 +151,7 @@ void Window::close_on_esc(entt::registry& registry) const
} }
} }
void Window::update_descriptor(entt::registry& registry) const void WindowDeprecated::update_descriptor(entt::registry& registry) const
{ {
auto dimensions = logical_dimensions(); auto dimensions = logical_dimensions();

View File

@@ -3,11 +3,23 @@
#include <entt/entt.hpp> #include <entt/entt.hpp>
#include <flecs.h> #include <flecs.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <input/input.h>
#include <memory> #include <memory>
class GLFWwindow; class GLFWwindow;
class Window void glfw_init();
struct Window{};
struct WindowModule
{
WindowModule(flecs::world& world);
};
void spawn_window(flecs::world& world);
class WindowDeprecated
{ {
public: public:
struct MouseCatched struct MouseCatched
@@ -24,8 +36,6 @@ public:
struct ResizeEvent struct ResizeEvent
{}; {};
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;
[[nodiscard]] auto logical_dimensions() const -> glm::u32vec2; [[nodiscard]] auto logical_dimensions() const -> glm::u32vec2;
@@ -35,11 +45,6 @@ public:
void close_on_esc(entt::registry& registry) const; void close_on_esc(entt::registry& registry) const;
private: private:
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);
static void framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height);
static void glfw_error_callback(int error, char const* description);
static constexpr float MOUSE_SENSITIVITY = 0.15F; static constexpr float MOUSE_SENSITIVITY = 0.15F;
flecs::world* world; flecs::world* world;