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 /**
255  * Converts a Size in points to pixels
256  * @param {cc.Size} sizeInPoints
257  * @return {cc.Size}
258  * @function
259  */
260 cc.SIZE_POINTS_TO_PIXELS = function (sizeInPoints) {
261     var scale = cc.CONTENT_SCALE_FACTOR();
262     return cc.size(sizeInPoints.width * scale, sizeInPoints.height * scale);
263 };
264 
265 /**
266  * Converts a size in pixels to points
267  * @param {cc.Size} sizeInPixels
268  * @return {cc.Size}
269  * @function
270  */
271 cc.SIZE_PIXELS_TO_POINTS = function (sizeInPixels) {
272     var scale = cc.CONTENT_SCALE_FACTOR();
273     return cc.size(sizeInPixels.width / scale, sizeInPixels.height / scale);
274 };
275 
276 /**
277  * Converts a Point in pixels to points
278  * @param {Point} pixels
279  * @function
280  */
281 cc.POINT_PIXELS_TO_POINTS = function (pixels) {
282     var scale = cc.CONTENT_SCALE_FACTOR();
283     return cc.p(pixels.x / scale, pixels.y / scale);
284 };
285 
286 /**
287  * Converts a rect in pixels to points
288  * @param {cc.Rect} pixel
289  * @function
290  */
291 cc.RECT_PIXELS_TO_POINTS = cc.IS_RETINA_DISPLAY_SUPPORTED ? function (pixel) {
292     var scale = cc.CONTENT_SCALE_FACTOR();
293     return cc.rect(pixel.x / scale, pixel.y / scale,
294         pixel.width / scale, pixel.height / scale);
295 } : function (p) {
296     return p;
297 };
298 
299 /**
300  * Converts a rect in points to pixels
301  * @param {cc.Rect} point
302  * @function
303  */
304 cc.RECT_POINTS_TO_PIXELS = cc.IS_RETINA_DISPLAY_SUPPORTED ? function (point) {
305    var scale = cc.CONTENT_SCALE_FACTOR();
306     return cc.rect(point.x * scale, point.y * scale,
307         point.width * scale, point.height * scale);
308 } : function (p) {
309     return p;
310 };
311 
312 if (!cc.Browser.supportWebGL) {
313     /**
314      * WebGL constants
315      * @type {object}
316      */
317     var gl = gl || {};
318 
319     /**
320      * @constant
321      * @type Number
322      */
323     gl.ONE = 1;
324 
325     /**
326      * @constant
327      * @type Number
328      */
329     gl.ZERO = 0;
330 
331     /**
332      * @constant
333      * @type Number
334      */
335     gl.SRC_ALPHA = 0x0302;
336 
337     /**
338      * @constant
339      * @type Number
340      */
341     gl.ONE_MINUS_SRC_ALPHA = 0x0303;
342 
343     /**
344      * @constant
345      * @type Number
346      */
347     gl.ONE_MINUS_DST_COLOR = 0x0307;
348 }
349 
350 cc.CHECK_GL_ERROR_DEBUG = function () {
351     if (cc.renderMode == cc.WEBGL) {
352         var _error = cc.renderContext.getError();
353         if (_error) {
354             cc.log("WebGL error " + _error);
355         }
356     }
357 };
358