Preliminary Move of Renderr

NON-WORKING STATE. interim commit.
This commit is contained in:
Unknown 2018-10-25 17:40:55 +02:00
parent d79aceeebf
commit d80e149dbc
3 changed files with 53 additions and 46 deletions

View file

@ -1,5 +1,6 @@
import 'dart:html' as html;
import 'package:rules_of_living/src/Renderer.dart';
import 'package:rules_of_living/src/Simulation.dart';
class Engine {
@ -37,7 +38,12 @@ class Engine {
/// be used if no canvas was defined at engine creation and it should be
/// rendered later.
html.CanvasElement canvas;
Simulation _simulation;
Map<int, bool> _lastSimulationUpdate = {};
Renderer _renderer;
bool running = false;
Engine() {
@ -70,6 +76,7 @@ class Engine {
}
}
// TODO Should be renamed to something more intention revealing like hasReachedUpdateTimestepThreshold
bool _shouldUpdate(int updateLag, int elapsed, int timeOut) {
if (updateLag < _MS_PER_STEP) return false;
if (elapsed > timeOut) throw StackOverflowError;
@ -85,10 +92,10 @@ class Engine {
void update() {
if (_simulation == null) return;
Map<int, bool> simulationUpdate = _simulation.update();
_simulation.mergeStateChanges(simulationUpdate);
_lastSimulationUpdate = _simulation.update();
_simulation.mergeStateChanges(_lastSimulationUpdate);
if (simulationUpdate.length == 0) running = false;
if (_lastSimulationUpdate.length == 0) running = false;
}
/// Advances Logic One Update
@ -107,9 +114,9 @@ class Engine {
/// the internal engine processing. Does not do anything if no canvas is
/// defined.
void render([num interp]) {
if (canvas == null || _simulation == null) return;
if (canvas == null || _renderer == null) return;
_simulation.render(canvas, interp);
_renderer.render(canvas, _lastSimulationUpdate);
}
void set simulation(Simulation value) => _simulation = value;

40
lib/src/Renderer.dart Normal file
View file

@ -0,0 +1,40 @@
import 'dart:html' as html;
import 'dart:math' as math;
import 'package:rules_of_living/src/Grid.dart';
class Renderer {
bool _dirty = true;
bool _renderEdges = true;
bool get renderEdges => _renderEdges;
void set renderEdges(bool value) {
_renderEdges = value;
_dirty = true;
}
void render(html.CanvasElement canvas, Map<int, bool> map) {
// only renders if any cells changed between renders
if (!_dirty) return;
html.CanvasRenderingContext2D ctx = canvas.getContext('2d');
int brickW = (canvas.width ~/ map.width);
int brickH = (canvas.height ~/ map.height);
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (int i = 0; i < map.length; i++) {
math.Point p = map.toCoordinates(i);
if (_renderEdges) {
ctx.setStrokeColorRgb(100, 100, 100);
ctx.strokeRect(p.x * brickW, p.y * brickH, brickW, brickH);
}
if (map[i] == true)
ctx.setFillColorRgb(155, 155, 255);
else
ctx.setFillColorRgb(0, 0, 0);
ctx.fillRect(p.x * brickW, p.y * brickH, brickW, brickH);
}
_dirty = false;
}
}

View file

@ -1,4 +1,3 @@
import 'dart:html' as html;
import 'dart:math' as math;
import 'dart:math';
@ -14,12 +13,6 @@ class Simulation {
RuleSet rules = GameOfLife();
bool _dirty = true;
bool get dirty => _dirty;
bool _renderEdges = true;
bool get renderEdges => _renderEdges;
int _amount;
int _dispersal;
@ -36,9 +29,9 @@ class Simulation {
Simulation.fromGrid(Grid<bool> map) : this.map = map;
//TODO this needs to RETURN a changeSet, just like the others do
Grid<bool> reset([Grid<bool> map]) {
map ??= this.map;
_dirty = true;
map.setAll(0, List.filled(map.length, false));
return map;
}
@ -60,8 +53,6 @@ class Simulation {
cx + rng.nextInt(_dispersal), cy + rng.nextInt(_dispersal), true);
if (sanityCheck > 100 && sanityCheck > i * 3) break;
}
_dirty = true;
}
void setCellState(int x, int y, bool state) {
@ -91,7 +82,6 @@ class Simulation {
void mergeStateChanges(Map<int, bool> stateChanges) {
stateChanges.forEach((i, el) => map[i] = el);
if (stateChanges.length != 0) _dirty = true;
}
Map<int, bool> calculateNextState(Grid<bool> oldState) {
@ -125,39 +115,9 @@ class Simulation {
return count;
}
void render(html.CanvasElement canvas, [num interp]) {
// only renders if any cells changed between renders
if (!_dirty) return;
html.CanvasRenderingContext2D ctx = canvas.getContext('2d');
int brickW = (canvas.width ~/ map.width);
int brickH = (canvas.height ~/ map.height);
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (int i = 0; i < map.length; i++) {
math.Point p = map.toCoordinates(i);
if (_renderEdges) {
ctx.setStrokeColorRgb(100, 100, 100);
ctx.strokeRect(p.x * brickW, p.y * brickH, brickW, brickH);
}
if (map[i] == true)
ctx.setFillColorRgb(155, 155, 255);
else
ctx.setFillColorRgb(0, 0, 0);
ctx.fillRect(p.x * brickW, p.y * brickH, brickW, brickH);
}
_dirty = false;
}
void set renderEdges(bool on) {
_renderEdges = on;
_dirty = true;
}
void saveSnapshot() => _snapshot = Grid.from(map);
Grid<bool> loadSnapshot() {
map = Grid.from(_snapshot);
_dirty = true;
return map;
}
}