game.ejs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. function __initApp () { // init app
  2. require('./libs/wrapper/builtin/index');
  3. const firstScreen = require('./first-screen');
  4. window.DOMParser = require('./libs/common/xmldom/dom-parser').DOMParser;
  5. window.__globalAdapter = {};
  6. require('./libs/wrapper/unify');
  7. require('./libs/wrapper/fs-utils');
  8. <%- include(cocosTemplate, {}) %>
  9. // Adapt for IOS, swap if opposite
  10. if (canvas){
  11. var _w = canvas.width;
  12. var _h = canvas.height;
  13. if (screen.width < screen.height) {
  14. if (canvas.width > canvas.height) {
  15. _w = canvas.height;
  16. _h = canvas.width;
  17. }
  18. } else {
  19. if (canvas.width < canvas.height) {
  20. _w = canvas.height;
  21. _h = canvas.width;
  22. }
  23. }
  24. canvas.width = _w;
  25. canvas.height = _h;
  26. }
  27. // Adjust initial canvas size
  28. if (canvas && window.devicePixelRatio >= 2) {canvas.width *= 2; canvas.height *= 2;}
  29. const importMap = require("<%= importMapFile%>").default;
  30. System.warmup({
  31. importMap,
  32. importMapUrl: '<%= importMapFile%>',
  33. defaultHandler: (urlNoSchema) => {
  34. require('.' + urlNoSchema);
  35. },
  36. handlers: {
  37. 'plugin:': (urlNoSchema) => {
  38. requirePlugin(urlNoSchema);
  39. },
  40. },
  41. });
  42. /**
  43. * Fetch WebAssembly binaries.
  44. *
  45. * Whereas WeChat expects the argument passed to `WebAssembly.instantiate`
  46. * to be file path and the path should be relative from project's root dir,
  47. * we do the path conversion and directly return the converted path.
  48. *
  49. * @param path The path to `.wasm` file **relative from engine's out dir**(no leading `./`).
  50. * See 'assetURLFormat' field of build engine options.
  51. */
  52. function fetchWasm(path) {
  53. const engineDir = '<%-engineDir%>'; // Relative from project out
  54. return `${engineDir}/${path}`;
  55. }
  56. firstScreen.start('<%= alpha %>', '<%= antialias %>', '<%= useWebgl2 %>').then(() => {
  57. return System.import('<%= applicationJs %>');
  58. }).then((module) => {
  59. return firstScreen.setProgress(0.2).then(() => Promise.resolve(module));
  60. }).then(({ createApplication }) => {
  61. return createApplication({
  62. loadJsListFile: (url) => require(url),
  63. fetchWasm,
  64. });
  65. }).then((application) => {
  66. return firstScreen.setProgress(0.4).then(() => Promise.resolve(application));
  67. }).then((application) => {
  68. return onApplicationCreated(application);
  69. }).catch((err) => {
  70. console.error(err);
  71. });
  72. function onApplicationCreated(application) {
  73. return application.import('cc').then((module) => {
  74. return firstScreen.setProgress(0.6).then(() => Promise.resolve(module));
  75. }).then((cc) => {
  76. require('./libs/common/engine/index.js');
  77. require('./libs/wrapper/engine/index');
  78. require('./libs/common/cache-manager.js');
  79. // Adjust devicePixelRatio
  80. cc.view._maxPixelRatio = 4;
  81. // Release Image objects after uploaded gl texture
  82. cc.macro.CLEANUP_IMAGE_CACHE = false;
  83. return firstScreen.end().then(() => application.start({
  84. findCanvas: () => {
  85. var container = document.createElement('div');
  86. return { frame: container, canvas: window.canvas, container };
  87. },
  88. }));
  89. });
  90. }
  91. } // init app
  92. // NOTE: on WeChat Android end, we can only get the correct screen size at the second tick of game.
  93. var sysInfo = wx.getSystemInfoSync();
  94. if (sysInfo.platform.toLocaleLowerCase() === 'android') {
  95. GameGlobal.requestAnimationFrame (__initApp);
  96. } else {
  97. __initApp();
  98. }