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._LogInfos.swap);
 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  * get a random number from 0 to 0xffffff
104  * @function
105  * @returns {number}
106  */
107 cc.rand = function () {
108 	return Math.random() * 0xffffff;
109 };
110 
111 /**
112  * returns a random float between -1 and 1
113  * @return {Number}
114  * @function
115  */
116 cc.randomMinus1To1 = function () {
117     return (Math.random() - 0.5) * 2;
118 };
119 
120 /**
121  * returns a random float between 0 and 1
122  * @return {Number}
123  * @function
124  */
125 cc.random0To1 = Math.random;
126 
127 /**
128  * converts degrees to radians
129  * @param {Number} angle
130  * @return {Number}
131  * @function
132  */
133 cc.degreesToRadians = function (angle) {
134     return angle * cc.RAD;
135 };
136 
137 /**
138  * converts radians to degrees
139  * @param {Number} angle
140  * @return {Number}
141  * @function
142  */
143 cc.radiansToDegress = function (angle) {
144     return angle * cc.DEG;
145 };
146 
147 /**
148  * @constant
149  * @type Number
150  */
151 cc.REPEAT_FOREVER = Number.MAX_VALUE - 1;
152 
153 /**
154  * default gl blend src function. Compatible with premultiplied alpha images.
155  * @constant
156  * @type Number
157  */
158 cc.BLEND_SRC = cc.OPTIMIZE_BLEND_FUNC_FOR_PREMULTIPLIED_ALPHA ? 1 : 0x0302;
159 
160 /**
161  * default gl blend dst function. Compatible with premultiplied alpha images.
162  * @constant
163  * @type Number
164  */
165 cc.BLEND_DST = 0x0303;
166 
167 /**
168  * Helpful macro that setups the GL server state, the correct GL program and sets the Model View Projection matrix
169  * @param {cc.Node} node setup node
170  * @function
171  */
172 cc.nodeDrawSetup = function (node) {
173     //cc.glEnable(node._glServerState);
174     if (node._shaderProgram) {
175         //cc._renderContext.useProgram(node._shaderProgram._programObj);
176         node._shaderProgram.use();
177         node._shaderProgram.setUniformForModelViewAndProjectionMatrixWithMat4();
178     }
179 };
180 
181 /**
182  * <p>
183  *     GL states that are enabled:<br/>
184  *       - GL_TEXTURE_2D<br/>
185  *       - GL_VERTEX_ARRAY<br/>
186  *       - GL_TEXTURE_COORD_ARRAY<br/>
187  *       - GL_COLOR_ARRAY<br/>
188  * </p>
189  * @function
190  */
191 cc.enableDefaultGLStates = function () {
192     //TODO OPENGL STUFF
193     /*
194      glEnableClientState(GL_VERTEX_ARRAY);
195      glEnableClientState(GL_COLOR_ARRAY);
196      glEnableClientState(GL_TEXTURE_COORD_ARRAY);
197      glEnable(GL_TEXTURE_2D);*/
198 };
199 
200 /**
201  * <p>
202  *   Disable default GL states:<br/>
203  *     - GL_TEXTURE_2D<br/>
204  *     - GL_TEXTURE_COORD_ARRAY<br/>
205  *     - GL_COLOR_ARRAY<br/>
206  * </p>
207  * @function
208  */
209 cc.disableDefaultGLStates = function () {
210     //TODO OPENGL
211     /*
212      glDisable(GL_TEXTURE_2D);
213      glDisableClientState(GL_COLOR_ARRAY);
214      glDisableClientState(GL_TEXTURE_COORD_ARRAY);
215      glDisableClientState(GL_VERTEX_ARRAY);
216      */
217 };
218 
219 /**
220  * <p>
221  *  Increments the GL Draws counts by one.<br/>
222  *  The number of calls per frame are displayed on the screen when the CCDirector's stats are enabled.<br/>
223  * </p>
224  * @param {Number} addNumber
225  * @function
226  */
227 cc.incrementGLDraws = function (addNumber) {
228     cc.g_NumberOfDraws += addNumber;
229 };
230 
231 /**
232  * @constant
233  * @type Number
234  */
235 cc.FLT_EPSILON = 0.0000001192092896;
236 
237 /**
238  * <p>
239  *     On Mac it returns 1;<br/>
240  *     On iPhone it returns 2 if RetinaDisplay is On. Otherwise it returns 1
241  * </p>
242  * @function
243  */
244 cc.contentScaleFactor = cc.IS_RETINA_DISPLAY_SUPPORTED ? function () {
245     return cc.director.getContentScaleFactor();
246 } : function () {
247     return 1;
248 };
249 
250 /**
251  * Converts a Point in points to pixels
252  * @param {cc.Point} points
253  * @return {cc.Point}
254  * @function
255  */
256 cc.pointPointsToPixels = function (points) {
257     var scale = cc.contentScaleFactor();
258     return cc.p(points.x * scale, points.y * scale);
259 };
260 
261 /**
262  * Converts a Point in pixels to points
263  * @param {Point} pixels
264  * @function
265  */
266 cc.pointPixelsToPoints = function (pixels) {
267 	var scale = cc.contentScaleFactor();
268 	return cc.p(pixels.x / scale, pixels.y / scale);
269 };
270 
271 cc._pointPixelsToPointsOut = function(pixels, outPoint){
272 	var scale = cc.contentScaleFactor();
273 	outPoint.x = pixels.x / scale;
274 	outPoint.y = pixels.y / scale;
275 };
276 
277 /**
278  * Converts a Size in points to pixels
279  * @param {cc.Size} sizeInPoints
280  * @return {cc.Size}
281  * @function
282  */
283 cc.sizePointsToPixels = function (sizeInPoints) {
284     var scale = cc.contentScaleFactor();
285     return cc.size(sizeInPoints.width * scale, sizeInPoints.height * scale);
286 };
287 
288 /**
289  * Converts a size in pixels to points
290  * @param {cc.Size} sizeInPixels
291  * @return {cc.Size}
292  * @function
293  */
294 cc.sizePixelsToPoints = function (sizeInPixels) {
295     var scale = cc.contentScaleFactor();
296     return cc.size(sizeInPixels.width / scale, sizeInPixels.height / scale);
297 };
298 
299 cc._sizePixelsToPointsOut = function (sizeInPixels, outSize) {
300     var scale = cc.contentScaleFactor();
301     outSize.width = sizeInPixels.width / scale;
302     outSize.height = sizeInPixels.height / scale;
303 };
304 
305 /**
306  * Converts a rect in pixels to points
307  * @param {cc.Rect} pixel
308  * @function
309  */
310 cc.rectPixelsToPoints = cc.IS_RETINA_DISPLAY_SUPPORTED ? function (pixel) {
311     var scale = cc.contentScaleFactor();
312     return cc.rect(pixel.x / scale, pixel.y / scale,
313         pixel.width / scale, pixel.height / scale);
314 } : function (p) {
315     return p;
316 };
317 
318 /**
319  * Converts a rect in points to pixels
320  * @param {cc.Rect} point
321  * @function
322  */
323 cc.rectPointsToPixels = cc.IS_RETINA_DISPLAY_SUPPORTED ? function (point) {
324    var scale = cc.contentScaleFactor();
325     return cc.rect(point.x * scale, point.y * scale,
326         point.width * scale, point.height * scale);
327 } : function (p) {
328     return p;
329 };
330 
331 /**
332  * @constant
333  * @type Number
334  */
335 cc.ONE = 1;
336 
337 /**
338  * @constant
339  * @type Number
340  */
341 cc.ZERO = 0;
342 
343 /**
344  * @constant
345  * @type Number
346  */
347 cc.SRC_ALPHA = 0x0302;
348 
349 /**
350  * @constant
351  * @type Number
352  */
353 cc.SRC_ALPHA_SATURATE = 0x308;
354 
355 /**
356  * @constant
357  * @type Number
358  */
359 cc.SRC_COLOR = 0x300;
360 
361 /**
362  * @constant
363  * @type Number
364  */
365 cc.DST_ALPHA = 0x304;
366 
367 /**
368  * @constant
369  * @type Number
370  */
371 cc.DST_COLOR = 0x306;
372 
373 /**
374  * @constant
375  * @type Number
376  */
377 cc.ONE_MINUS_SRC_ALPHA = 0x0303;
378 
379 /**
380  * @constant
381  * @type Number
382  */
383 cc.ONE_MINUS_SRC_COLOR = 0x301;
384 
385 /**
386  * @constant
387  * @type Number
388  */
389 cc.ONE_MINUS_DST_ALPHA = 0x305;
390 
391 /**
392  * @constant
393  * @type Number
394  */
395 cc.ONE_MINUS_DST_COLOR = 0x0307;
396 
397 /**
398  * @constant
399  * @type Number
400  */
401 cc.ONE_MINUS_CONSTANT_ALPHA	= 0x8004;
402 
403 /**
404  * @constant
405  * @type Number
406  */
407 cc.ONE_MINUS_CONSTANT_COLOR	= 0x8002;
408 
409 cc.checkGLErrorDebug = function () {
410     if (cc.renderMode == cc._RENDER_TYPE_WEBGL) {
411         var _error = cc._renderContext.getError();
412         if (_error) {
413             cc.log(CC._localZOrder.checkGLErrorDebug, _error);
414         }
415     }
416 };
417 
418 //Possible device orientations
419 /**
420  * Device oriented vertically, home button on the bottom (UIDeviceOrientationPortrait)
421  * @constant
422  * @type Number
423  */
424 cc.DEVICE_ORIENTATION_PORTRAIT = 0;
425 
426 /**
427  * Device oriented horizontally, home button on the right (UIDeviceOrientationLandscapeLeft)
428  * @constant
429  * @type Number
430  */
431 cc.DEVICE_ORIENTATION_LANDSCAPE_LEFT = 1;
432 
433 /**
434  * Device oriented vertically, home button on the top (UIDeviceOrientationPortraitUpsideDown)
435  * @constant
436  * @type Number
437  */
438 cc.DEVICE_ORIENTATION_PORTRAIT_UPSIDE_DOWN = 2;
439 
440 /**
441  * Device oriented horizontally, home button on the left (UIDeviceOrientationLandscapeRight)
442  * @constant
443  * @type Number
444  */
445 cc.DEVICE_ORIENTATION_LANDSCAPE_RIGHT = 3;
446 
447 /**
448  * In browsers, we only support 2 orientations by change window size.
449  * @constant
450  * @type Number
451  */
452 cc.DEVICE_MAX_ORIENTATIONS = 2;
453 
454 
455 // ------------------- vertex attrib flags -----------------------------
456 /**
457  * @constant
458  * @type {Number}
459  */
460 cc.VERTEX_ATTRIB_FLAG_NONE = 0;
461 /**
462  * @constant
463  * @type {Number}
464  */
465 cc.VERTEX_ATTRIB_FLAG_POSITION = 1 << 0;
466 /**
467  * @constant
468  * @type {Number}
469  */
470 cc.VERTEX_ATTRIB_FLAG_COLOR = 1 << 1;
471 /**
472  * @constant
473  * @type {Number}
474  */
475 cc.VERTEX_ATTRIB_FLAG_TEX_COORDS = 1 << 2;
476 /**
477  * @constant
478  * @type {Number}
479  */
480 cc.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX = ( cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_COLOR | cc.VERTEX_ATTRIB_FLAG_TEX_COORDS );
481 
482 /**
483  * GL server side states
484  * @constant
485  * @type {Number}
486  */
487 cc.GL_ALL = 0;
488 
489 //-------------Vertex Attributes-----------
490 /**
491  * @constant
492  * @type {Number}
493  */
494 cc.VERTEX_ATTRIB_POSITION = 0;
495 /**
496  * @constant
497  * @type {Number}
498  */
499 cc.VERTEX_ATTRIB_COLOR = 1;
500 /**
501  * @constant
502  * @type {Number}
503  */
504 cc.VERTEX_ATTRIB_TEX_COORDS = 2;
505 /**
506  * @constant
507  * @type {Number}
508  */
509 cc.VERTEX_ATTRIB_MAX = 3;
510 
511 //------------Uniforms------------------
512 /**
513  * @constant
514  * @type {Number}
515  */
516 cc.UNIFORM_PMATRIX = 0;
517 /**
518  * @constant
519  * @type {Number}
520  */
521 cc.UNIFORM_MVMATRIX = 1;
522 /**
523  * @constant
524  * @type {Number}
525  */
526 cc.UNIFORM_MVPMATRIX = 2;
527 /**
528  * @constant
529  * @type {Number}
530  */
531 cc.UNIFORM_TIME = 3;
532 /**
533  * @constant
534  * @type {Number}
535  */
536 cc.UNIFORM_SINTIME = 4;
537 /**
538  * @constant
539  * @type {Number}
540  */
541 cc.UNIFORM_COSTIME = 5;
542 /**
543  * @constant
544  * @type {Number}
545  */
546 cc.UNIFORM_RANDOM01 = 6;
547 /**
548  * @constant
549  * @type {Number}
550  */
551 cc.UNIFORM_SAMPLER = 7;
552 /**
553  * @constant
554  * @type {Number}
555  */
556 cc.UNIFORM_MAX = 8;
557 
558 //------------Shader Name---------------
559 /**
560  * @constant
561  * @type {String}
562  */
563 cc.SHADER_POSITION_TEXTURECOLOR = "ShaderPositionTextureColor";
564 /**
565  * @constant
566  * @type {String}
567  */
568 cc.SHADER_POSITION_TEXTURECOLORALPHATEST = "ShaderPositionTextureColorAlphaTest";
569 /**
570  * @constant
571  * @type {String}
572  */
573 cc.SHADER_POSITION_COLOR = "ShaderPositionColor";
574 /**
575  * @constant
576  * @type {String}
577  */
578 cc.SHADER_POSITION_TEXTURE = "ShaderPositionTexture";
579 /**
580  * @constant
581  * @type {String}
582  */
583 cc.SHADER_POSITION_TEXTURE_UCOLOR = "ShaderPositionTexture_uColor";
584 /**
585  * @constant
586  * @type {String}
587  */
588 cc.SHADER_POSITION_TEXTUREA8COLOR = "ShaderPositionTextureA8Color";
589 /**
590  * @constant
591  * @type {String}
592  */
593 cc.SHADER_POSITION_UCOLOR = "ShaderPosition_uColor";
594 /**
595  * @constant
596  * @type {String}
597  */
598 cc.SHADER_POSITION_LENGTHTEXTURECOLOR = "ShaderPositionLengthTextureColor";
599 
600 //------------uniform names----------------
601 /**
602  * @constant
603  * @type {String}
604  */
605 cc.UNIFORM_PMATRIX_S = "CC_PMatrix";
606 /**
607  * @constant
608  * @type {String}
609  */
610 cc.UNIFORM_MVMATRIX_S = "CC_MVMatrix";
611 /**
612  * @constant
613  * @type {String}
614  */
615 cc.UNIFORM_MVPMATRIX_S = "CC_MVPMatrix";
616 /**
617  * @constant
618  * @type {String}
619  */
620 cc.UNIFORM_TIME_S = "CC_Time";
621 /**
622  * @constant
623  * @type {String}
624  */
625 cc.UNIFORM_SINTIME_S = "CC_SinTime";
626 /**
627  * @constant
628  * @type {String}
629  */
630 cc.UNIFORM_COSTIME_S = "CC_CosTime";
631 /**
632  * @constant
633  * @type {String}
634  */
635 cc.UNIFORM_RANDOM01_S = "CC_Random01";
636 /**
637  * @constant
638  * @type {String}
639  */
640 cc.UNIFORM_SAMPLER_S = "CC_Texture0";
641 /**
642  * @constant
643  * @type {String}
644  */
645 cc.UNIFORM_ALPHA_TEST_VALUE_S = "CC_alpha_value";
646 
647 //------------Attribute names--------------
648 /**
649  * @constant
650  * @type {String}
651  */
652 cc.ATTRIBUTE_NAME_COLOR = "a_color";
653 /**
654  * @constant
655  * @type {String}
656  */
657 cc.ATTRIBUTE_NAME_POSITION = "a_position";
658 /**
659  * @constant
660  * @type {String}
661  */
662 cc.ATTRIBUTE_NAME_TEX_COORD = "a_texCoord";
663 
664 
665 /**
666  * default size for font size
667  * @constant
668  * @type Number
669  */
670 cc.ITEM_SIZE = 32;
671 
672 /**
673  * default tag for current item
674  * @constant
675  * @type Number
676  */
677 cc.CURRENT_ITEM = 0xc0c05001;
678 /**
679  * default tag for zoom action tag
680  * @constant
681  * @type Number
682  */
683 cc.ZOOM_ACTION_TAG = 0xc0c05002;
684 /**
685  * default tag for normal
686  * @constant
687  * @type Number
688  */
689 cc.NORMAL_TAG = 8801;
690 
691 /**
692  * default selected tag
693  * @constant
694  * @type Number
695  */
696 cc.SELECTED_TAG = 8802;
697 
698 /**
699  * default disabled tag
700  * @constant
701  * @type Number
702  */
703 cc.DISABLE_TAG = 8803;