axmol/cocos/base/CCConfiguration.cpp

368 lines
9.9 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
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.
****************************************************************************/
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "base/CCConfiguration.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCFileUtils.h"
NS_CC_BEGIN
extern const char* cocos2dVersion();
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)
, _maxDirLightInShader(1)
, _maxPointLightInShader(1)
, _maxSpotLightInShader(1)
2015-03-30 15:57:58 +08:00
, _highAnimate3DQuality(true)
{
}
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
#if COCOS2D_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()
{
}
std::string Configuration::getInfo() const
{
// 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)\n");
#endif
#if CC_ENABLE_GL_STATE_CACHE == 0
CCLOG("cocos2d: **** WARNING **** CC_ENABLE_GL_STATE_CACHE is disabled. To improve performance, enable it (from ccConfig.h)\n");
#endif
// Dump
Value forDump = Value(_valueDict);
return forDump.getDescription();
}
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)
{
s_sharedConfiguration = new (std::nothrow) Configuration();
2013-08-08 16:31:44 +08:00
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);
}
// FIXME: deprecated
2013-08-08 16:31:44 +08:00
Configuration* Configuration::sharedConfiguration()
{
return Configuration::getInstance();
}
// FIXME: deprecated
2013-08-08 16:31:44 +08:00
void Configuration::purgeConfiguration()
{
Configuration::destroyInstance();
}
bool Configuration::checkForGLExtension(const std::string &searchName) const
{
2014-01-08 09:09:43 +08:00
return (_glExtensions && strstr(_glExtensions, searchName.c_str() ) ) ? true : false;
}
//
// 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
}
int Configuration::getMaxSupportDirLightInShader() const
{
return _maxDirLightInShader;
}
int Configuration::getMaxSupportPointLightInShader() const
{
return _maxPointLightInShader;
}
int Configuration::getMaxSupportSpotLightInShader() const
{
return _maxSpotLightInShader;
}
2015-03-30 15:57:58 +08:00
bool Configuration::getHighAnimate3DQuality() const
2015-03-26 11:30:22 +08:00
{
2015-03-30 15:57:58 +08:00
return _highAnimate3DQuality;
2015-03-26 11:30:22 +08:00
}
//
// generic getters for properties
//
const Value& Configuration::getValue(const std::string& key, const Value& defaultValue) const
{
auto iter = _valueDict.find(key);
2014-01-08 09:09:43 +08:00
if (iter != _valueDict.cend())
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
//
void Configuration::loadConfigFile(const std::string& 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");
2014-01-08 09:09:43 +08:00
if (metadataIter != dict.cend() && 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");
2014-01-08 09:09:43 +08:00
if (formatIter != metadata.cend())
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.c_str());
return;
}
auto dataIter = dict.find("data");
2014-01-08 09:09:43 +08:00
if (dataIter == dict.cend() || 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.c_str());
return;
}
// Add all keys in the existing dictionary
const auto& dataMap = dataIter->second.asValueMap();
2014-01-08 09:09:43 +08:00
for (auto dataMapIter = dataMap.cbegin(); dataMapIter != dataMap.cend(); ++dataMapIter)
{
2014-01-08 09:09:43 +08:00
if (_valueDict.find(dataMapIter->first) == _valueDict.cend())
_valueDict[dataMapIter->first] = dataMapIter->second;
else
CCLOG("Key already present. Ignoring '%s'",dataMapIter->first.c_str());
}
//light info
std::string name = "cocos2d.x.3d.max_dir_light_in_shader";
if (_valueDict.find(name) != _valueDict.end())
_maxDirLightInShader = _valueDict[name].asInt();
else
_valueDict[name] = Value(_maxDirLightInShader);
name = "cocos2d.x.3d.max_point_light_in_shader";
if (_valueDict.find(name) != _valueDict.end())
_maxPointLightInShader = _valueDict[name].asInt();
else
_valueDict[name] = Value(_maxPointLightInShader);
name = "cocos2d.x.3d.max_spot_light_in_shader";
if (_valueDict.find(name) != _valueDict.end())
_maxSpotLightInShader = _valueDict[name].asInt();
else
_valueDict[name] = Value(_maxSpotLightInShader);
2015-03-26 11:30:22 +08:00
name = "cocos2d.x.3d.animate_3d_quality";
if (_valueDict.find(name) != _valueDict.end())
2015-03-30 15:57:58 +08:00
_highAnimate3DQuality = _valueDict[name].asInt();
2015-03-26 11:30:22 +08:00
else
2015-03-30 15:57:58 +08:00
_valueDict[name] = Value(_highAnimate3DQuality);
}
NS_CC_END