From 9976180250d7168bdae3da94d8e6a9baa482f710 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 20 Jul 2018 16:28:29 +0200 Subject: [PATCH] Examples - Unify Loops --- lib/src/01-WhileLoop.dart | 23 +++++++++++++++++------ lib/src/03-VariableTimestep.dart | 6 ++++-- lib/src/04-FixedLoopVariableRender.dart | 9 ++++----- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/src/01-WhileLoop.dart b/lib/src/01-WhileLoop.dart index fb2d6fe..4c00373 100644 --- a/lib/src/01-WhileLoop.dart +++ b/lib/src/01-WhileLoop.dart @@ -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(); + while (true) { + update(); } } void update() { - print("i am updating"); + game.update(); + game.draw(); } -} \ No newline at end of file + + // Starting and stopping the loop for the example page + void stop() { + running = false; + } + + void start() { + running = true; + } +} diff --git a/lib/src/03-VariableTimestep.dart b/lib/src/03-VariableTimestep.dart index 6afe212..1a6b147 100644 --- a/lib/src/03-VariableTimestep.dart +++ b/lib/src/03-VariableTimestep.dart @@ -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); } } \ No newline at end of file diff --git a/lib/src/04-FixedLoopVariableRender.dart b/lib/src/04-FixedLoopVariableRender.dart index 95c65c7..6c00672 100644 --- a/lib/src/04-FixedLoopVariableRender.dart +++ b/lib/src/04-FixedLoopVariableRender.dart @@ -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); } } \ No newline at end of file