Skip to content

Commit 6468285

Browse files
committed
Calculate suggestedFps.
Deprecated timeCap.
1 parent c38f480 commit 6468285

1 file changed

Lines changed: 33 additions & 14 deletions

File tree

src/time/Time.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ Phaser.Time = function (game) {
5555
*/
5656
this.desiredFps = 60;
5757

58+
/**
59+
* @property {number} suggestedFps = null - The suggested frame-rate for this project.
60+
* NOTE: not available until after a few frames have passed, it is recommended to use this after a few seconds (eg. after the menus)
61+
*/
62+
this.suggestedFps = null;
63+
64+
/**
65+
* @property {number} _frameCount - count the number of calls to time.update since the last suggestedFps was calculated
66+
* @private
67+
*/
68+
this._frameCount = 0;
69+
70+
/**
71+
* @property {number} _elapsedAcumulator - sum of the elapsed time since the last suggestedFps was calculated
72+
* @private
73+
*/
74+
this._elapsedAccumulator = 0;
75+
5876
/**
5977
* @property {number} slowMotion = 1.0 - Scaling factor to make the game move smoothly in slow motion (1.0 = normal speed, 2.0 = half speed)
6078
* @type {Number}
@@ -107,6 +125,8 @@ Phaser.Time = function (game) {
107125
/**
108126
* @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.
109127
* - assumes a desiredFps of 60
128+
*
129+
* DEPRECATED: this no longer has any effect since the change to fixed-time stepping in game.update 3rd November 2014
110130
*/
111131
this.timeCap = 1000 / 60;
112132

@@ -270,13 +290,17 @@ Phaser.Time.prototype = {
270290
// time when the next call is expected if using timers
271291
this.timeCallExpected = time + this.timeToCall;
272292

273-
// spike-dislike
274-
if (this.elapsed > this.timeCap)
293+
// count the number of time.update calls
294+
this._frameCount++;
295+
this._elapsedAccumulator += this.elapsed;
296+
297+
// occasionally recalculate the suggestedFps based on the accumulated elapsed time
298+
if (this._frameCount >= this.desiredFps * 2)
275299
{
276-
// For some reason the time between now and the last time the game was updated was larger than our timeCap
277-
// This can happen if the Stage.disableVisibilityChange is true and you swap tabs, which makes the raf pause.
278-
// In this case we'll drop to some default values to stop the game timers going nuts.
279-
this.elapsed = this.timeCap;
300+
// this formula calculates suggestedFps in multiples of 5 fps
301+
this.suggestedFps = Math.floor(200 / (this._elapsedAccumulator / this._frameCount)) * 5;
302+
this._frameCount = 0;
303+
this._elapsedAccumulator = 0;
280304
}
281305

282306
// Set the physics elapsed time... this will always be 1 / this.desiredFps because we're using fixed time steps in game.update now
@@ -339,8 +363,6 @@ Phaser.Time.prototype = {
339363
*/
340364
gamePaused: function () {
341365

342-
// use Date.now (instead of time.now) because the gameResumed function uses Date.now and the two values must be compatible
343-
// (time.now may not be updated while the game is paused so we can't use that in both places)
344366
this._pauseStarted = Date.now();
345367

346368
this.events.pause();
@@ -362,13 +384,10 @@ Phaser.Time.prototype = {
362384
*/
363385
gameResumed: function () {
364386

365-
// TODO: check if this is needed. It won't work if the game is using RAF timing
366-
// (Date.now() values can't mix with RAF times) but the deltaCap should do the same thing anyway and the new fixed step timing will also help.
367-
368-
// Level out the elapsed timer to avoid spikes
369-
// this.time = this.now = Date.now();
387+
// Set the parameter which stores Date.now() to make sure it's correct on resume
388+
this.time = Date.now();
370389

371-
this.pauseDuration = Date.now() - this._pauseStarted;
390+
this.pauseDuration = this.time - this._pauseStarted;
372391

373392
this.events.resume();
374393

0 commit comments

Comments
 (0)