import { createCanvas, loadImageData } from "@common/display/canvas";
import life from "./life.c";

let context: CanvasRenderingContext2D | null;
let imageData: ImageData;

export default function main() {
    const imageDataPtr = life.initField();    
    imageData = loadImageData(life.data, imageDataPtr);

    const canvas = createCanvas(imageData.width, imageData.height);
    context = canvas.getContext('2d');

    console.log(life, imageData);

    requestAnimationFrame(loop);
}

let sum = 0;
let count = 0;

async function loop() {
    const start = performance.now();

    life.step();

    context?.putImageData(imageData, 0, 0);
    context?.clearRect(0, 0, 35, 15);

    const end = performance.now();

    sum += end - start;
    count++;

    context?.fillText(`${(sum / count).toFixed(1)} ms`, 2, 10);
    requestAnimationFrame(loop);
}