From 284ef9be53dda6485b46309fe3f666a570a933fa Mon Sep 17 00:00:00 2001 From: light7734 Date: Fri, 11 Jul 2025 00:49:04 +0330 Subject: [PATCH] refactor: application --- .../include/engine/core/application.hpp | 18 ++- .../include/engine/layer/layer_stack.hpp | 1 - modules/engine/src/core/application.cpp | 131 ++++++++---------- 3 files changed, 72 insertions(+), 78 deletions(-) diff --git a/modules/engine/include/engine/core/application.hpp b/modules/engine/include/engine/core/application.hpp index 29d6a98..2426053 100644 --- a/modules/engine/include/engine/core/application.hpp +++ b/modules/engine/include/engine/core/application.hpp @@ -1,15 +1,15 @@ #pragma once - #include +#include #include +#include namespace lt { class Renderer; class Window; class Event; -class UserInterface; class GraphicsContext; extern Scope create_application(); @@ -25,7 +25,7 @@ public: auto operator=(Application &&) -> Application & = delete; - virtual ~Application(); + virtual ~Application() = default; [[nodiscard]] auto sanity_check() const -> bool; @@ -42,9 +42,19 @@ protected: Application(); private: + void update_layers(); + + void render_layers(); + + void render_user_interface(); + + void poll_events(); + void on_event(const Event &event); - void log_debug_data(); + void log_debug_data() const; + + Timer m_timer; Scope m_window; diff --git a/modules/engine/include/engine/layer/layer_stack.hpp b/modules/engine/include/engine/layer/layer_stack.hpp index eff2203..37b2f7f 100644 --- a/modules/engine/include/engine/layer/layer_stack.hpp +++ b/modules/engine/include/engine/layer/layer_stack.hpp @@ -1,7 +1,6 @@ #pragma once - namespace lt { class Layer; diff --git a/modules/engine/src/core/application.cpp b/modules/engine/src/core/application.cpp index b9341fa..452c579 100644 --- a/modules/engine/src/core/application.cpp +++ b/modules/engine/src/core/application.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -24,10 +23,6 @@ Application::Application(): m_window(nullptr) lt_assert(!s_instance, "Application constructed twice"); s_instance = this; - log_debug_data(); - - lt::Instrumentor::begin_session("data/logs/profile_startup.json"); - m_window = Window::create([this](auto &&PH1) { on_event(std::forward(PH1)); }); // create graphics context @@ -73,79 +68,17 @@ Application::Application(): m_window(nullptr) ); } -Application::~Application() -{ - log_trc("Application::~Application()"); - Instrumentor::end_session(); -} - void Application::game_loop() { - // check - lt_assert(!LayerStack::instance().is_empty(), "layer_stack is empty"); - - // log debug data - m_graphics_context->log_debug_data(); - m_user_interface->log_debug_data(); - - // reveal window m_window->set_visibility(true); - Instrumentor::end_session(); - Instrumentor::begin_session("data/logs/profile_game_loop.json"); - - /* game loop */ - auto timer = Timer {}; while (!m_window->is_closed()) { - { - // update layers - lt_profile_scope("game_loop::update"); - - for (auto &it : LayerStack::instance()) - { - it->on_update(timer.get_elapsed_time()); - } - - // TODO: each layer should have their own "delta time" - timer.reset(); - } - - { - // render layers - lt_profile_scope("game_loop::Render"); - m_renderer->begin_frame(); - - for (auto &it : LayerStack::instance()) - { - it->on_render(); - } - - m_renderer->end_frame(); - } - - { - // render user interface - lt_profile_scope("game_loop::UserInterface"); - m_user_interface->begin(); - - for (auto &it : LayerStack::instance()) - { - it->on_user_interface_update(); - } - - m_user_interface->end(); - } - - { - // poll events - lt_profile_scope("game_loop::Events"); - m_window->poll_events(); - } + update_layers(); + render_layers(); + render_user_interface(); + poll_events(); } - - Instrumentor::end_session(); - Instrumentor::begin_session("data/logs/profile_cleanup.json"); } void Application::quit() @@ -153,6 +86,46 @@ void Application::quit() s_instance->m_window->close(); } +void Application::update_layers() +{ + for (auto &it : LayerStack::instance()) + { + it->on_update(m_timer.get_elapsed_time()); + } + + // TODO(Light): each layer should have their own "delta time" + m_timer.reset(); +} + +void Application::render_layers() +{ + m_renderer->begin_frame(); + + for (auto &it : LayerStack::instance()) + { + it->on_render(); + } + + m_renderer->end_frame(); +} + +void Application::render_user_interface() +{ + m_user_interface->begin(); + + for (auto &it : LayerStack::instance()) + { + 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 @@ -188,11 +161,23 @@ void Application::on_event(const Event &event) [[nodiscard]] auto Application::sanity_check() const -> bool { - // TODO(Light): verify sanity of the application state + log_inf("Checking application sanity..."); + lt_assert(s_instance, "Application not constructed!?"); + lt_assert(m_window, "Window is not initialized"); + lt_assert(m_user_interface, "User interface is not initialized"); + lt_assert(m_graphics_context, "Graphics context is not initialized"); + lt_assert(m_renderer, "Renderer is not initialized"); + lt_assert(!LayerStack::instance().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; } -void Application::log_debug_data() +void Application::log_debug_data() const { log_inf("Platform::"); log_inf(" Platform name: {}", constants::platform_name);