2018-07-20 12:56:09 +00:00
|
|
|
import 'dart:html';
|
|
|
|
import 'package:browserloop/game/Game.dart';
|
|
|
|
import 'package:browserloop/game/LoopExample.dart';
|
|
|
|
|
|
|
|
/// The Variable Timestep Loop.
|
|
|
|
///
|
|
|
|
/// Perhaps one of the most widely used loops
|
|
|
|
/// in games for many years. Many game frameworks
|
|
|
|
/// make use of a loop similar to this.
|
|
|
|
class VariableTimestep implements LoopExample {
|
|
|
|
Game game;
|
|
|
|
num id;
|
|
|
|
Stopwatch elapsed = new Stopwatch();
|
|
|
|
|
2018-07-20 17:02:54 +00:00
|
|
|
VariableTimestep(Game this.game);
|
2018-07-20 12:56:09 +00:00
|
|
|
|
|
|
|
void eventloop(num time) {
|
|
|
|
int dt = elapsed.elapsedMilliseconds;
|
|
|
|
game.update(dt);
|
|
|
|
game.draw();
|
|
|
|
elapsed.reset();
|
|
|
|
|
|
|
|
id = window.requestAnimationFrame(eventloop);
|
|
|
|
}
|
|
|
|
|
2018-07-20 14:28:29 +00:00
|
|
|
// Starting and stopping the loop for the example page
|
2018-07-20 12:56:09 +00:00
|
|
|
void stop() {
|
2018-07-20 14:28:29 +00:00
|
|
|
elapsed.stop();
|
2018-07-20 12:56:09 +00:00
|
|
|
window.cancelAnimationFrame(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void start() {
|
2018-07-20 14:28:29 +00:00
|
|
|
elapsed.start();
|
|
|
|
elapsed.reset();
|
2018-07-20 12:56:09 +00:00
|
|
|
window.requestAnimationFrame(eventloop);
|
|
|
|
}
|
2018-07-24 11:35:25 +00:00
|
|
|
}
|