48 lines
1 KiB
Dart
48 lines
1 KiB
Dart
|
//void main() {
|
||
|
// double lastTime = performance.now();
|
||
|
// while(true) {
|
||
|
// double currentTime = performance.now();
|
||
|
// double delta = currentTime - lastTime;
|
||
|
//
|
||
|
// update(delta);
|
||
|
//
|
||
|
// lastTime = currentTime;
|
||
|
// }
|
||
|
//}
|
||
|
|
||
|
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();
|
||
|
|
||
|
VariableTimestep(Game this.game) {
|
||
|
elapsed.start();
|
||
|
window.requestAnimationFrame(eventloop);
|
||
|
}
|
||
|
|
||
|
void eventloop(num time) {
|
||
|
int dt = elapsed.elapsedMilliseconds;
|
||
|
game.update(dt);
|
||
|
game.draw();
|
||
|
elapsed.reset();
|
||
|
|
||
|
id = window.requestAnimationFrame(eventloop);
|
||
|
}
|
||
|
|
||
|
void stop() {
|
||
|
window.cancelAnimationFrame(id);
|
||
|
}
|
||
|
|
||
|
void start() {
|
||
|
window.requestAnimationFrame(eventloop);
|
||
|
}
|
||
|
}
|