perf(render): redraw on demand and only changed cells
Rendering used to clear the whole canvas and redraw every cell on every frame callback (~60 fps) even though the simulation ticks at 20 tps and starts paused, keeping CPU usage at ~85% while idle. Split simulation timing out of redraw() into the event loop and render only when displayed content can have changed (tick, painting, pattern placement, preview movement, grid-line toggle, resize); idle frames commit only a new frame callback. Repaint incrementally by diffing the grid against a rendered-state snapshot per row and redrawing just the changed cells, with damage limited to the repainted bounding box. Idle CPU drops to ~1% of one core; running frames render at tick rate (~20 fps) at microsecond cost. Adds grid_copy() snapshot helper with tests and archives the optimize-render-on-demand change with the new on-demand-rendering spec.
This commit is contained in:
@@ -19,6 +19,32 @@ int main(void) {
|
||||
grid_clear(&g);
|
||||
assert(grid_get(&g, 0, 0) == 0);
|
||||
|
||||
/* grid_copy: same-size round trip */
|
||||
grid_t snap;
|
||||
grid_init(&snap, 1, 1);
|
||||
grid_set(&g, 2, 1, 1);
|
||||
grid_copy(&snap, &g);
|
||||
assert(snap.cols == 4 && snap.rows == 4);
|
||||
assert(grid_get(&snap, 2, 1) == 1);
|
||||
assert(grid_get(&snap, 0, 0) == 0);
|
||||
|
||||
/* grid_copy: grow preserves cells, new area is dead */
|
||||
grid_set(&g, 3, 3, 1);
|
||||
grid_resize(&g, 6, 6);
|
||||
grid_copy(&snap, &g);
|
||||
assert(snap.cols == 6 && snap.rows == 6);
|
||||
assert(grid_get(&snap, 2, 1) == 1);
|
||||
assert(grid_get(&snap, 3, 3) == 1);
|
||||
assert(grid_get(&snap, 5, 5) == 0);
|
||||
|
||||
/* grid_copy: shrink truncates out-of-bounds cells */
|
||||
grid_resize(&g, 2, 2);
|
||||
grid_copy(&snap, &g);
|
||||
assert(snap.cols == 2 && snap.rows == 2);
|
||||
assert(grid_get(&snap, 2, 1) == 0);
|
||||
assert(grid_get(&snap, 1, 1) == 0);
|
||||
|
||||
grid_free(&snap);
|
||||
grid_free(&g);
|
||||
printf("grid tests passed\n");
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user