I have a list of 'cell' objects in a 2d array of length [sizeX][sizeY]. These Cell
objects contain an array of type *Cell
, which should point to each of the given cell's adjacent cells in the format North, East, South, West (Never Eat Soggy Waffles).
This array is called compass
, and is defined with a length of 4. After the cells have been initialized (at which time all values of compass
are set to nullptr
), I have a loop which attempts to fill Cell.compass
with pointers to the appropriate nearby cells.
However, I have found that despite all this, in each cell I have found that compass
still is full of null values.
In this loop, I also run a function Cell::computeTopology()
which populates a vector (which is a property of Cell
) of the valid non-null indexes of compass
. This is similarly empty.
I have made a breakpoint both before and after this function has been called to the exact same effect. Regardless I will include this function as well. I'm utterly perplexed and some help here would be greatly appreciated.
Problem area:
const int sizeX = SCREEN_WIDTH / SCALE;
const int sizeY = SCREEN_HEIGHT / SCALE;
Cell cells[sizeX][sizeY];
for (int x = 0; x < sizeX; x++){
for (int y = 0; y < sizeY; y++){
cells[x][y].setPos(x, y);
cells[x][y] = Cell();
//cells[x][y].setColor(rand() % 255, rand() % 255, rand() % 255);
}
}
for (int x = 0; x < sizeX; x++) {
for (int y = 0; y < sizeY; y++) {
Cell c = cells[x][y];
if (x - 1 >= 0) {
c.compass[3] = &cells[x - 1][y];
}
if (x + 1 < (SCREEN_WIDTH / SCALE)) {
c.compass[1] = &cells[x + 1][y];
}
if (y - 1 >= 0) {
c.compass[0] = &cells[x][y - 1];
}
if (y + 1 < (SCREEN_HEIGHT / SCALE)) {
c.compass[2] = &cells[x][y + 1];
}
c.computeTopology();
}
}
And the computeTopology()
function
void Cell::computeTopology()
{
int i = 0;
for (Cell *c : compass) {
if (c != nullptr) {
notNull.push_back(i);
i++;
}
}
}