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