From 191b764747ff697d90158b13397518a75df31b9a Mon Sep 17 00:00:00 2001 From: ternaryop8479 Date: Sun, 15 Feb 2026 15:49:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=88=A0=E9=99=A4config=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E6=97=A0=E7=94=A8=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/core/renderer.h | 11 +--- include/utils/config.h | 87 +++---------------------- src/utils/config.cpp | 139 ---------------------------------------- 3 files changed, 9 insertions(+), 228 deletions(-) diff --git a/include/core/renderer.h b/include/core/renderer.h index e54b757..2141d76 100644 --- a/include/core/renderer.h +++ b/include/core/renderer.h @@ -8,20 +8,11 @@ #include "core/screen_blit.h" #include "core/shader_manager.h" #include "scene/scene.h" +#include "utils/config.h" #include namespace are { -// Main renderer configuration -struct RendererConfig { - uint width_; - uint height_; - uint samples_per_pixel_; - uint max_ray_depth_; - bool enable_denoising_; - bool enable_accumulation_; -}; - // Main rendering engine interface class Renderer { public: diff --git a/include/utils/config.h b/include/utils/config.h index e0ee70c..d80d4ab 100644 --- a/include/utils/config.h +++ b/include/utils/config.h @@ -6,85 +6,14 @@ namespace are { -/* - * @brief Configuration manager for loading engine settings - * @note This module should be implemented by the user - */ -class Config { -public: - /* - * @brief Load configuration from file - * @param path Configuration file path - * @return True if loading succeeded - */ - static bool load(const std::string &path); - - /* - * @brief Save configuration to file - * @param path Configuration file path - * @return True if saving succeeded - */ - static bool save(const std::string &path); - - /* - * @brief Get string value - * @param key Configuration key - * @param default_value Default value if key not found - * @return Configuration value - */ - static std::string get_string(const std::string &key, const std::string &default_value = ""); - - /* - * @brief Get integer value - * @param key Configuration key - * @param default_value Default value if key not found - * @return Configuration value - */ - static int get_int(const std::string &key, int default_value = 0); - - /* - * @brief Get float value - * @param key Configuration key - * @param default_value Default value if key not found - * @return Configuration value - */ - static float get_float(const std::string &key, float default_value = 0.0f); - - /* - * @brief Get boolean value - * @param key Configuration key - * @param default_value Default value if key not found - * @return Configuration value - */ - static bool get_bool(const std::string &key, bool default_value = false); - - /* - * @brief Set string value - * @param key Configuration key - * @param value Configuration value - */ - static void set_string(const std::string &key, const std::string &value); - - /* - * @brief Set integer value - * @param key Configuration key - * @param value Configuration value - */ - static void set_int(const std::string &key, int value); - - /* - * @brief Set float value - * @param key Configuration key - * @param value Configuration value - */ - static void set_float(const std::string &key, float value); - - /* - * @brief Set boolean value - * @param key Configuration key - * @param value Configuration value - */ - static void set_bool(const std::string &key, bool value); +// Configuration struct for renderer +struct RendererConfig { + uint width_; + uint height_; + uint samples_per_pixel_; + uint max_ray_depth_; + bool enable_denoising_; + bool enable_accumulation_; }; } // namespace are diff --git a/src/utils/config.cpp b/src/utils/config.cpp index 80ef6b0..d354357 100644 --- a/src/utils/config.cpp +++ b/src/utils/config.cpp @@ -1,144 +1,5 @@ #include "utils/config.h" -#include "utils/logger.h" -#include -#include -#include namespace are { -// Static storage -static std::unordered_map g_config_map; - -// Helper function to trim whitespace -static std::string trim(const std::string& str) { - size_t first = str.find_first_not_of(" \t\r\n"); - if (first == std::string::npos) return ""; - size_t last = str.find_last_not_of(" \t\r\n"); - return str.substr(first, last - first + 1); -} - -bool Config::load(const std::string& path) { - std::ifstream file(path); - if (!file.is_open()) { - ARE_LOG_ERROR("Failed to open config file: " + path); - return false; - } - - g_config_map.clear(); - - std::string line; - std::string current_section; - - while (std::getline(file, line)) { - line = trim(line); - - // Skip empty lines and comments - if (line.empty() || line[0] == '#' || line[0] == ';') { - continue; - } - - // Section header - if (line[0] == '[' && line.back() == ']') { - current_section = line.substr(1, line.length() - 2); - continue; - } - - // Key-value pair - size_t pos = line.find('='); - if (pos != std::string::npos) { - std::string key = trim(line.substr(0, pos)); - std::string value = trim(line.substr(pos + 1)); - - // Add section prefix if in a section - if (!current_section.empty()) { - key = current_section + "." + key; - } - - g_config_map[key] = value; - } - } - - ARE_LOG_INFO("Config loaded: " + path + " (" + std::to_string(g_config_map.size()) + " entries)"); - return true; -} - -bool Config::save(const std::string& path) { - std::ofstream file(path); - if (!file.is_open()) { - ARE_LOG_ERROR("Failed to open config file for writing: " + path); - return false; - } - - for (const auto& pair : g_config_map) { - file << pair.first << "=" << pair.second << std::endl; - } - - ARE_LOG_INFO("Config saved: " + path); - return true; -} - -std::string Config::get_string(const std::string& key, const std::string& default_value) { - auto it = g_config_map.find(key); - if (it != g_config_map.end()) { - return it->second; - } - return default_value; -} - -int Config::get_int(const std::string& key, int default_value) { - auto it = g_config_map.find(key); - if (it != g_config_map.end()) { - try { - return std::stoi(it->second); - } catch (...) { - ARE_LOG_WARN("Failed to parse int for key: " + key); - } - } - return default_value; -} - -float Config::get_float(const std::string& key, float default_value) { - auto it = g_config_map.find(key); - if (it != g_config_map.end()) { - try { - return std::stof(it->second); - } catch (...) { - ARE_LOG_WARN("Failed to parse float for key: " + key); - } - } - return default_value; -} - -bool Config::get_bool(const std::string& key, bool default_value) { - auto it = g_config_map.find(key); - if (it != g_config_map.end()) { - std::string value = it->second; - std::transform(value.begin(), value.end(), value.begin(), ::tolower); - - if (value == "true" || value == "1" || value == "yes" || value == "on") { - return true; - } - if (value == "false" || value == "0" || value == "no" || value == "off") { - return false; - } - } - return default_value; -} - -void Config::set_string(const std::string& key, const std::string& value) { - g_config_map[key] = value; -} - -void Config::set_int(const std::string& key, int value) { - g_config_map[key] = std::to_string(value); -} - -void Config::set_float(const std::string& key, float value) { - g_config_map[key] = std::to_string(value); -} - -void Config::set_bool(const std::string& key, bool value) { - g_config_map[key] = value ? "true" : "false"; -} - } // namespace are