An interactive sandbox
The supplied main loop exposes sand, stone, water and glass selection through the number keys. A circular brush paints particles with the left mouse button and removes particles with the right button; the brush size is adjustable.
A compact engine structure
The source separates the main loop, particle handling and rendering into dedicated modules. Screen dimensions determine the particle grid, using a four-pixel scale per cell.
Timing and controls
The event loop accumulates elapsed time and uses a 60 Hz target interval for rendering. The target is a scheduling choice, not a measured performance guarantee. Escape closes the application.
Explore the implementation
git clone https://github.com/SAED2906/CelularAutomata.gitCode example
int grid_x = mouse_x / PARTICLE_SCALE;
int grid_y = mouse_y / PARTICLE_SCALE;
for (int i = -brush_size/2; i <= brush_size/2; i++) {
for (int j = -brush_size/2; j <= brush_size/2; j++) {
if (i*i + j*j > (brush_size/2)*(brush_size/2)) continue;
if (remove) {
remove_particle(grid_x + i, grid_y + j);
} else if (rand() % 3 != 0) {
add_particle(grid_x + i, grid_y + j, current_particle_type);
}
}
}