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