Examples - Unify Loops
This commit is contained in:
parent
cc1d3ac4da
commit
9976180250
3 changed files with 25 additions and 13 deletions
|
@ -12,20 +12,31 @@ import 'package:browserloop/game/LoopExample.dart';
|
|||
/// with Timer.periodic(16, callbackFunction);
|
||||
/// To see how I got it running for the example,
|
||||
/// see file 02-AnimationFrameWhile.dart
|
||||
class WhileLoop implements LoopExample {
|
||||
class SimpleLoop implements LoopExample {
|
||||
Game game;
|
||||
bool running = false;
|
||||
|
||||
WhileLoop(Game this.game) {
|
||||
SimpleLoop(Game this.game) {
|
||||
eventloop();
|
||||
}
|
||||
|
||||
void eventloop() {
|
||||
while (true) {
|
||||
game.update();
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void update() {
|
||||
print("i am updating");
|
||||
game.update();
|
||||
game.draw();
|
||||
}
|
||||
|
||||
// Starting and stopping the loop for the example page
|
||||
void stop() {
|
||||
running = false;
|
||||
}
|
||||
|
||||
void start() {
|
||||
running = true;
|
||||
}
|
||||
}
|
|
@ -25,8 +25,6 @@ class VariableTimestep implements LoopExample {
|
|||
Stopwatch elapsed = new Stopwatch();
|
||||
|
||||
VariableTimestep(Game this.game) {
|
||||
elapsed.start();
|
||||
window.requestAnimationFrame(eventloop);
|
||||
}
|
||||
|
||||
void eventloop(num time) {
|
||||
|
@ -38,11 +36,15 @@ class VariableTimestep implements LoopExample {
|
|||
id = window.requestAnimationFrame(eventloop);
|
||||
}
|
||||
|
||||
// Starting and stopping the loop for the example page
|
||||
void stop() {
|
||||
elapsed.stop();
|
||||
window.cancelAnimationFrame(id);
|
||||
}
|
||||
|
||||
void start() {
|
||||
elapsed.start();
|
||||
elapsed.reset();
|
||||
window.requestAnimationFrame(eventloop);
|
||||
}
|
||||
}
|
|
@ -12,7 +12,6 @@ class FixedLoopVariableRender implements LoopExample {
|
|||
num id;
|
||||
|
||||
FixedLoopVariableRender(this.game) {
|
||||
|
||||
elapsed.start();
|
||||
window.requestAnimationFrame(eventloop);
|
||||
}
|
||||
|
@ -31,22 +30,22 @@ class FixedLoopVariableRender implements LoopExample {
|
|||
}
|
||||
|
||||
void update() {
|
||||
print('updating');
|
||||
game.update();
|
||||
return;
|
||||
}
|
||||
|
||||
void render(double interp) {
|
||||
print('rendering, interp:$interp');
|
||||
game.draw(interp);
|
||||
return;
|
||||
}
|
||||
|
||||
// Starting and stopping the loop for the example page
|
||||
void stop() {
|
||||
elapsed.stop();
|
||||
window.cancelAnimationFrame(id);
|
||||
}
|
||||
|
||||
void start() {
|
||||
elapsed.start();
|
||||
elapsed.reset();
|
||||
window.requestAnimationFrame(eventloop);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue