CCConfiguration.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies 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. /**
  23. * cc.configuration is a singleton object which contains some openGL variables
  24. * @class
  25. * @name cc.configuration
  26. * @example
  27. * var textureSize = cc.configuration.getMaxTextureSize();
  28. */
  29. cc.configuration = /** @lends cc.configuration# */{
  30. // Type constants
  31. /*
  32. * ERROR type
  33. * @public
  34. * @const
  35. * @type {Number}
  36. */
  37. ERROR:0,
  38. /*
  39. * STRING type
  40. * @public
  41. * @const
  42. * @type {Number}
  43. */
  44. STRING:1,
  45. /*
  46. * INT type
  47. * @public
  48. * @const
  49. * @type {Number}
  50. */
  51. INT:2,
  52. /*
  53. * DOUBLE type
  54. * @public
  55. * @const
  56. * @type {Number}
  57. */
  58. DOUBLE:3,
  59. /*
  60. * BOOLEAN type
  61. * @public
  62. * @const
  63. * @type {Number}
  64. */
  65. BOOLEAN:4,
  66. _maxTextureSize:0,
  67. _maxModelviewStackDepth:0,
  68. _supportsPVRTC:false,
  69. _supportsNPOT:false,
  70. _supportsBGRA8888:false,
  71. _supportsDiscardFramebuffer:false,
  72. _supportsShareableVAO:false,
  73. _maxSamplesAllowed:0,
  74. _maxTextureUnits:0,
  75. _GlExtensions:"",
  76. _valueDict:{},
  77. _inited: false,
  78. _init:function () {
  79. var locValueDict = this._valueDict;
  80. locValueDict["cocos2d.x.version"] = cc.ENGINE_VERSION;
  81. locValueDict["cocos2d.x.compiled_with_profiler"] = false;
  82. locValueDict["cocos2d.x.compiled_with_gl_state_cache"] = cc.ENABLE_GL_STATE_CACHE;
  83. this._inited = true;
  84. },
  85. /**
  86. * OpenGL Max texture size.
  87. * @return {Number}
  88. */
  89. getMaxTextureSize:function () {
  90. return this._maxTextureSize;
  91. },
  92. /**
  93. * OpenGL Max Modelview Stack Depth.
  94. * @return {Number}
  95. */
  96. getMaxModelviewStackDepth:function () {
  97. return this._maxModelviewStackDepth;
  98. },
  99. /**
  100. * returns the maximum texture units
  101. * @return {Number}
  102. */
  103. getMaxTextureUnits:function () {
  104. return this._maxTextureUnits;
  105. },
  106. /**
  107. * Whether or not the GPU supports NPOT (Non Power Of Two) textures.
  108. * OpenGL ES 2.0 already supports NPOT (iOS).
  109. * @return {Boolean}
  110. */
  111. supportsNPOT:function () {
  112. return this._supportsNPOT;
  113. },
  114. /**
  115. * Whether or not PVR Texture Compressed is supported
  116. * @return {Boolean}
  117. */
  118. supportsPVRTC: function () {
  119. return this._supportsPVRTC;
  120. },
  121. /**
  122. * Whether or not ETC Texture Compressed is supported
  123. * @return {Boolean}
  124. */
  125. supportsETC: function() {
  126. return false;
  127. },
  128. /**
  129. * Whether or not S3TC Texture Compressed is supported
  130. * @return {Boolean}
  131. */
  132. supportsS3TC: function() {
  133. return false;
  134. },
  135. /**
  136. * Whether or not ATITC Texture Compressed is supported
  137. * @return {Boolean}
  138. */
  139. supportsATITC: function() {
  140. return false;
  141. },
  142. /**
  143. * Whether or not BGRA8888 textures are supported.
  144. * @return {Boolean}
  145. */
  146. supportsBGRA8888:function () {
  147. return this._supportsBGRA8888;
  148. },
  149. /**
  150. * Whether or not glDiscardFramebufferEXT is supported
  151. * @return {Boolean}
  152. */
  153. supportsDiscardFramebuffer:function () {
  154. return this._supportsDiscardFramebuffer;
  155. },
  156. /**
  157. * Whether or not shareable VAOs are supported.
  158. * @return {Boolean}
  159. */
  160. supportsShareableVAO:function () {
  161. return this._supportsShareableVAO;
  162. },
  163. /**
  164. * returns whether or not an OpenGL is supported
  165. * @param {String} searchName
  166. */
  167. checkForGLExtension:function (searchName) {
  168. return this._GlExtensions.indexOf(searchName) > -1;
  169. },
  170. /**
  171. * Returns the value of a given key. If the key is not found, it will return the default value
  172. * @param {String} key
  173. * @param {String|Bool|Number|Object} [default_value=null]
  174. * @returns {String|Bool|Number|Object}
  175. */
  176. getValue: function(key, default_value){
  177. if(!this._inited)
  178. this._init();
  179. var locValueDict = this._valueDict;
  180. if(locValueDict[key])
  181. return locValueDict[key];
  182. return default_value;
  183. },
  184. /**
  185. * Sets a new key/value pair in the configuration dictionary
  186. * @param {string} key
  187. * @param {String|Bool|Number|Object} value
  188. */
  189. setValue: function(key, value){
  190. this._valueDict[key] = value;
  191. },
  192. /**
  193. * Dumps the current configuration on the console
  194. */
  195. dumpInfo: function(){
  196. if(cc.ENABLE_GL_STATE_CACHE === 0){
  197. cc.log("");
  198. cc.log(cc._LogInfos.configuration_dumpInfo);
  199. cc.log("")
  200. }
  201. },
  202. /**
  203. * gathers OpenGL / GPU information
  204. */
  205. gatherGPUInfo: function(){
  206. if(cc._renderType === cc._RENDER_TYPE_CANVAS)
  207. return;
  208. if(!this._inited)
  209. this._init();
  210. var gl = cc._renderContext;
  211. var locValueDict = this._valueDict;
  212. locValueDict["gl.vendor"] = gl.getParameter(gl.VENDOR);
  213. locValueDict["gl.renderer"] = gl.getParameter(gl.RENDERER);
  214. locValueDict["gl.version"] = gl.getParameter(gl.VERSION);
  215. this._GlExtensions = "";
  216. var extArr = gl.getSupportedExtensions();
  217. for (var i = 0; i < extArr.length; i++)
  218. this._GlExtensions += extArr[i] + " ";
  219. this._maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
  220. locValueDict["gl.max_texture_size"] = this._maxTextureSize;
  221. this._maxTextureUnits = gl.getParameter(gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS);
  222. locValueDict["gl.max_texture_units"] = this._maxTextureUnits;
  223. this._supportsPVRTC = this.checkForGLExtension("GL_IMG_texture_compression_pvrtc");
  224. locValueDict["gl.supports_PVRTC"] = this._supportsPVRTC;
  225. this._supportsNPOT = false; //true;
  226. locValueDict["gl.supports_NPOT"] = this._supportsNPOT;
  227. this._supportsBGRA8888 = this.checkForGLExtension("GL_IMG_texture_format_BGRA888");
  228. locValueDict["gl.supports_BGRA8888"] = this._supportsBGRA8888;
  229. this._supportsDiscardFramebuffer = this.checkForGLExtension("GL_EXT_discard_framebuffer");
  230. locValueDict["gl.supports_discard_framebuffer"] = this._supportsDiscardFramebuffer;
  231. this._supportsShareableVAO = this.checkForGLExtension("vertex_array_object");
  232. locValueDict["gl.supports_vertex_array_object"] = this._supportsShareableVAO;
  233. cc.checkGLErrorDebug();
  234. },
  235. /**
  236. * Loads a config file. If the keys are already present, then they are going to be replaced. Otherwise the new keys are added.
  237. * @param {string} url
  238. */
  239. loadConfigFile: function( url){
  240. if(!this._inited)
  241. this._init();
  242. var dict = cc.loader.getRes(url);
  243. if(!dict) throw "Please load the resource first : " + url;
  244. cc.assert(dict, cc._LogInfos.configuration_loadConfigFile_2, url);
  245. var getDatas = dict["data"];
  246. if(!getDatas){
  247. cc.log(cc._LogInfos.configuration_loadConfigFile, url);
  248. return;
  249. }
  250. // Add all keys in the existing dictionary
  251. for(var selKey in getDatas)
  252. this._valueDict[selKey] = getDatas[selKey];
  253. }
  254. };