Skip to content

Commit 7bf529b

Browse files
committed
RAF timer being used when RAF controlling loops.
Time.time used for Date.now but Time.now may hold RAF hi-res value. Start of separation of game/render update. Minor adjustments to Time.update for clarity.
1 parent c37173a commit 7bf529b

3 files changed

Lines changed: 40 additions & 17 deletions

File tree

src/core/Game.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,12 +640,19 @@ Phaser.Game.prototype = {
640640
*
641641
* @method Phaser.Game#update
642642
* @protected
643-
* @param {number} time - The current time as provided by RequestAnimationFrame.
643+
* @param {number} time - The current time as provided by Date.now (see updateRAF in RequestAnimationFrame.js) in milliseconds
644644
*/
645645
update: function (time) {
646646

647647
this.time.update(time);
648648

649+
this.updateLogic(1.0 / this.time.desiredFps);
650+
this.updateRender(this.time.elapsed);
651+
652+
},
653+
654+
updateLogic: function (timeStep) {
655+
649656
if (!this._paused && !this.pendingStep)
650657
{
651658
if (this.stepping)
@@ -687,6 +694,10 @@ Phaser.Game.prototype = {
687694
}
688695
}
689696

697+
},
698+
699+
updateRender: function (elapsedTime) {
700+
690701
if (this.renderType != Phaser.HEADLESS)
691702
{
692703
this.state.preRender();

src/system/RequestAnimationFrame.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,11 @@ Phaser.RequestAnimationFrame.prototype = {
103103
/**
104104
* The update method for the requestAnimationFrame
105105
* @method Phaser.RequestAnimationFrame#updateRAF
106+
*
106107
*/
107-
updateRAF: function () {
108+
updateRAF: function (rafTime) {
108109

109-
this.game.update(Date.now());
110+
this.game.update(Math.floor(rafTime));
110111

111112
this._timeOutID = window.requestAnimationFrame(this._onLoop);
112113

src/time/Time.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Phaser.Time = function (game) {
2121

2222
/**
2323
* @property {number} time - Game time counter. If you need a value for in-game calculation please use Phaser.Time.now instead.
24+
* - This always contains Date.now, but Phaser.Time.now will hold the high resolution RAF timer value (if RAF is available)
2425
* @protected
2526
*/
2627
this.time = 0;
@@ -49,6 +50,11 @@ Phaser.Time = function (game) {
4950
*/
5051
this.pausedTime = 0;
5152

53+
/**
54+
* @property {number} desiredFps = 60 - The desired frame-rate for this project.
55+
*/
56+
this.desiredFps = 60;
57+
5258
/**
5359
* @property {boolean} advancedTiming - If true Phaser.Time will perform advanced profiling including the fps rate, fps min/max and msMin and msMax.
5460
* @default
@@ -93,9 +99,10 @@ Phaser.Time = function (game) {
9399
this.deltaCap = 0;
94100

95101
/**
96-
* @property {number} timeCap - If the difference in time between two frame updates exceeds this value, the frame time is reset to avoid huge elapsed counts.
102+
* @property {number} timeCap - If the difference in time between two frame updates exceeds this value in ms, the frame time is reset to avoid huge elapsed counts.
103+
* - assumes a desiredFps of 60
97104
*/
98-
this.timeCap = 1 / 60 * 1000;
105+
this.timeCap = 1000 / 60;
99106

100107
/**
101108
* @property {number} frames - The number of frames record in the last second. Only calculated if Time.advancedTiming is true.
@@ -113,9 +120,9 @@ Phaser.Time = function (game) {
113120
this.timeToCall = 0;
114121

115122
/**
116-
* @property {number} lastTime - Internal value used by timeToCall as part of the setTimeout loop
123+
* @property {number} timeExpected - The time when the next call is expected when using setTimer to control the update loop
117124
*/
118-
this.lastTime = 0;
125+
this.timeExpected = 0;
119126

120127
/**
121128
* @property {Phaser.Timer} events - This is a Phaser.Timer object bound to the master clock to which you can add timed events.
@@ -242,13 +249,20 @@ Phaser.Time.prototype = {
242249
*/
243250
update: function (time) {
244251

245-
this.prevTime = this.now;
252+
// this.time always holds Date.now, this.now may hold the RAF high resolution time value if RAF is available (otherwise it also holds Date.now)
253+
this.time = Date.now;
246254

255+
// 'now' is currently still holding the time of the last call, move it into prevTime
256+
this.prevTime = this.now;
257+
// update 'now' to hold the current time
247258
this.now = time;
259+
// elapsed time between previous call and now
260+
this.elapsed = this.now - this.prevTime;
248261

249-
this.timeToCall = this.game.math.max(0, 16 - (time - this.lastTime));
250-
251-
this.elapsed = this.now - this.time;
262+
// time to call this function again in ms in case we're using timers instead of RequestAnimationFrame to update the game
263+
this.timeToCall = Math.floor(this.game.math.max(0, (1000.0 / this.desiredFps) - (this.timeCallExpected - time)));
264+
// time when the next call is expected if using timers
265+
this.timeCallExpected = time + this.timeToCall;
252266

253267
// spike-dislike
254268
if (this.elapsed > this.timeCap)
@@ -259,8 +273,8 @@ Phaser.Time.prototype = {
259273
this.elapsed = this.timeCap;
260274
}
261275

262-
// Calculate physics elapsed, ensure it's > 0, use 1/60 as a fallback
263-
this.physicsElapsed = this.elapsed / 1000 || 1 / 60;
276+
// Calculate physics elapsed, ensure it's > 0, use 1/this.desiredFps as a fallback
277+
this.physicsElapsed = this.elapsed / 1000 || 1 / this.desiredFps;
264278

265279
if (this.deltaCap > 0 && this.physicsElapsed > this.deltaCap)
266280
{
@@ -284,9 +298,6 @@ Phaser.Time.prototype = {
284298
}
285299
}
286300

287-
this.time = this.now;
288-
this.lastTime = time + this.timeToCall;
289-
290301
// Paused but still running?
291302
if (!this.game.paused)
292303
{
@@ -346,7 +357,7 @@ Phaser.Time.prototype = {
346357
// Level out the elapsed timer to avoid spikes
347358
this.time = this.now = Date.now();
348359

349-
this.pauseDuration = this.time - this._pauseStarted;
360+
this.pauseDuration = this.now - this._pauseStarted;
350361

351362
this.events.resume();
352363

0 commit comments

Comments
 (0)