From 3383fc5f046a417adc9ad442a6c698a9a021e0a1 Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Thu, 30 May 2013 16:10:31 -0700 Subject: [PATCH 1/2] CCConfiguration supports default values in the query Adds tests for the new API as well --- cocos2dx/CCConfiguration.cpp | 15 +++----- cocos2dx/CCConfiguration.h | 15 +++++--- .../ConfigurationTest/ConfigurationTest.cpp | 38 ++++++++++++++++++- .../ConfigurationTest/ConfigurationTest.h | 8 ++++ 4 files changed, 60 insertions(+), 16 deletions(-) diff --git a/cocos2dx/CCConfiguration.cpp b/cocos2dx/CCConfiguration.cpp index af9ff6c13c..96c7019a75 100644 --- a/cocos2dx/CCConfiguration.cpp +++ b/cocos2dx/CCConfiguration.cpp @@ -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__ From 3cbb6c4abb8247498d120bb0cb911b99b03a5a76 Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Thu, 30 May 2013 16:29:36 -0700 Subject: [PATCH 2/2] uses the cocos2d.x namespace for "cocos2d x" configuration values --- cocos2dx/CCConfiguration.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cocos2dx/CCConfiguration.cpp b/cocos2dx/CCConfiguration.cpp index 96c7019a75..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;