Added settings.ini
This commit is contained in:
112
src/core/Settings.cpp
Normal file
112
src/core/Settings.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
#include "Settings.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
// Singleton instance
|
||||
Settings& Settings::instance() {
|
||||
static Settings s_instance;
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
Settings::Settings() {
|
||||
// Constructor - defaults already set in header
|
||||
}
|
||||
|
||||
std::string Settings::getSettingsPath() {
|
||||
// Save settings.ini in the game's directory
|
||||
return "settings.ini";
|
||||
}
|
||||
|
||||
bool Settings::load() {
|
||||
std::ifstream file(getSettingsPath());
|
||||
if (!file.is_open()) {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Settings file not found, using defaults");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
std::string currentSection;
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
// Trim whitespace
|
||||
size_t start = line.find_first_not_of(" \t\r\n");
|
||||
size_t end = line.find_last_not_of(" \t\r\n");
|
||||
if (start == std::string::npos) continue; // Empty line
|
||||
line = line.substr(start, end - start + 1);
|
||||
|
||||
// Skip comments
|
||||
if (line[0] == ';' || line[0] == '#') continue;
|
||||
|
||||
// Check for section header
|
||||
if (line[0] == '[' && line[line.length() - 1] == ']') {
|
||||
currentSection = line.substr(1, line.length() - 2);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Parse key=value
|
||||
size_t equalsPos = line.find('=');
|
||||
if (equalsPos == std::string::npos) continue;
|
||||
|
||||
std::string key = line.substr(0, equalsPos);
|
||||
std::string value = line.substr(equalsPos + 1);
|
||||
|
||||
// Trim key and value
|
||||
key.erase(key.find_last_not_of(" \t") + 1);
|
||||
value.erase(0, value.find_first_not_of(" \t"));
|
||||
|
||||
// Parse settings
|
||||
if (currentSection == "Display") {
|
||||
if (key == "Fullscreen") {
|
||||
m_fullscreen = (value == "1" || value == "true" || value == "True");
|
||||
}
|
||||
} else if (currentSection == "Audio") {
|
||||
if (key == "Music") {
|
||||
m_musicEnabled = (value == "1" || value == "true" || value == "True");
|
||||
} else if (key == "Sound") {
|
||||
m_soundEnabled = (value == "1" || value == "true" || value == "True");
|
||||
}
|
||||
} else if (currentSection == "Player") {
|
||||
if (key == "Name") {
|
||||
m_playerName = value;
|
||||
}
|
||||
} else if (currentSection == "Debug") {
|
||||
if (key == "Enabled") {
|
||||
m_debugEnabled = (value == "1" || value == "true" || value == "True");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Settings loaded from %s", getSettingsPath().c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Settings::save() {
|
||||
std::ofstream file(getSettingsPath());
|
||||
if (!file.is_open()) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to save settings to %s", getSettingsPath().c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Write settings in INI format
|
||||
file << "; Tetris Game Settings\n";
|
||||
file << "; This file is auto-generated\n\n";
|
||||
|
||||
file << "[Display]\n";
|
||||
file << "Fullscreen=" << (m_fullscreen ? "1" : "0") << "\n\n";
|
||||
|
||||
file << "[Audio]\n";
|
||||
file << "Music=" << (m_musicEnabled ? "1" : "0") << "\n";
|
||||
file << "Sound=" << (m_soundEnabled ? "1" : "0") << "\n\n";
|
||||
|
||||
file << "[Player]\n";
|
||||
file << "Name=" << m_playerName << "\n\n";
|
||||
|
||||
file << "[Debug]\n";
|
||||
file << "Enabled=" << (m_debugEnabled ? "1" : "0") << "\n";
|
||||
|
||||
file.close();
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Settings saved to %s", getSettingsPath().c_str());
|
||||
return true;
|
||||
}
|
||||
49
src/core/Settings.h
Normal file
49
src/core/Settings.h
Normal file
@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* Settings - Persistent game settings manager
|
||||
* Handles loading/saving settings to settings.ini
|
||||
*/
|
||||
class Settings {
|
||||
public:
|
||||
// Singleton access
|
||||
static Settings& instance();
|
||||
|
||||
// Load settings from file (returns true if file existed)
|
||||
bool load();
|
||||
|
||||
// Save settings to file
|
||||
bool save();
|
||||
|
||||
// Settings accessors
|
||||
bool isFullscreen() const { return m_fullscreen; }
|
||||
void setFullscreen(bool value) { m_fullscreen = value; }
|
||||
|
||||
bool isMusicEnabled() const { return m_musicEnabled; }
|
||||
void setMusicEnabled(bool value) { m_musicEnabled = value; }
|
||||
|
||||
bool isSoundEnabled() const { return m_soundEnabled; }
|
||||
void setSoundEnabled(bool value) { m_soundEnabled = value; }
|
||||
|
||||
bool isDebugEnabled() const { return m_debugEnabled; }
|
||||
void setDebugEnabled(bool value) { m_debugEnabled = value; }
|
||||
|
||||
const std::string& getPlayerName() const { return m_playerName; }
|
||||
void setPlayerName(const std::string& name) { m_playerName = name; }
|
||||
|
||||
// Get the settings file path
|
||||
static std::string getSettingsPath();
|
||||
|
||||
private:
|
||||
Settings(); // Private constructor for singleton
|
||||
Settings(const Settings&) = delete;
|
||||
Settings& operator=(const Settings&) = delete;
|
||||
|
||||
// Settings values
|
||||
bool m_fullscreen = false;
|
||||
bool m_musicEnabled = true;
|
||||
bool m_soundEnabled = true;
|
||||
bool m_debugEnabled = false;
|
||||
std::string m_playerName = "Player";
|
||||
};
|
||||
Reference in New Issue
Block a user