Initial Source Commit

This commit is contained in:
Marty Oehme 2018-07-20 11:58:11 +02:00
parent f27b385066
commit 2eacf58314
10 changed files with 400 additions and 0 deletions

64
lib/src/00-oldcode.dart Normal file
View file

@ -0,0 +1,64 @@
import 'dart:html' as html;
import 'dart:math' as math;
html.CanvasRenderingContext2D ctx;
html.CanvasElement el;
math.Random rng = new math.Random();
int GRIDNUM = 10;
int animID = 0;
// These are the functions to start the animation and stop the animation.
// (They are connected to the start/stop buttons in the demo setup.)
var _stop = () => html.window.cancelAnimationFrame(animID);
var _start = () => html.window.requestAnimationFrame(eventloop);
// This is what ticks every frame of the animation.
void eventloop(currentTime) {
print(currentTime.toString());
ctx.setFillColorRgb(rng.nextInt(255), rng.nextInt(255), rng.nextInt(255));
int BRICKSIZE = (el.width ~/ GRIDNUM);
int x = rng.nextInt(GRIDNUM) * BRICKSIZE;
int y = rng.nextInt(GRIDNUM) * BRICKSIZE;
ctx.fillRect(x, y, (BRICKSIZE), (BRICKSIZE));
animID = html.window.requestAnimationFrame(
eventloop); // recursive call to our eventloop - calls it forever as soon as browser is willing to give us his time
}
void main() {
_demoSetup();
}
//buttons and canvas setup
void _demoSetup() {
el = new html.CanvasElement(width: 480, height: 480);
html.querySelector('#basic_output').append(el);
ctx = el.getContext('2d');
var _clear = () {
_stop();
ctx.setFillColorRgb(rng.nextInt(255), rng.nextInt(255), rng.nextInt(255));
ctx.fillRect(0, 0, el.width, el.height);
ctx.setFillColorRgb(rng.nextInt(255), rng.nextInt(255), rng.nextInt(255));
};
var _changeGrid = (int delta) {
_clear();
int newgrid = GRIDNUM + delta;
GRIDNUM = (newgrid == 0) ? GRIDNUM : newgrid;
_start();
};
_clear();
html.querySelector('#basic_start').onClick.listen((e) {
print('started');
_stop();
_start();
});
html.querySelector('#basic_stop').onClick.listen((e) => _stop());
html
.querySelector('#basic_reset')
.onClick
.listen((e) => _clear());
html.querySelector('#basic_plus').onClick.listen((e) => _changeGrid(-5));
html.querySelector('#basic_minus').onClick.listen((e) => _changeGrid(5));
}

31
lib/src/01-WhileLoop.dart Normal file
View file

@ -0,0 +1,31 @@
import 'package:browserloop/game/Game.dart';
import 'package:browserloop/game/LoopExample.dart';
/// The most basic update loop possible.
///
/// Best not to use this loop since it has a tendency to
/// crash your browser and not load correctly at all.
/// The way to get this to run would be the setInterval method
/// simple is either through setInterval in Javascript:
/// https://www.w3schools.com/howto/howto_js_animate.asp
/// or through the Timer API in Dart, especially
/// with Timer.periodic(16, callbackFunction);
/// To see how I got it running for the example,
/// see file 02-AnimationFrameWhile.dart
class WhileLoop implements LoopExample {
Game game;
WhileLoop(Game this.game) {
eventloop();
}
void eventloop() {
while(true) {
game.update();
}
}
void update() {
print("i am updating");
}
}

View file

@ -0,0 +1,32 @@
import 'dart:html';
import 'package:browserloop/game/Game.dart';
import 'package:browserloop/game/LoopExample.dart';
/// The most basic update loop possible.
///
/// Note that this is technically cheating since it is using
/// animationFrames to improve browser performance for the examples in the
/// blog post. The real, actual simplest code is in file 01-WhileLoop.dart
class WhileLoop implements LoopExample {
Game game;
num id;
WhileLoop(Game this.game) {
window.requestAnimationFrame(eventloop);
}
void eventloop(num time) {
game.update();
game.draw();
id = window.requestAnimationFrame(eventloop);
}
void stop() {
window.cancelAnimationFrame(id);
}
void start() {
window.requestAnimationFrame(eventloop);
}
}

View file

@ -0,0 +1,52 @@
import 'dart:html';
import 'package:browserloop/game/Game.dart';
import 'package:browserloop/game/LoopExample.dart';
class FixedLoopVariableRender implements LoopExample {
static final double MS_PER_UPDATE = 1000.0;
static final double SAFE_GUARD = 500.0;
Stopwatch elapsed = new Stopwatch();
double lag = 0.0;
Game game;
num id;
FixedLoopVariableRender(this.game) {
elapsed.start();
window.requestAnimationFrame(eventloop);
}
void eventloop(num time) {
lag += elapsed.elapsedMilliseconds;
elapsed.reset();
while (lag >= MS_PER_UPDATE && elapsed.elapsedMilliseconds < SAFE_GUARD) {
update();
lag -= MS_PER_UPDATE;
}
render(lag / MS_PER_UPDATE);
id = window.requestAnimationFrame(eventloop);
}
void update() {
print('updating');
game.update();
return;
}
void render(double interp) {
print('rendering, interp:$interp');
game.draw(interp);
return;
}
void stop() {
window.cancelAnimationFrame(id);
}
void start() {
window.requestAnimationFrame(eventloop);
}
}