Stumbling about
This commit is contained in:
parent
bc6a3cd1c1
commit
a2f1e11af8
3 changed files with 41 additions and 33 deletions
38
main.cpp
38
main.cpp
|
@ -2,16 +2,16 @@
|
|||
#include <cstring>
|
||||
#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->cells[offset];
|
||||
unsigned int material = grid.out[offset];
|
||||
switch (material)
|
||||
{
|
||||
case simulation::VACUUM:
|
||||
|
@ -26,11 +26,15 @@ 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;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
void initGrid(simulation::SimulationGrid grid) {
|
||||
unsigned int offset = simulation::getCellOffset(0,1,0, grid);
|
||||
grid.in[offset] = simulation::WATER;
|
||||
}
|
||||
|
||||
int main() {
|
||||
unsigned char xMax = 50;
|
||||
unsigned char yMax = 20;
|
||||
unsigned char zMax = 1;
|
||||
|
@ -38,16 +42,20 @@ int main()
|
|||
auto inCells = static_cast<unsigned int*>(std::malloc(bufferSizeInBytes));
|
||||
std::memset(inCells, 0, bufferSizeInBytes);
|
||||
auto outCells = static_cast<unsigned int*>(std::malloc(bufferSizeInBytes));
|
||||
simulation::SimulationGrid inGrid = {
|
||||
inCells, xMax, yMax, zMax
|
||||
};
|
||||
simulation::SimulationGrid outGrid = {
|
||||
outCells, xMax, yMax, zMax
|
||||
simulation::SimulationGrid grid = {
|
||||
inCells,
|
||||
outCells,
|
||||
xMax,
|
||||
yMax,
|
||||
zMax
|
||||
};
|
||||
|
||||
step(0, &inGrid, &outGrid);
|
||||
initGrid(grid);
|
||||
step(0, grid);
|
||||
|
||||
printGrid(&outGrid);
|
||||
for (int n=0; n< 2; n++) {
|
||||
printGrid(grid);
|
||||
}
|
||||
std::free(inCells);
|
||||
std::free(outCells);
|
||||
return 0;
|
||||
|
|
|
@ -6,11 +6,11 @@ namespace simulation
|
|||
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);
|
||||
unsigned int offset = x + y * grid.x_max + z * (grid.y_max * grid.x_max);
|
||||
return offset;
|
||||
}
|
||||
return -1;
|
||||
|
@ -18,31 +18,30 @@ namespace simulation
|
|||
|
||||
void step(
|
||||
double millisecondsSinceLastStep,
|
||||
SimulationGrid* input,
|
||||
SimulationGrid* output)
|
||||
SimulationGrid grid)
|
||||
{
|
||||
for (unsigned char x = 0; x < input->x_max; x++)
|
||||
for (unsigned char x = 0; x < grid.x_max; x++)
|
||||
{
|
||||
for (unsigned char y = 0; y < input->y_max; y++)
|
||||
for (unsigned char y = 0; y < grid.y_max; y++)
|
||||
{
|
||||
for (unsigned char z = 0; z < input->z_max; z++)
|
||||
for (unsigned char z = 0; z < grid.z_max; z++)
|
||||
{
|
||||
unsigned int offset = getCellOffset(x, y, z, input);
|
||||
unsigned int material = input->cells[offset];
|
||||
unsigned 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, input);
|
||||
unsigned int downOffset = getCellOffset(x, y - 1, z, grid);
|
||||
// Flow down if cell is empty
|
||||
if (output->cells[downOffset] == 0)
|
||||
if (grid.out[downOffset] == 0)
|
||||
{
|
||||
output->cells[downOffset] = material;
|
||||
grid.out[downOffset] = material;
|
||||
}
|
||||
}
|
||||
default:
|
||||
output->cells[offset] = material;
|
||||
grid.out[offset] = material;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,8 @@ namespace simulation
|
|||
|
||||
struct SimulationGrid
|
||||
{
|
||||
unsigned int* cells;
|
||||
unsigned int* in;
|
||||
unsigned int* out;
|
||||
unsigned char x_max;
|
||||
unsigned char y_max;
|
||||
unsigned char z_max;
|
||||
|
@ -23,14 +24,14 @@ namespace simulation
|
|||
|
||||
void step(
|
||||
double millisecondsSinceLastStep,
|
||||
SimulationGrid* input,
|
||||
SimulationGrid* output);
|
||||
SimulationGrid grid
|
||||
);
|
||||
|
||||
unsigned int getCellOffset(
|
||||
unsigned char x,
|
||||
unsigned char y,
|
||||
unsigned char z,
|
||||
SimulationGrid* grid);
|
||||
SimulationGrid grid);
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue