Skip to content

Commit 9ee4160

Browse files
committed
Exposed game loop settings via game config object.
1 parent 9eb1676 commit 9ee4160

4 files changed

Lines changed: 49 additions & 21 deletions

File tree

v3/src/boot/Config.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,16 @@ var Config = function (config)
5757
this.bannerTextColor = GetValue(config, 'banner.text', defaultBannerTextColor);
5858
this.bannerBackgroundColor = GetValue(config, 'banner.background', defaultBannerColor);
5959

60-
this.fps = GetValue(config, 'fps', 60);
61-
this.forceSetTimeOut = GetValue(config, 'forceSetTimeOut', false);
60+
// Frame Rate config
61+
// fps: {
62+
// min: 10,
63+
// target: 60,
64+
// max: 120
65+
// forceSetTimeOut: false,
66+
// deltaHistory: 10
67+
// }
68+
69+
this.fps = GetValue(config, 'fps', null);
6270

6371
this.pixelArt = GetValue(config, 'pixelArt', false);
6472
this.transparent = GetValue(config, 'transparent', false);

v3/src/boot/Game.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Game.prototype = {
114114

115115
this.config.postBoot();
116116

117-
this.loop.start(!!this.config.forceSetTimeOut, this.step.bind(this));
117+
this.loop.start(this.step.bind(this));
118118
},
119119

120120
step: function (time, delta)

v3/src/boot/VariableTimeStep.js

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
var NOOP = require('../utils/NOOP');
2+
var GetValue = require('../utils/object/GetValue');
23
var RequestAnimationFrame = require('../dom/RequestAnimationFrame');
34

4-
var VariableTimeStep = function (game, framerate)
5+
// Frame Rate config
6+
// fps: {
7+
// min: 10,
8+
// target: 60,
9+
// max: 120
10+
// forceSetTimeOut: false,
11+
// deltaHistory: 10
12+
// }
13+
14+
var VariableTimeStep = function (game, config)
515
{
616
this.game = game;
717

818
this.raf = new RequestAnimationFrame();
919

1020
this.started = false;
1121
this.running = false;
22+
23+
this.minFps = GetValue(config, 'min', 5);
24+
this.maxFps = GetValue(config, 'max', 120);
25+
this.targetFps = GetValue(config, 'target', 60);
26+
27+
this._min = 1000 / this.minFps; // 200ms between frames (i.e. super slow!)
28+
this._max = 1000 / this.maxFps; // 8.333ms between frames (i.e. super fast, 120Hz displays)
29+
this._target = 1000 / this.targetFps; // 16.666ms between frames (i.e. normal)
1230

13-
// For fixed-step physics
14-
this.fps = framerate;
31+
// 200 / 1000 = 0.2 (5fps)
32+
// 8.333 / 1000 = 0.008333 (120fps)
33+
// 16.666 / 1000 = 0.01666 (60fps)
1534

1635
this.callback = NOOP;
1736

18-
this.useRAF = true;
37+
this.forceSetTimeOut = GetValue(config, 'forceSetTimeOut', false);
1938

2039
this.time = 0;
2140
this.startTime = 0;
@@ -24,14 +43,14 @@ var VariableTimeStep = function (game, framerate)
2443
this.delta = 0;
2544
this.deltaIndex = 0;
2645
this.deltaHistory = [];
27-
this.deltaSmoothingMax = 10;
46+
this.deltaSmoothingMax = GetValue(config, 'deltaHistory', 10);
2847
};
2948

3049
VariableTimeStep.prototype.constructor = VariableTimeStep;
3150

3251
VariableTimeStep.prototype = {
3352

34-
start: function (useRAF, callback)
53+
start: function (callback)
3554
{
3655
if (this.started)
3756
{
@@ -54,40 +73,41 @@ VariableTimeStep.prototype = {
5473

5574
for (var i = 0; i < this.deltaSmoothingMax; i++)
5675
{
57-
history[i] = 0.0166;
76+
history[i] = this._target;
5877
}
5978

6079
this.delta = 0;
6180
this.deltaIndex = 0;
6281
this.deltaHistory = history;
6382

64-
this.useRAF = useRAF;
6583
this.callback = callback;
6684

67-
this.raf.start(this.step.bind(this), useRAF);
85+
this.raf.start(this.step.bind(this), this.forceSetTimeOut);
6886
},
6987

88+
// time comes from requestAnimationFrame and is either a high res time value,
89+
// or Date.now if using setTimeout
7090
step: function (time)
7191
{
7292
var idx = this.deltaIndex;
7393
var history = this.deltaHistory;
7494
var max = this.deltaSmoothingMax;
7595

76-
// delta time
77-
var dt = (time - this.lastTime) / 1000;
96+
// delta time (time is in ms)
97+
var dt = (time - this.lastTime);
7898

79-
// min / max range
80-
if (dt < 0.0001 || dt > 0.5)
99+
// min / max range (yes, the < and > should be this way around)
100+
if (dt > this._min || dt < this._max)
81101
{
82-
// Probably super bad start time or browser tab inactivity / context loss
102+
// Probably super bad start time or browser tab context loss,
83103
// so use the last 'sane' dt value
84104

85105
console.log('dt sync', dt, 'ms over', history[idx]);
86106

87107
dt = history[idx];
88108

89-
// clamp delta to 0.0001 to 0.5 range
90-
dt = Math.max(Math.min(dt, 0.5), 0.0001);
109+
// Clamp delta to min max range (in case history has become corrupted somehow)
110+
dt = Math.max(Math.min(dt, this._max), this._min);
91111
}
92112

93113
// Smooth out the delta over the previous X frames
@@ -117,7 +137,7 @@ VariableTimeStep.prototype = {
117137
// Real-world timer advance
118138
this.time += avg;
119139

120-
this.callback(this.time, avg * 1000);
140+
this.callback(this.time, avg);
121141

122142
// Shift time value over
123143
this.lastTime = time;

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: 'f7392da0-2f9d-11e7-ba4c-5959ed8510b1'
2+
build: 'a4716c80-3016-11e7-a2a9-6b058a4cdd76'
33
};
44
module.exports = CHECKSUM;

0 commit comments

Comments
 (0)