Skip to content

Commit 07f72f4

Browse files
committed
Added TickerLoop and made it optional via config.
1 parent d2fa377 commit 07f72f4

5 files changed

Lines changed: 83 additions & 39 deletions

File tree

v3/src/boot/Config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ var Config = function (config)
7070
this.preBoot = GetValue(config, 'callbacks.preBoot', NOOP);
7171
this.postBoot = GetValue(config, 'callbacks.postBoot', NOOP);
7272

73+
this.useTicker = GetValue(config, 'useTicker', false);
74+
7375
// Default / Missing Images
7476
var pngPrefix = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAg';
7577

v3/src/boot/Game.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var AddToDOM = require('../dom/AddToDOM');
1212
var DOMContentLoaded = require('../dom/DOMContentLoaded');
1313

1414
var MainLoop = require('./MainLoop');
15+
var TickerLoop = require('./TickerLoop');
1516
var CreateRenderer = require('./CreateRenderer');
1617
var GlobalInputManager = require('../input/GlobalInputManager');
1718
var GlobalStateManager = require('../state/GlobalStateManager');
@@ -70,7 +71,14 @@ var Game = function (config)
7071
* @property {Phaser.MainLoop} mainloop - Main Loop handler.
7172
* @protected
7273
*/
73-
this.mainloop = new MainLoop(this, this.config.fps);
74+
if (this.config.useTicker)
75+
{
76+
this.loop = new TickerLoop(this, this.config.fps);
77+
}
78+
else
79+
{
80+
this.loop = new MainLoop(this, this.config.fps);
81+
}
7482

7583
// Wait for the DOM Ready event, then call boot.
7684
DOMContentLoaded(this.boot.bind(this));
@@ -105,7 +113,38 @@ Game.prototype = {
105113

106114
this.config.postBoot();
107115

108-
this.mainloop.start();
116+
this.loop.start(!!this.config.forceSetTimeOut, this.step.bind(this));
117+
},
118+
119+
step: function (time)
120+
{
121+
var active = this.state.active;
122+
var renderer = this.renderer;
123+
124+
// Global Managers (Time, Input, etc)
125+
126+
this.input.update(time);
127+
128+
// States
129+
130+
for (var i = 0; i < active.length; i++)
131+
{
132+
active[i].state.sys.step(time);
133+
}
134+
135+
// Render
136+
137+
// var interpolation = this.frameDelta / this.timestep;
138+
139+
renderer.preRender();
140+
141+
// This uses active.length, in case state.update removed the state from the active list
142+
for (i = 0; i < active.length; i++)
143+
{
144+
active[i].state.sys.render(0, renderer);
145+
}
146+
147+
renderer.postRender();
109148
}
110149

111150
};

v3/src/boot/TickerLoop.js

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var NOOP = require('../utils/NOOP');
12
var RequestAnimationFrame = require('../dom/RequestAnimationFrame');
23

34
var TickerLoop = function (game, framerate)
@@ -11,12 +12,16 @@ var TickerLoop = function (game, framerate)
1112

1213
this.lastUpdate = 0;
1314

15+
this.gap = 1 / (framerate || 60);
1416
this.startTime = 0;
1517
this.elapsed = 0;
1618
this.time = 0;
1719
this.nextTime = 0;
1820
this.frame = 0;
19-
this.fps = false;
21+
this.overlap = 0;
22+
this.fps = framerate;
23+
24+
this.callback = NOOP;
2025

2126
this.lagThreshold = 500;
2227
this.adjustedLag = 33;
@@ -28,7 +33,12 @@ TickerLoop.prototype.constructor = TickerLoop;
2833

2934
TickerLoop.prototype = {
3035

31-
start: function (fps, useRAF)
36+
toString: function ()
37+
{
38+
return 'time: ' + this.time + ' elapsed: ' + this.elapsed + ' overlap: ' + this.overlap;
39+
},
40+
41+
start: function (useRAF, callback)
3242
{
3343
if (this.started)
3444
{
@@ -41,13 +51,14 @@ TickerLoop.prototype = {
4151
this.startTime = Date.now();
4252
this.lastUpdate = Date.now();
4353

44-
this.useRAF = !!this.game.config.forceSetTimeOut;
54+
this.useRAF = useRAF;
55+
this.callback = callback;
4556

4657
this.raf.start(this.step.bind(this), useRAF);
4758
},
4859

49-
step: function (manual) {
50-
60+
step: function (manual)
61+
{
5162
var elapsed = Date.now() - this.lastUpdate;
5263

5364
if (elapsed > this.lagThreshold)
@@ -57,44 +68,22 @@ TickerLoop.prototype = {
5768

5869
this.lastUpdate += elapsed;
5970

60-
this.time = (this.lastUpdate - this.startTime) / 1000;
71+
var time = (this.lastUpdate - this.startTime) / 1000;
6172

6273
this.elapsed = elapsed;
6374

64-
var overlap = this.time - this.nextTime;
75+
var overlap = time - this.nextTime;
6576

66-
// var elapsed = _getTime() - _lastUpdate,
67-
// overlap, dispatch;
68-
// if (elapsed > _lagThreshold) {
69-
// _startTime += elapsed - _adjustedLag;
70-
// }
71-
// _lastUpdate += elapsed;
72-
// _self.time = (_lastUpdate - _startTime) / 1000;
73-
// overlap = _self.time - _nextTime;
77+
this.overlap = overlap;
7478

75-
if (!this.fps || overlap > 0 || manual)
79+
this.time = time;
80+
81+
if (overlap > 0 || manual)
7682
{
7783
this.frame++;
78-
this.nextTime += overlap + (overlap >= this.gap ? 0.004 : this.gap - overlap);
84+
this.nextTime += overlap + ((overlap >= this.gap) ? 0.004 : this.gap - overlap);
85+
this.callback(elapsed);
7986
}
80-
81-
// if (!manual)
82-
// {
83-
// }
84-
85-
// if (!_fps || overlap > 0 || manual === true) {
86-
// _self.frame++;
87-
// _nextTime += overlap + (overlap >= _gap ? 0.004 : _gap - overlap);
88-
// dispatch = true;
89-
// }
90-
// if (manual !== true) { //make sure the request is made before we dispatch the "tick" event so that timing is maintained. Otherwise, if processing the "tick" requires a bunch of time (like 15ms) and we're using a setTimeout() that's based on 16.7ms, it'd technically take 31.7ms between frames otherwise.
91-
// _id = _req(_tick);
92-
// }
93-
// if (dispatch) {
94-
// _self.dispatchEvent(_tickWord);
95-
// }
96-
97-
9887
},
9988

10089
tick: function ()
@@ -133,7 +122,7 @@ TickerLoop.prototype = {
133122
this.lastUpdate = Date.now() - this.lagThreshold + 5;
134123
}
135124

136-
this.raf.start(this.step.bind(this), useRAF);
125+
this.raf.start(this.step.bind(this), this.useRAF);
137126

138127
this.running = true;
139128

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '9ef13fb0-2a8f-11e7-ae43-2f2e7e8ba14e'
2+
build: '8967dbb0-2ae2-11e7-aa50-67ab1824371c'
33
};
44
module.exports = CHECKSUM;

v3/src/state/Systems.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ Systems.prototype = {
114114
this.state.data = this.data;
115115
},
116116

117+
step: function (time)
118+
{
119+
var list = this.children.list;
120+
121+
for (var i = 0; i < list.length; i++)
122+
{
123+
list[i].preUpdate(time);
124+
}
125+
126+
this.cameras.update(time);
127+
128+
this.state.update.call(this.state, time);
129+
},
130+
117131
// Called just once per frame, regardless of speed
118132
begin: function (timestamp, frameDelta)
119133
{

0 commit comments

Comments
 (0)