diff --git a/main.cpp b/main.cpp index fd166b4..594d787 100644 --- a/main.cpp +++ b/main.cpp @@ -2,16 +2,16 @@ #include #include "simulation.h" -void printGrid(simulation::SimulationGrid grid) +void printGrid(simulation::SimulationGrid* grid) { - std::cout << std::string(grid.x_max+2, '_') << std::endl; - for (unsigned char y = grid.y_max; y > 0; y--) + std::cout << std::string(grid->x_max+2, '_') << std::endl; + for (unsigned char y = grid->y_max; y > 0; y--) { std::cout << "|"; - for (unsigned char x = 0; x < grid.x_max; x++) + for (unsigned char x = 0; x < grid->x_max; x++) { - unsigned int offset = simulation::getCellOffset(x, y-1, 0, grid); - unsigned int material = grid.out[offset]; + unsigned int offset = getCellOffset(x, y-1, 0, grid); + unsigned int material = grid->out[offset]; switch (material) { case simulation::VACUUM: @@ -26,12 +26,12 @@ void printGrid(simulation::SimulationGrid grid) } std::cout << "|" << std::endl; } - std::cout << std::string(grid.x_max+2, '_') << std::endl; + std::cout << std::string(grid->x_max+2, '_') << std::endl; } -void initGrid(simulation::SimulationGrid grid) { - unsigned int offset = simulation::getCellOffset(0,1,0, grid); - grid.in[offset] = simulation::WATER; +void initGrid(simulation::SimulationGrid* grid) { + unsigned int offset = simulation::getCellOffset(0,2,0, grid); + grid->in[offset] = simulation::WATER; } int main() { @@ -50,11 +50,11 @@ int main() { zMax }; - initGrid(grid); - step(0, grid); - + initGrid(&grid); for (int n=0; n< 2; n++) { - printGrid(grid); + simulation::step(0, &grid); + printGrid(&grid); + prepareForNextStep(&grid); } std::free(inCells); std::free(outCells); diff --git a/simulation.cpp b/simulation.cpp index f26edb4..c06b317 100644 --- a/simulation.cpp +++ b/simulation.cpp @@ -1,16 +1,17 @@ #include "simulation.h" +#include namespace simulation { - unsigned int getCellOffset( + int getCellOffset( unsigned char x, unsigned char y, unsigned char z, - SimulationGrid grid) + SimulationGrid* grid) { - if (x < grid.x_max && y < grid.y_max && z < grid.z_max) + if (x < grid->x_max && y < grid->y_max && z < grid->z_max) { - unsigned int offset = x + y * grid.x_max + z * (grid.y_max * grid.x_max); + int offset = x + y * grid->x_max + z * (grid->y_max * grid->x_max); return offset; } return -1; @@ -18,33 +19,55 @@ namespace simulation void step( double millisecondsSinceLastStep, - SimulationGrid grid) + SimulationGrid* grid) { - for (unsigned char x = 0; x < grid.x_max; x++) + for (unsigned char x = 0; x < grid->x_max; x++) { - for (unsigned char y = 0; y < grid.y_max; y++) + for (unsigned char y = 0; y < grid->y_max; y++) { - for (unsigned char z = 0; z < grid.z_max; z++) + for (unsigned char z = 0; z < grid->z_max; z++) { - unsigned int offset = getCellOffset(x, y, z, grid); - unsigned int material = grid.in[offset]; + int offset = getCellOffset(x, y, z, grid); + unsigned int material = grid->in[offset]; switch (material) { case WATER: - if (y > 0) { - unsigned int downOffset = getCellOffset(x, y - 1, z, grid); + int downOffset = getCellOffset(x, y - 1, z, grid); // Flow down if cell is empty - if (grid.out[downOffset] == 0) + if (downOffset >= 0 && grid->out[downOffset] == 0) { - grid.out[downOffset] = material; + grid->out[downOffset] = material; + grid->out[offset] = VACUUM; + } else + { + // Stay in the same location + grid->out[offset] = material; } + break; + } + case MERCURY: + { + break; } default: - grid.out[offset] = material; + { + grid->out[offset] = material; + } } } } } } + + void prepareForNextStep(SimulationGrid* grid) + { + // Flip the buffers + unsigned int* inCells = grid->in; + grid->in = grid->out; + grid->out = inCells; + // Clear out buffer + size_t bufferSizeInBytes = sizeof(unsigned int) * grid->x_max * grid->y_max * grid->z_max; + std::memset(grid->out, VACUUM, bufferSizeInBytes); + } } diff --git a/simulation.h b/simulation.h index 4c72ef5..35469b6 100644 --- a/simulation.h +++ b/simulation.h @@ -24,15 +24,16 @@ namespace simulation void step( double millisecondsSinceLastStep, - SimulationGrid grid + SimulationGrid* grid ); - unsigned int getCellOffset( + int getCellOffset( unsigned char x, unsigned char y, unsigned char z, - SimulationGrid grid); + SimulationGrid* grid); + void prepareForNextStep(SimulationGrid* grid); } #endif //SIMULATION_H