Develop and Download Open Source Software

Browse Subversion Repository

Diff of /trunk/src/js/jgame.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 59 by tsugehara, Fri Mar 8 14:41:33 2013 UTC revision 62 by tsugehara, Thu Mar 14 10:56:28 2013 UTC
# Line 1  Line 1 
 if(!window.performance) {  
     window.performance = {  
     };  
 }  
 if(!performance.now) {  
     performance.now = Date.now;  
 }  
1  window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || ((function () {  window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || ((function () {
2      var lastTime = performance.now();      var lastTime = Date.now ? Date.now() : new Date().getTime();
3      var frame = 1000 / 60;      var frame = 1000 / 60;
4      return function (func) {      return function (func) {
5          var currentTime = performance.now();          var currentTime = Date.now ? Date.now() : new Date().getTime();
6          var _id = setTimeout(function () {          var _id = setTimeout(function () {
7              func(performance.now());              func(Date.now ? Date.now() : new Date().getTime());
8          }, Math.max(0, lastTime + frame - currentTime));          }, Math.max(0, lastTime + frame - currentTime));
9          lastTime = currentTime;          lastTime = currentTime;
10          return _id;          return _id;
# Line 592  var E = (function () { Line 585  var E = (function () {
585      };      };
586      E.prototype.offset = function () {      E.prototype.offset = function () {
587          var parent_offset = this.parent ? this.parent.offset() : {          var parent_offset = this.parent ? this.parent.offset() : {
588              x: 0,              x: this.scroll ? this.scroll.x : 0,
589              y: 0              y: this.scroll ? this.scroll.y : 0
590          };          };
591          return {          return {
592              x: this.x + parent_offset.x,              x: this.x + parent_offset.x,
# Line 2215  var GameRenderer = (function (_super) { Line 2208  var GameRenderer = (function (_super) {
2208              this.container = div;              this.container = div;
2209          }          }
2210          this.handler = document.createElement("div");          this.handler = document.createElement("div");
2211            this.handler.style.display = "inline-block";
2212          this.container.appendChild(this.handler);          this.container.appendChild(this.handler);
2213          this.changeTransferMode(transferMode ? transferMode : RenderTransferMode.Transfer);          this.changeTransferMode(transferMode ? transferMode : RenderTransferMode.Transfer);
2214          if(!disableBg) {          if(!disableBg) {
# Line 2301  var GameRenderer = (function (_super) { Line 2295  var GameRenderer = (function (_super) {
2295      GameRenderer.prototype.refresh = function () {      GameRenderer.prototype.refresh = function () {
2296          delete this.buffer;          delete this.buffer;
2297          this.buffer = new Array();          this.buffer = new Array();
2298            var bounding = this.handler.getBoundingClientRect();
2299            this._pageX = Math.round(window["scrollX"] || window.pageXOffset + bounding.left);
2300            this._pageY = Math.round(window["scrollY"] || window.pageYOffset + bounding.top);
2301          if(this.transferMode == RenderTransferMode.Flip) {          if(this.transferMode == RenderTransferMode.Flip) {
2302              this.handler.innerHTML = "";              this.handler.innerHTML = "";
2303              for(var i = 0; i < 2; i++) {              for(var i = 0; i < 2; i++) {
# Line 2314  var GameRenderer = (function (_super) { Line 2311  var GameRenderer = (function (_super) {
2311              this.bc = this.buffer[0].getContext("2d");              this.bc = this.buffer[0].getContext("2d");
2312              this.flipNo = 1;              this.flipNo = 1;
2313              if(this.frontCanvasSize) {              if(this.frontCanvasSize) {
2314                  this.buffer[1].style.width = this.frontCanvasSize.width + "px";                  JGUtil.scaleCanvas(this.buffer[1], this.frontCanvasSize);
                 this.buffer[1].style.height = this.frontCanvasSize.height + "px";  
2315              }              }
2316          } else if(this.transferMode == RenderTransferMode.Transfer) {          } else if(this.transferMode == RenderTransferMode.Transfer) {
2317              this.handler.innerHTML = "";              this.handler.innerHTML = "";
# Line 2335  var GameRenderer = (function (_super) { Line 2331  var GameRenderer = (function (_super) {
2331              this.bc = this.fc;              this.bc = this.fc;
2332          }          }
2333          if(this.frontCanvasSize) {          if(this.frontCanvasSize) {
2334              this.buffer[0].style.width = this.frontCanvasSize.width + "px";              JGUtil.scaleCanvas(this.buffer[0], this.frontCanvasSize);
             this.buffer[0].style.height = this.frontCanvasSize.height + "px";  
2335              if(this.frontCanvasOffset) {              if(this.frontCanvasOffset) {
2336                  this.handler.style.position = "relative";                  this.handler.style.position = "relative";
2337                  this.handler.style.left = this.frontCanvasOffset.x + "px";                  this.handler.style.left = this.frontCanvasOffset.x + "px";
# Line 2434  var BufferedRenderer = (function (_super Line 2429  var BufferedRenderer = (function (_super
2429      return BufferedRenderer;      return BufferedRenderer;
2430  })(Renderer);  })(Renderer);
2431  var GameTimer = (function () {  var GameTimer = (function () {
2432      function GameTimer(wait) {      function GameTimer(wait, now) {
2433          this.wait = wait;          this.wait = wait;
2434          this.tick = performance.now() + this.wait;          this.tick = now + this.wait;
2435          this.trigger = new Trigger();          this.trigger = new Trigger();
2436      }      }
2437      GameTimer.prototype.tryFire = function (t) {      GameTimer.prototype.tryFire = function (t) {
# Line 2525  var Game = (function () { Line 2520  var Game = (function () {
2520          }          }
2521      };      };
2522      Game.prototype.refresh = function () {      Game.prototype.refresh = function () {
2523          if(document.addEventListener) {          try  {
2524              if(this.isTouchEnable()) {              if(this.isTouchEnable()) {
2525                  this.renderer.handler.removeEventListener("mousedown", this.onmousedown, false);                  this.renderer.handler.removeEventListener("touchstart", this.onmousedown, false);
2526                  this.renderer.handler.removeEventListener("mousemove", this.onmousemove, false);                  this.renderer.handler.removeEventListener("touchmove", this.onmousemove, false);
2527                  this.renderer.handler.removeEventListener("mouseup", this.onmouseup, false);                  this.renderer.handler.removeEventListener("touchend", this.onmouseup, false);
2528              } else {              } else {
2529                  this.renderer.handler.removeEventListener("mousedown", this.onmousedown, false);                  this.renderer.handler.removeEventListener("mousedown", this.onmousedown, false);
2530                  this.renderer.handler.removeEventListener("mousemove", this.onmousemove, false);                  this.renderer.handler.removeEventListener("mousemove", this.onmousemove, false);
2531                  this.renderer.handler.removeEventListener("mouseup", this.onmouseup, false);                  this.renderer.handler.removeEventListener("mouseup", this.onmouseup, false);
2532              }              }
2533          } else {          } catch (ex) {
             if(this.isTouchEnable()) {  
                 this.renderer.handler.detachEvent("onmousedown", this.onmousedown);  
                 this.renderer.handler.detachEvent("onmousemove", this.onmousemove);  
                 this.renderer.handler.detachEvent("onmouseup", this.onmouseup);  
             } else {  
                 this.renderer.handler.detachEvent("onmousedown", this.onmousedown);  
                 this.renderer.handler.detachEvent("onmousemove", this.onmousemove);  
                 this.renderer.handler.detachEvent("onmouseup", this.onmouseup);  
             }  
2534          }          }
2535          this.renderer.refresh();          this.renderer.refresh();
2536          for(var i = 0; i < this.scenes.length; i++) {          for(var i = 0; i < this.scenes.length; i++) {
# Line 2557  var Game = (function () { Line 2543  var Game = (function () {
2543          div.setAttribute('ontouchstart', 'return');          div.setAttribute('ontouchstart', 'return');
2544          return typeof div.ontouchstart === 'function';          return typeof div.ontouchstart === 'function';
2545      };      };
2546      Game.prototype.pointHandler = function () {      Game.prototype.onmousedown = function (e) {
2547          var _this = this;          var layers = this.scene.getLayerArray();
2548          var dragParam = null;          var layer;
2549          this.onmousedown = function (e) {          var offset = {
2550              var layers = _this.scene.getLayerArray();              x: this.scale ? e.offsetX / this.scale : e.offsetX,
2551              var layer;              y: this.scale ? e.offsetY / this.scale : e.offsetY
2552            };
2553            while(layer = layers.pop()) {
2554                if(!layer.pointCapture) {
2555                    continue;
2556                }
2557                var dragObj = layer.getEntityByPoint(offset);
2558                if(!dragObj) {
2559                    dragObj = layer;
2560                }
2561                this.dragParam = new InputPointEvent(e, dragObj, this.scale);
2562                this.pointDown.fire(this.dragParam);
2563                if(this.scene.pointDown) {
2564                    this.scene.pointDown.fire(this.dragParam);
2565                }
2566                if(dragObj.pointDown) {
2567                    dragObj.pointDown.fire(this.dragParam);
2568                }
2569                break;
2570            }
2571            e.preventDefault();
2572        };
2573        Game.prototype.ontouchstart = function (e) {
2574            var layers = this.scene.getLayerArray();
2575            var layer;
2576            var touches = e.changedTouches;
2577            for(var i = 0, l = touches.length; i < l; i++) {
2578                var touch = touches[i];
2579                touch.offsetX = touch.pageX - this.renderer._pageX;
2580                touch.offsetY = touch.pageY - this.renderer._pageY;
2581              var offset = {              var offset = {
2582                  x: _this.scale ? e.offsetX / _this.scale : e.offsetX,                  x: this.scale ? touch.offsetX / this.scale : touch.offsetX,
2583                  y: _this.scale ? e.offsetY / _this.scale : e.offsetY                  y: this.scale ? touch.offsetY / this.scale : touch.offsetY
2584              };              };
2585              while(layer = layers.pop()) {              while(layer = layers.pop()) {
2586                  if(!layer.pointCapture) {                  if(!layer.pointCapture) {
# Line 2575  var Game = (function () { Line 2590  var Game = (function () {
2590                  if(!dragObj) {                  if(!dragObj) {
2591                      dragObj = layer;                      dragObj = layer;
2592                  }                  }
2593                  dragParam = new InputPointEvent(e, dragObj, _this.scale);                  this.dragParam = new InputPointEvent(touch, dragObj, this.scale);
2594                  _this.pointDown.fire(dragParam);                  this.pointDown.fire(this.dragParam);
2595                  if(_this.scene.pointDown) {                  if(this.scene.pointDown) {
2596                      _this.scene.pointDown.fire(dragParam);                      this.scene.pointDown.fire(this.dragParam);
2597                  }                  }
2598                  if(dragObj.pointDown) {                  if(dragObj.pointDown) {
2599                      dragObj.pointDown.fire(dragParam);                      dragObj.pointDown.fire(this.dragParam);
2600                  }                  }
2601                  break;                  break;
2602              }              }
2603              e.preventDefault();          }
2604          };          e.preventDefault();
2605          this.onmousemove = function (e) {      };
2606              if(!dragParam) {      Game.prototype.onmousemove = function (e) {
2607                  return;          if(!this.dragParam) {
2608              }              return;
2609              var param = new InputPointEvent(e, dragParam.entity, _this.scale);          }
2610              if(dragParam.entity.pointMove) {          var param = new InputPointEvent(e, this.dragParam.entity, this.scale);
2611                  dragParam.entity.pointMove.fire(param);          if(this.dragParam.entity.pointMove) {
2612              }              this.dragParam.entity.pointMove.fire(param);
2613              if(_this.scene.pointMove) {          }
2614                  _this.scene.pointMove.fire(param);          if(this.scene.pointMove) {
2615              }              this.scene.pointMove.fire(param);
2616              _this.pointMove.fire(param);          }
2617              e.preventDefault();          this.pointMove.fire(param);
2618          };          e.preventDefault();
2619          this.onmouseup = function (e) {      };
2620              if(!dragParam) {      Game.prototype.ontouchmove = function (e) {
2621                  return;          if(!this.dragParam) {
2622              }              return;
2623              var param = new InputPointEvent(e, dragParam.entity, _this.scale);          }
2624              if(dragParam.entity.pointUp) {          var touches = e.changedTouches;
2625                  dragParam.entity.pointUp.fire(param);          for(var i = 0, l = touches.length; i < l; i++) {
2626                var touch = touches[i];
2627                touch.offsetX = touch.pageX - this.renderer._pageX;
2628                touch.offsetY = touch.pageY - this.renderer._pageY;
2629                var param = new InputPointEvent(touch, this.dragParam.entity, this.scale);
2630                if(this.dragParam.entity.pointMove) {
2631                    this.dragParam.entity.pointMove.fire(param);
2632                }
2633                if(this.scene.pointMove) {
2634                    this.scene.pointMove.fire(param);
2635              }              }
2636              if(_this.scene.pointUp) {              this.pointMove.fire(param);
2637                  _this.scene.pointUp.fire(param);          }
2638            e.preventDefault();
2639        };
2640        Game.prototype.onmouseup = function (e) {
2641            if(!this.dragParam) {
2642                return;
2643            }
2644            var param = new InputPointEvent(e, this.dragParam.entity, this.scale);
2645            if(this.dragParam.entity.pointUp) {
2646                this.dragParam.entity.pointUp.fire(param);
2647            }
2648            if(this.scene.pointUp) {
2649                this.scene.pointUp.fire(param);
2650            }
2651            this.pointUp.fire(param);
2652            this.dragParam = null;
2653            e.preventDefault();
2654        };
2655        Game.prototype.ontouchend = function (e) {
2656            if(!this.dragParam) {
2657                return;
2658            }
2659            var touches = e.changedTouches;
2660            for(var i = 0, l = touches.length; i < l; i++) {
2661                var touch = touches[i];
2662                touch.offsetX = touch.pageX - this.renderer._pageX;
2663                touch.offsetY = touch.pageY - this.renderer._pageY;
2664                var param = new InputPointEvent(touch, this.dragParam.entity, this.scale);
2665                if(this.dragParam.entity.pointUp) {
2666                    this.dragParam.entity.pointUp.fire(param);
2667              }              }
2668              _this.pointUp.fire(param);              if(this.scene.pointUp) {
2669              dragParam = null;                  this.scene.pointUp.fire(param);
             e.preventDefault();  
         };  
         if(document.addEventListener) {  
             if(this.isTouchEnable()) {  
                 this.renderer.handler.addEventListener("mousedown", this.onmousedown, false);  
                 this.renderer.handler.addEventListener("mousemove", this.onmousemove, false);  
                 this.renderer.handler.addEventListener("mouseup", this.onmouseup, false);  
             } else {  
                 this.renderer.handler.addEventListener("mousedown", this.onmousedown, false);  
                 this.renderer.handler.addEventListener("mousemove", this.onmousemove, false);  
                 this.renderer.handler.addEventListener("mouseup", this.onmouseup, false);  
2670              }              }
2671          } else {              this.pointUp.fire(param);
2672                this.dragParam = null;
2673            }
2674            e.preventDefault();
2675        };
2676        Game.prototype.pointHandler = function () {
2677            this.dragParam = null;
2678            try  {
2679              if(this.isTouchEnable()) {              if(this.isTouchEnable()) {
2680                  this.renderer.handler.attachEvent("onmousedown", onmousedown);                  this.renderer.handler.addEventListener("touchstart", JGUtil.proxy(this.ontouchstart, this), false);
2681                  this.renderer.handler.attachEvent("onmousemove", onmousemove);                  this.renderer.handler.addEventListener("touchmove", JGUtil.proxy(this.ontouchmove, this), false);
2682                  this.renderer.handler.attachEvent("onmouseup", onmouseup);                  this.renderer.handler.addEventListener("touchend", JGUtil.proxy(this.ontouchend, this), false);
2683              } else {              } else {
2684                  this.renderer.handler.attachEvent("onmousedown", onmousedown);                  this.renderer.handler.addEventListener("mousedown", JGUtil.proxy(this.onmousedown, this), false);
2685                  this.renderer.handler.attachEvent("onmousemove", onmousemove);                  this.renderer.handler.addEventListener("mousemove", JGUtil.proxy(this.onmousemove, this), false);
2686                  this.renderer.handler.attachEvent("onmouseup", onmouseup);                  this.renderer.handler.addEventListener("mouseup", JGUtil.proxy(this.onmouseup, this), false);
2687              }              }
2688            } catch (ex) {
2689            }
2690        };
2691        Game.prototype.onkeydown = function (e) {
2692            var keyParam = new InputKeyboardEvent(this.keymap[e.keyCode], e);
2693            this.keyDown.fire(keyParam);
2694            if(this.scene.keyDown) {
2695                this.scene.keyDown.fire(keyParam);
2696            }
2697            if(this.keymap[e.keyCode] != undefined) {
2698                e.preventDefault();
2699            }
2700        };
2701        Game.prototype.onkeyup = function (e) {
2702            var keyParam = new InputKeyboardEvent(this.keymap[e.keyCode], e);
2703            this.keyUp.fire(keyParam);
2704            if(this.scene.keyUp) {
2705                this.scene.keyUp.fire(keyParam);
2706            }
2707            if(this.keymap[e.keyCode] != undefined) {
2708                e.preventDefault();
2709          }          }
2710      };      };
2711      Game.prototype.keyboardHandler = function () {      Game.prototype.keyboardHandler = function () {
         var _this = this;  
2712          this.keymap = {          this.keymap = {
2713              13: Keytype.enter,              13: Keytype.enter,
2714              27: Keytype.esc,              27: Keytype.esc,
# Line 2648  var Game = (function () { Line 2717  var Game = (function () {
2717              39: Keytype.right,              39: Keytype.right,
2718              40: Keytype.down              40: Keytype.down
2719          };          };
2720          var onkeydown = function (e) {          try  {
2721              var keyParam = new InputKeyboardEvent(_this.keymap[e.keyCode], e);              document.addEventListener("keydown", JGUtil.proxy(this.onkeydown, this), false);
2722              _this.keyDown.fire(keyParam);              document.addEventListener("keyup", JGUtil.proxy(this.onkeyup, this), false);
2723              if(_this.scene.keyDown) {          } catch (ex) {
                 _this.scene.keyDown.fire(keyParam);  
             }  
             if(_this.keymap[e.keyCode] != undefined) {  
                 e.preventDefault();  
             }  
         };  
         var onkeyup = function (e) {  
             var keyParam = new InputKeyboardEvent(_this.keymap[e.keyCode], e);  
             _this.keyUp.fire(keyParam);  
             if(_this.scene.keyUp) {  
                 _this.scene.keyUp.fire(keyParam);  
             }  
             if(_this.keymap[e.keyCode] != undefined) {  
                 e.preventDefault();  
             }  
         };  
         if(document.addEventListener) {  
             document.addEventListener("keydown", onkeydown, false);  
             document.addEventListener("keyup", onkeyup, false);  
         } else {  
             document.attachEvent("onkeydown", onkeydown);  
             document.attachEvent("onkeyup", onkeyup);  
2724          }          }
2725      };      };
2726      Game.prototype.addTimer = function (wait, owner, handler) {      Game.prototype.addTimer = function (wait, owner, handler) {
# Line 2685  var Game = (function () { Line 2732  var Game = (function () {
2732              }              }
2733          }          }
2734          if(timer == null) {          if(timer == null) {
2735              timer = new GameTimer(wait);              timer = new GameTimer(wait, this.tick === undefined ? 0 : this.tick);
2736              this.timers.push(timer);              this.timers.push(timer);
2737          }          }
2738          timer.trigger.handle(owner, handler);          timer.trigger.handle(owner, handler);
# Line 2800  var Game = (function () { Line 2847  var Game = (function () {
2847      Game.prototype.main = function () {      Game.prototype.main = function () {
2848          var _this = this;          var _this = this;
2849          var fps_stack = new Array();          var fps_stack = new Array();
2850          var _main = function () {          var _main = function (t) {
2851              var t = performance.now();              if(t === undefined) {
2852                    t = Date.now ? Date.now() : new Date().getTime();
2853                }
2854              if(_this.tick > (t + 10000) || (_this.tick + 10000) < t) {              if(_this.tick > (t + 10000) || (_this.tick + 10000) < t) {
2855                  _this.tick = t - 1;                  _this.tick = t - 1;
2856                  _this.renderTick = t - _this.targetFps;                  _this.renderTick = t - _this.targetFps;
# Line 2847  var Game = (function () { Line 2896  var Game = (function () {
2896                  window.requestAnimationFrame(_main);                  window.requestAnimationFrame(_main);
2897              }              }
2898          };          };
2899          this.tick = performance.now();          this.tick = 0;
2900          this.renderTick = this.tick - this.targetFps;          this.renderTick = 0;
2901          window.requestAnimationFrame(_main);          window.requestAnimationFrame(_main);
2902      };      };
2903      Game.prototype.fullscreen = function () {      Game.prototype.fullscreen = function () {
# Line 4467  var JGUtil = (function () { Line 4516  var JGUtil = (function () {
4516          var context = canvas.getContext("2d");          var context = canvas.getContext("2d");
4517          return context.createPattern(image, repeat == undefined ? "repeat" : repeat);          return context.createPattern(image, repeat == undefined ? "repeat" : repeat);
4518      };      };
4519        JGUtil.scaleCanvas = function scaleCanvas(canvas, size) {
4520            if(canvas.style['webkitTransform'] !== undefined) {
4521                canvas.style['webkitTransformOrigin'] = '0 0';
4522                canvas.style['webkitTransform'] = 'scale(' + Math.max(size.width / canvas.width, size.height / canvas.height) + ')';
4523            } else {
4524                canvas.style.width = size.width + "px";
4525                canvas.style.height = size.height + "px";
4526            }
4527        };
4528      JGUtil.getBrowser = function getBrowser() {      JGUtil.getBrowser = function getBrowser() {
4529          if(JGUtil.browser) {          if(JGUtil.browser) {
4530              return JGUtil.browser;              return JGUtil.browser;
# Line 4495  var JGUtil = (function () { Line 4553  var JGUtil = (function () {
4553          }          }
4554          game.refresh();          game.refresh();
4555      };      };
4556        JGUtil.proxy = function proxy(func, self) {
4557            return function () {
4558                func.apply(self, arguments);
4559            };
4560        };
4561      return JGUtil;      return JGUtil;
4562  })();  })();
4563  var Line = (function (_super) {  var Line = (function (_super) {

Legend:
Removed from v.59  
changed lines
  Added in v.62

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26