Examples - Add Variable Timestep Example

This commit is contained in:
Marty Oehme 2018-07-20 14:56:09 +02:00
parent 5b7552216d
commit 828b39c1d0
4 changed files with 54 additions and 4 deletions

View file

@ -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);
}
}

View file

@ -12,7 +12,8 @@
<body> <body>
<!--Basic Game-loop--> <!--Basic Game-loop-->
<div id="while_output"></div> <div id="while_output"></div>
<div id="variable_output"></div> <div id="variable_timestep"></div>
<div id="fixed_variable"></div>
<!--<button id="basic_start">start</button>--> <!--<button id="basic_start">start</button>-->
<!--<button id="basic_stop">stop</button>--> <!--<button id="basic_stop">stop</button>-->
<!--<button id="basic_reset">reset</button>--> <!--<button id="basic_reset">reset</button>-->

View file

@ -1,12 +1,13 @@
import 'dart:html'; import 'dart:html';
import 'package:browserloop/game/Game.dart'; import 'package:browserloop/game/Game.dart';
import 'package:browserloop/game/LoopExample.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'; import 'package:browserloop/src/02-AnimationFrameWhile.dart';
List<Example> examples = [ List<Example> examples = [
Example("While Loop Example", "#while_output"), 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; LoopExample active;
@ -84,7 +85,7 @@ void activate(MouseEvent e) {
print("#while_output loop added"); print("#while_output loop added");
ex.loop = new WhileLoop(new Game(ex.canvas)); ex.loop = new WhileLoop(new Game(ex.canvas));
break; break;
case "#variable_output": case "#fixed_variable":
print("#variable_output loop added"); print("#variable_output loop added");
ex.loop = new FixedLoopVariableRender(new Game(ex.canvas)); ex.loop = new FixedLoopVariableRender(new Game(ex.canvas));
break; break;