From 828b39c1d0e13815a6f6f6d5317503f74160db56 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 20 Jul 2018 14:56:09 +0200 Subject: [PATCH] Examples - Add Variable Timestep Example --- lib/src/03-VariableTimestep.dart | 48 +++++++++++++++++++ ...r.dart => 04-FixedLoopVariableRender.dart} | 0 web/index.html | 3 +- web/main.dart | 7 +-- 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 lib/src/03-VariableTimestep.dart rename lib/src/{03-FixedLoopVariableRender.dart => 04-FixedLoopVariableRender.dart} (100%) diff --git a/lib/src/03-VariableTimestep.dart b/lib/src/03-VariableTimestep.dart new file mode 100644 index 0000000..6afe212 --- /dev/null +++ b/lib/src/03-VariableTimestep.dart @@ -0,0 +1,48 @@ +//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); + } +} \ No newline at end of file diff --git a/lib/src/03-FixedLoopVariableRender.dart b/lib/src/04-FixedLoopVariableRender.dart similarity index 100% rename from lib/src/03-FixedLoopVariableRender.dart rename to lib/src/04-FixedLoopVariableRender.dart diff --git a/web/index.html b/web/index.html index 5ef061d..5dbd6e8 100644 --- a/web/index.html +++ b/web/index.html @@ -12,7 +12,8 @@
-
+
+
diff --git a/web/main.dart b/web/main.dart index a0f6973..acaff96 100644 --- a/web/main.dart +++ b/web/main.dart @@ -1,12 +1,13 @@ import 'dart:html'; import 'package:browserloop/game/Game.dart'; import 'package:browserloop/game/LoopExample.dart'; -import 'package:browserloop/src/03-FixedLoopVariableRender.dart'; +import 'package:browserloop/src/04-FixedLoopVariableRender.dart'; import 'package:browserloop/src/02-AnimationFrameWhile.dart'; List examples = [ Example("While Loop Example", "#while_output"), - Example("Fixed Update, Variable Render", "#variable_output") + Example("Fixed Update, Variable Render", "#fixed_variable"), + Example("Variable Timestep", "#variable_timestep") ]; LoopExample active; @@ -84,7 +85,7 @@ void activate(MouseEvent e) { print("#while_output loop added"); ex.loop = new WhileLoop(new Game(ex.canvas)); break; - case "#variable_output": + case "#fixed_variable": print("#variable_output loop added"); ex.loop = new FixedLoopVariableRender(new Game(ex.canvas)); break;