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