Allow setting a canvas for an engine at any point

This commit is contained in:
Marty Oehme 2018-08-25 14:36:50 +02:00
parent 4c6dff35c3
commit 66bf87d9d8
2 changed files with 33 additions and 3 deletions

View file

@ -22,6 +22,11 @@ class Engine {
num _updateLag = 0.0; num _updateLag = 0.0;
num _drawLag = 0.0; num _drawLag = 0.0;
/// The Canvas to Paint on
///
/// Manually define or change the canvas the engine should render to. Should
/// be used if no canvas was defined at engine creation and it should be
/// rendered later.
html.CanvasElement canvas; html.CanvasElement canvas;
Grid _grid = new Grid(GRID_X, GRID_Y); Grid _grid = new Grid(GRID_X, GRID_Y);
bool running = false; bool running = false;
@ -68,15 +73,30 @@ class Engine {
} }
} }
/// Update Engine Logic
///
/// Updates the logic of the engine by one tick. Should usually not be called
/// directly, since it is automatically taken care of by the processing function.
/// If simulation should be advanced manually one time, prefer using step().
void update() { void update() {
if (!_grid.update()) running = false; if (!_grid.update()) running = false;
} }
/// Advances Logic One Update
///
/// Moves logic of the engine one step forward and pauses the
/// simulation. Does not automatically re-render the new state
/// (though this should usually not pose a problem).
void step() { void step() {
running = false; running = false;
_grid.update(); _grid.update();
} }
/// Renders the Current Simulation State
///
/// Renders the simulation once. Will usually automatically be called by
/// the internal engine processing. Does not do anything if no canvas is
/// defined.
void render([num interp]) { void render([num interp]) {
if(canvas != null) _grid.render(canvas, interp); if(canvas != null) _grid.render(canvas, interp);
} }

View file

@ -1,22 +1,32 @@
import 'dart:html' as html;
@TestOn('browser') @TestOn('browser')
import 'package:rules_of_living/src/Engine.dart'; import 'package:rules_of_living/src/Engine.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
void main() { void main() {
Engine sut;
setUp(() {
sut = Engine();
});
test("Engine can be instantiated without canvas", () { test("Engine can be instantiated without canvas", () {
expect(Engine(), isNot(throwsNoSuchMethodError)); expect(sut, isNot(throwsNoSuchMethodError));
}); });
test("Engine does not throw errors when calling render directly", () { test("Engine does not throw errors when calling render directly", () {
Engine sut = new Engine();
// anonymous function necessary since throws can not use functions with args // anonymous function necessary since throws can not use functions with args
expect(() => sut.render(), isNot(throwsNoSuchMethodError)); expect(() => sut.render(), isNot(throwsNoSuchMethodError));
}); });
test("Engine does not throw errors when processing without attached canvas", () { test("Engine does not throw errors when processing without attached canvas", () {
Engine sut = new Engine();
// anonymous function necessary since throws can not use functions with args // anonymous function necessary since throws can not use functions with args
expect(() => sut.process(1000), isNot(throwsNoSuchMethodError)); expect(() => sut.process(1000), isNot(throwsNoSuchMethodError));
}); });
test("setCanvas allows setting a canvas for an engine at any point", () {
sut.canvas = new html.CanvasElement();
expect(sut.canvas, isNotNull);
});
} }