From 2b96a85b6293820f8e83f79f735277e6621e73e6 Mon Sep 17 00:00:00 2001 From: light7734 Date: Sat, 26 Jul 2025 18:01:27 +0330 Subject: [PATCH] feat: systems feat: surface system This commit puts the project in major jeopardy as it overhauls the architecture such as removing the layer stack completely, etc. I am filled with determination. --- modules/CMakeLists.txt | 19 +- modules/app/CMakeLists.txt | 23 +- modules/app/private/application.cpp | 197 ++-------------- modules/app/private/layer.cpp | 47 ---- modules/app/private/layer_stack.cpp | 18 -- modules/app/public/application.hpp | 61 ++--- modules/app/public/entrypoint.hpp | 14 +- modules/app/public/layer.hpp | 116 ---------- modules/app/public/layer_stack.hpp | 50 ---- modules/app/public/system.hpp | 25 ++ modules/asset_manager/CMakeLists.txt | 2 +- modules/ecs/CMakeLists.txt | 5 +- modules/ecs/private/scene.cpp | 64 ----- modules/ecs/public/scene.hpp | 24 +- modules/input/CMakeLists.txt | 2 +- modules/input/private/input.cpp | 5 - modules/input/private/system.cpp | 5 + modules/input/public/events/char.hpp | 41 ---- modules/input/public/events/event.hpp | 56 ----- modules/input/public/events/keyboard.hpp | 107 --------- modules/input/public/events/mouse.hpp | 151 ------------ modules/input/public/events/window.hpp | 133 ----------- modules/input/public/system.hpp | 64 +++++ modules/mirror/CMakeLists.txt | 13 +- modules/mirror/private/entrypoint/mirror.cpp | 70 ++++-- modules/renderer/CMakeLists.txt | 13 ++ modules/renderer/private/system.cpp | 18 ++ modules/renderer/private/system.test.cpp | 50 ++++ .../private/vk/device.cpp} | 0 .../private/vk/device.hpp} | 0 modules/renderer/private/vk/device.test.cpp | 0 modules/renderer/private/vk/instance.cpp | 5 + modules/renderer/private/vk/instance.hpp | 19 ++ modules/renderer/private/vk/instance.test.cpp | 8 + modules/renderer/public/components.hpp | 12 + modules/renderer/public/context.hpp | 9 + modules/renderer/public/system.hpp | 60 +++++ modules/surface/CMakeLists.txt | 13 ++ modules/surface/private/linux/system.cpp | 198 ++++++++++++++++ modules/surface/private/system.cpp | 27 +++ modules/surface/private/system.test.cpp | 0 modules/surface/public/components.hpp | 77 ++++++ modules/surface/public/events/keyboard.hpp | 91 ++++++++ modules/surface/public/events/mouse.hpp | 103 ++++++++ modules/surface/public/events/surface.hpp | 81 +++++++ modules/surface/public/system.hpp | 80 +++++++ modules/window/CMakeLists.txt | 13 -- modules/window/private/linux/window.cpp | 219 ------------------ modules/window/private/linux/window.hpp | 60 ----- modules/window/private/windows/window.cpp | 219 ------------------ modules/window/private/windows/window.hpp | 60 ----- modules/window/public/window.hpp | 98 -------- 52 files changed, 1094 insertions(+), 1751 deletions(-) delete mode 100644 modules/app/private/layer.cpp delete mode 100644 modules/app/private/layer_stack.cpp delete mode 100644 modules/app/public/layer.hpp delete mode 100644 modules/app/public/layer_stack.hpp create mode 100644 modules/app/public/system.hpp create mode 100644 modules/input/private/system.cpp delete mode 100644 modules/input/public/events/char.hpp delete mode 100644 modules/input/public/events/event.hpp delete mode 100644 modules/input/public/events/keyboard.hpp delete mode 100644 modules/input/public/events/mouse.hpp delete mode 100644 modules/input/public/events/window.hpp create mode 100644 modules/input/public/system.hpp create mode 100644 modules/renderer/private/system.cpp create mode 100644 modules/renderer/private/system.test.cpp rename modules/{window/private/mac/window.cpp => renderer/private/vk/device.cpp} (100%) rename modules/{window/private/mac/window.hpp => renderer/private/vk/device.hpp} (100%) create mode 100644 modules/renderer/private/vk/device.test.cpp create mode 100644 modules/renderer/private/vk/instance.cpp create mode 100644 modules/renderer/private/vk/instance.hpp create mode 100644 modules/renderer/private/vk/instance.test.cpp create mode 100644 modules/renderer/public/components.hpp create mode 100644 modules/renderer/public/context.hpp create mode 100644 modules/renderer/public/system.hpp create mode 100644 modules/surface/CMakeLists.txt create mode 100644 modules/surface/private/linux/system.cpp create mode 100644 modules/surface/private/system.cpp create mode 100644 modules/surface/private/system.test.cpp create mode 100644 modules/surface/public/components.hpp create mode 100644 modules/surface/public/events/keyboard.hpp create mode 100644 modules/surface/public/events/mouse.hpp create mode 100644 modules/surface/public/events/surface.hpp create mode 100644 modules/surface/public/system.hpp delete mode 100644 modules/window/CMakeLists.txt delete mode 100644 modules/window/private/linux/window.cpp delete mode 100644 modules/window/private/linux/window.hpp delete mode 100644 modules/window/private/windows/window.cpp delete mode 100644 modules/window/private/windows/window.hpp delete mode 100644 modules/window/public/window.hpp diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index 09e490d..238b03d 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -4,22 +4,21 @@ add_subdirectory(./time) add_subdirectory(./logger) add_subdirectory(./debug) add_subdirectory(./math) - +# add_subdirectory(./asset_baker) add_subdirectory(./asset_parser) -add_subdirectory(./asset_manager) - +# add_subdirectory(./asset_manager) +# add_subdirectory(./camera) -add_subdirectory(./input) -add_subdirectory(./ui) - -add_subdirectory(./window) -add_subdirectory(./renderer) +# add_subdirectory(./input) +# add_subdirectory(./ui) +# +add_subdirectory(./surface) +# add_subdirectory(./renderer) add_subdirectory(./ecs) - +# add_subdirectory(./app) # apps add_subdirectory(./mirror) - add_subdirectory(test) diff --git a/modules/app/CMakeLists.txt b/modules/app/CMakeLists.txt index d2c53c7..0a16270 100644 --- a/modules/app/CMakeLists.txt +++ b/modules/app/CMakeLists.txt @@ -1,21 +1,2 @@ -add_library_module(app - application.cpp - layer.cpp - layer_stack.cpp -) - -target_link_libraries(app -PUBLIC - renderer - logger - ui - asset_parser - asset_manager - lt_debug - ecs - window - glad - time - opengl::opengl - EnTT::EnTT -) +add_library_module(app application.cpp) +target_link_libraries(app PRIVATE lt_debug) diff --git a/modules/app/private/application.cpp b/modules/app/private/application.cpp index f526f83..c8ed190 100644 --- a/modules/app/private/application.cpp +++ b/modules/app/private/application.cpp @@ -1,203 +1,48 @@ #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include -namespace lt { - -Application *Application::s_instance = nullptr; - -Application::Application(): m_window(nullptr) -{ - ensure(!s_instance, "Application constructed twice"); - s_instance = this; - - m_window = Window::create([this](auto &&PH1) { on_event(std::forward(PH1)); }); - - // create graphics context - m_graphics_context = GraphicsContext::create( - GraphicsAPI::OpenGL, - (GLFWwindow *)m_window->get_handle() - ); - - AssetManager::load_shader( - "LT_ENGINE_RESOURCES_TEXTURE_SHADER", - "data/assets/shaders/texture/vs.asset", - "data/assets/shaders/texture/ps.asset" - ); - - AssetManager::load_shader( - "LT_ENGINE_RESOURCES_TINTED_TEXTURE_SHADER", - "data/assets/shaders/tinted_texture/vs.asset", - "data/assets/shaders/tinted_texture/ps.asset" - ); - - AssetManager::load_shader( - "LT_ENGINE_RESOURCES_QUAD_SHADER", - "data/assets/shaders/quads/vs.asset", - "data/assets/shaders/quads/ps.asset" - ); - - m_renderer = Renderer::create( - (GLFWwindow *)m_window->get_handle(), - lt::GraphicsContext::get_shared_context(), - Renderer::CreateInfo { - .quad_renderer_shader = AssetManager::get_shader("LT_ENGINE_RESOURCES_QUAD_SHADER"), - .texture_renderer_shader = AssetManager::get_shader( - "LT_ENGINE_RESOURCES_TEXTURE_SHADER" - ), - .tinted_texture_renderer_shader = AssetManager::get_shader( - "LT_ENGINE_RESOURCES_TINTED_" - "TEXTURE_SHADER" - ), - } - ); - ensure(m_graphics_context, "lWindow::lWindow: failed to create 'GraphicsContext'"); - - m_user_interface = UserInterface::create( - (GLFWwindow *)m_window->get_handle(), - lt::GraphicsContext::get_shared_context() - ); - - m_layer_stack = create_scope(); -} - -Application::~Application() -{ - /** This is required to make forward-declarations possible: - * https://stackoverflow.com/questions/34072862/why-is-error-invalid-application-of-sizeof-to-an-incomplete-type-using-uniqu - */ -} +namespace lt::app { void Application::game_loop() { - m_window->set_visibility(true); - - while (!m_window->is_closed()) + for (auto &system : m_systems) { - update_layers(); - render_layers(); - render_user_interface(); - poll_events(); - } -} - -void Application::quit() -{ - s_instance->m_window->close(); -} - -void Application::update_layers() -{ - for (auto &it : *m_layer_stack) - { - // narrowing double -> float - it->on_update(static_cast(m_timer.elapsed_time().count())); + system->init(); } - // TODO(Light): each layer should have their own "delta time" - m_timer.reset(); -} - -void Application::render_layers() -{ - m_renderer->begin_frame(); - - for (auto &it : *m_layer_stack) + while (true) { - it->on_render(); - } - - m_renderer->end_frame(); -} - -void Application::render_user_interface() -{ - m_user_interface->begin(); - - for (auto &it : *m_layer_stack) - { - it->on_user_interface_update(); - } - - m_user_interface->end(); -} - -void Application::poll_events() -{ - m_window->poll_events(); -} - -void Application::on_event(const Event &event) -{ - // window - if (event.has_category(WindowEventCategory)) - { - m_window->on_event(event); - - if (event.get_event_type() == EventType::WindowResized) + for (auto &system : m_systems) { - m_renderer->on_window_resize(dynamic_cast(event)); + if (system->tick()) + { + return; + } } - } - // input - if (event.has_category(InputEventCategory)) - { - Input::instance().on_event(event); - - if (!Input::instance().is_receiving_game_events()) + for (auto &system : m_systems_to_be_removed) { - return; + m_systems.erase( + std::remove(m_systems.begin(), m_systems.end(), system), + m_systems.end() + ); } - } - for (auto &it : std::ranges::reverse_view(*m_layer_stack)) - { - if (it->on_event(event)) + if (m_systems.empty()) { return; } } } -[[nodiscard]] auto Application::sanity_check() const -> bool +void Application::register_system(Ref system) { - log_inf("Checking application sanity..."); - ensure(s_instance, "Application not constructed!?"); - ensure(m_window, "Window is not initialized"); - ensure(m_user_interface, "User interface is not initialized"); - ensure(m_graphics_context, "Graphics context is not initialized"); - ensure(m_renderer, "Renderer is not initialized"); - ensure(m_layer_stack, "Layer_stack is not initialized"); - ensure(!m_layer_stack->is_empty(), "Layer_stack is empty"); - - log_inf("Logging application state..."); - this->log_debug_data(); - m_graphics_context->log_debug_data(); - m_user_interface->log_debug_data(); - - return true; + m_systems.emplace_back(std::move(system)); } -void Application::log_debug_data() const +void Application::unregister_system(Ref system) { - log_inf("Platform::"); - log_inf(" Platform name: {}", constants::platform_name); - log_inf(" Platform identifier: {}", std::to_underlying(constants::platform)); - log_inf(" CWD: {}", std::filesystem::current_path().generic_string()); + m_systems_to_be_removed.emplace_back(std::move(system)); } -} // namespace lt +} // namespace lt::app diff --git a/modules/app/private/layer.cpp b/modules/app/private/layer.cpp deleted file mode 100644 index 7b1998a..0000000 --- a/modules/app/private/layer.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include -#include -#include -#include -#include -#include - -namespace lt { - -Layer::Layer(std::string name): m_layer_name(std::move(name)) -{ -} - -auto Layer::on_event(const Event &event) -> bool -{ - switch (event.get_event_type()) - { - case EventType::MouseMoved: return on_mouse_moved(dynamic_cast(event)); - case EventType::ButtonPressed: - return on_button_pressed(dynamic_cast(event)); - case EventType::ButtonReleased: - return on_button_released(dynamic_cast(event)); - case EventType::WheelScrolled: - return on_wheel_scrolled(dynamic_cast(event)); - - case EventType::KeyPressed: return on_key_pressed(dynamic_cast(event)); - case EventType::KeyRepeated: return on_key_repeat(dynamic_cast(event)); - case EventType::KeyReleased: - return on_key_released(dynamic_cast(event)); - case EventType::SetChar: return on_set_char(dynamic_cast(event)); - - case EventType::WindowClosed: - return on_window_closed(dynamic_cast(event)); - case EventType::WindowResized: - return on_window_resized(dynamic_cast(event)); - case EventType::WindowMoved: - return on_window_moved(dynamic_cast(event)); - case EventType::WindowLostFocus: - return on_window_lost_focus(dynamic_cast(event)); - case EventType::WindowGainFocus: - return on_window_gain_focus(dynamic_cast(event)); - - default: ensure(false, "Invalid event: {}", event.get_info_lt_log()); - } -} - -} // namespace lt diff --git a/modules/app/private/layer_stack.cpp b/modules/app/private/layer_stack.cpp deleted file mode 100644 index c6457a0..0000000 --- a/modules/app/private/layer_stack.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include - -namespace lt { - -void LayerStack::attach_layer(Ref layer) -{ - log_trc("Attaching layer [{}]", layer->get_name()); - m_layers.emplace_back(std::move(layer)); -} - -void LayerStack::detach_layer(const Ref &layer) -{ - log_trc("Detaching layer [{}]", layer->get_name()); - m_layers.erase(std::find(m_layers.begin(), m_layers.end(), layer)); -} - -} // namespace lt diff --git a/modules/app/public/application.hpp b/modules/app/public/application.hpp index 5485cc6..bc92196 100644 --- a/modules/app/public/application.hpp +++ b/modules/app/public/application.hpp @@ -1,18 +1,15 @@ #pragma once -#include