Optimize life to dynamically allocate memory
This commit is contained in:
parent
890cada87e
commit
a217f2bd39
|
|
@ -8,12 +8,12 @@ const canvas = createCanvas(width, height);
|
|||
const context = canvas.getContext('2d')!;
|
||||
const imageData = context.createImageData(width, height);
|
||||
|
||||
const initField = life.initField as CallableFunction;
|
||||
const step = life.step as CallableFunction;
|
||||
const pixels = new Uint8Array(life.memory.buffer, life.getPixels(), width * height * 4);
|
||||
let pixels: Uint8Array;
|
||||
|
||||
export default function main() {
|
||||
initField(Date.now());
|
||||
const pixelsPtr = life.initField(Date.now());
|
||||
pixels = new Uint8Array(life.memory.buffer, pixelsPtr, width * height * 4);
|
||||
|
||||
console.log(life, pixels.length);
|
||||
|
||||
|
|
|
|||
|
|
@ -4,27 +4,28 @@
|
|||
#define width 512
|
||||
#define height 512
|
||||
|
||||
static uint8_t field[width * height];
|
||||
static uint8_t nextField[width * height];
|
||||
static uint8_t pixels[width * height * 4];
|
||||
static uint8_t* field;
|
||||
static uint8_t* nextField;
|
||||
static uint8_t* pixels;
|
||||
|
||||
static uint8_t rand8(void);
|
||||
static int countNeighbours(int x, int y);
|
||||
|
||||
EXPORT(getWidth) int get_width(void) { return width; }
|
||||
EXPORT(getHeight) int get_height(void) { return height; }
|
||||
EXPORT(getPixels) uint8_t* get_pixels(void) { return pixels; }
|
||||
|
||||
EXPORT(step) void step(void)
|
||||
{
|
||||
EXPORT(step) void step(void) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
int count = countNeighbours(x, y);
|
||||
uint8_t currentCell = field[x + y * width];
|
||||
int nextCell = currentCell;
|
||||
if (currentCell && count < 2) nextCell = 0;
|
||||
else if (currentCell && count > 3) nextCell = 0;
|
||||
else if (!currentCell && count == 3) nextCell = 1;
|
||||
if (currentCell && count < 2)
|
||||
nextCell = 0;
|
||||
else if (currentCell && count > 3)
|
||||
nextCell = 0;
|
||||
else if (!currentCell && count == 3)
|
||||
nextCell = 1;
|
||||
|
||||
nextField[x + y * width] = nextCell;
|
||||
}
|
||||
|
|
@ -47,12 +48,12 @@ EXPORT(step) void step(void)
|
|||
}
|
||||
}
|
||||
|
||||
static int countNeighbours(int x, int y)
|
||||
{
|
||||
static int countNeighbours(int x, int y) {
|
||||
int count = 0;
|
||||
for (int i = -1; i <= 1; i++) {
|
||||
for (int j = -1; j <= 1; j++) {
|
||||
if (i == 0 && j == 0) continue;
|
||||
if (i == 0 && j == 0)
|
||||
continue;
|
||||
|
||||
int xx = (x + i + width) % width;
|
||||
int yy = (y + j + height) % height;
|
||||
|
|
@ -71,10 +72,9 @@ static int countNeighbours(int x, int y)
|
|||
#define MULT_LO (MULT & 255)
|
||||
#define MULT_HI (MULT & 256)
|
||||
|
||||
static uint8_t rand_state[STATE_BYTES] = { 0x87, 0xdd, 0xdc, 0x10, 0x35, 0xbc, 0x5c };
|
||||
static uint8_t rand_state[STATE_BYTES] = {0x87, 0xdd, 0xdc, 0x10, 0x35, 0xbc, 0x5c};
|
||||
|
||||
static uint8_t rand8(void)
|
||||
{
|
||||
static uint8_t rand8(void) {
|
||||
static uint16_t c = 0x42;
|
||||
static uint32_t i = 0;
|
||||
uint16_t t;
|
||||
|
|
@ -93,11 +93,16 @@ static uint8_t rand8(void)
|
|||
return x;
|
||||
}
|
||||
|
||||
EXPORT(initField) void init_field(uint32_t randomSeed)
|
||||
{
|
||||
EXPORT(initField) uint8_t* init_field(uint32_t randomSeed) {
|
||||
*((uint32_t*)&rand_state[STATE_BYTES - sizeof(uint32_t)]) = randomSeed; // Voodoo
|
||||
|
||||
field = malloc(width * height);
|
||||
nextField = malloc(width * height);
|
||||
pixels = malloc(width * height * 4);
|
||||
|
||||
for (int i = 0; i < width * height; i++) {
|
||||
field[i] = rand8() & 1;
|
||||
}
|
||||
|
||||
return pixels;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue