Fix Timestep observing

This commit is contained in:
Marty Oehme 2018-07-06 14:27:17 +02:00
parent 9aff30452e
commit 4c40dd2baa

View file

@ -6,15 +6,16 @@ import 'package:rules_of_living/Grid.dart';
class App { class App {
// Elapsed Time Counter - useful for Safety Timeout // Elapsed Time Counter - useful for Safety Timeout
Stopwatch _elapsed = new Stopwatch(); Stopwatch _elapsed = new Stopwatch();
num lastNow = 0;
// Game Tick Rate - *does* impact game speed // Game Tick Rate - *does* impact game speed
final int _MS_PER_STEP = 1000 ~/ 1; final int _MS_PER_STEP = 1000;
// Max Frame (i.e. Rendering) rate - does *not* impact game speed // Max Frame (i.e. Rendering) rate - does *not* impact game speed
final int _MS_PER_FRAME = 1000 ~/ 1; final int _MS_PER_FRAME = 1000;
// ms stuck in updateloop after which game will declare itself unresponsive // ms stuck in updateloop after which game will declare itself unresponsive
final int SAFETY_TIMEOUT = 1000; final int SAFETY_TIMEOUT = 2000;
num _updateLag = 0.0; num _updateLag = 0.0;
num _drawLag = 0.0; num _drawLag = 0.0;
@ -22,18 +23,24 @@ class App {
final html.CanvasElement canvas; final html.CanvasElement canvas;
final Grid grid = new Grid(20,20); final Grid grid = new Grid(20,20);
final List<List<Cell>> map = new Grid(20, 20).map; List<List<Cell>> map;
App(this.canvas); App(this.canvas) {
this.map = this.grid.map;
}
void process(num now) { void process(num now) {
_drawLag = now; _drawLag = now - lastNow;
print(_drawLag);
lastNow = now;
_updateLag += _drawLag; _updateLag += _drawLag;
_elapsed.reset(); _elapsed.reset();
while (_updateLag >= _MS_PER_STEP) { while (_updateLag >= _MS_PER_STEP) {
if (_elapsed.elapsedMilliseconds > SAFETY_TIMEOUT) { if (_elapsed.elapsedMilliseconds > SAFETY_TIMEOUT) {
// TODO stub - give warning etc when this occurs // TODO stub - give warning etc when this occurs
print("ERROR STUCK IN UPDATE LOOP");
break; break;
} }
update(); update();
@ -47,6 +54,7 @@ class App {
} }
void update() { void update() {
print("updating");
grid.update(); grid.update();
} }