CCApplication.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. * Device type
  24. * @constant
  25. * @type {Object}
  26. */
  27. cc.TARGET_PLATFORM = {
  28. WINDOWS:0,
  29. LINUX:1,
  30. MACOS:2,
  31. ANDROID:3,
  32. IPHONE:4,
  33. IPAD:5,
  34. BLACKBERRY:6,
  35. NACL:7,
  36. EMSCRIPTEN:8,
  37. MOBILE_BROWSER:100,
  38. PC_BROWSER:101
  39. };
  40. /**
  41. * Device oriented vertically, home button on the bottom
  42. * @constant
  43. * @type Number
  44. */
  45. cc.ORIENTATION_PORTRAIT = 0;
  46. /**
  47. * Device oriented vertically, home button on the top
  48. * @constant
  49. * @type Number
  50. */
  51. cc.ORIENTATION_PORTRAIT_UPSIDE_DOWN = 1;
  52. /**
  53. * Device oriented horizontally, home button on the right
  54. * @constant
  55. * @type Number
  56. */
  57. cc.ORIENTATION_LANDSCAPE_LEFT = 2;
  58. /**
  59. * Device oriented horizontally, home button on the left
  60. * @constant
  61. * @type Number
  62. */
  63. cc.ORIENTATION_LANDSCAPE_RIGHT = 3;
  64. //engine render type
  65. /**
  66. * Canvas of render type
  67. * @constant
  68. * @type Number
  69. */
  70. cc.CANVAS = 0;
  71. /**
  72. * WebGL of render type
  73. * @constant
  74. * @type Number
  75. */
  76. cc.WEBGL = 1;
  77. /**
  78. * drawing primitive of game engine
  79. * @type cc.DrawingPrimitive
  80. */
  81. cc.drawingUtil = null;
  82. /**
  83. * main Canvas 2D/3D Context of game engine
  84. * @type CanvasRenderingContext2D|WebGLRenderingContext
  85. */
  86. cc.renderContext = null;
  87. /**
  88. * main Canvas of game engine
  89. * @type HTMLCanvasElement
  90. */
  91. cc.canvas = null;
  92. /**
  93. * This Div element contain all game canvas
  94. * @type HTMLDivElement
  95. */
  96. cc.gameDiv = null;
  97. /**
  98. * current render type of game engine
  99. * @type Number
  100. */
  101. cc.renderContextType = cc.CANVAS;
  102. /**
  103. * save original size of canvas, use for resize canvas
  104. * @type cc.Size
  105. */
  106. cc.originalCanvasSize = cc.size(0, 0);
  107. window.requestAnimFrame = (function () {
  108. return window.requestAnimationFrame ||
  109. window.webkitRequestAnimationFrame ||
  110. window.mozRequestAnimationFrame ||
  111. window.oRequestAnimationFrame ||
  112. window.msRequestAnimationFrame
  113. })();
  114. if (!window.console) {
  115. window.console = {};
  116. window.console.log = function () {
  117. };
  118. window.console.assert = function () {
  119. };
  120. }
  121. cc.isAddedHiddenEvent = false;
  122. /**
  123. * <p>
  124. * setup game main canvas,renderContext,gameDiv and drawingUtil with argument <br/>
  125. * <br/>
  126. * can receive follow type of arguemnt: <br/>
  127. * - empty: create a canvas append to document's body, and setup other option <br/>
  128. * - string: search the element by document.getElementById(), <br/>
  129. * if this element is HTMLCanvasElement, set this element as main canvas of engine, and set it's ParentNode as cc.gameDiv.<br/>
  130. * if this element is HTMLDivElement, set it's ParentNode to cc.gameDiv, and create a canvas as main canvas of engine. <br/>
  131. * </p>
  132. * @function
  133. * @example
  134. * //setup with null
  135. * cc.setup();
  136. *
  137. * // setup with HTMLCanvasElement, gameCanvas is Canvas element
  138. * // declare like this: <canvas id="gameCanvas" width="800" height="450"></canvas>
  139. * cc.setup("gameCanvas");
  140. *
  141. * //setup with HTMLDivElement, gameDiv is Div element
  142. * // declare like this: <div id="Cocos2dGameContainer" width="800" height="450"></div>
  143. * cc.setup("Cocos2dGameContainer");
  144. */
  145. cc.setup = function (el, width, height) {
  146. var element = cc.$(el) || cc.$('#' + el);
  147. var localCanvas, localContainer, localConStyle;
  148. if (element.tagName == "CANVAS") {
  149. width = width || element.width;
  150. height = height || element.height;
  151. //it is already a canvas, we wrap it around with a div
  152. localContainer = cc.container = cc.$new("DIV");
  153. localConStyle = localContainer.style;
  154. localCanvas = cc.canvas = element;
  155. localCanvas.parentNode.insertBefore(localContainer, localCanvas);
  156. localCanvas.appendTo(localContainer);
  157. localConStyle.width = (width || 480) + "px";
  158. localConStyle.height = (height || 320) + "px";
  159. localContainer.setAttribute('id', 'Cocos2dGameContainer');
  160. localConStyle.margin = "0 auto";
  161. localCanvas.setAttribute("width", width || 480);
  162. localCanvas.setAttribute("height", height || 320);
  163. } else {//we must make a new canvas and place into this element
  164. if (element.tagName != "DIV") {
  165. cc.log("Warning: target element is not a DIV or CANVAS");
  166. }
  167. width = width || element.clientWidth;
  168. height = height || element.clientHeight;
  169. localCanvas = cc.canvas = cc.$new("CANVAS");
  170. localCanvas.addClass("gameCanvas");
  171. localCanvas.setAttribute("width", width || 480);
  172. localCanvas.setAttribute("height", height || 320);
  173. localContainer = cc.container = element;
  174. localConStyle = localContainer.style;
  175. element.appendChild(localCanvas);
  176. localConStyle.width = (width || 480) + "px";
  177. localConStyle.height = (height || 320) + "px";
  178. localConStyle.margin = "0 auto";
  179. }
  180. localConStyle.position = 'relative';
  181. localConStyle.overflow = 'hidden';
  182. localContainer.top = '100%';
  183. if(cc.__renderDoesnotSupport)
  184. return;
  185. if (cc.Browser.supportWebGL)
  186. cc.renderContext = cc.webglContext = cc.create3DContext(localCanvas,{
  187. 'stencil': true,
  188. 'preserveDrawingBuffer': true,
  189. 'antialias': !cc.Browser.isMobile,
  190. 'alpha': false});
  191. if(cc.renderContext){
  192. cc.renderContextType = cc.WEBGL;
  193. window.gl = cc.renderContext; // global variable declared in CCMacro.js
  194. cc.drawingUtil = new cc.DrawingPrimitiveWebGL(cc.renderContext);
  195. cc.TextureCache.getInstance()._initializingRenderer();
  196. } else {
  197. cc.renderContext = localCanvas.getContext("2d");
  198. cc.mainRenderContextBackup = cc.renderContext;
  199. cc.renderContextType = cc.CANVAS;
  200. cc.renderContext.translate(0, localCanvas.height);
  201. cc.drawingUtil = cc.DrawingPrimitiveCanvas ? new cc.DrawingPrimitiveCanvas(cc.renderContext) : null;
  202. }
  203. cc.originalCanvasSize = cc.size(localCanvas.width, localCanvas.height);
  204. cc.gameDiv = localContainer;
  205. cc.log(cc.ENGINE_VERSION);
  206. cc.Configuration.getInstance();
  207. cc.setContextMenuEnable(false);
  208. if(cc.Browser.isMobile){
  209. cc._addUserSelectStatus();
  210. }
  211. var isScreenHidden = false;
  212. var hidden, visibilityChange;
  213. if (typeof document.hidden !== "undefined") {
  214. hidden = "hidden";
  215. visibilityChange = "visibilitychange";
  216. } else if (typeof document.mozHidden !== "undefined") {
  217. hidden = "mozHidden";
  218. visibilityChange = "mozvisibilitychange";
  219. } else if (typeof document.msHidden !== "undefined") {
  220. hidden = "msHidden";
  221. visibilityChange = "msvisibilitychange";
  222. } else if (typeof document.webkitHidden !== "undefined") {
  223. hidden = "webkitHidden";
  224. visibilityChange = "webkitvisibilitychange";
  225. }
  226. function handleFocus() {
  227. if (!cc.AudioEngine || !isScreenHidden) return;
  228. isScreenHidden = false;
  229. var audioEngine = cc.AudioEngine.getInstance();
  230. audioEngine.resumeAllEffects();
  231. audioEngine.resumeMusic();
  232. }
  233. function handleBlur() {
  234. if (!cc.AudioEngine || isScreenHidden) return;
  235. isScreenHidden = true;
  236. var audioEngine = cc.AudioEngine.getInstance();
  237. audioEngine.pauseAllEffects();
  238. audioEngine.pauseMusic();
  239. }
  240. function handleVisibilityChange() {
  241. if (!document[hidden])
  242. handleFocus();
  243. else
  244. handleBlur();
  245. }
  246. if (typeof document.addEventListener === "undefined" ||
  247. typeof hidden === "undefined") {
  248. cc.isAddedHiddenEvent = false;
  249. window.addEventListener("focus", handleFocus, false);
  250. window.addEventListener("blur", handleBlur, false);
  251. } else {
  252. cc.isAddedHiddenEvent = true;
  253. document.addEventListener(visibilityChange, handleVisibilityChange, false);
  254. }
  255. if ("onpageshow" in window && "onpagehide" in window) {
  256. window.addEventListener("pageshow", handleFocus, false);
  257. window.addEventListener("pagehide", handleBlur, false);
  258. }
  259. };
  260. cc._addUserSelectStatus = function(){
  261. var fontStyle = document.createElement("style");
  262. fontStyle.type = "text/css";
  263. document.body.appendChild(fontStyle);
  264. fontStyle.textContent = "body,canvas,div{ -moz-user-select: none;-webkit-user-select: none;-ms-user-select: none;-khtml-user-select: none;"
  265. +"-webkit-tap-highlight-color:rgba(0,0,0,0);}";
  266. };
  267. cc._isContextMenuEnable = false;
  268. /**
  269. * enable/disable contextMenu for Canvas
  270. * @param {Boolean} enabled
  271. */
  272. cc.setContextMenuEnable = function (enabled) {
  273. cc._isContextMenuEnable = enabled;
  274. if (!cc._isContextMenuEnable) {
  275. cc.canvas.oncontextmenu = function () {
  276. return false;
  277. };
  278. } else {
  279. cc.canvas.oncontextmenu = function () {
  280. };
  281. }
  282. };
  283. /**
  284. * Run main loop of game engine
  285. * @class
  286. * @extends cc.Class
  287. */
  288. cc.Application = cc.Class.extend(/** @lends cc.Application# */{
  289. _animationInterval:null,
  290. /**
  291. * Constructor
  292. */
  293. ctor:function () {
  294. this._animationInterval = 0;
  295. if(cc._sharedApplication)
  296. throw "Application has been initialized";
  297. cc._sharedApplication = this;
  298. },
  299. /**
  300. * Callback by cc.Director for limit FPS.
  301. * @param {Number} interval The time, which expressed in second, between current frame and next.
  302. */
  303. setAnimationInterval:function (interval) {
  304. this._animationInterval = interval;
  305. },
  306. /**
  307. * Get status bar rectangle in EGLView window.
  308. * @param {cc.Rect} rect
  309. * @deprecated
  310. */
  311. statusBarFrame:function (rect) {
  312. if (rect) {
  313. // Windows doesn't have status bar.
  314. rect = cc.rect(0, 0, 0, 0);
  315. }
  316. },
  317. getTargetPlatform:function(){
  318. return cc.Browser.isMobile ? cc.TARGET_PLATFORM.MOBILE_BROWSER : cc.TARGET_PLATFORM.PC_BROWSER;
  319. },
  320. /**
  321. * Run the message loop.
  322. * @return {Number}
  323. */
  324. run:function () {
  325. // Initialize instance and cocos2d.
  326. if (!this.applicationDidFinishLaunching())
  327. return 0;
  328. var callback, director = cc.Director.getInstance(), w = window;
  329. cc.director = director;
  330. if (w.requestAnimFrame && this._animationInterval == 1 / 60) {
  331. callback = function () {
  332. director.mainLoop();
  333. w.requestAnimFrame(callback);
  334. };
  335. //cc.log(window.requestAnimFrame);
  336. w.requestAnimFrame(callback);
  337. } else {
  338. callback = function () {
  339. director.mainLoop();
  340. };
  341. setInterval(callback, this._animationInterval * 1000);
  342. }
  343. return 0;
  344. }
  345. });
  346. /**
  347. * Get current application instance.
  348. * @return {cc.Application} Current application instance pointer.
  349. */
  350. cc.Application.getInstance = function () {
  351. return cc._sharedApplication;
  352. };
  353. /**
  354. * Get current language config
  355. * @return {Number} Current language config
  356. */
  357. cc.Application.getCurrentLanguage = function () {
  358. var ret = cc.LANGUAGE_ENGLISH;
  359. var currentLang = navigator.language;
  360. if(!currentLang){
  361. currentLang = navigator.browserLanguage || navigator.userLanguage;
  362. }
  363. if(!currentLang){
  364. return ret;
  365. }
  366. currentLang = currentLang.substring(0,2).toLowerCase();
  367. switch (currentLang) {
  368. case "cn":
  369. case "zh":
  370. ret = cc.LANGUAGE_CHINESE;
  371. break;
  372. case "fr":
  373. ret = cc.LANGUAGE_FRENCH;
  374. break;
  375. case "it":
  376. ret = cc.LANGUAGE_ITALIAN;
  377. break;
  378. case "de":
  379. ret = cc.LANGUAGE_GERMAN;
  380. break;
  381. case "es":
  382. ret = cc.LANGUAGE_SPANISH;
  383. break;
  384. case "ru":
  385. ret = cc.LANGUAGE_RUSSIAN;
  386. break;
  387. case "ko":
  388. ret = cc.LANGUAGE_KOREAN;
  389. break;
  390. case "ja":
  391. ret = cc.LANGUAGE_JAPANESE;
  392. break;
  393. case "hu":
  394. ret = cc.LANGUAGE_HUNGARIAN;
  395. break;
  396. case "pt":
  397. ret = cc.LANGUAGE_PORTUGUESE;
  398. break;
  399. case "ar":
  400. ret = cc.LANGUAGE_ARABIC;
  401. break;
  402. case "no":
  403. ret = cc.LANGUAGE_NORWEGIAN;
  404. break;
  405. case "pl":
  406. ret = cc.LANGUAGE_POLISH;
  407. break;
  408. }
  409. return ret;
  410. };
  411. cc._sharedApplication = null;