Merge pull request #1 from cocos2d/develop

Develop
This commit is contained in:
Rene 2013-05-31 16:27:23 -07:00
commit 61ec83b742
4 changed files with 65 additions and 21 deletions

View File

@ -60,19 +60,19 @@ bool CCConfiguration::init(void)
m_pDefaults = CCDictionary::create(); m_pDefaults = CCDictionary::create();
m_pDefaults->retain(); m_pDefaults->retain();
m_pDefaults->setObject( CCString::create( cocos2dVersion() ), "cocos2d.version"); m_pDefaults->setObject( CCString::create( cocos2dVersion() ), "cocos2d.x.version");
#if CC_ENABLE_PROFILERS #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 #else
m_pDefaults->setObject( CCBool::create(false), "cocos2d.compiled_with_profiler"); m_pDefaults->setObject( CCBool::create(false), "cocos2d.x.compiled_with_profiler");
#endif #endif
#if CC_ENABLE_GL_STATE_CACHE == 0 #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 #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 #endif
return true; return true;
@ -220,7 +220,7 @@ bool CCConfiguration::supportsShareableVAO(void) const
// //
// generic getters for properties // 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); CCObject *ret = m_pDefaults->objectForKey(key);
if( ret ) { if( ret ) {
@ -231,12 +231,11 @@ const char *CCConfiguration::getCString( const char *key ) const
} }
// XXX: Should it throw an exception ? // XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key ); return default_value;
return NULL;
} }
/** returns the value of a given key as a boolean */ /** 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); CCObject *ret = m_pDefaults->objectForKey(key);
if( ret ) { if( ret ) {
@ -248,12 +247,11 @@ bool CCConfiguration::getBool( const char *key ) const
} }
// XXX: Should it throw an exception ? // XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key ); return default_value;
return false;
} }
/** returns the value of a given key as a double */ /** 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); CCObject *ret = m_pDefaults->objectForKey(key);
if( ret ) { if( ret ) {
@ -270,8 +268,7 @@ double CCConfiguration::getNumber( const char *key ) const
} }
// XXX: Should it throw an exception ? // XXX: Should it throw an exception ?
CCLOG("Key not found: '%s'", key ); return default_value;
return 0.0;
} }
CCObject * CCConfiguration::getObject( const char *key ) const CCObject * CCConfiguration::getObject( const char *key ) const

View File

@ -106,14 +106,17 @@ public:
bool init(void); bool init(void);
/** returns the value of a given key as a string */ /** returns the value of a given key as a string.
const char* getCString( const char *key ) const; 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 */ /** returns the value of a given key as a boolean.
bool getBool( const char *key ) const; 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 */ /** returns the value of a given key as a double.
double getNumber( const char *key ) const; 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 */ /** returns the value of a given key as a double */
CCObject * getObject( const char *key ) const; CCObject * getObject( const char *key ) const;

View File

@ -6,11 +6,13 @@
TESTLAYER_CREATE_FUNC(ConfigurationLoadConfig); TESTLAYER_CREATE_FUNC(ConfigurationLoadConfig);
TESTLAYER_CREATE_FUNC(ConfigurationQuery); TESTLAYER_CREATE_FUNC(ConfigurationQuery);
TESTLAYER_CREATE_FUNC(ConfigurationInvalid); TESTLAYER_CREATE_FUNC(ConfigurationInvalid);
TESTLAYER_CREATE_FUNC(ConfigurationDefault);
static NEWTESTFUNC createFunctions[] = { static NEWTESTFUNC createFunctions[] = {
CF(ConfigurationLoadConfig), CF(ConfigurationLoadConfig),
CF(ConfigurationQuery), CF(ConfigurationQuery),
CF(ConfigurationInvalid) CF(ConfigurationInvalid),
CF(ConfigurationDefault)
}; };
static int sceneIdx=-1; static int sceneIdx=-1;
@ -186,3 +188,37 @@ std::string ConfigurationInvalid::subtitle()
{ {
return "Loading an invalid config file"; 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";
}

View File

@ -52,4 +52,12 @@ public:
virtual std::string subtitle(); virtual std::string subtitle();
}; };
class ConfigurationDefault : public ConfigurationBase
{
public:
virtual void onEnter();
virtual std::string subtitle();
};
#endif // __CONFIGURATIONTEST_H__ #endif // __CONFIGURATIONTEST_H__