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  * @constant
 29  * @type Number
 30  */
 31 cc.INVALID_INDEX = -1;
 32 
 33 /**
 34  * PI is the ratio of a circle's circumference to its diameter.
 35  * @constant
 36  * @type Number
 37  */
 38 cc.PI = Math.PI;
 39 
 40 /**
 41  * @constant
 42  * @type Number
 43  */
 44 cc.FLT_MAX = parseFloat('3.402823466e+38F');
 45 
 46 /**
 47  * @constant
 48  * @type Number
 49  */
 50 cc.RAD = cc.PI / 180;
 51 
 52 /**
 53  * @constant
 54  * @type Number
 55  */
 56 cc.DEG = 180 / cc.PI;
 57 
 58 /**
 59  * maximum unsigned int value
 60  * @constant
 61  * @type Number
 62  */
 63 cc.UINT_MAX = 0xffffffff;
 64 
 65 /**
 66  * <p>
 67  * simple macro that swaps 2 variables<br/>
 68  *  modified from c++ macro, you need to pass in the x and y variables names in string, <br/>
 69  *  and then a reference to the whole object as third variable
 70  * </p>
 71  * @param x
 72  * @param y
 73  * @param ref
 74  * @function
 75  * @deprecated
 76  */
 77 cc.SWAP = function (x, y, ref) {
 78     if ((typeof ref) == 'object' && (typeof ref.x) != 'undefined' && (typeof ref.y) != 'undefined') {
 79         var tmp = ref[x];
 80         ref[x] = ref[y];
 81         ref[y] = tmp;
 82     } else
 83         cc.log("cc.SWAP is being modified from original macro, please check usage");
 84 };
 85 
 86 /**
 87  * <p>
 88  *     Linear interpolation between 2 numbers, the ratio sets how much it is biased to each end
 89  * </p>
 90  * @param {Number} a number A
 91  * @param {Number} b number B
 92  * @param {Number} r ratio between 0 and 1
 93  * @function
 94  * @example
 95  * cc.lerp(2,10,0.5)//returns 6<br/>
 96  * cc.lerp(2,10,0.2)//returns 3.6
 97  */
 98 cc.lerp = function (a, b, r) {
 99     return a + (b - a) * r;
100 };
101 
102 /**
103  * returns a random float between -1 and 1
104  * @return {Number}
105  * @function
106  */
107 cc.RANDOM_MINUS1_1 = function () {
108     return (Math.random() - 0.5) * 2;
109 };
110 
111 /**
112  * returns a random float between 0 and 1
113  * @return {Number}
114  * @function
115  */
116 cc.RANDOM_0_1 = function () {
117     return Math.random();
118 };
119 
120 /**
121  * converts degrees to radians
122  * @param {Number} angle
123  * @return {Number}
124  * @function
125  */
126 cc.DEGREES_TO_RADIANS = function (angle) {
127     return angle * cc.RAD;
128 };
129 
130 /**
131  * converts radians to degrees
132  * @param {Number} angle
133  * @return {Number}
134  * @function
135  */
136 cc.RADIANS_TO_DEGREES = function (angle) {
137     return angle * cc.DEG;
138 };
139 
140 /**
141  * @constant
142  * @type Number
143  */
144 cc.REPEAT_FOREVER = Number.MAX_VALUE - 1;
145 
146 /**
147  * default gl blend src function. Compatible with premultiplied alpha images.
148  * @constant
149  * @type Number
150  */
151 cc.BLEND_SRC = cc.OPTIMIZE_BLEND_FUNC_FOR_PREMULTIPLIED_ALPHA ? 1 : 0x0302;
152 
153 /**
154  * default gl blend dst function. Compatible with premultiplied alpha images.
155  * @constant
156  * @type Number
157  */
158 cc.BLEND_DST = 0x0303;
159 
160 /**
161  * Helpful macro that setups the GL server state, the correct GL program and sets the Model View Projection matrix
162  * @param {cc.Node} node setup node
163  * @function
164  */
165 cc.NODE_DRAW_SETUP = function (node) {
166     //cc.glEnable(node._glServerState);
167     if (node._shaderProgram) {
168         //cc.renderContext.useProgram(node._shaderProgram._programObj);
169         node._shaderProgram.use();
170         node._shaderProgram.setUniformForModelViewAndProjectionMatrixWithMat4();
171     }
172 };
173 
174 /**
175  * <p>
176  *     GL states that are enabled:<br/>
177  *       - GL_TEXTURE_2D<br/>
178  *       - GL_VERTEX_ARRAY<br/>
179  *       - GL_TEXTURE_COORD_ARRAY<br/>
180  *       - GL_COLOR_ARRAY<br/>
181  * </p>
182  * @function
183  */
184 cc.ENABLE_DEFAULT_GL_STATES = function () {
185     //TODO OPENGL STUFF
186     /*
187      glEnableClientState(GL_VERTEX_ARRAY);
188      glEnableClientState(GL_COLOR_ARRAY);
189      glEnableClientState(GL_TEXTURE_COORD_ARRAY);
190      glEnable(GL_TEXTURE_2D);*/
191 };
192 
193 /**
194  * <p>
195  *   Disable default GL states:<br/>
196  *     - GL_TEXTURE_2D<br/>
197  *     - GL_TEXTURE_COORD_ARRAY<br/>
198  *     - GL_COLOR_ARRAY<br/>
199  * </p>
200  * @function
201  */
202 cc.DISABLE_DEFAULT_GL_STATES = function () {
203     //TODO OPENGL
204     /*
205      glDisable(GL_TEXTURE_2D);
206      glDisableClientState(GL_COLOR_ARRAY);
207      glDisableClientState(GL_TEXTURE_COORD_ARRAY);
208      glDisableClientState(GL_VERTEX_ARRAY);
209      */
210 };
211 
212 /**
213  * <p>
214  *  Increments the GL Draws counts by one.<br/>
215  *  The number of calls per frame are displayed on the screen when the CCDirector's stats are enabled.<br/>
216  * </p>
217  * @param {Number} addNumber
218  * @function
219  */
220 cc.INCREMENT_GL_DRAWS = function (addNumber) {
221     cc.g_NumberOfDraws += addNumber;
222 };
223 
224 /**
225  * @constant
226  * @type Number
227  */
228 cc.FLT_EPSILON = 0.0000001192092896;
229 
230 /**
231  * <p>
232  *     On Mac it returns 1;<br/>
233  *     On iPhone it returns 2 if RetinaDisplay is On. Otherwise it returns 1
234  * </p>
235  * @function
236  */
237 cc.CONTENT_SCALE_FACTOR = cc.IS_RETINA_DISPLAY_SUPPORTED ? function () {
238     return cc.Director.getInstance().getContentScaleFactor();
239 } : function () {
240     return 1;
241 };
242 
243 /**
244  * Converts a Point in points to pixels
245  * @param {cc.Point} points
246  * @return {cc.Point}
247  * @function
248  */
249 cc.POINT_POINTS_TO_PIXELS = function (points) {
250     var scale = cc.CONTENT_SCALE_FACTOR();
251     return cc.p(points.x * scale, points.y * scale);
252 };
253 
254 cc._POINT_POINTS_TO_PIXELS_OUT = function (points, outPixels) {
255     var scale = cc.CONTENT_SCALE_FACTOR();
256     outPixels._x = points.x * scale;
257     outPixels._y = points.y * scale;
258 };
259 
260 /**
261  * Converts a Size in points to pixels
262  * @param {cc.Size} sizeInPoints
263  * @return {cc.Size}
264  * @function
265  */
266 cc.SIZE_POINTS_TO_PIXELS = function (sizeInPoints) {
267     var scale = cc.CONTENT_SCALE_FACTOR();
268     return cc.size(sizeInPoints.width * scale, sizeInPoints.height * scale);
269 };
270 
271 /**
272  * Converts a size in pixels to points
273  * @param {cc.Size} sizeInPixels
274  * @return {cc.Size}
275  * @function
276  */
277 cc.SIZE_PIXELS_TO_POINTS = function (sizeInPixels) {
278     var scale = cc.CONTENT_SCALE_FACTOR();
279     return cc.size(sizeInPixels.width / scale, sizeInPixels.height / scale);
280 };
281 
282 cc._SIZE_PIXELS_TO_POINTS_OUT = function (sizeInPixels, outSize) {
283     var scale = cc.CONTENT_SCALE_FACTOR();
284     outSize._width = sizeInPixels.width / scale;
285     outSize._height = sizeInPixels.height / scale;
286 };
287 
288 /**
289  * Converts a Point in pixels to points
290  * @param {Point} pixels
291  * @function
292  */
293 cc.POINT_PIXELS_TO_POINTS = function (pixels) {
294     var scale = cc.CONTENT_SCALE_FACTOR();
295     return cc.p(pixels.x / scale, pixels.y / scale);
296 };
297 
298 cc._POINT_PIXELS_TO_POINTS_OUT = function(pixels, outPoint){
299     var scale = cc.CONTENT_SCALE_FACTOR();
300     outPoint._x = pixels.x / scale;
301     outPoint._y = pixels.y / scale;
302 };
303 
304 /**
305  * Converts a rect in pixels to points
306  * @param {cc.Rect} pixel
307  * @function
308  */
309 cc.RECT_PIXELS_TO_POINTS = cc.IS_RETINA_DISPLAY_SUPPORTED ? function (pixel) {
310     var scale = cc.CONTENT_SCALE_FACTOR();
311     return cc.rect(pixel.x / scale, pixel.y / scale,
312         pixel.width / scale, pixel.height / scale);
313 } : function (p) {
314     return p;
315 };
316 
317 /**
318  * Converts a rect in points to pixels
319  * @param {cc.Rect} point
320  * @function
321  */
322 cc.RECT_POINTS_TO_PIXELS = cc.IS_RETINA_DISPLAY_SUPPORTED ? function (point) {
323    var scale = cc.CONTENT_SCALE_FACTOR();
324     return cc.rect(point.x * scale, point.y * scale,
325         point.width * scale, point.height * scale);
326 } : function (p) {
327     return p;
328 };
329 
330 if (!cc.Browser.supportWebGL) {
331     /**
332      * WebGL constants
333      * @type {object}
334      */
335     var gl = gl || {};
336 
337     /**
338      * @constant
339      * @type Number
340      */
341     gl.ONE = 1;
342 
343     /**
344      * @constant
345      * @type Number
346      */
347     gl.ZERO = 0;
348 
349     /**
350      * @constant
351      * @type Number
352      */
353     gl.SRC_ALPHA = 0x0302;
354 
355     /**
356      * @constant
357      * @type Number
358      */
359     gl.ONE_MINUS_SRC_ALPHA = 0x0303;
360 
361     /**
362      * @constant
363      * @type Number
364      */
365     gl.ONE_MINUS_DST_COLOR = 0x0307;
366 }
367 
368 cc.CHECK_GL_ERROR_DEBUG = function () {
369     if (cc.renderMode == cc.WEBGL) {
370         var _error = cc.renderContext.getError();
371         if (_error) {
372             cc.log("WebGL error " + _error);
373         }
374     }
375 };
376