axmol/cocos2dx/CCConfiguration.cpp

378 lines
9.9 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
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"
2013-08-08 16:31:44 +08:00
#include <string.h>
#include "ccMacros.h"
#include "ccConfig.h"
#include "cocoa/CCDictionary.h"
#include "cocoa/CCInteger.h"
#include "cocoa/CCBool.h"
#include "cocos2d.h"
#include "platform/CCFileUtils.h"
using namespace std;
NS_CC_BEGIN
2013-08-08 16:31:44 +08:00
Configuration* Configuration::s_sharedConfiguration = nullptr;
2013-08-08 16:31:44 +08:00
Configuration::Configuration()
: _maxTextureSize(0)
, _maxModelviewStackDepth(0)
, _supportsPVRTC(false)
, _supportsETC(false)
2013-08-06 11:19:45 +08:00
, _supportsS3TC(false)
, _supportsNPOT(false)
, _supportsBGRA8888(false)
, _supportsDiscardFramebuffer(false)
, _supportsShareableVAO(false)
, _maxSamplesAllowed(0)
, _maxTextureUnits(0)
2013-08-08 16:31:44 +08:00
, _glExtensions(nullptr)
, _valueDict(nullptr)
{
}
2013-08-08 16:31:44 +08:00
bool Configuration::init()
{
_valueDict = Dictionary::create();
_valueDict->retain();
2013-08-08 16:31:44 +08:00
_valueDict->setObject(String::create( cocos2dVersion() ), "cocos2d.x.version");
#if CC_ENABLE_PROFILERS
2013-08-08 16:31:44 +08:00
_valueDict->setObject(Bool::create(true), "cocos2d.x.compiled_with_profiler");
#else
2013-08-08 16:31:44 +08:00
_valueDict->setObject(Bool::create(false), "cocos2d.x.compiled_with_profiler");
#endif
#if CC_ENABLE_GL_STATE_CACHE == 0
2013-08-08 16:31:44 +08:00
_valueDict->setObject(Bool::create(false), "cocos2d.x.compiled_with_gl_state_cache");
#else
2013-08-08 16:31:44 +08:00
_valueDict->setObject(Bool::create(true), "cocos2d.x.compiled_with_gl_state_cache");
#endif
return true;
}
2013-08-08 16:31:44 +08:00
Configuration::~Configuration()
{
_valueDict->release();
}
2013-08-08 16:31:44 +08:00
void Configuration::dumpInfo() const
{
// Dump
PrettyPrinter visitor(0);
_valueDict->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 Configuration::gatherGPUInfo()
{
2013-08-08 16:31:44 +08:00
_valueDict->setObject(String::create((const char*)glGetString(GL_VENDOR)), "gl.vendor");
_valueDict->setObject(String::create((const char*)glGetString(GL_RENDERER)), "gl.renderer");
_valueDict->setObject(String::create((const char*)glGetString(GL_VERSION)), "gl.version");
_glExtensions = (char *)glGetString(GL_EXTENSIONS);
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_maxTextureSize);
2013-08-08 16:31:44 +08:00
_valueDict->setObject(Integer::create((int)_maxTextureSize), "gl.max_texture_size");
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_maxTextureUnits);
2013-08-08 16:31:44 +08:00
_valueDict->setObject(Integer::create((int)_maxTextureUnits), "gl.max_texture_units");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
glGetIntegerv(GL_MAX_SAMPLES_APPLE, &_maxSamplesAllowed);
2013-08-08 16:31:44 +08:00
_valueDict->setObject(Integer::create((int)_maxSamplesAllowed), "gl.max_samples_allowed");
#endif
_supportsETC = checkForGLExtension("GL_OES_compressed_ETC1_RGB8_texture");
2013-08-08 16:31:44 +08:00
_valueDict->setObject(Bool::create(_supportsETC), "gl.supports_ETC");
2013-08-06 11:19:45 +08:00
_supportsS3TC = checkForGLExtension("GL_EXT_texture_compression_s3tc");
_valueDict->setObject( Bool::create(_supportsS3TC), "gl.supports_S3TC");
_supportsPVRTC = checkForGLExtension("GL_IMG_texture_compression_pvrtc");
2013-08-08 16:31:44 +08:00
_valueDict->setObject(Bool::create(_supportsPVRTC), "gl.supports_PVRTC");
_supportsNPOT = true;
2013-08-08 16:31:44 +08:00
_valueDict->setObject(Bool::create(_supportsNPOT), "gl.supports_NPOT");
_supportsBGRA8888 = checkForGLExtension("GL_IMG_texture_format_BGRA888");
2013-08-08 16:31:44 +08:00
_valueDict->setObject(Bool::create(_supportsBGRA8888), "gl.supports_BGRA8888");
_supportsDiscardFramebuffer = checkForGLExtension("GL_EXT_discard_framebuffer");
2013-08-08 16:31:44 +08:00
_valueDict->setObject(Bool::create(_supportsDiscardFramebuffer), "gl.supports_discard_framebuffer");
_supportsShareableVAO = checkForGLExtension("vertex_array_object");
2013-08-08 16:31:44 +08:00
_valueDict->setObject(Bool::create(_supportsShareableVAO), "gl.supports_vertex_array_object");
CHECK_GL_ERROR_DEBUG();
}
Configuration* Configuration::getInstance()
{
2013-08-08 16:31:44 +08:00
if (! s_sharedConfiguration)
{
2013-08-08 16:31:44 +08:00
s_sharedConfiguration = new Configuration();
s_sharedConfiguration->init();
}
2013-08-08 16:31:44 +08:00
return s_sharedConfiguration;
}
void Configuration::destroyInstance()
{
2013-08-08 16:31:44 +08:00
CC_SAFE_RELEASE_NULL(s_sharedConfiguration);
}
// XXX: deprecated
2013-08-08 16:31:44 +08:00
Configuration* Configuration::sharedConfiguration()
{
return Configuration::getInstance();
}
// XXX: deprecated
2013-08-08 16:31:44 +08:00
void Configuration::purgeConfiguration()
{
Configuration::destroyInstance();
}
bool Configuration::checkForGLExtension(const string &searchName) const
{
2013-08-08 16:31:44 +08:00
bool ret = false;
const char *kSearchName = searchName.c_str();
if (_glExtensions &&
strstr(_glExtensions, kSearchName))
{
2013-08-08 16:31:44 +08:00
ret = true;
}
2013-08-08 16:31:44 +08:00
return ret;
}
//
// getters for specific variables.
// Mantained for backward compatiblity reasons only.
//
2013-08-08 16:31:44 +08:00
int Configuration::getMaxTextureSize() const
{
return _maxTextureSize;
}
2013-08-08 16:31:44 +08:00
int Configuration::getMaxModelviewStackDepth() const
{
return _maxModelviewStackDepth;
}
2013-08-08 16:31:44 +08:00
int Configuration::getMaxTextureUnits() const
{
return _maxTextureUnits;
}
2013-08-08 16:31:44 +08:00
bool Configuration::supportsNPOT() const
{
return _supportsNPOT;
}
2013-08-08 16:31:44 +08:00
bool Configuration::supportsPVRTC() const
{
return _supportsPVRTC;
}
bool Configuration::supportsETC() const
{
//GL_ETC1_RGB8_OES is not defined in old opengl version
#ifdef GL_ETC1_RGB8_OES
return _supportsETC;
#else
return false;
#endif
}
2013-08-06 11:19:45 +08:00
bool Configuration::supportsS3TC() const
{
return _supportsS3TC;
}
2013-08-08 16:31:44 +08:00
bool Configuration::supportsBGRA8888() const
{
return _supportsBGRA8888;
}
2013-08-08 16:31:44 +08:00
bool Configuration::supportsDiscardFramebuffer() const
{
return _supportsDiscardFramebuffer;
}
2013-08-08 16:31:44 +08:00
bool Configuration::supportsShareableVAO() const
{
return _supportsShareableVAO;
}
//
// generic getters for properties
//
2013-08-08 16:31:44 +08:00
const char *Configuration::getCString(const char *key, const char *defaultValue) const
{
Object *ret = _valueDict->objectForKey(key);
2013-08-08 16:31:44 +08:00
if (ret)
{
if (String *str=dynamic_cast<String*>(ret))
return str->getCString();
CCASSERT(false, "Key found, but from different type");
}
// XXX: Should it throw an exception ?
2013-08-08 16:31:44 +08:00
return defaultValue;
}
/** returns the value of a given key as a boolean */
2013-08-08 16:31:44 +08:00
bool Configuration::getBool(const char *key, bool defaultValue) const
{
Object *ret = _valueDict->objectForKey(key);
2013-08-08 16:31:44 +08:00
if (ret)
{
if (Bool *boolobj=dynamic_cast<Bool*>(ret))
return boolobj->getValue();
2013-08-08 16:31:44 +08:00
if (String *strobj=dynamic_cast<String*>(ret))
return strobj->boolValue();
CCASSERT(false, "Key found, but from different type");
}
// XXX: Should it throw an exception ?
2013-08-08 16:31:44 +08:00
return defaultValue;
}
/** returns the value of a given key as a double */
2013-08-08 16:31:44 +08:00
double Configuration::getNumber( const char *key, double defaultValue ) const
{
Object *ret = _valueDict->objectForKey(key);
2013-08-08 16:31:44 +08:00
if( ret )
{
if (Double *obj=dynamic_cast<Double*>(ret))
return obj->getValue();
2013-08-08 16:31:44 +08:00
if (Integer *obj=dynamic_cast<Integer*>(ret))
return obj->getValue();
2013-08-08 16:31:44 +08:00
if (String *strobj=dynamic_cast<String*>(ret))
return strobj->doubleValue();
CCASSERT(false, "Key found, but from different type");
}
// XXX: Should it throw an exception ?
2013-08-08 16:31:44 +08:00
return defaultValue;
}
2013-08-08 16:31:44 +08:00
Object * Configuration::getObject(const char *key) const
{
return _valueDict->objectForKey(key);
}
2013-08-08 16:31:44 +08:00
void Configuration::setObject(const char *key, Object *value)
{
_valueDict->setObject(value, key);
}
//
// load file
//
2013-08-08 16:31:44 +08:00
void Configuration::loadConfigFile(const char *filename)
{
Dictionary *dict = Dictionary::createWithContentsOfFile(filename);
CCASSERT(dict, "cannot create dictionary");
// search for metadata
2013-08-08 16:31:44 +08:00
bool validMetadata = false;
Object *metadata = dict->objectForKey("metadata");
2013-08-08 16:31:44 +08:00
if (metadata && dynamic_cast<Dictionary*>(metadata))
{
Object *format_o = static_cast<Dictionary*>(metadata)->objectForKey("format");
// XXX: cocos2d-x returns Strings when importing from .plist. This bug will be addressed in cocos2d-x v3.x
2013-08-08 16:31:44 +08:00
if (format_o && dynamic_cast<String*>(format_o))
{
int format = static_cast<String*>(format_o)->intValue();
// Support format: 1
2013-08-08 16:31:44 +08:00
if (format == 1)
{
validMetadata = true;
}
}
}
2013-08-08 16:31:44 +08:00
if (! validMetadata)
{
CCLOG("Invalid config format for file: %s", filename);
return;
}
Object *data = dict->objectForKey("data");
2013-08-08 16:31:44 +08:00
if (!data || !dynamic_cast<Dictionary*>(data))
{
CCLOG("Expected 'data' dict, but not found. Config file: %s", filename);
return;
}
// Add all keys in the existing dictionary
Dictionary *data_dict = static_cast<Dictionary*>(data);
DictElement* element;
CCDICT_FOREACH(data_dict, element)
{
2013-08-08 16:31:44 +08:00
if(! _valueDict->objectForKey( element->getStrKey() ))
_valueDict->setObject(element->getObject(), element->getStrKey());
else
2013-08-08 16:31:44 +08:00
CCLOG("Key already present. Ignoring '%s'", element->getStrKey());
}
}
NS_CC_END