CCMacro.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * @constant
  24. * @type Number
  25. */
  26. cc.INVALID_INDEX = -1;
  27. /**
  28. * PI is the ratio of a circle's circumference to its diameter.
  29. * @constant
  30. * @type Number
  31. */
  32. cc.PI = Math.PI;
  33. /**
  34. * @constant
  35. * @type Number
  36. */
  37. cc.FLT_MAX = parseFloat('3.402823466e+38F');
  38. /**
  39. * @constant
  40. * @type Number
  41. */
  42. cc.RAD = cc.PI / 180;
  43. /**
  44. * @constant
  45. * @type Number
  46. */
  47. cc.DEG = 180 / cc.PI;
  48. /**
  49. * maximum unsigned int value
  50. * @constant
  51. * @type Number
  52. */
  53. cc.UINT_MAX = 0xffffffff;
  54. /**
  55. * <p>
  56. * simple macro that swaps 2 variables<br/>
  57. * modified from c++ macro, you need to pass in the x and y variables names in string, <br/>
  58. * and then a reference to the whole object as third variable
  59. * </p>
  60. * @param x
  61. * @param y
  62. * @param ref
  63. * @function
  64. * @deprecated
  65. */
  66. cc.SWAP = function (x, y, ref) {
  67. if ((typeof ref) == 'object' && (typeof ref.x) != 'undefined' && (typeof ref.y) != 'undefined') {
  68. var tmp = ref[x];
  69. ref[x] = ref[y];
  70. ref[y] = tmp;
  71. } else
  72. cc.log("cc.SWAP is being modified from original macro, please check usage");
  73. };
  74. /**
  75. * <p>
  76. * Linear interpolation between 2 numbers, the ratio sets how much it is biased to each end
  77. * </p>
  78. * @param {Number} a number A
  79. * @param {Number} b number B
  80. * @param {Number} r ratio between 0 and 1
  81. * @function
  82. * @example
  83. * cc.lerp(2,10,0.5)//returns 6<br/>
  84. * cc.lerp(2,10,0.2)//returns 3.6
  85. */
  86. cc.lerp = function (a, b, r) {
  87. return a + (b - a) * r;
  88. };
  89. /**
  90. * returns a random float between -1 and 1
  91. * @return {Number}
  92. * @function
  93. */
  94. cc.RANDOM_MINUS1_1 = function () {
  95. return (Math.random() - 0.5) * 2;
  96. };
  97. /**
  98. * returns a random float between 0 and 1
  99. * @return {Number}
  100. * @function
  101. */
  102. cc.RANDOM_0_1 = function () {
  103. return Math.random();
  104. };
  105. /**
  106. * converts degrees to radians
  107. * @param {Number} angle
  108. * @return {Number}
  109. * @function
  110. */
  111. cc.DEGREES_TO_RADIANS = function (angle) {
  112. return angle * cc.RAD;
  113. };
  114. /**
  115. * converts radians to degrees
  116. * @param {Number} angle
  117. * @return {Number}
  118. * @function
  119. */
  120. cc.RADIANS_TO_DEGREES = function (angle) {
  121. return angle * cc.DEG;
  122. };
  123. /**
  124. * @constant
  125. * @type Number
  126. */
  127. cc.REPEAT_FOREVER = Number.MAX_VALUE - 1;
  128. /**
  129. * default gl blend src function. Compatible with premultiplied alpha images.
  130. * @constant
  131. * @type Number
  132. */
  133. cc.BLEND_SRC = cc.OPTIMIZE_BLEND_FUNC_FOR_PREMULTIPLIED_ALPHA ? 1 : 0x0302;
  134. /**
  135. * default gl blend dst function. Compatible with premultiplied alpha images.
  136. * @constant
  137. * @type Number
  138. */
  139. cc.BLEND_DST = 0x0303;
  140. /**
  141. * Helpful macro that setups the GL server state, the correct GL program and sets the Model View Projection matrix
  142. * @param {cc.Node} node setup node
  143. * @function
  144. */
  145. cc.NODE_DRAW_SETUP = function (node) {
  146. //cc.glEnable(node._glServerState);
  147. if (node._shaderProgram) {
  148. //cc.renderContext.useProgram(node._shaderProgram._programObj);
  149. node._shaderProgram.use();
  150. node._shaderProgram.setUniformForModelViewAndProjectionMatrixWithMat4();
  151. }
  152. };
  153. /**
  154. * <p>
  155. * GL states that are enabled:<br/>
  156. * - GL_TEXTURE_2D<br/>
  157. * - GL_VERTEX_ARRAY<br/>
  158. * - GL_TEXTURE_COORD_ARRAY<br/>
  159. * - GL_COLOR_ARRAY<br/>
  160. * </p>
  161. * @function
  162. */
  163. cc.ENABLE_DEFAULT_GL_STATES = function () {
  164. //TODO OPENGL STUFF
  165. /*
  166. glEnableClientState(GL_VERTEX_ARRAY);
  167. glEnableClientState(GL_COLOR_ARRAY);
  168. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  169. glEnable(GL_TEXTURE_2D);*/
  170. };
  171. /**
  172. * <p>
  173. * Disable default GL states:<br/>
  174. * - GL_TEXTURE_2D<br/>
  175. * - GL_TEXTURE_COORD_ARRAY<br/>
  176. * - GL_COLOR_ARRAY<br/>
  177. * </p>
  178. * @function
  179. */
  180. cc.DISABLE_DEFAULT_GL_STATES = function () {
  181. //TODO OPENGL
  182. /*
  183. glDisable(GL_TEXTURE_2D);
  184. glDisableClientState(GL_COLOR_ARRAY);
  185. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  186. glDisableClientState(GL_VERTEX_ARRAY);
  187. */
  188. };
  189. /**
  190. * <p>
  191. * Increments the GL Draws counts by one.<br/>
  192. * The number of calls per frame are displayed on the screen when the CCDirector's stats are enabled.<br/>
  193. * </p>
  194. * @param {Number} addNumber
  195. * @function
  196. */
  197. cc.INCREMENT_GL_DRAWS = function (addNumber) {
  198. cc.g_NumberOfDraws += addNumber;
  199. };
  200. /**
  201. * @constant
  202. * @type Number
  203. */
  204. cc.FLT_EPSILON = 0.0000001192092896;
  205. /**
  206. * <p>
  207. * On Mac it returns 1;<br/>
  208. * On iPhone it returns 2 if RetinaDisplay is On. Otherwise it returns 1
  209. * </p>
  210. * @function
  211. */
  212. cc.CONTENT_SCALE_FACTOR = cc.IS_RETINA_DISPLAY_SUPPORTED ? function () {
  213. return cc.Director.getInstance().getContentScaleFactor();
  214. } : function () {
  215. return 1;
  216. };
  217. /**
  218. * Converts a Point in points to pixels
  219. * @param {cc.Point} points
  220. * @return {cc.Point}
  221. * @function
  222. */
  223. cc.POINT_POINTS_TO_PIXELS = function (points) {
  224. var scale = cc.CONTENT_SCALE_FACTOR();
  225. return cc.p(points.x * scale, points.y * scale);
  226. };
  227. /**
  228. * Converts a Size in points to pixels
  229. * @param {cc.Size} sizeInPoints
  230. * @return {cc.Size}
  231. * @function
  232. */
  233. cc.SIZE_POINTS_TO_PIXELS = function (sizeInPoints) {
  234. var scale = cc.CONTENT_SCALE_FACTOR();
  235. return cc.size(sizeInPoints.width * scale, sizeInPoints.height * scale);
  236. };
  237. /**
  238. * Converts a size in pixels to points
  239. * @param {cc.Size} sizeInPixels
  240. * @return {cc.Size}
  241. * @function
  242. */
  243. cc.SIZE_PIXELS_TO_POINTS = function (sizeInPixels) {
  244. var scale = cc.CONTENT_SCALE_FACTOR();
  245. return cc.size(sizeInPixels.width / scale, sizeInPixels.height / scale);
  246. };
  247. /**
  248. * Converts a Point in pixels to points
  249. * @param {Point} pixels
  250. * @function
  251. */
  252. cc.POINT_PIXELS_TO_POINTS = function (pixels) {
  253. var scale = cc.CONTENT_SCALE_FACTOR();
  254. return cc.p(pixels.x / scale, pixels.y / scale);
  255. };
  256. /**
  257. * Converts a rect in pixels to points
  258. * @param {cc.Rect} pixel
  259. * @function
  260. */
  261. cc.RECT_PIXELS_TO_POINTS = cc.IS_RETINA_DISPLAY_SUPPORTED ? function (pixel) {
  262. var scale = cc.CONTENT_SCALE_FACTOR();
  263. return cc.rect(pixel.x / scale, pixel.y / scale,
  264. pixel.width / scale, pixel.height / scale);
  265. } : function (p) {
  266. return p;
  267. };
  268. /**
  269. * Converts a rect in points to pixels
  270. * @param {cc.Rect} point
  271. * @function
  272. */
  273. cc.RECT_POINTS_TO_PIXELS = cc.IS_RETINA_DISPLAY_SUPPORTED ? function (point) {
  274. var scale = cc.CONTENT_SCALE_FACTOR();
  275. return cc.rect(point.x * scale, point.y * scale,
  276. point.width * scale, point.height * scale);
  277. } : function (p) {
  278. return p;
  279. };
  280. if (!cc.Browser.supportWebGL) {
  281. /**
  282. * WebGL constants
  283. * @type {object}
  284. */
  285. var gl = gl || {};
  286. /**
  287. * @constant
  288. * @type Number
  289. */
  290. gl.ONE = 1;
  291. /**
  292. * @constant
  293. * @type Number
  294. */
  295. gl.ZERO = 0;
  296. /**
  297. * @constant
  298. * @type Number
  299. */
  300. gl.SRC_ALPHA = 0x0302;
  301. /**
  302. * @constant
  303. * @type Number
  304. */
  305. gl.ONE_MINUS_SRC_ALPHA = 0x0303;
  306. /**
  307. * @constant
  308. * @type Number
  309. */
  310. gl.ONE_MINUS_DST_COLOR = 0x0307;
  311. }
  312. cc.CHECK_GL_ERROR_DEBUG = function () {
  313. if (cc.renderMode == cc.WEBGL) {
  314. var _error = cc.renderContext.getError();
  315. if (_error) {
  316. cc.log("WebGL error " + _error);
  317. }
  318. }
  319. };