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 = 0xffffffff; 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.Assert(false, "CC_SWAP is being modified from original macro, please check usage"); 84 } 85 }; 86 87 /** 88 * <p> 89 * Linear interpolation between 2 numbers, the ratio sets how much it is biased to each end 90 * </p> 91 * @param {Number} a number A 92 * @param {Number} b number B 93 * @param {Number} r ratio between 0 and 1 94 * @function 95 * @example 96 * cc.lerp(2,10,0.5)//returns 6<br/> 97 * cc.lerp(2,10,0.2)//returns 3.6 98 */ 99 cc.lerp = function (a, b, r) { 100 return a + (b - a) * r; 101 }; 102 103 /** 104 * returns a random float between -1 and 1 105 * @return {Number} 106 * @function 107 */ 108 cc.RANDOM_MINUS1_1 = function () { 109 return (Math.random() - 0.5) * 2; 110 }; 111 112 /** 113 * returns a random float between 0 and 1 114 * @return {Number} 115 * @function 116 */ 117 cc.RANDOM_0_1 = function () { 118 return Math.random(); 119 }; 120 121 /** 122 * converts degrees to radians 123 * @param {Number} angle 124 * @return {Number} 125 * @function 126 */ 127 cc.DEGREES_TO_RADIANS = function (angle) { 128 return angle * cc.RAD; 129 }; 130 131 /** 132 * converts radians to degrees 133 * @param {Number} angle 134 * @return {Number} 135 * @function 136 */ 137 cc.RADIANS_TO_DEGREES = function (angle) { 138 return angle * cc.DEG; 139 }; 140 141 /** 142 * @constant 143 * @type Number 144 */ 145 cc.REPEAT_FOREVER = Number.MAX_VALUE - 1; 146 147 /** 148 * default gl blend src function. Compatible with premultiplied alpha images. 149 * @constant 150 * @type Number 151 */ 152 cc.BLEND_SRC = cc.OPTIMIZE_BLEND_FUNC_FOR_PREMULTIPLIED_ALPHA ? 1 : 0x0302; 153 154 /** 155 * default gl blend dst function. Compatible with premultiplied alpha images. 156 * @constant 157 * @type Number 158 */ 159 cc.BLEND_DST = 0x0303; 160 161 /** 162 * Helpful macro that setups the GL server state, the correct GL program and sets the Model View Projection matrix 163 * @param {cc.Node} node setup node 164 * @function 165 */ 166 cc.NODE_DRAW_SETUP = function (node) { 167 ccGLEnable(node._glServerState); 168 cc.Assert(node.getShaderProgram(), "No shader program set for this node"); 169 { 170 node.getShaderProgram().use(); 171 node.getShaderProgram().setUniformForModelViewProjectionMatrix(); 172 } 173 }; 174 175 /** 176 * <p> 177 * GL states that are enabled:<br/> 178 * - GL_TEXTURE_2D<br/> 179 * - GL_VERTEX_ARRAY<br/> 180 * - GL_TEXTURE_COORD_ARRAY<br/> 181 * - GL_COLOR_ARRAY<br/> 182 * </p> 183 * @function 184 */ 185 cc.ENABLE_DEFAULT_GL_STATES = function () { 186 //TODO OPENGL STUFF 187 /* 188 glEnableClientState(GL_VERTEX_ARRAY); 189 glEnableClientState(GL_COLOR_ARRAY); 190 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 191 glEnable(GL_TEXTURE_2D);*/ 192 }; 193 194 /** 195 * <p> 196 * Disable default GL states:<br/> 197 * - GL_TEXTURE_2D<br/> 198 * - GL_TEXTURE_COORD_ARRAY<br/> 199 * - GL_COLOR_ARRAY<br/> 200 * </p> 201 * @function 202 */ 203 cc.DISABLE_DEFAULT_GL_STATES = function () { 204 //TODO OPENGL 205 /* 206 glDisable(GL_TEXTURE_2D); 207 glDisableClientState(GL_COLOR_ARRAY); 208 glDisableClientState(GL_TEXTURE_COORD_ARRAY); 209 glDisableClientState(GL_VERTEX_ARRAY); 210 */ 211 }; 212 213 /** 214 * <p> 215 * Increments the GL Draws counts by one.<br/> 216 * The number of calls per frame are displayed on the screen when the CCDirector's stats are enabled.<br/> 217 * </p> 218 * @param {Number} addNumber 219 * @function 220 */ 221 cc.INCREMENT_GL_DRAWS = function (addNumber) { 222 cc.g_NumberOfDraws += addNumber; 223 }; 224 225 /** 226 * @constant 227 * @type Number 228 */ 229 cc.FLT_EPSILON = 0.0000001192092896; 230 231 /** 232 * <p> 233 * On Mac it returns 1;<br/> 234 * On iPhone it returns 2 if RetinaDisplay is On. Otherwise it returns 1 235 * </p> 236 * @function 237 */ 238 cc.CONTENT_SCALE_FACTOR = cc.IS_RETINA_DISPLAY_SUPPORTED ? function () { 239 return cc.Director.getInstance().getContentScaleFactor(); 240 } : function () { 241 return 1; 242 }; 243 244 /** 245 * Converts a rect in points to pixels 246 * @param {cc.Point} points 247 * @return {cc.Point} 248 * @function 249 */ 250 cc.POINT_POINTS_TO_PIXELS = function (points) { 251 return cc.p(points.x * cc.CONTENT_SCALE_FACTOR(), points.y * cc.CONTENT_SCALE_FACTOR()) 252 }; 253 254 /** 255 * Converts a rect 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 return cc.size(sizeInPoints.width * cc.CONTENT_SCALE_FACTOR(), sizeInPoints.height * cc.CONTENT_SCALE_FACTOR()); 262 }; 263 264 /** 265 * Converts a rect in pixels to points 266 * @param {cc.Size} sizeInPixels 267 * @return {cc.Size} 268 * @function 269 */ 270 cc.SIZE_PIXELS_TO_POINTS = function (sizeInPixels) { 271 return cc.size(sizeInPixels.width / cc.CONTENT_SCALE_FACTOR(), sizeInPixels.height / cc.CONTENT_SCALE_FACTOR()); 272 }; 273 274 /** 275 * Converts a rect in pixels to points 276 * @param pixels 277 * @function 278 */ 279 cc.POINT_PIXELS_TO_POINTS = function (pixels) { 280 return cc.p(pixels.x / cc.CONTENT_SCALE_FACTOR(), pixels.y / cc.CONTENT_SCALE_FACTOR()); 281 }; 282 283 284 /** 285 * Converts a rect in pixels to points 286 * @param {cc.Rect} pixel 287 * @function 288 */ 289 cc.RECT_PIXELS_TO_POINTS = cc.IS_RETINA_DISPLAY_SUPPORTED ? function (pixel) { 290 return cc.rect(pixel.origin.x / cc.CONTENT_SCALE_FACTOR(), pixel.origin.y / cc.CONTENT_SCALE_FACTOR(), 291 pixel.size.width / cc.CONTENT_SCALE_FACTOR(), pixel.size.height / cc.CONTENT_SCALE_FACTOR()); 292 } : function (p) { 293 return p; 294 }; 295 296 /** 297 * Converts a rect in points to pixels 298 * @param {cc.Rect} point 299 * @function 300 */ 301 cc.RECT_POINTS_TO_PIXELS = cc.IS_RETINA_DISPLAY_SUPPORTED ? function (point) { 302 return cc.rect(point.origin.x * cc.CONTENT_SCALE_FACTOR(), point.origin.y * cc.CONTENT_SCALE_FACTOR(), 303 point.size.width * cc.CONTENT_SCALE_FACTOR(), point.size.height * cc.CONTENT_SCALE_FACTOR()); 304 } : function (p) { 305 return p; 306 }; 307 308 309 /** 310 * WebGL constants 311 * @type {object} 312 */ 313 var gl = gl || {}; 314 315 /** 316 * @constant 317 * @type Number 318 */ 319 gl.NEAREST = 0x2600; 320 321 /** 322 * @constant 323 * @type Number 324 */ 325 gl.LINEAR = 0x2601; 326 /** 327 * @constant 328 * @type Number 329 */ 330 gl.REPEAT = 0x2901; 331 /** 332 * @constant 333 * @type Number 334 */ 335 gl.CLAMP_TO_EDGE = 0x812F; 336 /** 337 * @constant 338 * @type Number 339 */ 340 gl.CLAMP_TO_BORDER = 0x812D; 341 /** 342 * @constant 343 * @type Number 344 */ 345 gl.LINEAR_MIPMAP_NEAREST = 0x2701; 346 /** 347 * @constant 348 * @type Number 349 */ 350 gl.NEAREST_MIPMAP_NEAREST = 0x2700; 351 /** 352 * @constant 353 * @type Number 354 */ 355 gl.ZERO = 0; 356 /** 357 * @constant 358 * @type Number 359 */ 360 gl.ONE = 1; 361 /** 362 * @constant 363 * @type Number 364 */ 365 gl.SRC_COLOR = 0x0300; 366 /** 367 * @constant 368 * @type Number 369 */ 370 gl.ONE_MINUS_SRC_COLOR = 0x0301; 371 /** 372 * @constant 373 * @type Number 374 */ 375 gl.SRC_ALPHA = 0x0302; 376 /** 377 * @constant 378 * @type Number 379 */ 380 gl.ONE_MINUS_SRC_ALPHA = 0x0303; 381 /** 382 * @constant 383 * @type Number 384 */ 385 gl.DST_ALPHA = 0x0304; 386 /** 387 * @constant 388 * @type Number 389 */ 390 gl.ONE_MINUS_DST_ALPHA = 0x0305; 391 /** 392 * @constant 393 * @type Number 394 */ 395 gl.DST_COLOR = 0x0306; 396 /** 397 * @constant 398 * @type Number 399 */ 400 gl.ONE_MINUS_DST_COLOR = 0x0307; 401 /** 402 * @constant 403 * @type Number 404 */ 405 gl.SRC_ALPHA_SATURATE = 0x0308; 406 407