CCConfiguration.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. cc.ConfigurationType = {ConfigurationError:0, ConfigurationString:1, ConfigurationInt:2, ConfigurationDouble:3, ConfigurationBoolean:4};
  23. /**
  24. * cc.Configuration contains some openGL variables
  25. * @class
  26. * @extends cc.Class
  27. */
  28. cc.Configuration = cc.Class.extend(/** @lends cc.Configuration# */{
  29. _maxTextureSize:0,
  30. _maxModelviewStackDepth:0,
  31. _supportsPVRTC:false,
  32. _supportsNPOT:false,
  33. _supportsBGRA8888:false,
  34. _supportsDiscardFramebuffer:false,
  35. _supportsShareableVAO:false,
  36. _maxSamplesAllowed:0,
  37. _maxTextureUnits:0,
  38. _GlExtensions:"",
  39. _valueDict:null,
  40. ctor: function () {
  41. this._maxTextureSize = 0;
  42. this._maxModelviewStackDepth = 0;
  43. this._supportsPVRTC = false;
  44. this._supportsNPOT = false;
  45. this._supportsBGRA8888 = false;
  46. this._supportsDiscardFramebuffer = false;
  47. this._supportsShareableVAO = false;
  48. this._maxSamplesAllowed = 0;
  49. this._maxTextureUnits = 0;
  50. this._GlExtensions = "";
  51. this._valueDict = {};
  52. },
  53. /**
  54. * OpenGL Max texture size.
  55. * @return {Number}
  56. */
  57. getMaxTextureSize:function () {
  58. return this._maxTextureSize;
  59. },
  60. /**
  61. * OpenGL Max Modelview Stack Depth.
  62. * @return {Number}
  63. */
  64. getMaxModelviewStackDepth:function () {
  65. return this._maxModelviewStackDepth;
  66. },
  67. /**
  68. * returns the maximum texture units
  69. * @return {Number}
  70. */
  71. getMaxTextureUnits:function () {
  72. return this._maxTextureUnits;
  73. },
  74. /**
  75. * Whether or not the GPU supports NPOT (Non Power Of Two) textures.
  76. * OpenGL ES 2.0 already supports NPOT (iOS).
  77. * @return {Boolean}
  78. */
  79. supportsNPOT:function () {
  80. return this._supportsNPOT;
  81. },
  82. /**
  83. * Whether or not PVR Texture Compressed is supported
  84. * @return {Boolean}
  85. */
  86. supportsPVRTC:function () {
  87. return this._supportsPVRTC;
  88. },
  89. /**
  90. * Whether or not BGRA8888 textures are supported.
  91. * @return {Boolean}
  92. */
  93. supportsBGRA8888:function () {
  94. return this._supportsBGRA8888;
  95. },
  96. /**
  97. * Whether or not glDiscardFramebufferEXT is supported
  98. * @return {Boolean}
  99. */
  100. supportsDiscardFramebuffer:function () {
  101. return this._supportsDiscardFramebuffer;
  102. },
  103. /**
  104. * Whether or not shareable VAOs are supported.
  105. * @return {Boolean}
  106. */
  107. supportsShareableVAO:function () {
  108. return this._supportsShareableVAO;
  109. },
  110. /**
  111. * returns whether or not an OpenGL is supported
  112. * @param {String} searchName
  113. */
  114. checkForGLExtension:function (searchName) {
  115. return this._GlExtensions.indexOf(searchName) > -1;
  116. },
  117. init:function () {
  118. var locValueDict = this._valueDict;
  119. locValueDict["cocos2d.x.version"] = cc.ENGINE_VERSION;
  120. locValueDict["cocos2d.x.compiled_with_profiler"] = false;
  121. locValueDict["cocos2d.x.compiled_with_gl_state_cache"] = cc.ENABLE_GL_STATE_CACHE;
  122. return true;
  123. },
  124. /**
  125. * returns the value of a given key as a string. If the key is not found, it will return the default value
  126. * @param {String} key
  127. * @param {String} [default_value=null]
  128. * @returns {String}
  129. */
  130. getCString:function(key, default_value){
  131. var locValueDict = this._valueDict;
  132. if(locValueDict[key])
  133. return locValueDict[key];
  134. return default_value;
  135. },
  136. /**
  137. * returns the value of a given key as a boolean. If the key is not found, it will return the default value
  138. * @param {string} key
  139. * @param {boolean|null} [default_value=false]
  140. * @returns {boolean}
  141. */
  142. getBool: function(key, default_value){
  143. if(default_value == null)
  144. default_value = false;
  145. var locValueDict = this._valueDict;
  146. if(locValueDict[key])
  147. return locValueDict[key];
  148. return default_value;
  149. },
  150. /**
  151. * returns the value of a given key as a double. If the key is not found, it will return the default value
  152. * @param {string} key
  153. * @param {number} [default_value=0]
  154. * @returns {number}
  155. */
  156. getNumber: function(key, default_value){
  157. if(default_value == null)
  158. default_value = 0;
  159. var locValueDict = this._valueDict;
  160. if(locValueDict[key])
  161. return locValueDict[key];
  162. return default_value;
  163. },
  164. /**
  165. * returns the value of a given key as a double
  166. * @param {string} key
  167. * @returns {Object|null}
  168. */
  169. getObject:function(key){
  170. var locValueDict = this._valueDict;
  171. if(locValueDict[key])
  172. return locValueDict[key];
  173. return null;
  174. },
  175. /**
  176. * sets a new key/value pair in the configuration dictionary
  177. * @param {string} key
  178. * @param {Object} value
  179. */
  180. setObject: function(key, value){
  181. this._valueDict[key] = value;
  182. },
  183. /**
  184. * dumps the current configuration on the console
  185. */
  186. dumpInfo: function(){
  187. if(cc.ENABLE_GL_STATE_CACHE === 0){
  188. cc.log("");
  189. cc.log("cocos2d: **** WARNING **** CC_ENABLE_PROFILERS is defined. Disable it when you finish profiling (from ccConfig.js)");
  190. cc.log("")
  191. }
  192. },
  193. /**
  194. * gathers OpenGL / GPU information
  195. */
  196. gatherGPUInfo: function(){
  197. if(cc.renderContextType === cc.CANVAS)
  198. return;
  199. var gl = cc.renderContext;
  200. var locValueDict = this._valueDict;
  201. locValueDict["gl.vendor"] = gl.getParameter(gl.VENDOR);
  202. locValueDict["gl.renderer"] = gl.getParameter(gl.RENDERER);
  203. locValueDict["gl.version"] = gl.getParameter(gl.VERSION);
  204. this._GlExtensions = "";
  205. var extArr = gl.getSupportedExtensions();
  206. for (var i = 0; i < extArr.length; i++)
  207. this._GlExtensions += extArr[i] + " ";
  208. this._maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
  209. locValueDict["gl.max_texture_size"] = this._maxTextureSize;
  210. this._maxTextureUnits = gl.getParameter(gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS);
  211. locValueDict["gl.max_texture_units"] = this._maxTextureUnits;
  212. this._supportsPVRTC = this.checkForGLExtension("GL_IMG_texture_compression_pvrtc");
  213. locValueDict["gl.supports_PVRTC"] = this._supportsPVRTC;
  214. this._supportsNPOT = false; //true;
  215. locValueDict["gl.supports_NPOT"] = this._supportsNPOT;
  216. this._supportsBGRA8888 = this.checkForGLExtension("GL_IMG_texture_format_BGRA888");
  217. locValueDict["gl.supports_BGRA8888"] = this._supportsBGRA8888;
  218. this._supportsDiscardFramebuffer = this.checkForGLExtension("GL_EXT_discard_framebuffer");
  219. locValueDict["gl.supports_discard_framebuffer"] = this._supportsDiscardFramebuffer;
  220. this._supportsShareableVAO = this.checkForGLExtension("vertex_array_object");
  221. locValueDict["gl.supports_vertex_array_object"] = this._supportsShareableVAO;
  222. cc.CHECK_GL_ERROR_DEBUG();
  223. },
  224. /**
  225. * Loads a config file. If the keys are already present, then they are going to be replaced. Otherwise the new keys are added.
  226. * @param {string} filename
  227. */
  228. loadConfigFile: function( filename){
  229. var fileUtils = cc.FileUtils.getInstance();
  230. var fullPath = fileUtils.fullPathForFilename(filename);
  231. var dict = fileUtils.dictionaryWithContentsOfFileThreadSafe(fullPath);
  232. if(dict == null)
  233. return;
  234. var getDatas = dict["data"];
  235. if(!getDatas){
  236. cc.log("Expected 'data' dict, but not found. Config file: " + filename);
  237. return;
  238. }
  239. // Add all keys in the existing dictionary
  240. for(var selKey in getDatas)
  241. this._valueDict[selKey] = getDatas[selKey];
  242. }
  243. });
  244. cc.Configuration._sharedConfiguration = null;
  245. /**
  246. * returns a shared instance of CCConfiguration
  247. * @return {cc.Configuration}
  248. */
  249. cc.Configuration.getInstance = function () {
  250. if(!cc.Configuration._sharedConfiguration){
  251. cc.Configuration._sharedConfiguration = new cc.Configuration();
  252. cc.Configuration._sharedConfiguration.init();
  253. }
  254. return cc.Configuration._sharedConfiguration;
  255. };
  256. /**
  257. * purge the shared instance of CCConfiguration
  258. */
  259. cc.Configuration.purgeConfiguration = function () {
  260. cc.Configuration._sharedConfiguration = null;
  261. };