2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-06-08 14:11:48 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2010 Ricardo Quesada
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCConfiguration.h"
|
|
|
|
#include "ccMacros.h"
|
|
|
|
#include "ccConfig.h"
|
|
|
|
#include <string.h>
|
2013-05-23 04:00:34 +08:00
|
|
|
#include "cocoa/CCDictionary.h"
|
|
|
|
#include "cocoa/CCInteger.h"
|
|
|
|
#include "cocoa/CCBool.h"
|
|
|
|
#include "cocos2d.h"
|
2013-05-23 09:11:37 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-05-23 04:00:34 +08:00
|
|
|
|
2012-04-20 11:39:12 +08:00
|
|
|
CCConfiguration* CCConfiguration::s_gSharedConfiguration = NULL;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCConfiguration::CCConfiguration(void)
|
2012-04-20 11:39:12 +08:00
|
|
|
: m_nMaxTextureSize(0)
|
|
|
|
, m_nMaxModelviewStackDepth(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
, m_bSupportsPVRTC(false)
|
|
|
|
, m_bSupportsNPOT(false)
|
|
|
|
, m_bSupportsBGRA8888(false)
|
|
|
|
, m_bSupportsDiscardFramebuffer(false)
|
2013-03-03 10:32:09 +08:00
|
|
|
, m_bSupportsShareableVAO(false)
|
2012-04-19 14:35:52 +08:00
|
|
|
, m_nMaxSamplesAllowed(0)
|
2013-03-03 10:32:09 +08:00
|
|
|
, m_nMaxTextureUnits(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
, m_pGlExtensions(NULL)
|
2013-05-23 04:00:34 +08:00
|
|
|
, m_pDefaults(NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCConfiguration::init(void)
|
|
|
|
{
|
2013-05-23 04:00:34 +08:00
|
|
|
m_pDefaults = CCDictionary::create();
|
2013-05-23 08:03:00 +08:00
|
|
|
m_pDefaults->retain();
|
2013-05-23 04:00:34 +08:00
|
|
|
|
2013-05-31 07:29:36 +08:00
|
|
|
m_pDefaults->setObject( CCString::create( cocos2dVersion() ), "cocos2d.x.version");
|
2013-05-23 04:00:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
#if CC_ENABLE_PROFILERS
|
2013-05-31 07:29:36 +08:00
|
|
|
m_pDefaults->setObject( CCBool::create(true), "cocos2d.x.compiled_with_profiler");
|
2013-05-23 04:00:34 +08:00
|
|
|
#else
|
2013-05-31 07:29:36 +08:00
|
|
|
m_pDefaults->setObject( CCBool::create(false), "cocos2d.x.compiled_with_profiler");
|
2013-05-23 04:00:34 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE == 0
|
2013-05-31 07:29:36 +08:00
|
|
|
m_pDefaults->setObject( CCBool::create(false), "cocos2d.x.compiled_with_gl_state_cache");
|
2013-05-23 04:00:34 +08:00
|
|
|
#else
|
2013-05-31 07:29:36 +08:00
|
|
|
m_pDefaults->setObject( CCBool::create(true), "cocos2d.x.compiled_with_gl_state_cache");
|
2013-05-23 04:00:34 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCConfiguration::~CCConfiguration(void)
|
|
|
|
{
|
|
|
|
m_pDefaults->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCConfiguration::dumpInfo(void) const
|
|
|
|
{
|
|
|
|
// Dump
|
|
|
|
CCPrettyPrinter visitor(0);
|
|
|
|
m_pDefaults->acceptVisitor(visitor);
|
|
|
|
|
|
|
|
CCLOG("%s", visitor.getResult().c_str());
|
|
|
|
|
|
|
|
|
|
|
|
// And Dump some warnings as well
|
|
|
|
#if CC_ENABLE_PROFILERS
|
|
|
|
CCLOG("cocos2d: **** WARNING **** CC_ENABLE_PROFILERS is defined. Disable it when you finish profiling (from ccConfig.h)");
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CC_ENABLE_GL_STATE_CACHE == 0
|
|
|
|
CCLOG("");
|
|
|
|
CCLOG("cocos2d: **** WARNING **** CC_ENABLE_GL_STATE_CACHE is disabled. To improve performance, enable it (from ccConfig.h)");
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCConfiguration::gatherGPUInfo()
|
|
|
|
{
|
|
|
|
m_pDefaults->setObject( CCString::create( (const char*)glGetString(GL_VENDOR)), "gl.vendor");
|
|
|
|
m_pDefaults->setObject( CCString::create( (const char*)glGetString(GL_RENDERER)), "gl.renderer");
|
|
|
|
m_pDefaults->setObject( CCString::create( (const char*)glGetString(GL_VERSION)), "gl.version");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
m_pGlExtensions = (char *)glGetString(GL_EXTENSIONS);
|
|
|
|
|
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &m_nMaxTextureSize);
|
2013-05-23 04:00:34 +08:00
|
|
|
m_pDefaults->setObject( CCInteger::create((int)m_nMaxTextureSize), "gl.max_texture_size");
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &m_nMaxTextureUnits);
|
2013-05-23 04:00:34 +08:00
|
|
|
m_pDefaults->setObject( CCInteger::create((int)m_nMaxTextureUnits), "gl.max_texture_units");
|
|
|
|
|
2012-04-20 15:12:20 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2012-04-20 11:39:12 +08:00
|
|
|
glGetIntegerv(GL_MAX_SAMPLES_APPLE, &m_nMaxSamplesAllowed);
|
2013-05-23 04:00:34 +08:00
|
|
|
m_pDefaults->setObject( CCInteger::create((int)m_nMaxSamplesAllowed), "gl.max_samples_allowed");
|
2012-04-20 15:12:20 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
m_bSupportsPVRTC = checkForGLExtension("GL_IMG_texture_compression_pvrtc");
|
2013-05-23 04:00:34 +08:00
|
|
|
m_pDefaults->setObject( CCBool::create(m_bSupportsPVRTC), "gl.supports_PVRTC");
|
|
|
|
|
2012-04-20 11:39:12 +08:00
|
|
|
m_bSupportsNPOT = true;
|
2013-05-23 04:00:34 +08:00
|
|
|
m_pDefaults->setObject( CCBool::create(m_bSupportsNPOT), "gl.supports_NPOT");
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bSupportsBGRA8888 = checkForGLExtension("GL_IMG_texture_format_BGRA888");
|
2013-05-23 04:00:34 +08:00
|
|
|
m_pDefaults->setObject( CCBool::create(m_bSupportsBGRA8888), "gl.supports_BGRA8888");
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bSupportsDiscardFramebuffer = checkForGLExtension("GL_EXT_discard_framebuffer");
|
2013-05-23 04:00:34 +08:00
|
|
|
m_pDefaults->setObject( CCBool::create(m_bSupportsDiscardFramebuffer), "gl.supports_discard_framebuffer");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
m_bSupportsShareableVAO = checkForGLExtension("vertex_array_object");
|
2013-05-23 04:00:34 +08:00
|
|
|
m_pDefaults->setObject( CCBool::create(m_bSupportsShareableVAO), "gl.supports_vertex_array_object");
|
2012-04-20 11:39:12 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCConfiguration* CCConfiguration::sharedConfiguration(void)
|
|
|
|
{
|
2012-04-20 11:39:12 +08:00
|
|
|
if (! s_gSharedConfiguration)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-04-20 11:39:12 +08:00
|
|
|
s_gSharedConfiguration = new CCConfiguration();
|
|
|
|
s_gSharedConfiguration->init();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-04-20 11:39:12 +08:00
|
|
|
return s_gSharedConfiguration;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-05-23 12:11:53 +08:00
|
|
|
void CCConfiguration::purgeConfiguration(void)
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(s_gSharedConfiguration);
|
|
|
|
}
|
|
|
|
|
2013-05-23 04:00:34 +08:00
|
|
|
bool CCConfiguration::checkForGLExtension(const string &searchName) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
const char *kSearchName = searchName.c_str();
|
|
|
|
|
|
|
|
if (m_pGlExtensions &&
|
|
|
|
strstr(m_pGlExtensions, kSearchName))
|
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2013-05-23 04:00:34 +08:00
|
|
|
//
|
|
|
|
// getters for specific variables.
|
|
|
|
// Mantained for backward compatiblity reasons only.
|
|
|
|
//
|
|
|
|
int CCConfiguration::getMaxTextureSize(void) const
|
|
|
|
{
|
|
|
|
return m_nMaxTextureSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCConfiguration::getMaxModelviewStackDepth(void) const
|
|
|
|
{
|
|
|
|
return m_nMaxModelviewStackDepth;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCConfiguration::getMaxTextureUnits(void) const
|
|
|
|
{
|
|
|
|
return m_nMaxTextureUnits;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCConfiguration::supportsNPOT(void) const
|
|
|
|
{
|
|
|
|
return m_bSupportsNPOT;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCConfiguration::supportsPVRTC(void) const
|
|
|
|
{
|
|
|
|
return m_bSupportsPVRTC;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCConfiguration::supportsBGRA8888(void) const
|
|
|
|
{
|
|
|
|
return m_bSupportsBGRA8888;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCConfiguration::supportsDiscardFramebuffer(void) const
|
|
|
|
{
|
|
|
|
return m_bSupportsDiscardFramebuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCConfiguration::supportsShareableVAO(void) const
|
|
|
|
{
|
|
|
|
return m_bSupportsShareableVAO;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// generic getters for properties
|
|
|
|
//
|
2013-05-31 07:10:31 +08:00
|
|
|
const char *CCConfiguration::getCString( const char *key, const char *default_value ) const
|
2013-05-23 04:00:34 +08:00
|
|
|
{
|
|
|
|
CCObject *ret = m_pDefaults->objectForKey(key);
|
2013-05-23 08:03:00 +08:00
|
|
|
if( ret ) {
|
2013-05-23 04:00:34 +08:00
|
|
|
if( CCString *str=dynamic_cast<CCString*>(ret) )
|
|
|
|
return str->getCString();
|
|
|
|
|
2013-05-23 08:03:00 +08:00
|
|
|
CCAssert(false, "Key found, but from different type");
|
|
|
|
}
|
|
|
|
|
2013-05-23 04:00:34 +08:00
|
|
|
// XXX: Should it throw an exception ?
|
2013-05-31 07:10:31 +08:00
|
|
|
return default_value;
|
2013-05-23 04:00:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** returns the value of a given key as a boolean */
|
2013-05-31 07:10:31 +08:00
|
|
|
bool CCConfiguration::getBool( const char *key, bool default_value ) const
|
2013-05-23 04:00:34 +08:00
|
|
|
{
|
|
|
|
CCObject *ret = m_pDefaults->objectForKey(key);
|
2013-05-23 08:03:00 +08:00
|
|
|
if( ret ) {
|
2013-05-30 06:49:34 +08:00
|
|
|
if( CCBool *boolobj=dynamic_cast<CCBool*>(ret) )
|
|
|
|
return boolobj->getValue();
|
|
|
|
if( CCString *strobj=dynamic_cast<CCString*>(ret) )
|
|
|
|
return strobj->boolValue();
|
2013-05-23 08:03:00 +08:00
|
|
|
CCAssert(false, "Key found, but from different type");
|
|
|
|
}
|
2013-05-23 04:00:34 +08:00
|
|
|
|
|
|
|
// XXX: Should it throw an exception ?
|
2013-05-31 07:10:31 +08:00
|
|
|
return default_value;
|
2013-05-23 04:00:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** returns the value of a given key as a double */
|
2013-05-31 07:10:31 +08:00
|
|
|
double CCConfiguration::getNumber( const char *key, double default_value ) const
|
2013-05-23 04:00:34 +08:00
|
|
|
{
|
|
|
|
CCObject *ret = m_pDefaults->objectForKey(key);
|
|
|
|
if( ret ) {
|
|
|
|
if( CCDouble *obj=dynamic_cast<CCDouble*>(ret) )
|
|
|
|
return obj->getValue();
|
|
|
|
|
|
|
|
if( CCInteger *obj=dynamic_cast<CCInteger*>(ret) )
|
|
|
|
return obj->getValue();
|
2013-05-23 08:03:00 +08:00
|
|
|
|
2013-05-30 06:49:34 +08:00
|
|
|
if( CCString *strobj=dynamic_cast<CCString*>(ret) )
|
|
|
|
return strobj->doubleValue();
|
|
|
|
|
2013-05-23 08:03:00 +08:00
|
|
|
CCAssert(false, "Key found, but from different type");
|
2013-05-23 04:00:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: Should it throw an exception ?
|
2013-05-31 07:10:31 +08:00
|
|
|
return default_value;
|
2013-05-23 04:00:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCObject * CCConfiguration::getObject( const char *key ) const
|
|
|
|
{
|
|
|
|
return m_pDefaults->objectForKey(key);
|
|
|
|
}
|
|
|
|
|
2013-05-23 08:03:00 +08:00
|
|
|
//
|
|
|
|
// load file
|
|
|
|
//
|
|
|
|
void CCConfiguration::loadConfigFile( const char *filename )
|
2013-05-23 04:00:34 +08:00
|
|
|
{
|
2013-05-23 08:03:00 +08:00
|
|
|
CCDictionary *dict = CCDictionary::createWithContentsOfFile(filename);
|
|
|
|
CCAssert(dict, "cannot create dictionary");
|
|
|
|
|
2013-05-29 09:15:16 +08:00
|
|
|
// search for metadata
|
|
|
|
bool metadata_ok = false;
|
|
|
|
CCObject *metadata = dict->objectForKey("metadata");
|
|
|
|
if( metadata && dynamic_cast<CCDictionary*>(metadata) ) {
|
|
|
|
CCObject *format_o = static_cast<CCDictionary*>(metadata)->objectForKey("format");
|
|
|
|
|
|
|
|
// XXX: cocos2d-x returns CCStrings when importing from .plist. This bug will be addressed in cocos2d-x v3.x
|
|
|
|
if( format_o && dynamic_cast<CCString*>(format_o) ) {
|
|
|
|
int format = static_cast<CCString*>(format_o)->intValue();
|
|
|
|
|
|
|
|
// Support format: 1
|
|
|
|
if( format == 1 ) {
|
|
|
|
metadata_ok = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! metadata_ok ) {
|
|
|
|
CCLOG("Invalid config format for file: %s", filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCObject *data = dict->objectForKey("data");
|
|
|
|
if( !data || !dynamic_cast<CCDictionary*>(data) ) {
|
|
|
|
CCLOG("Expected 'data' dict, but not found. Config file: %s", filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-23 08:03:00 +08:00
|
|
|
// Add all keys in the existing dictionary
|
2013-05-29 09:15:16 +08:00
|
|
|
CCDictionary *data_dict = static_cast<CCDictionary*>(data);
|
2013-05-23 08:03:00 +08:00
|
|
|
CCDictElement* element;
|
2013-05-29 09:15:16 +08:00
|
|
|
CCDICT_FOREACH(data_dict, element)
|
2013-05-23 08:03:00 +08:00
|
|
|
{
|
|
|
|
if( ! m_pDefaults->objectForKey( element->getStrKey() ) )
|
|
|
|
m_pDefaults->setObject(element->getObject(), element->getStrKey() );
|
|
|
|
else
|
|
|
|
CCLOG("Key already present. Ignoring '%s'", element->getStrKey() );
|
|
|
|
}
|
2013-05-23 04:00:34 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|