refactor: application
This commit is contained in:
		
							parent
							
								
									7f7eb8439c
								
							
						
					
					
						commit
						284ef9be53
					
				
					 3 changed files with 72 additions and 78 deletions
				
			
		| 
						 | 
					@ -1,15 +1,15 @@
 | 
				
			||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
#include <engine/layer/layer_stack.hpp>
 | 
					#include <engine/layer/layer_stack.hpp>
 | 
				
			||||||
 | 
					#include <engine/time/timer.hpp>
 | 
				
			||||||
#include <input/input.hpp>
 | 
					#include <input/input.hpp>
 | 
				
			||||||
 | 
					#include <ui/ui.hpp>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace lt {
 | 
					namespace lt {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Renderer;
 | 
					class Renderer;
 | 
				
			||||||
class Window;
 | 
					class Window;
 | 
				
			||||||
class Event;
 | 
					class Event;
 | 
				
			||||||
class UserInterface;
 | 
					 | 
				
			||||||
class GraphicsContext;
 | 
					class GraphicsContext;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern Scope<class Application> create_application();
 | 
					extern Scope<class Application> create_application();
 | 
				
			||||||
| 
						 | 
					@ -25,7 +25,7 @@ public:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	auto operator=(Application &&) -> Application & = delete;
 | 
						auto operator=(Application &&) -> Application & = delete;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	virtual ~Application();
 | 
						virtual ~Application() = default;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	[[nodiscard]] auto sanity_check() const -> bool;
 | 
						[[nodiscard]] auto sanity_check() const -> bool;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -42,9 +42,19 @@ protected:
 | 
				
			||||||
	Application();
 | 
						Application();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
 | 
						void update_layers();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						void render_layers();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						void render_user_interface();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						void poll_events();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void on_event(const Event &event);
 | 
						void on_event(const Event &event);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void log_debug_data();
 | 
						void log_debug_data() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Timer m_timer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Scope<Window> m_window;
 | 
						Scope<Window> m_window;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,7 +1,6 @@
 | 
				
			||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
namespace lt {
 | 
					namespace lt {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Layer;
 | 
					class Layer;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,7 +2,6 @@
 | 
				
			||||||
#include <debug/assertions.hpp>
 | 
					#include <debug/assertions.hpp>
 | 
				
			||||||
#include <engine/core/application.hpp>
 | 
					#include <engine/core/application.hpp>
 | 
				
			||||||
#include <engine/core/window.hpp>
 | 
					#include <engine/core/window.hpp>
 | 
				
			||||||
#include <engine/debug/instrumentor.hpp>
 | 
					 | 
				
			||||||
#include <engine/layer/layer.hpp>
 | 
					#include <engine/layer/layer.hpp>
 | 
				
			||||||
#include <engine/time/timer.hpp>
 | 
					#include <engine/time/timer.hpp>
 | 
				
			||||||
#include <input/events/event.hpp>
 | 
					#include <input/events/event.hpp>
 | 
				
			||||||
| 
						 | 
					@ -24,10 +23,6 @@ Application::Application(): m_window(nullptr)
 | 
				
			||||||
	lt_assert(!s_instance, "Application constructed twice");
 | 
						lt_assert(!s_instance, "Application constructed twice");
 | 
				
			||||||
	s_instance = this;
 | 
						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<decltype(PH1)>(PH1)); });
 | 
						m_window = Window::create([this](auto &&PH1) { on_event(std::forward<decltype(PH1)>(PH1)); });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// create graphics context
 | 
						// create graphics context
 | 
				
			||||||
| 
						 | 
					@ -73,47 +68,37 @@ Application::Application(): m_window(nullptr)
 | 
				
			||||||
	);
 | 
						);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Application::~Application()
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	log_trc("Application::~Application()");
 | 
					 | 
				
			||||||
	Instrumentor::end_session();
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void Application::game_loop()
 | 
					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);
 | 
						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())
 | 
						while (!m_window->is_closed())
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		{
 | 
							update_layers();
 | 
				
			||||||
			// update layers
 | 
							render_layers();
 | 
				
			||||||
			lt_profile_scope("game_loop::update");
 | 
							render_user_interface();
 | 
				
			||||||
 | 
							poll_events();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Application::quit()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						s_instance->m_window->close();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Application::update_layers()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
	for (auto &it : LayerStack::instance())
 | 
						for (auto &it : LayerStack::instance())
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
				it->on_update(timer.get_elapsed_time());
 | 
							it->on_update(m_timer.get_elapsed_time());
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// TODO: each layer should have their own "delta time"
 | 
						// TODO(Light): each layer should have their own "delta time"
 | 
				
			||||||
			timer.reset();
 | 
						m_timer.reset();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Application::render_layers()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
			// render layers
 | 
					 | 
				
			||||||
			lt_profile_scope("game_loop::Render");
 | 
					 | 
				
			||||||
	m_renderer->begin_frame();
 | 
						m_renderer->begin_frame();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for (auto &it : LayerStack::instance())
 | 
						for (auto &it : LayerStack::instance())
 | 
				
			||||||
| 
						 | 
					@ -124,9 +109,8 @@ void Application::game_loop()
 | 
				
			||||||
	m_renderer->end_frame();
 | 
						m_renderer->end_frame();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Application::render_user_interface()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
			// render user interface
 | 
					 | 
				
			||||||
			lt_profile_scope("game_loop::UserInterface");
 | 
					 | 
				
			||||||
	m_user_interface->begin();
 | 
						m_user_interface->begin();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for (auto &it : LayerStack::instance())
 | 
						for (auto &it : LayerStack::instance())
 | 
				
			||||||
| 
						 | 
					@ -137,21 +121,10 @@ void Application::game_loop()
 | 
				
			||||||
	m_user_interface->end();
 | 
						m_user_interface->end();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Application::poll_events()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
			// poll events
 | 
					 | 
				
			||||||
			lt_profile_scope("game_loop::Events");
 | 
					 | 
				
			||||||
	m_window->poll_events();
 | 
						m_window->poll_events();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	Instrumentor::end_session();
 | 
					 | 
				
			||||||
	Instrumentor::begin_session("data/logs/profile_cleanup.json");
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void Application::quit()
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	s_instance->m_window->close();
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Application::on_event(const Event &event)
 | 
					void Application::on_event(const Event &event)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -188,11 +161,23 @@ void Application::on_event(const Event &event)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[[nodiscard]] auto Application::sanity_check() const -> bool
 | 
					[[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;
 | 
						return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Application::log_debug_data()
 | 
					void Application::log_debug_data() const
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	log_inf("Platform::");
 | 
						log_inf("Platform::");
 | 
				
			||||||
	log_inf("       Platform name: {}", constants::platform_name);
 | 
						log_inf("       Platform name: {}", constants::platform_name);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue