diff --git a/src/app/service_based_app.cpp b/src/app/service_based_app.cpp index 40910c9..116b115 100644 --- a/src/app/service_based_app.cpp +++ b/src/app/service_based_app.cpp @@ -117,8 +117,7 @@ void ServiceBasedApp::RegisterServices() { logging::TraceGuard trace("ServiceBasedApp::RegisterServices"); // Event bus (needed by window service) - auto eventBus = std::make_shared(); - registry_.RegisterService(eventBus); + registry_.RegisterService(); // Configuration service services::impl::RuntimeConfig runtimeConfig; @@ -126,10 +125,12 @@ void ServiceBasedApp::RegisterServices() { registry_.RegisterService(runtimeConfig); // Window service - registry_.RegisterService(eventBus); + registry_.RegisterService( + registry_.GetService()); // Input service - registry_.RegisterService(eventBus); + registry_.RegisterService( + registry_.GetService()); // Vulkan device service registry_.RegisterService(); @@ -151,13 +152,14 @@ void ServiceBasedApp::RegisterServices() { registry_.GetService(), registry_.GetService()); - // Graphics service (facade) - temporarily disabled - // registry_.RegisterService( - // registry_.GetService(), - // registry_.GetService(), - // registry_.GetService(), - // registry_.GetService(), - // registry_.GetService()); + // Graphics service (facade) + registry_.RegisterService( + registry_.GetService(), + registry_.GetService(), + registry_.GetService(), + registry_.GetService(), + registry_.GetService(), + registry_.GetService()); // Script service registry_.RegisterService(scriptPath_); diff --git a/src/events/event_bus.hpp b/src/events/event_bus.hpp index 30965c9..17307db 100644 --- a/src/events/event_bus.hpp +++ b/src/events/event_bus.hpp @@ -1,5 +1,6 @@ #pragma once +#include "../di/lifecycle.hpp" #include "event_listener.hpp" #include "event_types.hpp" #include @@ -39,7 +40,7 @@ namespace sdl3cpp::events { * eventBus.ProcessQueue(); // Call once per frame * @endcode */ -class EventBus { +class EventBus : public di::IInitializable, public di::IShutdownable { public: EventBus() = default; ~EventBus() = default; @@ -129,6 +130,12 @@ public: */ size_t GetGlobalListenerCount() const; + // IInitializable interface + void Initialize() override {} + + // IShutdownable interface + void Shutdown() noexcept override {} + private: // Event type -> list of listeners std::unordered_map> listeners_; diff --git a/src/script/shader_manager.cpp b/src/script/shader_manager.cpp index 85a79e3..1c2af0f 100644 --- a/src/script/shader_manager.cpp +++ b/src/script/shader_manager.cpp @@ -29,7 +29,7 @@ std::unordered_map ShaderManager::L throw std::runtime_error("'get_shader_paths' did not return a table"); } - std::unordered_map shaderMap; + std::unordered_map shaderMap; lua_pushnil(L_); while (lua_next(L_, -2) != 0) { if (lua_isstring(L_, -2) && lua_istable(L_, -1)) { @@ -47,7 +47,7 @@ std::unordered_map ShaderManager::L } sdl3cpp::services::ShaderPaths ShaderManager::ReadShaderPathsTable(int index) { - ShaderPaths paths; + sdl3cpp::services::ShaderPaths paths; int absIndex = lua_absindex(L_, index); lua_getfield(L_, absIndex, "vertex"); diff --git a/src/services/impl/graphics_service.cpp b/src/services/impl/graphics_service.cpp index 5a811cc..1188114 100644 --- a/src/services/impl/graphics_service.cpp +++ b/src/services/impl/graphics_service.cpp @@ -8,15 +8,17 @@ GraphicsService::GraphicsService(std::shared_ptr deviceSer std::shared_ptr swapchainService, std::shared_ptr pipelineService, std::shared_ptr bufferService, - std::shared_ptr renderCommandService) + std::shared_ptr renderCommandService, + std::shared_ptr windowService) : deviceService_(deviceService), swapchainService_(swapchainService), pipelineService_(pipelineService), bufferService_(bufferService), - renderCommandService_(renderCommandService) { + renderCommandService_(renderCommandService), + windowService_(windowService) { logging::TraceGuard trace("GraphicsService::GraphicsService"); - if (!deviceService_ || !swapchainService_ || !pipelineService_ || !bufferService_ || !renderCommandService_) { + if (!deviceService_ || !swapchainService_ || !pipelineService_ || !bufferService_ || !renderCommandService_ || !windowService_) { throw std::invalid_argument("All graphics services must be provided"); } } @@ -66,8 +68,9 @@ void GraphicsService::InitializeSwapchain() { throw std::runtime_error("Graphics service not initialized"); } - // Swapchain service handles swapchain initialization - swapchainService_->Initialize(); + // Get window size and create swapchain + auto [width, height] = windowService_->GetSize(); + swapchainService_->CreateSwapchain(width, height); } void GraphicsService::RecreateSwapchain() { @@ -77,7 +80,9 @@ void GraphicsService::RecreateSwapchain() { throw std::runtime_error("Graphics service not initialized"); } - swapchainService_->RecreateSwapchain(); + // Get current window size and recreate swapchain + auto [width, height] = windowService_->GetSize(); + swapchainService_->RecreateSwapchain(width, height); } void GraphicsService::LoadShaders(const std::unordered_map& shaders) { @@ -91,7 +96,7 @@ void GraphicsService::LoadShaders(const std::unordered_mapRegisterShader(key, paths); } - pipelineService_->CompileAll(swapchainService_->GetRenderPass(), swapchainService_->GetExtent()); + pipelineService_->CompileAll(swapchainService_->GetRenderPass(), swapchainService_->GetSwapchainExtent()); } void GraphicsService::UploadVertexData(const std::vector& vertices) { @@ -121,7 +126,7 @@ bool GraphicsService::BeginFrame() { return false; } - return renderCommandService_->BeginFrame(); + return renderCommandService_->BeginFrame(currentImageIndex_); } void GraphicsService::RenderScene(const std::vector& commands, @@ -132,7 +137,7 @@ void GraphicsService::RenderScene(const std::vector& commands, return; } - renderCommandService_->RecordCommands(commands, viewProj); + renderCommandService_->RecordCommands(currentImageIndex_, commands, viewProj); } bool GraphicsService::EndFrame() { @@ -142,7 +147,7 @@ bool GraphicsService::EndFrame() { return false; } - return renderCommandService_->EndFrame(); + return renderCommandService_->EndFrame(currentImageIndex_); } void GraphicsService::WaitIdle() { @@ -182,17 +187,7 @@ VkExtent2D GraphicsService::GetSwapchainExtent() const { return {0, 0}; } - return swapchainService_->GetExtent(); -} - -uint32_t GraphicsService::GetSwapchainImageCount() const { - logging::TraceGuard trace("GraphicsService::GetSwapchainImageCount"); - - if (!initialized_) { - return 0; - } - - return swapchainService_->GetImageCount(); + return swapchainService_->GetSwapchainExtent(); } VkFormat GraphicsService::GetSwapchainFormat() const { @@ -202,7 +197,7 @@ VkFormat GraphicsService::GetSwapchainFormat() const { return VK_FORMAT_UNDEFINED; } - return swapchainService_->GetFormat(); + return swapchainService_->GetSwapchainImageFormat(); } VkCommandBuffer GraphicsService::GetCurrentCommandBuffer() const { diff --git a/src/services/impl/graphics_service.hpp b/src/services/impl/graphics_service.hpp index 55110ba..d045d07 100644 --- a/src/services/impl/graphics_service.hpp +++ b/src/services/impl/graphics_service.hpp @@ -6,6 +6,7 @@ #include "../interfaces/i_pipeline_service.hpp" #include "../interfaces/i_buffer_service.hpp" #include "../interfaces/i_render_command_service.hpp" +#include "../interfaces/i_window_service.hpp" #include "../../di/lifecycle.hpp" #include @@ -25,7 +26,8 @@ public: std::shared_ptr swapchainService, std::shared_ptr pipelineService, std::shared_ptr bufferService, - std::shared_ptr renderCommandService); + std::shared_ptr renderCommandService, + std::shared_ptr windowService); ~GraphicsService() override; // IInitializable interface @@ -49,7 +51,6 @@ public: VkDevice GetDevice() const override; VkPhysicalDevice GetPhysicalDevice() const override; VkExtent2D GetSwapchainExtent() const override; - uint32_t GetSwapchainImageCount() const override; VkFormat GetSwapchainFormat() const override; VkCommandBuffer GetCurrentCommandBuffer() const override; VkQueue GetGraphicsQueue() const override; @@ -60,7 +61,9 @@ private: std::shared_ptr pipelineService_; std::shared_ptr bufferService_; std::shared_ptr renderCommandService_; + std::shared_ptr windowService_; bool initialized_ = false; + uint32_t currentImageIndex_ = 0; }; } // namespace sdl3cpp::services::impl \ No newline at end of file