Grid Control Object

Hi Iβm working on creating a grid control object that will manage an array of bytes corresponding to cells in a grid.
The idea is to handle grid based movement easier. Each byte will have the state of 8 cells. The state is either 0 (empty) or 1 (occupied). The size of each cell is 8x8 pixels.

Iβve been wracking my brain unable to get it working, so if anyone has any suggestions I would greatly appreciate it.
Here is the code:

``````    class Grid
{
public:
Grid()
{
for (int i = 0; i < 16; i++)
grid[i] = 0;
};

void setCell(uint8_t x, uint8_t y, bool filled)
{
//if (!(x >= 0 && x < 128 && y >= 0 && y < 64))
//return;

uint8_t row = (uint8_t)(y / 8);
uint8_t bp = ((x / 8) % 8);
if (filled)
{
grid[(row * width) + (x / 64)] |= _BV(bp);
}
else
{
grid[(row * width) + (x / 64)] &= ~_BV(bp);
}
}

bool getCell(uint8_t x, uint8_t y)
{
uint8_t row = y / 8;
uint8_t bp = ((x / 8) % 8);
return ((grid[(row * width) + (x / 64)] & _BV(bp)) >> bp);
}

void drawGrid()
{
for (int xx = 0; xx < 16; xx++)
{
for (int yy = 0; yy < 8; yy++)
{
if (getCell(xx, yy))
display.drawCircle((xx * 8) + 4, (yy * 8) + 4, 5, 1);
else
display.drawPixel((xx * 8) + 4, (yy * 8) + 4, 1);
}
}
}

private:
uint8_t grid[16];
static const uint8_t width = 2;
};
``````

Hopefully itβs not too jumbled!
Gavin

1 Like

Hi- Sorry I canβt offer helpβ¦ but Iβve also been wanting to look at a grid system to recreate some old βOdyssey 2β computer games: http://videogamecritic.com/oddmr.htm
This systems is a bit more flexible, but requires more bits per cell to describe borders, etc. Full tech details here: http://atarihq.com/danb/files/o2doc.pdf

If you donβt need a grid with a height greater than 8:

``````void setCell(byte x, byte y, bool filled)
{
if(filled)
grid[x] |= 1 << y;
else
grid[x] &= ~(1 << y);
}

bool getCell(byte x, byte y)
{
return grid[x] & 1 << y;
}
``````

Otherwise:

``````void setCell(byte x, byte y, bool filled)
{
if(filled)
grid[(y >> 3) * width + x] |= 1 << (y & 7);
else
grid[(y >> 3) * width + x] &= ~(1 << (y & 7));
}

bool getCell(byte x, byte y)
{
return grid[(y >> 3) * width + x] & 1 << (y & 7);
}
``````

Both methods were tested and work on the actual Arduboy (dev kit) hardware.

A grid with 8 width and 16 height would be arranged in memory like this:

``````Y Xβ
β1  2  3  4  5  6  7  8
ββββββββββββββββββββββββ
β8ββ8ββ8ββ8ββ8ββ8ββ8ββ8β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β7ββ7ββ7ββ7ββ7ββ7ββ7ββ7β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β6ββ6ββ6ββ6ββ6ββ6ββ6ββ6β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β5ββ5ββ5ββ5ββ5ββ5ββ5ββ5β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β4ββ4ββ4ββ4ββ4ββ4ββ4ββ4β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β3ββ3ββ3ββ3ββ3ββ3ββ3ββ3β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β2ββ2ββ2ββ2ββ2ββ2ββ2ββ2β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β1ββ1ββ1ββ1ββ1ββ1ββ1ββ1β
ββββββββββββββββββββββββ
9 10 11 12 13 14 15 16
ββββββββββββββββββββββββ
β8ββ8ββ8ββ8ββ8ββ8ββ8ββ8β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β7ββ7ββ7ββ7ββ7ββ7ββ7ββ7β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β6ββ6ββ6ββ6ββ6ββ6ββ6ββ6β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β5ββ5ββ5ββ5ββ5ββ5ββ5ββ5β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β4ββ4ββ4ββ4ββ4ββ4ββ4ββ4β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β3ββ3ββ3ββ3ββ3ββ3ββ3ββ3β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β2ββ2ββ2ββ2ββ2ββ2ββ2ββ2β
βββ€βββ€βββ€βββ€βββ€βββ€βββ€βββ€
β1ββ1ββ1ββ1ββ1ββ1ββ1ββ1β
ββββββββββββββββββββββββ``````
1 Like

Thank you, Atomic! I think I found a way for it to work. I decided to make it 1 byte per cell so I can store tile information as well as whether or not it is solid. Since Iβm going to make each room the size of the screen it shouldnβt be an issue. Weβll see how it works. If I get something good going Iβll post the code.