egret_loader.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.startGame = function () {
  28. var wid = document.documentElement.clientWidth;
  29. var hei = document.documentElement.clientHeight;
  30. var bet=1;//iphone系列比较特殊,尺寸需要翻倍
  31. switch(wid){
  32. case 320://iphone4\5
  33. bet = 2;
  34. break;
  35. case 375://iphone 6
  36. bet = 2;
  37. break;
  38. case 414://iphone 6+
  39. bet = 3;
  40. break;
  41. default :
  42. bet =1;
  43. break;
  44. }
  45. wid *= bet; hei*= bet;
  46. var gW = GameData.GameWidth;
  47. var gh = GameData.GameHeight;
  48. var _scale = 1;
  49. var scale_1 = 640 / 916;
  50. var scale_2 = scale_1/(wid / hei);
  51. if(wid < hei){ // 竖屏
  52. _scale = gW / wid;
  53. gh = _scale * hei;
  54. if(wid > 750){
  55. if(gh < GameData.limitH2){
  56. gh = GameData.limitH2;
  57. }
  58. }else{
  59. if(gh < GameData.limitH1){
  60. gh = GameData.limitH1;
  61. if(scale_1 < 1){
  62. gh += Math.floor(scale_1*20);
  63. }
  64. }
  65. }
  66. GameData.GameHeight = gh;
  67. }else{
  68. }
  69. var s = 'w:'+wid+" h:"+hei+'gh'+gh+'GameData.limitH1'+GameData.limitH1 + '_scale' + _scale + 'scale_2' + scale_2
  70. // alert(s);
  71. // console.log(s);
  72. var context = egret.MainContext.instance;
  73. context.touchContext = new egret.HTML5TouchContext();
  74. context.deviceContext = new egret.HTML5DeviceContext(60);
  75. context.netContext = new egret.HTML5NetContext();
  76. //egret.StageDelegate.getInstance().setDesignSize(480, 800);
  77. egret.StageDelegate.getInstance().setDesignSize(GameData.GameWidth, GameData.GameHeight);
  78. context.stage = new egret.Stage();
  79. var scaleMode = egret.MainContext.deviceType == egret.MainContext.DEVICE_MOBILE ? egret.StageScaleMode.SHOW_ALL : egret.StageScaleMode.NO_SCALE;
  80. context.stage.scaleMode = scaleMode;
  81. //WebGL是egret的Beta特性,默认关闭
  82. var rendererType = 0;
  83. if (rendererType == 1) {// egret.WebGLUtils.checkCanUseWebGL()) {
  84. context.rendererContext = new egret.WebGLRenderer();
  85. }
  86. else {
  87. context.rendererContext = new egret.HTML5CanvasRenderer();
  88. }
  89. egret.MainContext.instance.rendererContext.texture_scale_factor = 1;
  90. context.run();
  91. var rootClass;
  92. if(document_class){
  93. rootClass = egret.getDefinitionByName(document_class);
  94. }
  95. if(rootClass) {
  96. var rootContainer = new rootClass();
  97. if(rootContainer instanceof egret.DisplayObjectContainer){
  98. context.stage.addChild(rootContainer);
  99. }
  100. else{
  101. throw new Error("文档类必须是egret.DisplayObjectContainer的子类!");
  102. }
  103. }
  104. else{
  105. throw new Error("找不到文档类!");
  106. }
  107. //处理屏幕大小改变
  108. var resizeTimer = null;
  109. var doResize = function () {
  110. context.stage.changeSize();
  111. resizeTimer = null;
  112. };
  113. window.onresize = function () {
  114. if (resizeTimer == null) {
  115. resizeTimer = setTimeout(doResize, 300);
  116. }
  117. };
  118. };