mirror of
https://github.com/johndoe6345789/SDL3CPlusPlus.git
synced 2026-04-24 13:44:58 +00:00
182 lines
7.3 KiB
C++
182 lines
7.3 KiB
C++
#include "service_based_app.hpp"
|
|
#include "logging/logger.hpp"
|
|
#include "events/event_bus.hpp"
|
|
#include "services/interfaces/i_window_service.hpp"
|
|
#include "services/interfaces/i_graphics_service.hpp"
|
|
#include "services/impl/json_config_service.hpp"
|
|
#include "services/impl/sdl_window_service.hpp"
|
|
#include "services/impl/sdl_input_service.hpp"
|
|
#include "services/impl/vulkan_device_service.hpp"
|
|
#include "services/impl/swapchain_service.hpp"
|
|
#include "services/impl/pipeline_service.hpp"
|
|
#include "services/impl/buffer_service.hpp"
|
|
#include "services/impl/render_command_service.hpp"
|
|
#include "services/impl/graphics_service.hpp"
|
|
#include "services/impl/lua_script_service.hpp"
|
|
#include "services/impl/scene_service.hpp"
|
|
#include "services/impl/sdl_audio_service.hpp"
|
|
#include "services/impl/vulkan_gui_service.hpp"
|
|
#include "services/impl/bullet_physics_service.hpp"
|
|
#include <stdexcept>
|
|
|
|
namespace sdl3cpp::app {
|
|
|
|
ServiceBasedApp::ServiceBasedApp(const std::filesystem::path& scriptPath)
|
|
: scriptPath_(scriptPath) {
|
|
logging::Logger::GetInstance().Info("ServiceBasedApp constructor starting");
|
|
|
|
try {
|
|
logging::Logger::GetInstance().Info("Setting up SDL");
|
|
SetupSDL();
|
|
logging::Logger::GetInstance().Info("Registering services");
|
|
RegisterServices();
|
|
logging::Logger::GetInstance().Info("Creating controllers");
|
|
|
|
lifecycleController_ = std::make_unique<controllers::LifecycleController>(registry_);
|
|
applicationController_ = std::make_unique<controllers::ApplicationController>(registry_);
|
|
|
|
logging::Logger::GetInstance().Info("ServiceBasedApp constructor completed");
|
|
} catch (const std::exception& e) {
|
|
logging::Logger::GetInstance().Error("Failed to initialize ServiceBasedApp: " + std::string(e.what()));
|
|
throw;
|
|
}
|
|
}
|
|
|
|
ServiceBasedApp::~ServiceBasedApp() {
|
|
logging::TraceGuard trace("ServiceBasedApp::~ServiceBasedApp");
|
|
|
|
applicationController_.reset();
|
|
lifecycleController_.reset();
|
|
}
|
|
|
|
void ServiceBasedApp::Run() {
|
|
logging::TraceGuard trace("ServiceBasedApp::Run");
|
|
|
|
try {
|
|
// Initialize all services
|
|
lifecycleController_->InitializeAll();
|
|
|
|
// Create the window
|
|
auto windowService = registry_.GetService<services::IWindowService>();
|
|
if (windowService) {
|
|
services::WindowConfig config;
|
|
config.width = 1024;
|
|
config.height = 768;
|
|
config.title = "SDL3 + Vulkan Application";
|
|
config.resizable = true;
|
|
windowService->CreateWindow(config);
|
|
}
|
|
|
|
// Initialize graphics after window is created
|
|
// Temporarily disabled for testing
|
|
/*
|
|
auto graphicsService = registry_.GetService<services::IGraphicsService>();
|
|
if (graphicsService && windowService) {
|
|
services::GraphicsConfig graphicsConfig;
|
|
graphicsConfig.deviceExtensions = {"VK_KHR_swapchain"};
|
|
graphicsConfig.enableValidationLayers = false;
|
|
graphicsService->InitializeDevice(windowService->GetNativeHandle(), graphicsConfig);
|
|
graphicsService->InitializeSwapchain();
|
|
}
|
|
*/
|
|
|
|
// Connect services that depend on each other
|
|
auto scriptService = registry_.GetService<services::IScriptService>();
|
|
auto audioService = registry_.GetService<services::IAudioService>();
|
|
if (scriptService && audioService) {
|
|
// The script service needs access to the audio player for Lua audio commands
|
|
// This is a bit of a hack - ideally the audio service would provide an interface
|
|
// But for now, we'll get the audio player from the impl
|
|
auto audioServiceImpl = std::dynamic_pointer_cast<services::impl::SdlAudioService>(audioService);
|
|
if (audioServiceImpl) {
|
|
// We need to access the private audioPlayer_ - this is not ideal
|
|
// TODO: Refactor to provide proper interface
|
|
}
|
|
}
|
|
|
|
// Run the main application loop
|
|
applicationController_->Run();
|
|
|
|
// Shutdown all services
|
|
lifecycleController_->ShutdownAll();
|
|
|
|
} catch (const std::exception& e) {
|
|
logging::Logger::GetInstance().Error("Application error: " + std::string(e.what()));
|
|
throw;
|
|
}
|
|
}
|
|
|
|
void ServiceBasedApp::SetupSDL() {
|
|
logging::TraceGuard trace("ServiceBasedApp::SetupSDL");
|
|
|
|
// SDL initialization is handled by the window service
|
|
// Don't initialize SDL here to avoid double initialization
|
|
}
|
|
|
|
void ServiceBasedApp::RegisterServices() {
|
|
logging::TraceGuard trace("ServiceBasedApp::RegisterServices");
|
|
|
|
// Event bus (needed by window service)
|
|
registry_.RegisterService<events::EventBus, events::EventBus>();
|
|
|
|
// Configuration service
|
|
services::impl::RuntimeConfig runtimeConfig;
|
|
runtimeConfig.scriptPath = scriptPath_;
|
|
registry_.RegisterService<services::IConfigService, services::impl::JsonConfigService>(runtimeConfig);
|
|
|
|
// Window service
|
|
registry_.RegisterService<services::IWindowService, services::impl::SdlWindowService>(
|
|
registry_.GetService<events::EventBus>());
|
|
|
|
// Input service
|
|
registry_.RegisterService<services::IInputService, services::impl::SdlInputService>(
|
|
registry_.GetService<events::EventBus>());
|
|
|
|
// Vulkan device service
|
|
registry_.RegisterService<services::IVulkanDeviceService, services::impl::VulkanDeviceService>();
|
|
|
|
// Swapchain service
|
|
registry_.RegisterService<services::ISwapchainService, services::impl::SwapchainService>(
|
|
registry_.GetService<services::IVulkanDeviceService>(),
|
|
registry_.GetService<events::EventBus>());
|
|
|
|
// Pipeline service
|
|
registry_.RegisterService<services::IPipelineService, services::impl::PipelineService>(
|
|
registry_.GetService<services::IVulkanDeviceService>());
|
|
|
|
// Buffer service
|
|
registry_.RegisterService<services::IBufferService, services::impl::BufferService>(
|
|
registry_.GetService<services::IVulkanDeviceService>());
|
|
|
|
// Render command service
|
|
registry_.RegisterService<services::IRenderCommandService, services::impl::RenderCommandService>(
|
|
registry_.GetService<services::IVulkanDeviceService>(),
|
|
registry_.GetService<services::ISwapchainService>());
|
|
|
|
// 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_);
|
|
|
|
// Scene service
|
|
registry_.RegisterService<services::ISceneService, services::impl::SceneService>(
|
|
registry_.GetService<services::IScriptService>());
|
|
|
|
// Audio service
|
|
registry_.RegisterService<services::IAudioService, services::impl::SdlAudioService>();
|
|
|
|
// GUI service
|
|
registry_.RegisterService<services::IGuiService, services::impl::VulkanGuiService>();
|
|
|
|
// Physics service
|
|
registry_.RegisterService<services::IPhysicsService, services::impl::BulletPhysicsService>();
|
|
}
|
|
|
|
} // namespace sdl3cpp::app
|