mirror of
https://github.com/johndoe6345789/SDL3CPlusPlus.git
synced 2026-04-27 15:14:58 +00:00
refactor: Enhance service architecture by introducing IPlatformService and updating dependencies
- Removed core/platform.hpp and core/vulkan_utils.cpp, integrating their functionality into new platform_service implementations. - Updated service registrations to utilize IPlatformService for improved modularity. - Refactored event bus usage across services to leverage IEventBus interface. - Enhanced buffer management in BufferService with detailed logging and error handling. - Updated GUI rendering services to utilize buffer service for resource management. - Cleaned up includes and improved overall code organization for better maintainability.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#include "buffer_service.hpp"
|
||||
#include "../../core/vulkan_utils.hpp"
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace sdl3cpp::services::impl {
|
||||
|
||||
@@ -97,7 +97,94 @@ void BufferService::CreateBuffer(VkDeviceSize size, VkBufferUsageFlags usage,
|
||||
auto device = deviceService_->GetDevice();
|
||||
auto physicalDevice = deviceService_->GetPhysicalDevice();
|
||||
|
||||
vulkan::utils::CreateBuffer(device, physicalDevice, size, usage, properties, buffer, bufferMemory);
|
||||
if (logger_) {
|
||||
logger_->Debug("Creating buffer with size " + std::to_string(size) + " bytes");
|
||||
}
|
||||
|
||||
if (size == 0) {
|
||||
if (logger_) {
|
||||
logger_->Error("Cannot create buffer with size 0");
|
||||
}
|
||||
throw std::runtime_error("Cannot create buffer with size 0");
|
||||
}
|
||||
|
||||
VkPhysicalDeviceMemoryProperties memProps;
|
||||
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProps);
|
||||
|
||||
uint64_t totalAvailable = 0;
|
||||
for (uint32_t i = 0; i < memProps.memoryHeapCount; ++i) {
|
||||
if (memProps.memoryHeaps[i].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) {
|
||||
totalAvailable += memProps.memoryHeaps[i].size;
|
||||
}
|
||||
}
|
||||
|
||||
if (size > totalAvailable) {
|
||||
throw std::runtime_error("Requested buffer size (" +
|
||||
std::to_string(size / (1024 * 1024)) + " MB) exceeds available GPU memory (" +
|
||||
std::to_string(totalAvailable / (1024 * 1024)) + " MB)");
|
||||
}
|
||||
|
||||
VkBufferCreateInfo bufferInfo{};
|
||||
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||
bufferInfo.size = size;
|
||||
bufferInfo.usage = usage;
|
||||
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
|
||||
VkResult createResult = vkCreateBuffer(device, &bufferInfo, nullptr, &buffer);
|
||||
if (createResult != VK_SUCCESS) {
|
||||
throw std::runtime_error("Failed to create buffer (error code: " +
|
||||
std::to_string(createResult) + ", size: " +
|
||||
std::to_string(size / 1024) + " KB)");
|
||||
}
|
||||
|
||||
VkMemoryRequirements memRequirements;
|
||||
vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
|
||||
|
||||
VkMemoryAllocateInfo allocInfo{};
|
||||
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||
allocInfo.allocationSize = memRequirements.size;
|
||||
|
||||
bool foundType = false;
|
||||
for (uint32_t i = 0; i < memProps.memoryTypeCount; ++i) {
|
||||
if ((memRequirements.memoryTypeBits & (1 << i)) &&
|
||||
(memProps.memoryTypes[i].propertyFlags & properties) == properties) {
|
||||
allocInfo.memoryTypeIndex = i;
|
||||
foundType = true;
|
||||
if (logger_) {
|
||||
logger_->Debug("Found suitable memory type: " + std::to_string(i));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!foundType) {
|
||||
vkDestroyBuffer(device, buffer, nullptr);
|
||||
if (logger_) {
|
||||
logger_->Error("Failed to find suitable memory type");
|
||||
}
|
||||
throw std::runtime_error("Failed to find suitable memory type");
|
||||
}
|
||||
|
||||
VkResult allocResult = vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory);
|
||||
if (allocResult != VK_SUCCESS) {
|
||||
vkDestroyBuffer(device, buffer, nullptr);
|
||||
std::string errorMsg = "Failed to allocate buffer memory.\n";
|
||||
errorMsg += "Requested: " + std::to_string(memRequirements.size / (1024 * 1024)) + " MB\n";
|
||||
errorMsg += "Error code: " + std::to_string(allocResult) + "\n";
|
||||
if (allocResult == VK_ERROR_OUT_OF_DEVICE_MEMORY) {
|
||||
errorMsg += "\nOut of GPU memory. Try:\n";
|
||||
errorMsg += "- Closing other GPU-intensive applications\n";
|
||||
errorMsg += "- Reducing window resolution\n";
|
||||
errorMsg += "- Upgrading GPU or system memory";
|
||||
} else if (allocResult == VK_ERROR_OUT_OF_HOST_MEMORY) {
|
||||
errorMsg += "\nOut of system memory. Try:\n";
|
||||
errorMsg += "- Closing other applications\n";
|
||||
errorMsg += "- Adding more RAM to your system";
|
||||
}
|
||||
throw std::runtime_error(errorMsg);
|
||||
}
|
||||
|
||||
vkBindBufferMemory(device, buffer, bufferMemory, 0);
|
||||
}
|
||||
|
||||
void BufferService::CleanupBuffers() {
|
||||
|
||||
80
src/services/impl/platform_service.cpp
Normal file
80
src/services/impl/platform_service.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include "platform_service.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <utility>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
namespace sdl3cpp::services::impl {
|
||||
|
||||
PlatformService::PlatformService(std::shared_ptr<ILogger> logger)
|
||||
: logger_(std::move(logger)) {
|
||||
}
|
||||
|
||||
std::optional<std::filesystem::path> PlatformService::GetUserConfigDirectory() const {
|
||||
if (logger_) {
|
||||
logger_->Trace("PlatformService", "GetUserConfigDirectory");
|
||||
}
|
||||
#ifdef _WIN32
|
||||
if (const char* appData = std::getenv("APPDATA")) {
|
||||
return std::filesystem::path(appData) / "sdl3cpp";
|
||||
}
|
||||
#else
|
||||
if (const char* xdgConfig = std::getenv("XDG_CONFIG_HOME")) {
|
||||
return std::filesystem::path(xdgConfig) / "sdl3cpp";
|
||||
}
|
||||
if (const char* home = std::getenv("HOME")) {
|
||||
return std::filesystem::path(home) / ".config" / "sdl3cpp";
|
||||
}
|
||||
#endif
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
std::string PlatformService::FormatWin32Error(unsigned long errorCode) const {
|
||||
if (errorCode == ERROR_SUCCESS) {
|
||||
return "ERROR_SUCCESS";
|
||||
}
|
||||
LPSTR buffer = nullptr;
|
||||
DWORD length = FormatMessageA(
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
nullptr,
|
||||
errorCode,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
reinterpret_cast<LPSTR>(&buffer),
|
||||
0,
|
||||
nullptr
|
||||
);
|
||||
|
||||
std::string message;
|
||||
if (length > 0 && buffer != nullptr) {
|
||||
message = std::string(buffer, length);
|
||||
while (!message.empty() && (message.back() == '\n' || message.back() == '\r')) {
|
||||
message.pop_back();
|
||||
}
|
||||
LocalFree(buffer);
|
||||
} else {
|
||||
message = "(FormatMessage failed)";
|
||||
}
|
||||
return message;
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string PlatformService::GetPlatformError() const {
|
||||
if (logger_) {
|
||||
logger_->Trace("PlatformService", "GetPlatformError");
|
||||
}
|
||||
#ifdef _WIN32
|
||||
DWORD win32Error = ::GetLastError();
|
||||
if (win32Error != ERROR_SUCCESS) {
|
||||
return "Win32 error " + std::to_string(win32Error) + ": " + FormatWin32Error(win32Error);
|
||||
}
|
||||
return "No platform error";
|
||||
#else
|
||||
return "No platform error";
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace sdl3cpp::services::impl
|
||||
24
src/services/impl/platform_service.hpp
Normal file
24
src/services/impl/platform_service.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "../interfaces/i_platform_service.hpp"
|
||||
#include "../interfaces/i_logger.hpp"
|
||||
#include <memory>
|
||||
|
||||
namespace sdl3cpp::services::impl {
|
||||
|
||||
class PlatformService : public IPlatformService {
|
||||
public:
|
||||
explicit PlatformService(std::shared_ptr<ILogger> logger = nullptr);
|
||||
~PlatformService() override = default;
|
||||
|
||||
std::optional<std::filesystem::path> GetUserConfigDirectory() const override;
|
||||
std::string GetPlatformError() const override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<ILogger> logger_;
|
||||
#ifdef _WIN32
|
||||
std::string FormatWin32Error(unsigned long errorCode) const;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace sdl3cpp::services::impl
|
||||
@@ -12,7 +12,7 @@ const std::unordered_map<SDL_Keycode, std::string> SdlInputService::kGuiKeyNames
|
||||
{SDLK_RALT, "ralt"}
|
||||
};
|
||||
|
||||
SdlInputService::SdlInputService(std::shared_ptr<events::EventBus> eventBus, std::shared_ptr<ILogger> logger)
|
||||
SdlInputService::SdlInputService(std::shared_ptr<events::IEventBus> eventBus, std::shared_ptr<ILogger> logger)
|
||||
: eventBus_(std::move(eventBus)), logger_(logger) {
|
||||
|
||||
// Subscribe to input events
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "../interfaces/i_input_service.hpp"
|
||||
#include "../interfaces/i_gui_script_service.hpp"
|
||||
#include "../interfaces/i_logger.hpp"
|
||||
#include "../../events/event_bus.hpp"
|
||||
#include "../../events/i_event_bus.hpp"
|
||||
#include <memory>
|
||||
|
||||
namespace sdl3cpp::services::impl {
|
||||
@@ -23,7 +23,7 @@ public:
|
||||
*
|
||||
* @param eventBus Event bus to subscribe to
|
||||
*/
|
||||
explicit SdlInputService(std::shared_ptr<events::EventBus> eventBus, std::shared_ptr<ILogger> logger);
|
||||
explicit SdlInputService(std::shared_ptr<events::IEventBus> eventBus, std::shared_ptr<ILogger> logger);
|
||||
|
||||
// IInputService interface
|
||||
void ProcessEvent(const SDL_Event& event) override;
|
||||
@@ -36,7 +36,7 @@ public:
|
||||
void UpdateGuiInput() override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<events::EventBus> eventBus_;
|
||||
std::shared_ptr<events::IEventBus> eventBus_;
|
||||
std::shared_ptr<ILogger> logger_;
|
||||
InputState state_;
|
||||
script::GuiInputSnapshot guiInputSnapshot_;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "sdl_window_service.hpp"
|
||||
#include "../interfaces/i_logger.hpp"
|
||||
#include "../../core/platform.hpp"
|
||||
#include <SDL3/SDL_vulkan.h>
|
||||
#include <chrono>
|
||||
#include <sstream>
|
||||
@@ -10,7 +9,7 @@ namespace sdl3cpp::services::impl {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string BuildSdlErrorMessage(const char* context) {
|
||||
std::string BuildSdlErrorMessage(const char* context, const std::shared_ptr<IPlatformService>& platformService) {
|
||||
std::ostringstream oss;
|
||||
oss << context;
|
||||
const char* sdlError = SDL_GetError();
|
||||
@@ -20,17 +19,19 @@ std::string BuildSdlErrorMessage(const char* context) {
|
||||
oss << ": (SDL_GetError returned an empty string)";
|
||||
}
|
||||
|
||||
std::string platformError = sdl3cpp::platform::GetPlatformError();
|
||||
if (!platformError.empty() && platformError != "No platform error") {
|
||||
oss << " [" << platformError << "]";
|
||||
if (platformService) {
|
||||
std::string platformError = platformService->GetPlatformError();
|
||||
if (!platformError.empty() && platformError != "No platform error") {
|
||||
oss << " [" << platformError << "]";
|
||||
}
|
||||
}
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void ThrowSdlErrorIfFailed(bool success, const char* context) {
|
||||
void ThrowSdlErrorIfFailed(bool success, const char* context, const std::shared_ptr<IPlatformService>& platformService) {
|
||||
if (!success) {
|
||||
throw std::runtime_error(BuildSdlErrorMessage(context));
|
||||
throw std::runtime_error(BuildSdlErrorMessage(context, platformService));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,8 +46,12 @@ void ShowErrorDialog(const char* title, const std::string& message) {
|
||||
|
||||
} // namespace
|
||||
|
||||
SdlWindowService::SdlWindowService(std::shared_ptr<ILogger> logger, std::shared_ptr<events::EventBus> eventBus)
|
||||
: logger_(std::move(logger)), eventBus_(std::move(eventBus)) {
|
||||
SdlWindowService::SdlWindowService(std::shared_ptr<ILogger> logger,
|
||||
std::shared_ptr<IPlatformService> platformService,
|
||||
std::shared_ptr<events::IEventBus> eventBus)
|
||||
: logger_(std::move(logger)),
|
||||
platformService_(std::move(platformService)),
|
||||
eventBus_(std::move(eventBus)) {
|
||||
}
|
||||
|
||||
SdlWindowService::~SdlWindowService() {
|
||||
@@ -101,7 +106,7 @@ void SdlWindowService::CreateWindow(const WindowConfig& config) {
|
||||
// Initialize SDL here if not already initialized
|
||||
if (SDL_WasInit(0) == 0) {
|
||||
try {
|
||||
ThrowSdlErrorIfFailed(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO), "SDL_Init failed");
|
||||
ThrowSdlErrorIfFailed(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO), "SDL_Init failed", platformService_);
|
||||
} catch (const std::exception& e) {
|
||||
ShowErrorDialog("SDL Initialization Failed",
|
||||
std::string("Failed to initialize SDL subsystems.\n\nError: ") + e.what());
|
||||
@@ -109,7 +114,7 @@ void SdlWindowService::CreateWindow(const WindowConfig& config) {
|
||||
}
|
||||
|
||||
try {
|
||||
ThrowSdlErrorIfFailed(SDL_Vulkan_LoadLibrary(nullptr), "SDL_Vulkan_LoadLibrary failed");
|
||||
ThrowSdlErrorIfFailed(SDL_Vulkan_LoadLibrary(nullptr), "SDL_Vulkan_LoadLibrary failed", platformService_);
|
||||
} catch (const std::exception& e) {
|
||||
ShowErrorDialog("Vulkan Library Load Failed",
|
||||
std::string("Failed to load Vulkan library. Make sure Vulkan drivers are installed.\n\nError: ") + e.what());
|
||||
@@ -130,7 +135,7 @@ void SdlWindowService::CreateWindow(const WindowConfig& config) {
|
||||
);
|
||||
|
||||
if (!window_) {
|
||||
std::string errorMsg = BuildSdlErrorMessage("SDL_CreateWindow failed");
|
||||
std::string errorMsg = BuildSdlErrorMessage("SDL_CreateWindow failed", platformService_);
|
||||
ShowErrorDialog("Window Creation Failed",
|
||||
std::string("Failed to create application window.\n\nError: ") + errorMsg);
|
||||
throw std::runtime_error(errorMsg);
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
|
||||
#include "../interfaces/i_window_service.hpp"
|
||||
#include "../interfaces/i_logger.hpp"
|
||||
#include "../interfaces/i_platform_service.hpp"
|
||||
#include "../../di/lifecycle.hpp"
|
||||
#include "../../events/event_bus.hpp"
|
||||
#include "../../events/i_event_bus.hpp"
|
||||
#include <memory>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
@@ -25,7 +26,9 @@ public:
|
||||
* @param logger Logger service for logging
|
||||
* @param eventBus Event bus for publishing window/input events
|
||||
*/
|
||||
SdlWindowService(std::shared_ptr<ILogger> logger, std::shared_ptr<events::EventBus> eventBus);
|
||||
SdlWindowService(std::shared_ptr<ILogger> logger,
|
||||
std::shared_ptr<IPlatformService> platformService,
|
||||
std::shared_ptr<events::IEventBus> eventBus);
|
||||
|
||||
~SdlWindowService() override;
|
||||
|
||||
@@ -47,7 +50,8 @@ public:
|
||||
|
||||
private:
|
||||
std::shared_ptr<ILogger> logger_;
|
||||
std::shared_ptr<events::EventBus> eventBus_;
|
||||
std::shared_ptr<IPlatformService> platformService_;
|
||||
std::shared_ptr<events::IEventBus> eventBus_;
|
||||
SDL_Window* window_ = nullptr;
|
||||
bool shouldClose_ = false;
|
||||
bool initialized_ = false;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
namespace sdl3cpp::services::impl {
|
||||
|
||||
SwapchainService::SwapchainService(std::shared_ptr<IVulkanDeviceService> deviceService,
|
||||
std::shared_ptr<events::EventBus> eventBus,
|
||||
std::shared_ptr<events::IEventBus> eventBus,
|
||||
std::shared_ptr<ILogger> logger)
|
||||
: deviceService_(std::move(deviceService)), eventBus_(std::move(eventBus)), logger_(logger) {
|
||||
// Subscribe to window resize events
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "../interfaces/i_vulkan_device_service.hpp"
|
||||
#include "../interfaces/i_logger.hpp"
|
||||
#include "../../di/lifecycle.hpp"
|
||||
#include "../../events/event_bus.hpp"
|
||||
#include "../../events/i_event_bus.hpp"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
@@ -21,7 +21,7 @@ class SwapchainService : public ISwapchainService,
|
||||
public di::IShutdownable {
|
||||
public:
|
||||
explicit SwapchainService(std::shared_ptr<IVulkanDeviceService> deviceService,
|
||||
std::shared_ptr<events::EventBus> eventBus,
|
||||
std::shared_ptr<events::IEventBus> eventBus,
|
||||
std::shared_ptr<ILogger> logger);
|
||||
~SwapchainService() override;
|
||||
|
||||
@@ -50,7 +50,7 @@ public:
|
||||
|
||||
private:
|
||||
std::shared_ptr<IVulkanDeviceService> deviceService_;
|
||||
std::shared_ptr<events::EventBus> eventBus_;
|
||||
std::shared_ptr<events::IEventBus> eventBus_;
|
||||
std::shared_ptr<ILogger> logger_;
|
||||
|
||||
VkSwapchainKHR swapchain_ = VK_NULL_HANDLE;
|
||||
|
||||
@@ -4,8 +4,10 @@
|
||||
|
||||
namespace sdl3cpp::services::impl {
|
||||
|
||||
VulkanGuiService::VulkanGuiService(std::shared_ptr<ILogger> logger)
|
||||
: logger_(std::move(logger)) {
|
||||
VulkanGuiService::VulkanGuiService(std::shared_ptr<ILogger> logger,
|
||||
std::shared_ptr<IBufferService> bufferService)
|
||||
: logger_(std::move(logger)),
|
||||
bufferService_(std::move(bufferService)) {
|
||||
}
|
||||
|
||||
VulkanGuiService::~VulkanGuiService() {
|
||||
@@ -24,7 +26,7 @@ void VulkanGuiService::Initialize(VkDevice device,
|
||||
return;
|
||||
}
|
||||
|
||||
renderer_ = std::make_unique<gui::GuiRenderer>(device, physicalDevice, format, resourcePath);
|
||||
renderer_ = std::make_unique<gui::GuiRenderer>(device, physicalDevice, format, resourcePath, bufferService_);
|
||||
initialized_ = true;
|
||||
|
||||
logger_->Info("GUI service initialized");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "../interfaces/i_gui_service.hpp"
|
||||
#include "../interfaces/i_buffer_service.hpp"
|
||||
#include "../interfaces/i_logger.hpp"
|
||||
#include "../../gui/gui_renderer.hpp"
|
||||
#include "../../di/lifecycle.hpp"
|
||||
@@ -17,7 +18,8 @@ namespace sdl3cpp::services::impl {
|
||||
class VulkanGuiService : public IGuiService,
|
||||
public di::IShutdownable {
|
||||
public:
|
||||
explicit VulkanGuiService(std::shared_ptr<ILogger> logger);
|
||||
VulkanGuiService(std::shared_ptr<ILogger> logger,
|
||||
std::shared_ptr<IBufferService> bufferService);
|
||||
~VulkanGuiService() override;
|
||||
|
||||
// IGuiService interface
|
||||
@@ -38,6 +40,7 @@ public:
|
||||
|
||||
private:
|
||||
std::shared_ptr<ILogger> logger_;
|
||||
std::shared_ptr<IBufferService> bufferService_;
|
||||
std::unique_ptr<gui::GuiRenderer> renderer_;
|
||||
bool initialized_ = false;
|
||||
};
|
||||
|
||||
17
src/services/interfaces/i_platform_service.hpp
Normal file
17
src/services/interfaces/i_platform_service.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace sdl3cpp::services {
|
||||
|
||||
class IPlatformService {
|
||||
public:
|
||||
virtual ~IPlatformService() = default;
|
||||
|
||||
virtual std::optional<std::filesystem::path> GetUserConfigDirectory() const = 0;
|
||||
virtual std::string GetPlatformError() const = 0;
|
||||
};
|
||||
|
||||
} // namespace sdl3cpp::services
|
||||
Reference in New Issue
Block a user