egret_loader.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 context = egret.MainContext.instance;
  29. context.touchContext = new egret.HTML5TouchContext();
  30. context.deviceContext = new egret.HTML5DeviceContext();
  31. context.netContext = new egret.HTML5NetContext();
  32. //egret.StageDelegate.getInstance().setDesignSize(480, 800);
  33. //context.stage = new egret.Stage();
  34. //var scaleMode = egret.MainContext.deviceType == egret.MainContext.DEVICE_MOBILE ? egret.StageScaleMode.SHOW_ALL : egret.StageScaleMode.NO_SCALE;
  35. //context.stage.scaleMode = scaleMode;
  36. //第二种适配
  37. var winHeight;
  38. var winWidth;
  39. if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth){
  40. winHeight = document.documentElement.clientHeight;
  41. winWidth = document.documentElement.clientWidth;
  42. }else{
  43. winHeight = window.innerHeight;
  44. winWidth = window.innerWidth;
  45. }
  46. if (/iPhone/i.test(navigator.userAgent)) {
  47. var GameWin = {w:640,h:1136};
  48. } else {
  49. //var GameWin = {w:320,h:568};
  50. var GameWin = {w:480,h:852};
  51. //var GameWin = {w:640,h:1136};
  52. }
  53. var Gper = GameWin.h/GameWin.w;
  54. var per = winHeight/winWidth;
  55. if(per<=Gper){
  56. egret.StageDelegate.getInstance().setDesignSize(GameWin.h/per, GameWin.h);
  57. }else if(per>Gper){
  58. egret.StageDelegate.getInstance().setDesignSize(GameWin.w, GameWin.w*per);
  59. }
  60. context.stage = new egret.Stage();
  61. var scaleMode = egret.StageScaleMode.SHOW_ALL;//egret.MainContext.deviceType == egret.MainContext.DEVICE_MOBILE ? egret.StageScaleMode.SHOW_ALL :egret.StageScaleMode.NO_SCALE;
  62. context.stage.scaleMode = scaleMode;
  63. //WebGL是egret的Beta特性,默认关闭
  64. var rendererType = 0;
  65. if (rendererType == 1) {// egret.WebGLUtils.checkCanUseWebGL()) {
  66. console.log("使用WebGL模式");
  67. context.rendererContext = new egret.WebGLRenderer();
  68. }
  69. else {
  70. context.rendererContext = new egret.HTML5CanvasRenderer();
  71. }
  72. egret.MainContext.instance.rendererContext.texture_scale_factor = 1;
  73. context.run();
  74. var rootClass;
  75. if(document_class){
  76. rootClass = egret.getDefinitionByName(document_class);
  77. }
  78. if(rootClass) {
  79. var rootContainer = new rootClass();
  80. if(rootContainer instanceof egret.DisplayObjectContainer){
  81. context.stage.addChild(rootContainer);
  82. }
  83. else{
  84. throw new Error("文档类必须是egret.DisplayObjectContainer的子类!");
  85. }
  86. }
  87. else{
  88. throw new Error("找不到文档类!");
  89. }
  90. //处理屏幕大小改变
  91. var resizeTimer = null;
  92. var doResize = function () {
  93. context.stage.changeSize();
  94. resizeTimer = null;
  95. };
  96. window.onresize = function () {
  97. if (resizeTimer == null) {
  98. resizeTimer = setTimeout(doResize, 300);
  99. }
  100. };
  101. };