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