Examples - Unify Loops

This commit is contained in:
Marty Oehme 2018-07-20 16:28:29 +02:00
parent cc1d3ac4da
commit 9976180250
3 changed files with 25 additions and 13 deletions

View file

@ -12,20 +12,31 @@ import 'package:browserloop/game/LoopExample.dart';
/// with Timer.periodic(16, callbackFunction); /// with Timer.periodic(16, callbackFunction);
/// To see how I got it running for the example, /// To see how I got it running for the example,
/// see file 02-AnimationFrameWhile.dart /// see file 02-AnimationFrameWhile.dart
class WhileLoop implements LoopExample { class SimpleLoop implements LoopExample {
Game game; Game game;
bool running = false;
WhileLoop(Game this.game) { SimpleLoop(Game this.game) {
eventloop(); eventloop();
} }
void eventloop() { void eventloop() {
while(true) { while (true) {
game.update(); update();
} }
} }
void 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;
}
}

View file

@ -25,8 +25,6 @@ class VariableTimestep implements LoopExample {
Stopwatch elapsed = new Stopwatch(); Stopwatch elapsed = new Stopwatch();
VariableTimestep(Game this.game) { VariableTimestep(Game this.game) {
elapsed.start();
window.requestAnimationFrame(eventloop);
} }
void eventloop(num time) { void eventloop(num time) {
@ -38,11 +36,15 @@ class VariableTimestep implements LoopExample {
id = window.requestAnimationFrame(eventloop); id = window.requestAnimationFrame(eventloop);
} }
// Starting and stopping the loop for the example page
void stop() { void stop() {
elapsed.stop();
window.cancelAnimationFrame(id); window.cancelAnimationFrame(id);
} }
void start() { void start() {
elapsed.start();
elapsed.reset();
window.requestAnimationFrame(eventloop); window.requestAnimationFrame(eventloop);
} }
} }

View file

@ -12,7 +12,6 @@ class FixedLoopVariableRender implements LoopExample {
num id; num id;
FixedLoopVariableRender(this.game) { FixedLoopVariableRender(this.game) {
elapsed.start(); elapsed.start();
window.requestAnimationFrame(eventloop); window.requestAnimationFrame(eventloop);
} }
@ -31,22 +30,22 @@ class FixedLoopVariableRender implements LoopExample {
} }
void update() { void update() {
print('updating');
game.update(); game.update();
return;
} }
void render(double interp) { void render(double interp) {
print('rendering, interp:$interp');
game.draw(interp); game.draw(interp);
return;
} }
// Starting and stopping the loop for the example page
void stop() { void stop() {
elapsed.stop();
window.cancelAnimationFrame(id); window.cancelAnimationFrame(id);
} }
void start() { void start() {
elapsed.start();
elapsed.reset();
window.requestAnimationFrame(eventloop); window.requestAnimationFrame(eventloop);
} }
} }