-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.js
72 lines (57 loc) · 2.2 KB
/
render.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// GENERIC RENDERING
var g_doClear = true;
var g_doBox = false;
var g_undoBox = false;
var g_doFlipFlop = false;
var g_doRender = true;
var g_frameCounter = 1;
var TOGGLE_CLEAR = 'C'.charCodeAt(0);
var TOGGLE_BOX = 'B'.charCodeAt(0);
var TOGGLE_UNDO_BOX = 'U'.charCodeAt(0);
var TOGGLE_FLIPFLOP = 'F'.charCodeAt(0);
var TOGGLE_RENDER = 'R'.charCodeAt(0);
function render(ctx) {
// Process various option toggles
//
if (eatKey(TOGGLE_CLEAR)) g_doClear = !g_doClear;
if (eatKey(TOGGLE_BOX)) g_doBox = !g_doBox;
if (eatKey(TOGGLE_UNDO_BOX)) g_undoBox = !g_undoBox;
if (eatKey(TOGGLE_FLIPFLOP)) g_doFlipFlop = !g_doFlipFlop;
if (eatKey(TOGGLE_RENDER)) g_doRender = !g_doRender;
// I've pulled the clear out of `renderSimulation()` and into
// here, so that it becomes part of our "diagnostic" wrappers
//
if (g_doClear) util.clearCanvas(ctx);
// The main purpose of the box is to demonstrate that it is
// always deleted by the subsequent "undo" before you get to
// see it...
//
// i.e. double-buffering prevents flicker!
//
if (g_doBox) util.fillBox(ctx, 200, 200, 50, 50, "red");
// The core rendering of the actual game / simulation
//
if (g_doRender) renderSimulation(ctx);
// This flip-flip mechanism illustrates the pattern of alternation
// between frames, which provides a crude illustration of whether
// we are running "in sync" with the display refresh rate.
//
// e.g. in pathological cases, we might only see the "even" frames.
//
if (g_doFlipFlop) {
var boxX = 250,
boxY = g_isUpdateOdd ? 100 : 200;
// Draw flip-flop box
util.fillBox(ctx, boxX, boxY, 50, 50, "green");
// Display the current frame-counter in the box...
ctx.fillText(g_frameCounter % 1000, boxX + 10, boxY + 20);
// ..and its odd/even status too
var text = g_frameCounter % 2 ? "odd" : "even";
ctx.fillText(text, boxX + 10, boxY + 40);
}
// Optional erasure of diagnostic "box",
// to illustrate flicker-proof double-buffering
//
if (g_undoBox) ctx.clearRect(200, 200, 50, 50);
++g_frameCounter;
}