diff --git a/cocos2dx/CCConfiguration.cpp b/cocos2dx/CCConfiguration.cpp index af9ff6c13c..a102faea19 100644 --- a/cocos2dx/CCConfiguration.cpp +++ b/cocos2dx/CCConfiguration.cpp @@ -60,19 +60,19 @@ bool CCConfiguration::init(void) m_pDefaults = CCDictionary::create(); m_pDefaults->retain(); - m_pDefaults->setObject( CCString::create( cocos2dVersion() ), "cocos2d.version"); + m_pDefaults->setObject( CCString::create( cocos2dVersion() ), "cocos2d.x.version"); #if CC_ENABLE_PROFILERS - m_pDefaults->setObject( CCBool::create(true), "cocos2d.compiled_with_profiler"); + m_pDefaults->setObject( CCBool::create(true), "cocos2d.x.compiled_with_profiler"); #else - m_pDefaults->setObject( CCBool::create(false), "cocos2d.compiled_with_profiler"); + m_pDefaults->setObject( CCBool::create(false), "cocos2d.x.compiled_with_profiler"); #endif #if CC_ENABLE_GL_STATE_CACHE == 0 - m_pDefaults->setObject( CCBool::create(false), "cocos2d.compiled_with_gl_state_cache"); + m_pDefaults->setObject( CCBool::create(false), "cocos2d.x.compiled_with_gl_state_cache"); #else - m_pDefaults->setObject( CCBool::create(true), "cocos2d.compiled_with_gl_state_cache"); + m_pDefaults->setObject( CCBool::create(true), "cocos2d.x.compiled_with_gl_state_cache"); #endif return true; @@ -220,7 +220,7 @@ bool CCConfiguration::supportsShareableVAO(void) const // // generic getters for properties // -const char *CCConfiguration::getCString( const char *key ) const +const char *CCConfiguration::getCString( const char *key, const char *default_value ) const { CCObject *ret = m_pDefaults->objectForKey(key); if( ret ) { @@ -231,12 +231,11 @@ const char *CCConfiguration::getCString( const char *key ) const } // XXX: Should it throw an exception ? - CCLOG("Key not found: '%s'", key ); - return NULL; + return default_value; } /** returns the value of a given key as a boolean */ -bool CCConfiguration::getBool( const char *key ) const +bool CCConfiguration::getBool( const char *key, bool default_value ) const { CCObject *ret = m_pDefaults->objectForKey(key); if( ret ) { @@ -248,12 +247,11 @@ bool CCConfiguration::getBool( const char *key ) const } // XXX: Should it throw an exception ? - CCLOG("Key not found: '%s'", key ); - return false; + return default_value; } /** returns the value of a given key as a double */ -double CCConfiguration::getNumber( const char *key ) const +double CCConfiguration::getNumber( const char *key, double default_value ) const { CCObject *ret = m_pDefaults->objectForKey(key); if( ret ) { @@ -270,8 +268,7 @@ double CCConfiguration::getNumber( const char *key ) const } // XXX: Should it throw an exception ? - CCLOG("Key not found: '%s'", key ); - return 0.0; + return default_value; } CCObject * CCConfiguration::getObject( const char *key ) const diff --git a/cocos2dx/CCConfiguration.h b/cocos2dx/CCConfiguration.h index 5b8b0bbabb..8ac2a70ac7 100644 --- a/cocos2dx/CCConfiguration.h +++ b/cocos2dx/CCConfiguration.h @@ -106,14 +106,17 @@ public: bool init(void); - /** returns the value of a given key as a string */ - const char* getCString( const char *key ) const; + /** returns the value of a given key as a string. + If the key is not found, it will return the default value */ + const char* getCString( const char *key, const char *default_value=NULL ) const; - /** returns the value of a given key as a boolean */ - bool getBool( const char *key ) const; + /** returns the value of a given key as a boolean. + If the key is not found, it will return the default value */ + bool getBool( const char *key, bool default_value=false ) const; - /** returns the value of a given key as a double */ - double getNumber( const char *key ) const; + /** returns the value of a given key as a double. + If the key is not found, it will return the default value */ + double getNumber( const char *key, double default_value=0.0 ) const; /** returns the value of a given key as a double */ CCObject * getObject( const char *key ) const; diff --git a/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.cpp b/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.cpp index 80ab32d74a..0e06ffac6f 100644 --- a/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.cpp @@ -6,11 +6,13 @@ TESTLAYER_CREATE_FUNC(ConfigurationLoadConfig); TESTLAYER_CREATE_FUNC(ConfigurationQuery); TESTLAYER_CREATE_FUNC(ConfigurationInvalid); +TESTLAYER_CREATE_FUNC(ConfigurationDefault); static NEWTESTFUNC createFunctions[] = { CF(ConfigurationLoadConfig), CF(ConfigurationQuery), - CF(ConfigurationInvalid) + CF(ConfigurationInvalid), + CF(ConfigurationDefault) }; static int sceneIdx=-1; @@ -186,3 +188,37 @@ std::string ConfigurationInvalid::subtitle() { return "Loading an invalid config file"; } + +//------------------------------------------------------------------ +// +// ConfigurationDefault +// +//------------------------------------------------------------------ +void ConfigurationDefault::onEnter() +{ + ConfigurationBase::onEnter(); + + const char *c_value = CCConfiguration::sharedConfiguration()->getCString("invalid.key", "no key"); + if( strcmp(c_value, "no key") != 0 ) + CCLOG("1. Test failed!"); + else + CCLOG("1. Test OK!"); + + bool b_value = CCConfiguration::sharedConfiguration()->getBool("invalid.key", true); + if( ! b_value ) + CCLOG("2. Test failed!"); + else + CCLOG("2. Test OK!"); + + double d_value = CCConfiguration::sharedConfiguration()->getNumber("invalid.key", 42.42); + if( d_value != 42.42 ) + CCLOG("3. Test failed!"); + else + CCLOG("3. Test OK!"); + +} + +std::string ConfigurationDefault::subtitle() +{ + return "Tests defaults values"; +} diff --git a/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.h b/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.h index 7f2cbc1e93..b5da1be65a 100644 --- a/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.h +++ b/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.h @@ -52,4 +52,12 @@ public: virtual std::string subtitle(); }; +class ConfigurationDefault : public ConfigurationBase +{ +public: + virtual void onEnter(); + virtual std::string subtitle(); +}; + + #endif // __CONFIGURATIONTEST_H__