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 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 /**
 28  * cc.configuration is a singleton object which contains some openGL variables
 29  * @class
 30  * @name cc.configuration
 31  * @example
 32  * var textureSize = cc.configuration.getMaxTextureSize();
 33  */
 34 cc.configuration = /** @lends cc.configuration# */{
 35 	// Type constants
 36 	/*
 37 	 * ERROR type
 38 	 * @public
 39 	 * @const
 40 	 * @type {Number}
 41 	 */
 42 	ERROR:0,
 43 
 44 	/*
 45 	 * STRING type
 46 	 * @public
 47 	 * @const
 48 	 * @type {Number}
 49 	 */
 50 	STRING:1,
 51 
 52 	/*
 53 	 * INT type
 54 	 * @public
 55 	 * @const
 56 	 * @type {Number}
 57 	 */
 58 	INT:2,
 59 
 60 	/*
 61 	 * DOUBLE type
 62 	 * @public
 63 	 * @const
 64 	 * @type {Number}
 65 	 */
 66 	DOUBLE:3,
 67 
 68 	/*
 69 	 * BOOLEAN type
 70 	 * @public
 71 	 * @const
 72 	 * @type {Number}
 73 	 */
 74 	BOOLEAN:4,
 75 
 76     _maxTextureSize:0,
 77     _maxModelviewStackDepth:0,
 78     _supportsPVRTC:false,
 79     _supportsNPOT:false,
 80     _supportsBGRA8888:false,
 81     _supportsDiscardFramebuffer:false,
 82     _supportsShareableVAO:false,
 83     _maxSamplesAllowed:0,
 84     _maxTextureUnits:0,
 85     _GlExtensions:"",
 86     _valueDict:{},
 87 
 88 	_inited: false,
 89 
 90 	_init:function () {
 91 		var locValueDict = this._valueDict;
 92 		locValueDict["cocos2d.x.version"] = cc.ENGINE_VERSION;
 93 		locValueDict["cocos2d.x.compiled_with_profiler"] = false;
 94 		locValueDict["cocos2d.x.compiled_with_gl_state_cache"] = cc.ENABLE_GL_STATE_CACHE;
 95 		this._inited = true;
 96 	},
 97 
 98     /**
 99      * OpenGL Max texture size.
100      * @return {Number}
101      */
102     getMaxTextureSize:function () {
103         return this._maxTextureSize;
104     },
105 
106     /**
107      * OpenGL Max Modelview Stack Depth.
108      * @return {Number}
109      */
110     getMaxModelviewStackDepth:function () {
111         return this._maxModelviewStackDepth;
112     },
113 
114     /**
115      * returns the maximum texture units
116      * @return {Number}
117      */
118     getMaxTextureUnits:function () {
119         return this._maxTextureUnits;
120     },
121 
122     /**
123      * Whether or not the GPU supports NPOT (Non Power Of Two) textures.
124      * OpenGL ES 2.0 already supports NPOT (iOS).
125      * @return {Boolean}
126      */
127     supportsNPOT:function () {
128         return this._supportsNPOT;
129     },
130 
131     /**
132      * Whether or not PVR Texture Compressed is supported
133      * @return {Boolean}
134      */
135     supportsPVRTC: function () {
136         return this._supportsPVRTC;
137     },
138 
139 	/**
140 	 * Whether or not ETC Texture Compressed is supported
141 	 * @return {Boolean}
142 	 */
143 	supportsETC: function() {
144 		return false;
145 	},
146 
147 	/**
148 	 * Whether or not S3TC Texture Compressed is supported
149 	 * @return {Boolean}
150 	 */
151 	supportsS3TC: function() {
152 		return false;
153 	},
154 
155 	/**
156 	 * Whether or not ATITC Texture Compressed is supported
157 	 * @return {Boolean}
158 	 */
159 	supportsATITC: function() {
160 		return false;
161 	},
162 
163     /**
164      * Whether or not BGRA8888 textures are supported.
165      * @return {Boolean}
166      */
167     supportsBGRA8888:function () {
168         return this._supportsBGRA8888;
169     },
170 
171     /**
172      * Whether or not glDiscardFramebufferEXT is supported
173      * @return {Boolean}
174      */
175     supportsDiscardFramebuffer:function () {
176         return this._supportsDiscardFramebuffer;
177     },
178 
179     /**
180      * Whether or not shareable VAOs are supported.
181      * @return {Boolean}
182      */
183     supportsShareableVAO:function () {
184         return this._supportsShareableVAO;
185     },
186 
187     /**
188      * returns whether or not an OpenGL is supported
189      * @param {String} searchName
190      */
191     checkForGLExtension:function (searchName) {
192         return this._GlExtensions.indexOf(searchName) > -1;
193     },
194 
195     /**
196      * Returns the value of a given key.  If the key is not found, it will return the default value
197      * @param {String} key
198      * @param {String|Bool|Number|Object} [default_value=null]
199      * @returns {String|Bool|Number|Object}
200      */
201     getValue: function(key, default_value){
202 	    if(!this._inited)
203 		    this._init();
204         var locValueDict = this._valueDict;
205         if(locValueDict[key])
206             return locValueDict[key];
207         return default_value;
208     },
209 
210     /**
211      * Sets a new key/value pair  in the configuration dictionary
212      * @param {string} key
213      * @param {String|Bool|Number|Object} value
214      */
215     setValue: function(key, value){
216         this._valueDict[key] = value;
217     },
218 
219     /**
220      * Dumps the current configuration on the console
221      */
222     dumpInfo: function(){
223          if(cc.ENABLE_GL_STATE_CACHE === 0){
224              cc.log("");
225              cc.log(cc._LogInfos.configuration_dumpInfo);
226              cc.log("")
227          }
228     },
229 
230     /**
231      * gathers OpenGL / GPU information
232      */
233     gatherGPUInfo: function(){
234         if(cc._renderType === cc.game.RENDER_TYPE_CANVAS)
235             return;
236 
237 	    if(!this._inited)
238 		    this._init();
239         var gl = cc._renderContext;
240         var locValueDict = this._valueDict;
241         locValueDict["gl.vendor"] = gl.getParameter(gl.VENDOR);
242         locValueDict["gl.renderer"] = gl.getParameter(gl.RENDERER);
243         locValueDict["gl.version"] = gl.getParameter(gl.VERSION);
244 
245         this._GlExtensions = "";
246         var extArr = gl.getSupportedExtensions();
247         for (var i = 0; i < extArr.length; i++)
248             this._GlExtensions += extArr[i] + " ";
249 
250         this._maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
251         locValueDict["gl.max_texture_size"] = this._maxTextureSize;
252         this._maxTextureUnits = gl.getParameter(gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS);
253         locValueDict["gl.max_texture_units"] = this._maxTextureUnits;
254 
255         this._supportsPVRTC = this.checkForGLExtension("GL_IMG_texture_compression_pvrtc");
256         locValueDict["gl.supports_PVRTC"] = this._supportsPVRTC;
257 
258         this._supportsNPOT = false; //true;
259         locValueDict["gl.supports_NPOT"] = this._supportsNPOT;
260 
261         this._supportsBGRA8888 = this.checkForGLExtension("GL_IMG_texture_format_BGRA888");
262         locValueDict["gl.supports_BGRA8888"] = this._supportsBGRA8888;
263 
264         this._supportsDiscardFramebuffer = this.checkForGLExtension("GL_EXT_discard_framebuffer");
265         locValueDict["gl.supports_discard_framebuffer"] = this._supportsDiscardFramebuffer;
266 
267         this._supportsShareableVAO = this.checkForGLExtension("vertex_array_object");
268         locValueDict["gl.supports_vertex_array_object"] = this._supportsShareableVAO;
269 
270         cc.checkGLErrorDebug();
271     },
272 
273     /**
274      * Loads a config file. If the keys are already present, then they are going to be replaced. Otherwise the new keys are added.
275      * @param {string} url
276      */
277     loadConfigFile: function( url){
278 	    if(!this._inited)
279 		    this._init();
280         var dict = cc.loader.getRes(url);
281         if(!dict) throw new Error("Please load the resource first : " + url);
282         cc.assert(dict, cc._LogInfos.configuration_loadConfigFile_2, url);
283 
284         var getDatas = dict["data"];
285         if(!getDatas){
286             cc.log(cc._LogInfos.configuration_loadConfigFile, url);
287             return;
288         }
289 
290         // Add all keys in the existing dictionary
291         for(var selKey in getDatas)
292             this._valueDict[selKey] = getDatas[selKey];
293     }
294 };