import { createWasmCanvas } from "@common/display/canvas";
import life from "./life.c";
import { gameLoop } from "@common/game";

export default gameLoop(
    () => {
        const canvas = createWasmCanvas(life);
        const ctx = canvas.getContext('2d');

        if (!ctx) {
            throw new Error("Failed to create canvas context");
        }

        return {
            canvas, ctx,
            sum: 0, count: 0,
        };
    },
    (dt, state) => {
        state.sum += dt * 1000;
        state.count++;

        state.ctx.clearRect(0, 0, 35, 15);
        state.ctx.fillText(`${(state.sum / state.count).toFixed(1)} ms`, 2, 10);
    }
);
