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