aboutsummaryrefslogtreecommitdiff
path: root/src/Core
diff options
context:
space:
mode:
Diffstat (limited to 'src/Core')
-rw-r--r--src/Core/Application.cpp11
-rw-r--r--src/Core/SettingsManager.cpp178
-rw-r--r--src/Core/SettingsManager.h70
3 files changed, 258 insertions, 1 deletions
diff --git a/src/Core/Application.cpp b/src/Core/Application.cpp
index 302132a..b879535 100644
--- a/src/Core/Application.cpp
+++ b/src/Core/Application.cpp
@@ -1,6 +1,7 @@
#include "Application.h"
#include "Rendering/Renderer.h"
+#include "SettingsManager.h"
#include "States/SimulationState.h"
#include "States/ConfigState.h"
@@ -98,7 +99,14 @@ namespace Donut
{
Logger::Init();
- RendererAPI::SetAPI(RendererAPI::API::OpenGL);
+ // Initialize settings manager first
+ SettingsManager::Initialize();
+
+ // Apply loaded settings
+ const auto& settings = SettingsManager::GetSettingsConst();
+ RendererAPI::API api = (settings.graphics.renderAPI == "Vulkan") ? RendererAPI::API::Vulkan : RendererAPI::API::OpenGL;
+ RendererAPI::SetAPI(api);
+
Renderer::Init();
m_Window->InitImGui();
@@ -117,6 +125,7 @@ namespace Donut
m_StateManager->Shutdown();
Renderer::Shutdown();
+ SettingsManager::Shutdown();
Logger::Shutdown();
}
diff --git a/src/Core/SettingsManager.cpp b/src/Core/SettingsManager.cpp
new file mode 100644
index 0000000..f35a016
--- /dev/null
+++ b/src/Core/SettingsManager.cpp
@@ -0,0 +1,178 @@
+#include "SettingsManager.h"
+#include "Log.h"
+#include "ThemeManager.h"
+#include "Rendering/Renderer.h"
+
+#include <fstream>
+#include <filesystem>
+#include <algorithm>
+
+namespace Donut
+{
+ Settings SettingsManager::s_Settings;
+ bool SettingsManager::s_Initialized = false;
+
+ void SettingsManager::Initialize()
+ {
+ if (s_Initialized)
+ return;
+
+ LoadSettings();
+ s_Initialized = true;
+ DONUT_INFO("Settings Manager initialized");
+ }
+
+ void SettingsManager::Shutdown()
+ {
+ if (!s_Initialized)
+ return;
+
+ SaveSettings();
+ s_Initialized = false;
+ DONUT_INFO("Settings Manager shutdown");
+ }
+
+ void SettingsManager::LoadSettings()
+ {
+ std::string filePath = GetSettingsFilePath();
+
+ try
+ {
+ if (std::filesystem::exists(filePath))
+ {
+ auto config = toml::parse(filePath);
+
+ if (config.contains("simulation"))
+ {
+ auto sim = config["simulation"];
+ s_Settings.simulation.targetFPS = toml::find_or(sim, "target_fps", 60);
+ s_Settings.simulation.computeHeight = toml::find_or(sim, "compute_height", 512);
+ s_Settings.simulation.maxStepsMoving = toml::find_or(sim, "max_steps_moving", 30000);
+ s_Settings.simulation.maxStepsStatic = toml::find_or(sim, "max_steps_static", 15000);
+ s_Settings.simulation.earlyExitDistance = toml::find_or(sim, "early_exit_distance", 5e12f);
+ s_Settings.simulation.gravityEnabled = toml::find_or(sim, "gravity_enabled", true);
+
+ s_Settings.simulation.targetFPS = std::max(30, std::min(120, s_Settings.simulation.targetFPS));
+ s_Settings.simulation.computeHeight = std::max(64, std::min(2048, s_Settings.simulation.computeHeight));
+ s_Settings.simulation.maxStepsMoving = std::max(1000, std::min(60000, s_Settings.simulation.maxStepsMoving));
+ s_Settings.simulation.maxStepsStatic = std::max(1000, std::min(30000, s_Settings.simulation.maxStepsStatic));
+ s_Settings.simulation.earlyExitDistance = std::max(1e11f, std::min(1e13f, s_Settings.simulation.earlyExitDistance));
+ }
+
+ if (config.contains("graphics"))
+ {
+ auto gfx = config["graphics"];
+ s_Settings.graphics.renderAPI = toml::find_or(gfx, "render_api", std::string("OpenGL"));
+ s_Settings.graphics.vSyncEnabled = toml::find_or(gfx, "vsync_enabled", true);
+ s_Settings.graphics.showFPS = toml::find_or(gfx, "show_fps", true);
+ s_Settings.graphics.showPerformanceMetrics = toml::find_or(gfx, "show_performance_metrics", true);
+ s_Settings.graphics.showDebugInfo = toml::find_or(gfx, "show_debug_info", false);
+ s_Settings.graphics.enableAntiAliasing = toml::find_or(gfx, "enable_anti_aliasing", true);
+ s_Settings.graphics.selectedTheme = toml::find_or(gfx, "selected_theme", std::string("Dark"));
+
+ if (s_Settings.graphics.renderAPI != "OpenGL" &&
+ s_Settings.graphics.renderAPI != "Vulkan")
+ s_Settings.graphics.renderAPI = "OpenGL";
+ if (s_Settings.graphics.selectedTheme != "Dark" &&
+ s_Settings.graphics.selectedTheme != "Light" &&
+ s_Settings.graphics.selectedTheme != "Blue")
+ s_Settings.graphics.selectedTheme = "Dark";
+ }
+
+ DONUT_INFO("Settings loaded from {}", filePath);
+ }
+ else
+ {
+ LoadDefaultSettings();
+ SaveSettings();
+ DONUT_INFO("No settings file found, created default settings");
+ }
+ }
+ catch (const std::exception& e)
+ {
+ DONUT_ERROR("Failed to load settings: {}", e.what());
+ LoadDefaultSettings();
+ }
+ }
+
+ void SettingsManager::SaveSettings()
+ {
+ std::string filePath = GetSettingsFilePath();
+
+ try
+ {
+ std::filesystem::path path(filePath);
+ std::filesystem::create_directories(path.parent_path());
+
+ toml::value simulation = toml::table
+ {
+ {"target_fps", s_Settings.simulation.targetFPS },
+ {"compute_height", s_Settings.simulation.computeHeight },
+ {"max_steps_moving", s_Settings.simulation.maxStepsMoving },
+ {"max_steps_static", s_Settings.simulation.maxStepsStatic },
+ {"early_exit_distance", s_Settings.simulation.earlyExitDistance},
+ {"gravity_enabled", s_Settings.simulation.gravityEnabled }
+ };
+
+ toml::value graphics = toml::table
+ {
+ {"render_api", s_Settings.graphics.renderAPI },
+ {"vsync_enabled", s_Settings.graphics.vSyncEnabled },
+ {"show_fps", s_Settings.graphics.showFPS },
+ {"show_performance_metrics", s_Settings.graphics.showPerformanceMetrics},
+ {"show_debug_info", s_Settings.graphics.showDebugInfo },
+ {"enable_anti_aliasing", s_Settings.graphics.enableAntiAliasing },
+ {"selected_theme", s_Settings.graphics.selectedTheme }
+ };
+
+ toml::value config = toml::table
+ {
+ {"simulation", simulation},
+ {"graphics", graphics}
+ };
+
+ std::ofstream file(filePath);
+ file << config;
+ file.close();
+
+ DONUT_INFO("Settings saved to {}", filePath);
+ }
+ catch (const std::exception& e)
+ DONUT_ERROR("Failed to save settings: {}", e.what());
+ }
+
+ void SettingsManager::SetSimulationSettings(const SimulationSettings& settings)
+ {
+ s_Settings.simulation = settings;
+ SaveSettings();
+ }
+
+ void SettingsManager::SetGraphicsSettings(const GraphicsSettings& settings)
+ {
+ s_Settings.graphics = settings;
+ SaveSettings();
+ }
+
+ std::string SettingsManager::GetSettingsFilePath()
+ {
+ return "Config/settings.toml";
+ }
+
+ void SettingsManager::LoadDefaultSettings()
+ {
+ s_Settings.simulation.targetFPS = 60;
+ s_Settings.simulation.computeHeight = 512;
+ s_Settings.simulation.maxStepsMoving = 30000;
+ s_Settings.simulation.maxStepsStatic = 15000;
+ s_Settings.simulation.earlyExitDistance = 5e12f;
+ s_Settings.simulation.gravityEnabled = true;
+
+ s_Settings.graphics.renderAPI = "OpenGL";
+ s_Settings.graphics.vSyncEnabled = true;
+ s_Settings.graphics.showFPS = true;
+ s_Settings.graphics.showPerformanceMetrics = true;
+ s_Settings.graphics.showDebugInfo = false;
+ s_Settings.graphics.enableAntiAliasing = true;
+ s_Settings.graphics.selectedTheme = "Dark";
+ }
+}
diff --git a/src/Core/SettingsManager.h b/src/Core/SettingsManager.h
new file mode 100644
index 0000000..bea1162
--- /dev/null
+++ b/src/Core/SettingsManager.h
@@ -0,0 +1,70 @@
+#pragma once
+
+#include <string>
+#include <toml.hpp>
+
+namespace Donut
+{
+ struct SimulationSettings
+ {
+ int targetFPS = 60;
+ int computeHeight = 512;
+ int maxStepsMoving = 30000;
+ int maxStepsStatic = 15000;
+ float earlyExitDistance = 5e12f;
+ bool gravityEnabled = true;
+ };
+
+ struct GraphicsSettings
+ {
+ std::string renderAPI = "OpenGL";
+ bool vSyncEnabled = true;
+ bool showFPS = true;
+ bool showPerformanceMetrics = true;
+ bool showDebugInfo = false;
+ bool enableAntiAliasing = true;
+ std::string selectedTheme = "Dark";
+ };
+
+ struct Settings
+ {
+ SimulationSettings simulation;
+ GraphicsSettings graphics;
+ };
+
+ class SettingsManager
+ {
+ public:
+ static void Initialize();
+ static void Shutdown();
+
+ static void LoadSettings();
+ static void SaveSettings();
+
+ static Settings& GetSettings() { return s_Settings; }
+ static const Settings& GetSettingsConst() { return s_Settings; }
+
+ static void SetSimulationSettings(const SimulationSettings& settings);
+ static void SetGraphicsSettings(const GraphicsSettings& settings);
+
+ static int GetTargetFPS() { return s_Settings.simulation.targetFPS; }
+ static int GetComputeHeight() { return s_Settings.simulation.computeHeight; }
+ static int GetMaxStepsMoving() { return s_Settings.simulation.maxStepsMoving; }
+ static int GetMaxStepsStatic() { return s_Settings.simulation.maxStepsStatic; }
+ static float GetEarlyExitDistance() { return s_Settings.simulation.earlyExitDistance; }
+ static bool GetGravityEnabled() { return s_Settings.simulation.gravityEnabled; }
+ static std::string GetRenderAPI() { return s_Settings.graphics.renderAPI; }
+ static bool GetVSyncEnabled() { return s_Settings.graphics.vSyncEnabled; }
+ static bool GetShowFPS() { return s_Settings.graphics.showFPS; }
+ static bool GetShowPerformanceMetrics() { return s_Settings.graphics.showPerformanceMetrics; }
+ static bool GetShowDebugInfo() { return s_Settings.graphics.showDebugInfo; }
+ static bool GetEnableAntiAliasing() { return s_Settings.graphics.enableAntiAliasing; }
+ static std::string GetSelectedTheme() { return s_Settings.graphics.selectedTheme; }
+ private:
+ static std::string GetSettingsFilePath();
+ static void LoadDefaultSettings();
+ private:
+ static Settings s_Settings;
+ static bool s_Initialized;
+ };
+}