From b15cd04c5fab55d3b65ce5a854f8fc10f3998179 Mon Sep 17 00:00:00 2001 From: auria Date: Sun, 21 Jun 2009 16:15:27 +0000 Subject: [PATCH] Further improved XML generation for user config + prepared infrastructure for saving challenges/players (groups) git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3628 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/audio/sfx_manager.cpp | 1 + src/config/user_config.cpp | 157 +++++++++++++++++++++++++++++++++++-- src/config/user_config.hpp | 90 +++++++++++++++------ 3 files changed, 219 insertions(+), 29 deletions(-) diff --git a/src/audio/sfx_manager.cpp b/src/audio/sfx_manager.cpp index 6b1a7cd0d..9bbc4937e 100644 --- a/src/audio/sfx_manager.cpp +++ b/src/audio/sfx_manager.cpp @@ -92,6 +92,7 @@ bool SFXManager::sfxAllowed() */ void SFXManager::loadSfx() { + // TODO : implement as XML const lisp::Lisp* root = 0; std::string sfx_config_name = file_manager->getSFXFile("sfx.config"); try diff --git a/src/config/user_config.cpp b/src/config/user_config.cpp index 5b35a2228..179b7ee27 100644 --- a/src/config/user_config.cpp +++ b/src/config/user_config.cpp @@ -57,6 +57,57 @@ static std::vector all_params; #define PARAM_DEFAULT(X) = X #include "config/user_config.hpp" +// --------------------------------------------------------------------------------------- + +GroupUserConfigParam::GroupUserConfigParam(const char* groupName, const char* comment) +{ + this->paramName = groupName; + all_params.push_back(this); + if(comment != NULL) this->comment = comment; +} +void GroupUserConfigParam::write(std::ofstream& stream) const +{ + if(comment.size() > 0) stream << " \n"; + stream << " <" << paramName << "\n"; + + const int children_amount = m_children.size(); + for(int n=0; nparamName << "=\"" << m_children[n]->toString() << "\"\n"; + } + stream << " />\n\n"; +} + +void GroupUserConfigParam::readAsNode(const XMLNode* node) +{ + const XMLNode* child = node->getNode( paramName ); + if(child == NULL) + { + //std::cout << "Couldn't find parameter group " << paramName << std::endl; + return; + } + + const int children_amount = m_children.size(); + for(int n=0; nreadAsProperty(child); + } + +} +void GroupUserConfigParam::readAsProperty(const XMLNode* node) +{ +} +std::string GroupUserConfigParam::toString() const +{ + return ""; +} +void GroupUserConfigParam::addChild(UserConfigParam* child) +{ + m_children.push_back(child); +} + +// --------------------------------------------------------------------------------------- + IntUserConfigParam::IntUserConfigParam(int defaultValue, const char* paramName, const char* comment) { this->value = defaultValue; @@ -64,6 +115,14 @@ IntUserConfigParam::IntUserConfigParam(int defaultValue, const char* paramName, all_params.push_back(this); if(comment != NULL) this->comment = comment; } +IntUserConfigParam::IntUserConfigParam(int defaultValue, const char* paramName, + GroupUserConfigParam* group, const char* comment) +{ + this->value = defaultValue; + this->paramName = paramName; + group->addChild(this); + if(comment != NULL) this->comment = comment; +} void IntUserConfigParam::write(std::ofstream& stream) const { @@ -71,7 +130,14 @@ void IntUserConfigParam::write(std::ofstream& stream) const stream << " <" << paramName << " value=\"" << value << "\" />\n\n"; } -void IntUserConfigParam::read(const XMLNode* node) +std::string IntUserConfigParam::toString() const +{ + char buffer[16]; + sprintf(buffer, "%i", value); + return buffer; +} + +void IntUserConfigParam::readAsNode(const XMLNode* node) { const XMLNode* child = node->getNode( paramName ); if(child == NULL) @@ -83,6 +149,10 @@ void IntUserConfigParam::read(const XMLNode* node) child->get( "value", &value ); //std::cout << "read int " << paramName << ", value=" << value << std::endl; } +void IntUserConfigParam::readAsProperty(const XMLNode* node) +{ + node->get( paramName, &value ); +} // --------------------------------------------------------------------------------------- @@ -93,18 +163,37 @@ StringUserConfigParam::StringUserConfigParam(const char* defaultValue, const cha all_params.push_back(this); if(comment != NULL) this->comment = comment; } +StringUserConfigParam::StringUserConfigParam(const char* defaultValue, const char* paramName, + GroupUserConfigParam* group, const char* comment) +{ + this->value = defaultValue; + this->paramName = paramName; + group->addChild(this); + if(comment != NULL) this->comment = comment; +} + + void StringUserConfigParam::write(std::ofstream& stream) const { if(comment.size() > 0) stream << " \n"; stream << " <" << paramName << " value=\"" << value << "\" />\n\n"; } -void StringUserConfigParam::read(const XMLNode* node) +void StringUserConfigParam::readAsNode(const XMLNode* node) { const XMLNode* child = node->getNode( paramName ); if(child == NULL) return; child->get( "value", &value ); } +void StringUserConfigParam::readAsProperty(const XMLNode* node) +{ + node->get( paramName, &value ); +} + +std::string StringUserConfigParam::toString() const +{ + return value; +} // --------------------------------------------------------------------------------------- @@ -115,12 +204,22 @@ BoolUserConfigParam::BoolUserConfigParam(bool defaultValue, const char* paramNam all_params.push_back(this); if(comment != NULL) this->comment = comment; } +BoolUserConfigParam::BoolUserConfigParam(bool defaultValue, const char* paramName, + GroupUserConfigParam* group, const char* comment) +{ + this->value = defaultValue; + this->paramName = paramName; + group->addChild(this); + if(comment != NULL) this->comment = comment; +} + + void BoolUserConfigParam::write(std::ofstream& stream) const { if(comment.size() > 0) stream << " \n"; - stream << " <" << paramName << " value=\"" << (value ? "true" : "false" )<< "\" />\n\n"; + stream << " <" << paramName << " value=\"" << (value ? "true" : "false" ) << "\" />\n\n"; } -void BoolUserConfigParam::read(const XMLNode* node) +void BoolUserConfigParam::readAsNode(const XMLNode* node) { const XMLNode* child = node->getNode( paramName ); if(child == NULL) return; @@ -139,28 +238,70 @@ void BoolUserConfigParam::read(const XMLNode* node) else std::cerr << "Unknown value for " << paramName << "; expected true or false\n"; } +void BoolUserConfigParam::readAsProperty(const XMLNode* node) +{ + std::string textValue = ""; + node->get( paramName, &textValue ); + + if(textValue == "true") + { + value = true; + } + else if(textValue == "false") + { + value = false; + } + else + std::cerr << "Unknown value for " << paramName << "; expected true or false\n"; +} + +std::string BoolUserConfigParam::toString() const +{ + return (value ? "true" : "false" ); +} + // --------------------------------------------------------------------------------------- -FloatUserConfigParam::FloatUserConfigParam(bool defaultValue, const char* paramName, const char* comment) +FloatUserConfigParam::FloatUserConfigParam(float defaultValue, const char* paramName, const char* comment) { this->value = defaultValue; this->paramName = paramName; all_params.push_back(this); if(comment != NULL) this->comment = comment; } +FloatUserConfigParam::FloatUserConfigParam(float defaultValue, const char* paramName, + GroupUserConfigParam* group, const char* comment) +{ + this->value = defaultValue; + this->paramName = paramName; + group->addChild(this); + if(comment != NULL) this->comment = comment; +} + void FloatUserConfigParam::write(std::ofstream& stream) const { if(comment.size() > 0) stream << " \n"; stream << " <" << paramName << " value=\"" << value << "\" />\n\n"; } -void FloatUserConfigParam::read(const XMLNode* node) +void FloatUserConfigParam::readAsNode(const XMLNode* node) { const XMLNode* child = node->getNode( paramName ); if(child == NULL) return; child->get( "value", &value ); } +void FloatUserConfigParam::readAsProperty(const XMLNode* node) +{ + node->get( paramName, &value ); +} + +std::string FloatUserConfigParam::toString() const +{ + char buffer[16]; + sprintf(buffer, "%f", value); + return buffer; +} // ===================================================================================== // ===================================================================================== @@ -302,6 +443,7 @@ void UserConfig::loadConfig(const std::string& filename) return; } + // ---- Read config file version int configFileVersion = CURRENT_CONFIG_VERSION; if(!root->get("version", &configFileVersion) < 1) { @@ -347,10 +489,11 @@ void UserConfig::loadConfig(const std::string& filename) } // if configFileVersionread(root); + all_params[i]->readAsNode(root); } delete root; diff --git a/src/config/user_config.hpp b/src/config/user_config.hpp index 8bd3c2cd5..7eab9c1ca 100644 --- a/src/config/user_config.hpp +++ b/src/config/user_config.hpp @@ -58,13 +58,28 @@ class XMLNode; */ class UserConfigParam { + friend class GroupUserConfigParam; protected: std::string paramName, comment; public: virtual ~UserConfigParam() {} virtual void write(std::ofstream& stream) const = 0; - virtual void read(const XMLNode* node) = 0; + virtual void readAsNode(const XMLNode* node) = 0; + virtual void readAsProperty(const XMLNode* node) = 0; + virtual std::string toString() const = 0; +}; +class GroupUserConfigParam : public UserConfigParam +{ + std::vector m_children; +public: + GroupUserConfigParam(const char* name, const char* comment=NULL); + void write(std::ofstream& stream) const; + void readAsNode(const XMLNode* node); + void readAsProperty(const XMLNode* node); + + void addChild(UserConfigParam* child); + std::string toString() const; }; class IntUserConfigParam : public UserConfigParam @@ -72,8 +87,13 @@ class IntUserConfigParam : public UserConfigParam int value; public: IntUserConfigParam(int defaultValue, const char* paramName, const char* comment = NULL); + IntUserConfigParam(int defaultValue, const char* paramName, GroupUserConfigParam* group, const char* comment = NULL); + void write(std::ofstream& stream) const; - void read(const XMLNode* node); + void readAsNode(const XMLNode* node); + void readAsProperty(const XMLNode* node); + + std::string toString() const; operator int() const { return value; } int& operator=(const int& v) { value = v; return value; } @@ -84,8 +104,14 @@ class StringUserConfigParam : public UserConfigParam std::string value; public: StringUserConfigParam(const char* defaultValue, const char* paramName, const char* comment = NULL); + StringUserConfigParam(const char* defaultValue, const char* paramName, GroupUserConfigParam* group, const char* comment = NULL); + + void write(std::ofstream& stream) const; - void read(const XMLNode* node); + void readAsNode(const XMLNode* node); + void readAsProperty(const XMLNode* node); + + std::string toString() const; operator std::string() const { return value; } std::string& operator=(const std::string& v) { value = v; return value; } @@ -97,8 +123,14 @@ class BoolUserConfigParam : public UserConfigParam bool value; public: BoolUserConfigParam(bool defaultValue, const char* paramName, const char* comment = NULL); + BoolUserConfigParam(bool defaultValue, const char* paramName, GroupUserConfigParam* group, const char* comment = NULL); + + void write(std::ofstream& stream) const; - void read(const XMLNode* node); + void readAsNode(const XMLNode* node); + void readAsProperty(const XMLNode* node); + + std::string toString() const; operator bool() const { return value; } bool& operator=(const bool& v) { value = v; return value; } @@ -108,9 +140,14 @@ class FloatUserConfigParam : public UserConfigParam { float value; public: - FloatUserConfigParam(bool defaultValue, const char* paramName, const char* comment = NULL); + FloatUserConfigParam(float defaultValue, const char* paramName, const char* comment = NULL); + FloatUserConfigParam(float defaultValue, const char* paramName, GroupUserConfigParam* group, const char* comment = NULL); + void write(std::ofstream& stream) const; - void read(const XMLNode* node); + void readAsNode(const XMLNode* node); + void readAsProperty(const XMLNode* node); + + std::string toString() const; operator float() const { return value; } float& operator=(const float& v) { value = v; return value; } @@ -135,45 +172,54 @@ namespace UserConfigParams { // ---- Audio + PARAM_PREFIX GroupUserConfigParam m_audio_group + PARAM_DEFAULT( GroupUserConfigParam("Audio", "Audio Settings") ); + PARAM_PREFIX BoolUserConfigParam m_sfx - PARAM_DEFAULT( BoolUserConfigParam(true, "sfx_on", "Whether sound effects are enabled or not (true or false)") ); + PARAM_DEFAULT( BoolUserConfigParam(true, "sfx_on", &m_audio_group, "Whether sound effects are enabled or not (true or false)") ); PARAM_PREFIX BoolUserConfigParam m_music - PARAM_DEFAULT( BoolUserConfigParam(true, "music_on", "Whether musics are enabled or not (true or false)") ); + PARAM_DEFAULT( BoolUserConfigParam(true, "music_on", &m_audio_group, "Whether musics are enabled or not (true or false)") ); PARAM_PREFIX FloatUserConfigParam m_sfx_volume - PARAM_DEFAULT( FloatUserConfigParam(1.0, "sfx_volume", "Volume for sound effects, see openal AL_GAIN for interpretation") ); + PARAM_DEFAULT( FloatUserConfigParam(1.0, "sfx_volume", &m_audio_group, "Volume for sound effects, see openal AL_GAIN for interpretation") ); PARAM_PREFIX FloatUserConfigParam m_music_volume - PARAM_DEFAULT( FloatUserConfigParam(0.7f, "music_volume", "music volume from 0 to 1") ); + PARAM_DEFAULT( FloatUserConfigParam(0.7f, "music_volume", &m_audio_group, "music volume from 0 to 1") ); // ---- Race setup + PARAM_PREFIX GroupUserConfigParam m_race_setup_group + PARAM_DEFAULT( GroupUserConfigParam("RaceSetup", "Race Setup Settings") ); + PARAM_PREFIX IntUserConfigParam m_num_karts - PARAM_DEFAULT( IntUserConfigParam(4, "numkarts", "Default number of karts. -1 means use all") ); + PARAM_DEFAULT( IntUserConfigParam(4, "numkarts", &m_race_setup_group, "Default number of karts. -1 means use all") ); PARAM_PREFIX IntUserConfigParam m_num_laps - PARAM_DEFAULT( IntUserConfigParam(4, "numlaps", "Default number of laps.") ); + PARAM_DEFAULT( IntUserConfigParam(4, "numlaps", &m_race_setup_group, "Default number of laps.") ); PARAM_PREFIX IntUserConfigParam m_difficulty - PARAM_DEFAULT( IntUserConfigParam(0, "difficulty", "Default race difficulty. 0=easy, 1=medium, 2=hard") ); + PARAM_DEFAULT( IntUserConfigParam(0, "difficulty", &m_race_setup_group, "Default race difficulty. 0=easy, 1=medium, 2=hard") ); // ---- Video + PARAM_PREFIX GroupUserConfigParam m_video_group + PARAM_DEFAULT( GroupUserConfigParam("Video", "Video Settings") ); + PARAM_PREFIX IntUserConfigParam m_width - PARAM_DEFAULT( IntUserConfigParam(800, "width", "screen resolution width") ); + PARAM_DEFAULT( IntUserConfigParam(800, "width", &m_video_group, "screen resolution width") ); PARAM_PREFIX IntUserConfigParam m_height - PARAM_DEFAULT( IntUserConfigParam(600, "height", "screen resolution height") ); + PARAM_DEFAULT( IntUserConfigParam(600, "height", &m_video_group, "screen resolution height") ); PARAM_PREFIX BoolUserConfigParam m_fullscreen - PARAM_DEFAULT( BoolUserConfigParam(false, "fullscreen") ); + PARAM_DEFAULT( BoolUserConfigParam(false, "fullscreen", &m_video_group) ); PARAM_PREFIX IntUserConfigParam m_prev_width - PARAM_DEFAULT( IntUserConfigParam(800, "prev_width") ); + PARAM_DEFAULT( IntUserConfigParam(800, "prev_width", &m_video_group) ); PARAM_PREFIX IntUserConfigParam m_prev_height - PARAM_DEFAULT( IntUserConfigParam(600, "prev_height") ); + PARAM_DEFAULT( IntUserConfigParam(600, "prev_height", &m_video_group) ); PARAM_PREFIX BoolUserConfigParam m_prev_windowed - PARAM_DEFAULT( BoolUserConfigParam(true, "prev_windowed") ); + PARAM_DEFAULT( BoolUserConfigParam(true, "prev_windowed", &m_video_group) ); // TODO : adapt to be more powerful with irrlicht PARAM_PREFIX BoolUserConfigParam m_graphical_effects - PARAM_DEFAULT( BoolUserConfigParam(true, "gfx") ); + PARAM_DEFAULT( BoolUserConfigParam(true, "gfx", &m_video_group) ); PARAM_PREFIX BoolUserConfigParam m_display_fps - PARAM_DEFAULT( BoolUserConfigParam(false, "show_fps", "Display frame per seconds") ); + PARAM_DEFAULT( BoolUserConfigParam(false, "show_fps", &m_video_group, "Display frame per seconds") ); PARAM_PREFIX IntUserConfigParam m_max_fps - PARAM_DEFAULT( IntUserConfigParam(120, "max_fps", "maximum fps, should be at least 60") ); + PARAM_DEFAULT( IntUserConfigParam(120, "max_fps", &m_video_group, "maximum fps, should be at least 60") ); // ---- Debug PARAM_PREFIX BoolUserConfigParam m_gamepad_debug PARAM_DEFAULT( BoolUserConfigParam(false, "gamepad_debug") );