Skip to content

Commit 54f95a3

Browse files
committed
Removed old timesteps and renamed Variable to TimeStep.
1 parent 9ee4160 commit 54f95a3

4 files changed

Lines changed: 41 additions & 277 deletions

File tree

v3/src/boot/Game.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/**
2-
* @author Richard Davey <rich@photonstorm.com>
3-
* @copyright 2016 Photon Storm Ltd.
4-
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5-
*/
61

72
var Config = require('./Config');
83
var DebugHeader = require('./DebugHeader');
@@ -11,9 +6,7 @@ var Device = require('../device');
116
var AddToDOM = require('../dom/AddToDOM');
127
var DOMContentLoaded = require('../dom/DOMContentLoaded');
138

14-
var MainLoop = require('./MainLoop');
15-
var TickerLoop = require('./TickerLoop');
16-
var VariableTimeStep = require('./VariableTimeStep');
9+
var TimeStep = require('./TimeStep');
1710
var CreateRenderer = require('./CreateRenderer');
1811
var GlobalInputManager = require('../input/GlobalInputManager');
1912
var GlobalStateManager = require('../state/GlobalStateManager');
@@ -72,14 +65,7 @@ var Game = function (config)
7265
* @property {Phaser.MainLoop} mainloop - Main Loop handler.
7366
* @protected
7467
*/
75-
// if (this.config.useTicker)
76-
// {
77-
this.loop = new VariableTimeStep(this, this.config.fps);
78-
// }
79-
// else
80-
// {
81-
// this.loop = new MainLoop(this, this.config.fps);
82-
// }
68+
this.loop = new TimeStep(this, this.config.fps);
8369

8470
// Wait for the DOM Ready event, then call boot.
8571
DOMContentLoaded(this.boot.bind(this));

v3/src/boot/MainLoop.js

Lines changed: 0 additions & 250 deletions
This file was deleted.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var NOOP = require('../utils/NOOP');
22
var RequestAnimationFrame = require('../dom/RequestAnimationFrame');
33

4-
var TickerLoop = function (game, framerate)
4+
var Ticker = function (game, framerate)
55
{
66
this.game = game;
77

@@ -29,9 +29,9 @@ var TickerLoop = function (game, framerate)
2929
this.useRAF = true;
3030
};
3131

32-
TickerLoop.prototype.constructor = TickerLoop;
32+
Ticker.prototype.constructor = Ticker;
3333

34-
TickerLoop.prototype = {
34+
Ticker.prototype = {
3535

3636
toString: function ()
3737
{
@@ -157,4 +157,4 @@ TickerLoop.prototype = {
157157

158158
};
159159

160-
module.exports = TickerLoop;
160+
module.exports = Ticker;
Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var RequestAnimationFrame = require('../dom/RequestAnimationFrame');
1111
// deltaHistory: 10
1212
// }
1313

14-
var VariableTimeStep = function (game, config)
14+
var TimeStep = function (game, config)
1515
{
1616
this.game = game;
1717

@@ -32,6 +32,15 @@ var VariableTimeStep = function (game, config)
3232
// 8.333 / 1000 = 0.008333 (120fps)
3333
// 16.666 / 1000 = 0.01666 (60fps)
3434

35+
/**
36+
* @property {number} fps - An exponential moving average of the frames per second.
37+
* @readOnly
38+
*/
39+
this.actualFps = this.targetFps;
40+
41+
this.nextFpsUpdate = 0;
42+
this.framesThisSecond = 0;
43+
3544
this.callback = NOOP;
3645

3746
this.forceSetTimeOut = GetValue(config, 'forceSetTimeOut', false);
@@ -46,9 +55,9 @@ var VariableTimeStep = function (game, config)
4655
this.deltaSmoothingMax = GetValue(config, 'deltaHistory', 10);
4756
};
4857

49-
VariableTimeStep.prototype.constructor = VariableTimeStep;
58+
TimeStep.prototype.constructor = TimeStep;
5059

51-
VariableTimeStep.prototype = {
60+
TimeStep.prototype = {
5261

5362
start: function (callback)
5463
{
@@ -65,7 +74,8 @@ VariableTimeStep.prototype = {
6574
this.time = now;
6675
this.startTime = now;
6776
this.lastTime = now;
68-
this.runOff = 0;
77+
this.nextFpsUpdate = now + 1000;
78+
this.framesThisSecond = 0;
6979

7080
// Pre-populate smoothing array
7181

@@ -137,7 +147,26 @@ VariableTimeStep.prototype = {
137147
// Real-world timer advance
138148
this.time += avg;
139149

140-
this.callback(this.time, avg);
150+
// Update the estimate of the frame rate, `fps`. Every second, the number
151+
// of frames that occurred in that second are included in an exponential
152+
// moving average of all frames per second, with an alpha of 0.25. This
153+
// means that more recent seconds affect the estimated frame rate more than
154+
// older seconds.
155+
if (time > this.nextFpsUpdate)
156+
{
157+
// Compute the new exponential moving average with an alpha of 0.25.
158+
// Using constants inline is okay here.
159+
this.actualFps = 0.25 * this.framesThisSecond + 0.75 * this.actualFps;
160+
this.nextFpsUpdate = time + 1000;
161+
this.framesThisSecond = 0;
162+
}
163+
164+
this.framesThisSecond++;
165+
166+
// Interpolation - how far between what is expected and where we are?
167+
var interpolation = avg / this._target;
168+
169+
this.callback(this.time, avg, interpolation);
141170

142171
// Shift time value over
143172
this.lastTime = time;
@@ -197,7 +226,6 @@ VariableTimeStep.prototype = {
197226

198227
return this;
199228
}
200-
201229
};
202230

203-
module.exports = VariableTimeStep;
231+
module.exports = TimeStep;

0 commit comments

Comments
 (0)