axmol/cocos/2d/CCConfiguration.cpp

339 lines
8.7 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 "CCDictionary.h"
#include "CCInteger.h"
#include "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)
, _supportsETC1(false)
2013-08-06 11:19:45 +08:00
, _supportsS3TC(false)
, _supportsATITC(false)
, _supportsNPOT(false)
, _supportsBGRA8888(false)
, _supportsDiscardFramebuffer(false)
, _supportsShareableVAO(false)
, _maxSamplesAllowed(0)
, _maxTextureUnits(0)
2013-08-08 16:31:44 +08:00
, _glExtensions(nullptr)
{
}
2013-08-08 16:31:44 +08:00
bool Configuration::init()
{
_valueDict["cocos2d.x.version"] = Value(cocos2dVersion());
#if CC_ENABLE_PROFILERS
_valueDict["cocos2d.x.compiled_with_profiler"] = Value(true);
#else
_valueDict["cocos2d.x.compiled_with_profiler"] = Value(false);
#endif
#if CC_ENABLE_GL_STATE_CACHE == 0
_valueDict["cocos2d.x.compiled_with_gl_state_cache"] = Value(false);
#else
_valueDict["cocos2d.x.compiled_with_gl_state_cache"] = Value(true);
#endif
#ifdef DEBUG
_valueDict["cocos2d.x.build_type"] = Value("DEBUG");
#else
_valueDict["cocos2d.x.build_type"] = Value("RELEASE");
#endif
return true;
}
2013-08-08 16:31:44 +08:00
Configuration::~Configuration()
{
}
2013-08-08 16:31:44 +08:00
void Configuration::dumpInfo() const
{
// Dump
Value forDump = Value(_valueDict);
log("%s", forDump.getDescription().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()
{
_valueDict["gl.vendor"] = Value((const char*)glGetString(GL_VENDOR));
_valueDict["gl.renderer"] = Value((const char*)glGetString(GL_RENDERER));
_valueDict["gl.version"] = Value((const char*)glGetString(GL_VERSION));
_glExtensions = (char *)glGetString(GL_EXTENSIONS);
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_maxTextureSize);
_valueDict["gl.max_texture_size"] = Value((int)_maxTextureSize);
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_maxTextureUnits);
_valueDict["gl.max_texture_units"] = Value((int)_maxTextureUnits);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
glGetIntegerv(GL_MAX_SAMPLES_APPLE, &_maxSamplesAllowed);
_valueDict["gl.max_samples_allowed"] = Value((int)_maxSamplesAllowed);
#endif
_supportsETC1 = checkForGLExtension("GL_OES_compressed_ETC1_RGB8_texture");
_valueDict["gl.supports_ETC1"] = Value(_supportsETC1);
2013-08-06 11:19:45 +08:00
_supportsS3TC = checkForGLExtension("GL_EXT_texture_compression_s3tc");
_valueDict["gl.supports_S3TC"] = Value(_supportsS3TC);
_supportsATITC = checkForGLExtension("GL_AMD_compressed_ATC_texture");
_valueDict["gl.supports_ATITC"] = Value(_supportsATITC);
2013-08-06 11:19:45 +08:00
_supportsPVRTC = checkForGLExtension("GL_IMG_texture_compression_pvrtc");
_valueDict["gl.supports_PVRTC"] = Value(_supportsPVRTC);
_supportsNPOT = true;
_valueDict["gl.supports_NPOT"] = Value(_supportsNPOT);
_supportsBGRA8888 = checkForGLExtension("GL_IMG_texture_format_BGRA888");
_valueDict["gl.supports_BGRA8888"] = Value(_supportsBGRA8888);
_supportsDiscardFramebuffer = checkForGLExtension("GL_EXT_discard_framebuffer");
_valueDict["gl.supports_discard_framebuffer"] = Value(_supportsDiscardFramebuffer);
_supportsShareableVAO = checkForGLExtension("vertex_array_object");
_valueDict["gl.supports_vertex_array_object"] = Value(_supportsShareableVAO);
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 _supportsETC1;
#else
return false;
#endif
}
2013-08-06 11:19:45 +08:00
bool Configuration::supportsS3TC() const
{
return _supportsS3TC;
}
bool Configuration::supportsATITC() const
{
return _supportsATITC;
}
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
{
#if CC_TEXTURE_ATLAS_USE_VAO
return _supportsShareableVAO;
#else
return false;
#endif
}
//
// generic getters for properties
//
const Value& Configuration::getValue(const std::string& key, const Value& defaultValue) const
{
auto iter = _valueDict.find(key);
if (iter != _valueDict.end())
return _valueDict.at(key);
2013-08-08 16:31:44 +08:00
return defaultValue;
}
void Configuration::setValue(const std::string& key, const Value& value)
{
_valueDict[key] = value;
}
//
// load file
//
2013-08-08 16:31:44 +08:00
void Configuration::loadConfigFile(const char *filename)
{
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(filename);
CCASSERT(!dict.empty(), "cannot create dictionary");
// search for metadata
2013-08-08 16:31:44 +08:00
bool validMetadata = false;
auto metadataIter = dict.find("metadata");
if (metadataIter != dict.end() && metadataIter->second.getType() == Value::Type::MAP)
2013-08-08 16:31:44 +08:00
{
const auto& metadata = metadataIter->second.asValueMap();
auto formatIter = metadata.find("format");
if (formatIter != metadata.end())
2013-08-08 16:31:44 +08:00
{
int format = formatIter->second.asInt();
// 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;
}
auto dataIter = dict.find("data");
if (dataIter == dict.end() || dataIter->second.getType() != Value::Type::MAP)
2013-08-08 16:31:44 +08:00
{
CCLOG("Expected 'data' dict, but not found. Config file: %s", filename);
return;
}
// Add all keys in the existing dictionary
const auto& dataMap = dataIter->second.asValueMap();
for (auto dataMapIter = dataMap.begin(); dataMapIter != dataMap.end(); ++dataMapIter)
{
if (_valueDict.find(dataMapIter->first) == _valueDict.end())
_valueDict[dataMapIter->first] = dataMapIter->second;
else
CCLOG("Key already present. Ignoring '%s'",dataMapIter->first.c_str());
}
}
NS_CC_END