mirror of
https://github.com/johndoe6345789/SDL3CPlusPlus.git
synced 2026-04-24 13:44:58 +00:00
feat: Enhance GraphicsService to include window service and update swapchain handling
This commit is contained in:
@@ -117,8 +117,7 @@ void ServiceBasedApp::RegisterServices() {
|
||||
logging::TraceGuard trace("ServiceBasedApp::RegisterServices");
|
||||
|
||||
// Event bus (needed by window service)
|
||||
auto eventBus = std::make_shared<events::EventBus>();
|
||||
registry_.RegisterService<events::EventBus>(eventBus);
|
||||
registry_.RegisterService<events::EventBus, events::EventBus>();
|
||||
|
||||
// Configuration service
|
||||
services::impl::RuntimeConfig runtimeConfig;
|
||||
@@ -126,10 +125,12 @@ void ServiceBasedApp::RegisterServices() {
|
||||
registry_.RegisterService<services::IConfigService, services::impl::JsonConfigService>(runtimeConfig);
|
||||
|
||||
// Window service
|
||||
registry_.RegisterService<services::IWindowService, services::impl::SdlWindowService>(eventBus);
|
||||
registry_.RegisterService<services::IWindowService, services::impl::SdlWindowService>(
|
||||
registry_.GetService<events::EventBus>());
|
||||
|
||||
// Input service
|
||||
registry_.RegisterService<services::IInputService, services::impl::SdlInputService>(eventBus);
|
||||
registry_.RegisterService<services::IInputService, services::impl::SdlInputService>(
|
||||
registry_.GetService<events::EventBus>());
|
||||
|
||||
// Vulkan device service
|
||||
registry_.RegisterService<services::IVulkanDeviceService, services::impl::VulkanDeviceService>();
|
||||
@@ -151,13 +152,14 @@ void ServiceBasedApp::RegisterServices() {
|
||||
registry_.GetService<services::IVulkanDeviceService>(),
|
||||
registry_.GetService<services::ISwapchainService>());
|
||||
|
||||
// Graphics service (facade) - temporarily disabled
|
||||
// registry_.RegisterService<services::IGraphicsService, services::impl::GraphicsService>(
|
||||
// registry_.GetService<services::IVulkanDeviceService>(),
|
||||
// registry_.GetService<services::ISwapchainService>(),
|
||||
// registry_.GetService<services::IPipelineService>(),
|
||||
// registry_.GetService<services::IBufferService>(),
|
||||
// registry_.GetService<services::IRenderCommandService>());
|
||||
// Graphics service (facade)
|
||||
registry_.RegisterService<services::IGraphicsService, services::impl::GraphicsService>(
|
||||
registry_.GetService<services::IVulkanDeviceService>(),
|
||||
registry_.GetService<services::ISwapchainService>(),
|
||||
registry_.GetService<services::IPipelineService>(),
|
||||
registry_.GetService<services::IBufferService>(),
|
||||
registry_.GetService<services::IRenderCommandService>(),
|
||||
registry_.GetService<services::IWindowService>());
|
||||
|
||||
// Script service
|
||||
registry_.RegisterService<services::IScriptService, services::impl::LuaScriptService>(scriptPath_);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../di/lifecycle.hpp"
|
||||
#include "event_listener.hpp"
|
||||
#include "event_types.hpp"
|
||||
#include <mutex>
|
||||
@@ -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<EventType, std::vector<EventListener>> listeners_;
|
||||
|
||||
@@ -29,7 +29,7 @@ std::unordered_map<std::string, sdl3cpp::services::ShaderPaths> ShaderManager::L
|
||||
throw std::runtime_error("'get_shader_paths' did not return a table");
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, ShaderPaths> shaderMap;
|
||||
std::unordered_map<std::string, sdl3cpp::services::ShaderPaths> 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<std::string, sdl3cpp::services::ShaderPaths> 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");
|
||||
|
||||
@@ -8,15 +8,17 @@ GraphicsService::GraphicsService(std::shared_ptr<IVulkanDeviceService> deviceSer
|
||||
std::shared_ptr<ISwapchainService> swapchainService,
|
||||
std::shared_ptr<IPipelineService> pipelineService,
|
||||
std::shared_ptr<IBufferService> bufferService,
|
||||
std::shared_ptr<IRenderCommandService> renderCommandService)
|
||||
std::shared_ptr<IRenderCommandService> renderCommandService,
|
||||
std::shared_ptr<IWindowService> 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<std::string, ShaderPaths>& shaders) {
|
||||
@@ -91,7 +96,7 @@ void GraphicsService::LoadShaders(const std::unordered_map<std::string, ShaderPa
|
||||
for (const auto& [key, paths] : shaders) {
|
||||
pipelineService_->RegisterShader(key, paths);
|
||||
}
|
||||
pipelineService_->CompileAll(swapchainService_->GetRenderPass(), swapchainService_->GetExtent());
|
||||
pipelineService_->CompileAll(swapchainService_->GetRenderPass(), swapchainService_->GetSwapchainExtent());
|
||||
}
|
||||
|
||||
void GraphicsService::UploadVertexData(const std::vector<core::Vertex>& vertices) {
|
||||
@@ -121,7 +126,7 @@ bool GraphicsService::BeginFrame() {
|
||||
return false;
|
||||
}
|
||||
|
||||
return renderCommandService_->BeginFrame();
|
||||
return renderCommandService_->BeginFrame(currentImageIndex_);
|
||||
}
|
||||
|
||||
void GraphicsService::RenderScene(const std::vector<RenderCommand>& commands,
|
||||
@@ -132,7 +137,7 @@ void GraphicsService::RenderScene(const std::vector<RenderCommand>& 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 {
|
||||
|
||||
@@ -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 <memory>
|
||||
|
||||
@@ -25,7 +26,8 @@ public:
|
||||
std::shared_ptr<ISwapchainService> swapchainService,
|
||||
std::shared_ptr<IPipelineService> pipelineService,
|
||||
std::shared_ptr<IBufferService> bufferService,
|
||||
std::shared_ptr<IRenderCommandService> renderCommandService);
|
||||
std::shared_ptr<IRenderCommandService> renderCommandService,
|
||||
std::shared_ptr<IWindowService> 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<IPipelineService> pipelineService_;
|
||||
std::shared_ptr<IBufferService> bufferService_;
|
||||
std::shared_ptr<IRenderCommandService> renderCommandService_;
|
||||
std::shared_ptr<IWindowService> windowService_;
|
||||
bool initialized_ = false;
|
||||
uint32_t currentImageIndex_ = 0;
|
||||
};
|
||||
|
||||
} // namespace sdl3cpp::services::impl
|
||||
Reference in New Issue
Block a user