use a more compact memory rep

This commit is contained in:
histausse 2021-05-14 00:10:37 +02:00
parent b0799ea316
commit f6fa2759a6
Signed by: histausse
GPG key ID: 67486F107F62E9E9

View file

@ -7,7 +7,13 @@ const GRID_COLOR = "#444444";
const DEAD_COLOR = "#000000";
const ALIVE_COLOR = "#FFFFFF";
const universe = Universe.new_space_ship();
const seed = Math.random();
const universe = ((seed) => {
if (seed < 1/3) return Universe.new();
else if (seed < 2/3) return Universe.new_random();
else return Universe.new_space_ship();
})(seed);
const width = universe.width();
const height = universe.height();
@ -59,7 +65,7 @@ const getIndex = (row, column) => {
const drawCells = () => {
const cellsPtr = universe.cells();
const cells = new Uint8Array(memory.buffer, cellsPtr, width * height);
const cells = new Uint8Array(memory.buffer, cellsPtr, width * height / 8);
ctx.beginPath();
@ -67,9 +73,9 @@ const drawCells = () => {
for (let col = 0; col < width; col++) {
const i = getIndex(row, col);
ctx.fillStyle = cells[i] === Cell.Dead
? DEAD_COLOR
: ALIVE_COLOR;
ctx.fillStyle = bitIsSet(i, cells)
? ALIVE_COLOR
: DEAD_COLOR;
ctx.fillRect(
col * (CELL_SIZE + 1) + 1,
@ -83,4 +89,10 @@ const drawCells = () => {
ctx.stroke();
}
const bitIsSet = (n, arr) => {
const byte = Math.floor(n / 8);
const mask = 1 << (n % 8);
return (arr[byte] & mask) === mask;
}
requestAnimationFrame(renderLoop);