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 52 by tsugehara, Wed Mar 6 05:49:38 2013 UTC revision 54 by tsugehara, Wed Mar 6 14:20:49 2013 UTC
# Line 1  Line 1 
1  window.getTime = ((function () {  if(!window.performance) {
2      if(window.performance && window.performance.now) {      window.performance = {
3          return function () {      };
4              return window.performance.now();  }
5          };  if(!performance.now) {
6      } else if(window.performance && window.performance.webkitNow) {      performance.now = Date.now;
7          return function () {  }
             return window.performance.webkitNow();  
         };  
     } else {  
         return Date.now;  
     }  
 })());  
8  window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || ((function () {  window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || ((function () {
9      var lastTime = window.getTime();      var lastTime = performance.now();
10      var frame = 1000 / 60;      var frame = 1000 / 60;
11      return function (func) {      return function (func) {
12          var currentTime = window.getTime();          var currentTime = performance.now();
13          var _id = setTimeout(function () {          var _id = setTimeout(function () {
14              func(window.getTime());              func(performance.now());
15          }, Math.max(0, lastTime + frame - currentTime));          }, Math.max(0, lastTime + frame - currentTime));
16          lastTime = currentTime;          lastTime = currentTime;
17          return _id;          return _id;
# Line 1554  var Label = (function (_super) { Line 1548  var Label = (function (_super) {
1548      };      };
1549      return Label;      return Label;
1550  })(E);  })(E);
1551    var TextLineInfo = (function () {
1552        function TextLineInfo(offsetY) {
1553            this.width = 0;
1554            this.height = 0;
1555            this.offsetY = offsetY;
1556        }
1557        return TextLineInfo;
1558    })();
1559    var MultilineText = (function (_super) {
1560        __extends(MultilineText, _super);
1561        function MultilineText(size, offset) {
1562            _super.call(this);
1563            this.width = size.width;
1564            this.height = size.height;
1565            if(offset) {
1566                this.moveTo(offset.x, offset.y);
1567            } else {
1568                this.moveTo(0, 0);
1569            }
1570            this.defaultStyle = "#000";
1571            this.defaultFont = "14px sans-serif";
1572            this.defaultBlur = 0.8;
1573            this.defaultShadowColor = "#000";
1574            this.clip = new Line({
1575                x: 0,
1576                y: 0
1577            });
1578            this.clip.addLine(this.width, 0);
1579            this.clip.addLine(this.width, this.height);
1580            this.clip.addLine(0, this.height);
1581            this.clip.addLine(0, this.height);
1582            this.clip.addLine(0, this.height);
1583            this.clip.closePath = true;
1584            this.clip.setClip(true);
1585            this.entities = new Array();
1586            this.entities.push(this.clip);
1587            this.animeSpeed = 200;
1588            this.animated = new Trigger();
1589        }
1590        MultilineText.prototype.setText = function (text, offset) {
1591            var plainScript = text;
1592            return this.setScript(plainScript, offset);
1593        };
1594        MultilineText.prototype.setScript = function (script, offset) {
1595            this.script = script.replace(/\r\n?/g, "\n");
1596            this.updated();
1597            return this.createBuffer(offset);
1598        };
1599        MultilineText.prototype.getLineHeight = function (c) {
1600            var font = c.font;
1601            var firstPos = font.indexOf("px");
1602            var lastPos = font.lastIndexOf(" ", firstPos);
1603            if(lastPos < 0) {
1604                lastPos = 0;
1605            }
1606            if(firstPos < 0) {
1607                return 16;
1608            }
1609            var fontSize = parseInt(font.substring(lastPos, firstPos));
1610            return fontSize + 2;
1611        };
1612        MultilineText.prototype.createBuffer = function (offset) {
1613            var _this = this;
1614            this.buffer = window.createCanvas(this.width, this.height);
1615            if(offset === undefined) {
1616                offset = 0;
1617            }
1618            var script = this.script;
1619            var len = script.length;
1620            var pos = {
1621                x: 0,
1622                y: 0
1623            };
1624            var c = this.buffer.getContext("2d");
1625            var s;
1626            this.lines = new Array();
1627            c.fillStyle = this.defaultStyle;
1628            c.font = this.defaultFont;
1629            c.textBaseline = "top";
1630            c.shadowBlur = this.defaultBlur;
1631            c.shadowColor = this.defaultShadowColor;
1632            var lineHeight = this.getLineHeight(c);
1633            var lineInfo = new TextLineInfo(0);
1634            lineInfo.height = lineHeight;
1635            this.lines.push(lineInfo);
1636            var _newLine = function () {
1637                pos.x = 0;
1638                pos.y += lineInfo.height;
1639                lineInfo = new TextLineInfo(pos.y);
1640                lineInfo.height = lineHeight;
1641                _this.lines.push(lineInfo);
1642            };
1643            while(offset < len) {
1644                s = script.substr(offset, 1);
1645                switch(s) {
1646                    case "\n":
1647                        _newLine();
1648                        break;
1649                    default:
1650                        var metric = c.measureText(s);
1651                        if((pos.x + metric.width) > this.width) {
1652                            _newLine();
1653                        }
1654                        c.fillText(s, pos.x, pos.y);
1655                        pos.x += metric.width;
1656                        lineInfo.width += metric.width;
1657                }
1658                offset++;
1659            }
1660            this.sprite = new Sprite(this.width, this.height, this.buffer);
1661            this.sprite.moveTo(0, 0);
1662            if(this.entities.length == 1) {
1663                this.entities.push(this.sprite);
1664            } else {
1665                this.entities[1] = this.sprite;
1666            }
1667            return offset == len ? -1 : offset;
1668        };
1669        MultilineText.prototype.refresh = function () {
1670            this.createBuffer();
1671        };
1672        MultilineText.prototype.startAnimation = function (animeSpeed) {
1673            this.start();
1674            this.animeLine = 0;
1675            this.animePos = {
1676                x: 0,
1677                y: this.lines[this.animeLine].height
1678            };
1679            if(animeSpeed !== undefined) {
1680                this.animeSpeed = animeSpeed;
1681            }
1682            this.hideAll();
1683            this.clip.p[4].y = this.animePos.y;
1684            this.clip.p[5].y = this.animePos.y;
1685        };
1686        MultilineText.prototype.update = function (t) {
1687            this.animePos.x += this.animeSpeed / 1000 * t;
1688            if(this.animePos.x >= this.lines[this.animeLine].width) {
1689                this.animePos.x = 0;
1690                this.animePos.y += this.lines[this.animeLine].height;
1691                this.animeLine++;
1692                if(this.animeLine < this.lines.length) {
1693                    this.clip.p[2].y = this.lines[this.animeLine].offsetY;
1694                    this.clip.p[3].y = this.clip.p[2].y;
1695                    this.clip.p[4].y = this.animePos.y;
1696                    this.clip.p[5].y = this.animePos.y;
1697                }
1698            }
1699            if(this.animeLine >= this.lines.length) {
1700                this.stop();
1701                this.showAll();
1702                this.animated.fire();
1703            } else {
1704                this.clip.p[3].x = this.animePos.x;
1705                this.clip.p[4].x = this.clip.p[3].x;
1706            }
1707            this.updated();
1708        };
1709        MultilineText.prototype.hideAll = function () {
1710            this.clip.p[0] = {
1711                x: 0,
1712                y: 0
1713            };
1714            this.clip.p[1] = {
1715                x: this.width,
1716                y: 0
1717            };
1718            this.clip.p[2] = {
1719                x: this.width,
1720                y: 0
1721            };
1722            this.clip.p[3] = {
1723                x: 0,
1724                y: 0
1725            };
1726            this.clip.p[4] = {
1727                x: 0,
1728                y: 0
1729            };
1730            this.clip.p[5] = {
1731                x: 0,
1732                y: 0
1733            };
1734        };
1735        MultilineText.prototype.showAll = function () {
1736            this.clip.p[0] = {
1737                x: 0,
1738                y: 0
1739            };
1740            this.clip.p[1] = {
1741                x: this.width,
1742                y: 0
1743            };
1744            this.clip.p[2] = {
1745                x: this.width,
1746                y: this.height
1747            };
1748            this.clip.p[3] = {
1749                x: 0,
1750                y: this.height
1751            };
1752            this.clip.p[4] = {
1753                x: 0,
1754                y: this.height
1755            };
1756            this.clip.p[5] = {
1757                x: 0,
1758                y: this.height
1759            };
1760        };
1761        return MultilineText;
1762    })(E);
1763  var Tile = (function (_super) {  var Tile = (function (_super) {
1764      __extends(Tile, _super);      __extends(Tile, _super);
1765      function Tile(tileWidth, tileHeight, image) {      function Tile(tileWidth, tileHeight, image) {
# Line 2139  var BufferedRenderer = (function (_super Line 2345  var BufferedRenderer = (function (_super
2345  var GameTimer = (function () {  var GameTimer = (function () {
2346      function GameTimer(wait) {      function GameTimer(wait) {
2347          this.wait = wait;          this.wait = wait;
2348          this.tick = window.getTime() + this.wait;          this.tick = performance.now() + this.wait;
2349          this.trigger = new Trigger();          this.trigger = new Trigger();
2350      }      }
2351      GameTimer.prototype.tryFire = function (t) {      GameTimer.prototype.tryFire = function (t) {
# Line 2504  var Game = (function () { Line 2710  var Game = (function () {
2710          var _this = this;          var _this = this;
2711          var fps_stack = new Array();          var fps_stack = new Array();
2712          var _main = function () {          var _main = function () {
2713              var t = window.getTime();              var t = performance.now();
2714              if(_this.tick > (t + 10000) || (_this.tick + 10000) < t) {              if(_this.tick > (t + 10000) || (_this.tick + 10000) < t) {
2715                  _this.tick = t - 1;                  _this.tick = t - 1;
2716                  _this.renderTick = t - _this.targetFps;                  _this.renderTick = t - _this.targetFps;
# Line 2550  var Game = (function () { Line 2756  var Game = (function () {
2756                  window.requestAnimationFrame(_main);                  window.requestAnimationFrame(_main);
2757              }              }
2758          };          };
2759          this.tick = window.getTime();          this.tick = performance.now();
2760          this.renderTick = this.tick - this.targetFps;          this.renderTick = this.tick - this.targetFps;
2761          window.requestAnimationFrame(_main);          window.requestAnimationFrame(_main);
2762      };      };

Legend:
Removed from v.52  
changed lines
  Added in v.54

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