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.ONE_MINUS_DST_COLOR = 0x0307; 406 407 /** 408 * @constant 409 * @type Number 410 */ 411 cc.ONE_MINUS_CONSTANT_ALPHA = 0x8004; 412 413 /** 414 * @constant 415 * @type Number 416 */ 417 cc.ONE_MINUS_CONSTANT_COLOR = 0x8002; 418 419 cc.checkGLErrorDebug = function () { 420 if (cc.renderMode == cc._RENDER_TYPE_WEBGL) { 421 var _error = cc._renderContext.getError(); 422 if (_error) { 423 cc.log(CC._localZOrder.checkGLErrorDebug, _error); 424 } 425 } 426 }; 427 428 //Possible device orientations 429 /** 430 * Device oriented vertically, home button on the bottom (UIDeviceOrientationPortrait) 431 * @constant 432 * @type Number 433 */ 434 cc.DEVICE_ORIENTATION_PORTRAIT = 0; 435 436 /** 437 * Device oriented horizontally, home button on the right (UIDeviceOrientationLandscapeLeft) 438 * @constant 439 * @type Number 440 */ 441 cc.DEVICE_ORIENTATION_LANDSCAPE_LEFT = 1; 442 443 /** 444 * Device oriented vertically, home button on the top (UIDeviceOrientationPortraitUpsideDown) 445 * @constant 446 * @type Number 447 */ 448 cc.DEVICE_ORIENTATION_PORTRAIT_UPSIDE_DOWN = 2; 449 450 /** 451 * Device oriented horizontally, home button on the left (UIDeviceOrientationLandscapeRight) 452 * @constant 453 * @type Number 454 */ 455 cc.DEVICE_ORIENTATION_LANDSCAPE_RIGHT = 3; 456 457 /** 458 * In browsers, we only support 2 orientations by change window size. 459 * @constant 460 * @type Number 461 */ 462 cc.DEVICE_MAX_ORIENTATIONS = 2; 463 464 465 // ------------------- vertex attrib flags ----------------------------- 466 /** 467 * @constant 468 * @type {Number} 469 */ 470 cc.VERTEX_ATTRIB_FLAG_NONE = 0; 471 /** 472 * @constant 473 * @type {Number} 474 */ 475 cc.VERTEX_ATTRIB_FLAG_POSITION = 1 << 0; 476 /** 477 * @constant 478 * @type {Number} 479 */ 480 cc.VERTEX_ATTRIB_FLAG_COLOR = 1 << 1; 481 /** 482 * @constant 483 * @type {Number} 484 */ 485 cc.VERTEX_ATTRIB_FLAG_TEX_COORDS = 1 << 2; 486 /** 487 * @constant 488 * @type {Number} 489 */ 490 cc.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX = ( cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_COLOR | cc.VERTEX_ATTRIB_FLAG_TEX_COORDS ); 491 492 /** 493 * GL server side states 494 * @constant 495 * @type {Number} 496 */ 497 cc.GL_ALL = 0; 498 499 //-------------Vertex Attributes----------- 500 /** 501 * @constant 502 * @type {Number} 503 */ 504 cc.VERTEX_ATTRIB_POSITION = 0; 505 /** 506 * @constant 507 * @type {Number} 508 */ 509 cc.VERTEX_ATTRIB_COLOR = 1; 510 /** 511 * @constant 512 * @type {Number} 513 */ 514 cc.VERTEX_ATTRIB_TEX_COORDS = 2; 515 /** 516 * @constant 517 * @type {Number} 518 */ 519 cc.VERTEX_ATTRIB_MAX = 3; 520 521 //------------Uniforms------------------ 522 /** 523 * @constant 524 * @type {Number} 525 */ 526 cc.UNIFORM_PMATRIX = 0; 527 /** 528 * @constant 529 * @type {Number} 530 */ 531 cc.UNIFORM_MVMATRIX = 1; 532 /** 533 * @constant 534 * @type {Number} 535 */ 536 cc.UNIFORM_MVPMATRIX = 2; 537 /** 538 * @constant 539 * @type {Number} 540 */ 541 cc.UNIFORM_TIME = 3; 542 /** 543 * @constant 544 * @type {Number} 545 */ 546 cc.UNIFORM_SINTIME = 4; 547 /** 548 * @constant 549 * @type {Number} 550 */ 551 cc.UNIFORM_COSTIME = 5; 552 /** 553 * @constant 554 * @type {Number} 555 */ 556 cc.UNIFORM_RANDOM01 = 6; 557 /** 558 * @constant 559 * @type {Number} 560 */ 561 cc.UNIFORM_SAMPLER = 7; 562 /** 563 * @constant 564 * @type {Number} 565 */ 566 cc.UNIFORM_MAX = 8; 567 568 //------------Shader Name--------------- 569 /** 570 * @constant 571 * @type {String} 572 */ 573 cc.SHADER_POSITION_TEXTURECOLOR = "ShaderPositionTextureColor"; 574 /** 575 * @constant 576 * @type {String} 577 */ 578 cc.SHADER_POSITION_TEXTURECOLORALPHATEST = "ShaderPositionTextureColorAlphaTest"; 579 /** 580 * @constant 581 * @type {String} 582 */ 583 cc.SHADER_POSITION_COLOR = "ShaderPositionColor"; 584 /** 585 * @constant 586 * @type {String} 587 */ 588 cc.SHADER_POSITION_TEXTURE = "ShaderPositionTexture"; 589 /** 590 * @constant 591 * @type {String} 592 */ 593 cc.SHADER_POSITION_TEXTURE_UCOLOR = "ShaderPositionTexture_uColor"; 594 /** 595 * @constant 596 * @type {String} 597 */ 598 cc.SHADER_POSITION_TEXTUREA8COLOR = "ShaderPositionTextureA8Color"; 599 /** 600 * @constant 601 * @type {String} 602 */ 603 cc.SHADER_POSITION_UCOLOR = "ShaderPosition_uColor"; 604 /** 605 * @constant 606 * @type {String} 607 */ 608 cc.SHADER_POSITION_LENGTHTEXTURECOLOR = "ShaderPositionLengthTextureColor"; 609 610 //------------uniform names---------------- 611 /** 612 * @constant 613 * @type {String} 614 */ 615 cc.UNIFORM_PMATRIX_S = "CC_PMatrix"; 616 /** 617 * @constant 618 * @type {String} 619 */ 620 cc.UNIFORM_MVMATRIX_S = "CC_MVMatrix"; 621 /** 622 * @constant 623 * @type {String} 624 */ 625 cc.UNIFORM_MVPMATRIX_S = "CC_MVPMatrix"; 626 /** 627 * @constant 628 * @type {String} 629 */ 630 cc.UNIFORM_TIME_S = "CC_Time"; 631 /** 632 * @constant 633 * @type {String} 634 */ 635 cc.UNIFORM_SINTIME_S = "CC_SinTime"; 636 /** 637 * @constant 638 * @type {String} 639 */ 640 cc.UNIFORM_COSTIME_S = "CC_CosTime"; 641 /** 642 * @constant 643 * @type {String} 644 */ 645 cc.UNIFORM_RANDOM01_S = "CC_Random01"; 646 /** 647 * @constant 648 * @type {String} 649 */ 650 cc.UNIFORM_SAMPLER_S = "CC_Texture0"; 651 /** 652 * @constant 653 * @type {String} 654 */ 655 cc.UNIFORM_ALPHA_TEST_VALUE_S = "CC_alpha_value"; 656 657 //------------Attribute names-------------- 658 /** 659 * @constant 660 * @type {String} 661 */ 662 cc.ATTRIBUTE_NAME_COLOR = "a_color"; 663 /** 664 * @constant 665 * @type {String} 666 */ 667 cc.ATTRIBUTE_NAME_POSITION = "a_position"; 668 /** 669 * @constant 670 * @type {String} 671 */ 672 cc.ATTRIBUTE_NAME_TEX_COORD = "a_texCoord"; 673 674 675 /** 676 * default size for font size 677 * @constant 678 * @type Number 679 */ 680 cc.ITEM_SIZE = 32; 681 682 /** 683 * default tag for current item 684 * @constant 685 * @type Number 686 */ 687 cc.CURRENT_ITEM = 0xc0c05001; 688 /** 689 * default tag for zoom action tag 690 * @constant 691 * @type Number 692 */ 693 cc.ZOOM_ACTION_TAG = 0xc0c05002; 694 /** 695 * default tag for normal 696 * @constant 697 * @type Number 698 */ 699 cc.NORMAL_TAG = 8801; 700 701 /** 702 * default selected tag 703 * @constant 704 * @type Number 705 */ 706 cc.SELECTED_TAG = 8802; 707 708 /** 709 * default disabled tag 710 * @constant 711 * @type Number 712 */ 713 cc.DISABLE_TAG = 8803;