egret_loader.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright (c) 2014,Egret-Labs.org
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Egret-Labs.org nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY EGRET-LABS.ORG AND CONTRIBUTORS "AS IS" AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL EGRET-LABS.ORG AND CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. egret_h5 = {};
  28. egret_h5.prefix = "";
  29. egret_h5.loadScript = function (list, callback) {
  30. var loaded = 0;
  31. var loadNext = function () {
  32. egret_h5.loadSingleScript(egret_h5.prefix + list[loaded], function () {
  33. loaded++;
  34. if (loaded >= list.length) {
  35. callback();
  36. }
  37. else {
  38. loadNext();
  39. }
  40. })
  41. };
  42. loadNext();
  43. }
  44. egret_h5.loadSingleScript = function (src, callback) {
  45. var s = document.createElement('script');
  46. if (s.hasOwnProperty("async")) {
  47. s.async = false;
  48. }
  49. s.src = src;
  50. s.addEventListener('load', function () {
  51. this.removeEventListener('load', arguments.callee, false);
  52. callback();
  53. }, false);
  54. document.body.appendChild(s);
  55. }
  56. egret_h5.startGame = function () {
  57. var canvas = document.getElementById(egret.StageDelegate.canvas_name);
  58. context = egret.MainContext.instance;
  59. context.touchContext = new egret.HTML5TouchContext(canvas);
  60. context.deviceContext = new egret.HTML5DeviceContext();///帧数
  61. context.netContext = new egret.HTML5NetContext();
  62. //设置屏幕适配策略
  63. /*var container = new egret.EqualToFrame();
  64. var content = /*egret.Browser.getInstance().isMobile*///egret.MainContext.deviceType == egret.MainContext.DEVICE_MOBILE ? new egret.FixedWidth() : new egret.NoScale();
  65. /*var policy = new egret.ResolutionPolicy(container, content);
  66. egret.StageDelegate.getInstance().setDesignSize(480, 800, policy);*/
  67. egret.StageDelegate.getInstance().setDesignSize(480, 800);
  68. context.stage = new egret.Stage();
  69. var scaleMode = egret.MainContext.deviceType == egret.MainContext.DEVICE_MOBILE ? egret.StageScaleMode.SHOW_ALL : egret.StageScaleMode.NO_SCALE;
  70. context.stage.scaleMode = scaleMode;
  71. context.stage = new egret.Stage(canvas.width, canvas.height);
  72. //WebGL是egret的Beta特性,默认关闭
  73. if(false){// egret.WebGLUtils.checkCanUseWebGL()) {
  74. context.rendererContext = new egret.WebGLRenderer(canvas);
  75. }
  76. else {
  77. context.rendererContext = new egret.HTML5CanvasRenderer(canvas);
  78. }
  79. egret.MainContext.instance.rendererContext.texture_scale_factor = 1;
  80. context.run();
  81. var rootClass;
  82. if(document_class){
  83. rootClass = egret.getDefinitionByName(document_class);
  84. }
  85. if(rootClass) {
  86. var rootContainer = new rootClass();
  87. if(rootContainer instanceof egret.DisplayObjectContainer){
  88. context.stage.addChild(rootContainer);
  89. }
  90. else{
  91. throw new Error("文档类必须是egret.DisplayObjectContainer的子类!");
  92. }
  93. }
  94. else{
  95. throw new Error("找不到文档类!");
  96. }
  97. }
  98. egret_h5.preloadScript = function (list, prefix) {
  99. if (!egret_h5.preloadList) {
  100. egret_h5.preloadList = [];
  101. }
  102. egret_h5.preloadList = egret_h5.preloadList.concat(list.map(function (item) {
  103. return prefix + item;
  104. }))
  105. }
  106. egret_h5.startLoading = function () {
  107. var list = egret_h5.preloadList;
  108. egret_h5.loadScript(list, egret_h5.startGame);
  109. }