From e590203234c73b8d8300cd7445a03b18181a07f0 Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Mon, 14 Apr 2025 23:15:00 +0200 Subject: [PATCH] Start ECSsing the window --- apps/fall-fever/controller.cpp | 2 +- apps/flecs-test/main.cpp | 22 +++++------ src/core/application.h | 4 +- src/core/camera.cpp | 2 +- src/window/window.cpp | 69 ++++++++++++++++++++-------------- src/window/window.h | 21 +++++++---- 6 files changed, 69 insertions(+), 51 deletions(-) diff --git a/apps/fall-fever/controller.cpp b/apps/fall-fever/controller.cpp index fb11bb8..02674c8 100644 --- a/apps/fall-fever/controller.cpp +++ b/apps/fall-fever/controller.cpp @@ -58,7 +58,7 @@ void Controller::update() { Flycam::keyboard_movement(registry()); - if (registry().ctx().get().catched) { + if (registry().ctx().get().catched) { Flycam::mouse_orientation(registry()); } } diff --git a/apps/flecs-test/main.cpp b/apps/flecs-test/main.cpp index b14f4eb..9588a1a 100644 --- a/apps/flecs-test/main.cpp +++ b/apps/flecs-test/main.cpp @@ -7,15 +7,15 @@ int main() { - glfwInit(); - flecs::world world; - flecs::entity window_entity = world.entity(); - window_entity.add>(); - Window window(&world, window_entity); + + world.import(); + spawn_window(world); + auto window = world.singleton(); + auto glfw_window = window.get>(); - window_entity.observe([window_entity](Input::KeyInput& key_input) { - auto* key_state = window_entity.get_mut>(); + 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); @@ -24,15 +24,15 @@ int main() } }); - while (glfwWindowShouldClose(&window.handle()) == GLFW_FALSE) { + while (glfwWindowShouldClose(glfw_window->get()) == GLFW_FALSE) { glfwPollEvents(); // X just pressed - auto* keycode= window_entity.get>(); + auto* keycode= window.get>(); if (keycode->just_pressed(Input::KeyCode{GLFW_KEY_X})) std::cout << "X was just pressed!\n"; - Input::State::update_state(window_entity); - glfwSwapBuffers(&window.handle()); + Input::State::update_state(window); + glfwSwapBuffers(glfw_window->get()); } } \ No newline at end of file diff --git a/src/core/application.h b/src/core/application.h index 2a051ea..b401517 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -11,7 +11,7 @@ #include class Camera; -class Window; +class WindowDeprecated; namespace FeverCore { @@ -40,7 +40,7 @@ protected: virtual void register_context_variables(); void recreate_framebuffer(); - std::shared_ptr game_window; + std::shared_ptr game_window; Shader post_processing_shader{"post_processing", "data/shaders"}; Framebuffer post_processing_framebuffer; diff --git a/src/core/camera.cpp b/src/core/camera.cpp index e155490..83227d1 100644 --- a/src/core/camera.cpp +++ b/src/core/camera.cpp @@ -32,7 +32,7 @@ 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(); diff --git a/src/window/window.cpp b/src/window/window.cpp index 84b6b81..e7843d0 100644 --- a/src/window/window.cpp +++ b/src/window/window.cpp @@ -11,8 +11,19 @@ static constexpr unsigned INIT_WINDOW_WIDTH = 1280; static constexpr unsigned INIT_WINDOW_HEIGHT = 720; -Window::Window(flecs::world* world, flecs::entity window_entity) : - world(world), window_entity(window_entity) +static void glfw_error_callback(int error, char const* description); +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(); + world.component>(); +} + +void spawn_window(flecs::world& world) { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); @@ -26,7 +37,7 @@ Window::Window(flecs::world* world, flecs::entity window_entity) : glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE); #endif - glfw_window = std::shared_ptr( + auto glfw_window = std::shared_ptr( glfwCreateWindow(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT, "OpenGL", nullptr, nullptr), [](GLFWwindow* window) { glfwDestroyWindow(window); }); @@ -38,48 +49,50 @@ Window::Window(flecs::world* world, flecs::entity window_entity) : glfwMakeContextCurrent(glfw_window.get()); // Callbacks - glfwSetWindowUserPointer(glfw_window.get(), this); + glfwSetWindowUserPointer(glfw_window.get(), &world); glfwSetKeyCallback(glfw_window.get(), key_callback); glfwSetCursorPosCallback(glfw_window.get(), mouse_cursor_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 width, height; + glfwGetFramebufferSize(glfw_window.get(), &width, &height); + glViewport(0, 0, width, height); + + flecs::entity window = world.singleton(); + window.add>(); + window.set>(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); } -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(glfwGetWindowUserPointer(glfw_window)); - glViewport(0, 0, width, height); + // auto& window = *static_cast(glfwGetWindowUserPointer(glfw_window)); + // glViewport(0, 0, width, height); // world->event().emit(); // window.event_dispatcher.enqueue(); } -void Window::key_callback(GLFWwindow* glfw_window, - int key, - [[maybe_unused]] int scancode, - int action, - [[maybe_unused]] int mods) +void key_callback(GLFWwindow* glfw_window, + int key, + [[maybe_unused]] int scancode, + int action, + [[maybe_unused]] int mods) { - auto& window = *static_cast(glfwGetWindowUserPointer(glfw_window)); + auto* world = static_cast(glfwGetWindowUserPointer(glfw_window)); + flecs::entity window = world->component(); - window.window_entity.emit( - Input::KeyInput{.key_code = static_cast(key), - .action = static_cast(action)}); + window.emit(Input::KeyInput{.key_code = static_cast(key), + .action = static_cast(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(glfwGetWindowUserPointer(glfw_window)); @@ -99,7 +112,7 @@ void Window::mouse_cursor_callback(GLFWwindow* glfw_window, double xpos, double // window.event_dispatcher.enqueue(Input::MouseMotion{.delta = delta}); } -auto Window::logical_dimensions() const -> glm::u32vec2 +auto WindowDeprecated::logical_dimensions() const -> glm::u32vec2 { int width{}; int height{}; @@ -107,7 +120,7 @@ auto Window::logical_dimensions() const -> glm::u32vec2 return {width, height}; } -auto Window::physical_dimensions() const -> glm::u32vec2 +auto WindowDeprecated::physical_dimensions() const -> glm::u32vec2 { int width{}; int height{}; @@ -115,7 +128,7 @@ auto Window::physical_dimensions() const -> glm::u32vec2 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{.catched = false}); auto const& key_state = registry.ctx().get>(); @@ -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>(); @@ -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(); diff --git a/src/window/window.h b/src/window/window.h index 2506f2c..17518a3 100644 --- a/src/window/window.h +++ b/src/window/window.h @@ -3,11 +3,23 @@ #include #include #include +#include #include class GLFWwindow; -class Window +void glfw_init(); + +struct Window{}; + +struct WindowModule +{ + WindowModule(flecs::world& world); +}; + +void spawn_window(flecs::world& world); + +class WindowDeprecated { public: struct MouseCatched @@ -24,8 +36,6 @@ public: struct ResizeEvent {}; - 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; [[nodiscard]] auto logical_dimensions() const -> glm::u32vec2; @@ -35,11 +45,6 @@ public: void close_on_esc(entt::registry& registry) const; 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; flecs::world* world;