Added function to get dimensions.

This commit is contained in:
Sheldon Lee 2020-07-04 00:02:55 +01:00
parent 974f99b8f8
commit 4e9422b672
2 changed files with 12 additions and 7 deletions

17
grid.c
View File

@ -38,8 +38,7 @@ static void contain(int* num, int min, int max)
unsigned int toIndex(Grid* grid, int x, int y)
{
unsigned int width, height;
width = grid->width;
height = grid->size/width;
getDimensions(grid, &width, &height);
contain(&x, 0, width);
contain(&y, 0, height);
return (grid->width*y + x);
@ -50,6 +49,12 @@ bool getPixel(Grid* grid, int x, int y)
return grid->state[toIndex(grid, x, y)];
}
void getDimensions(Grid* grid, unsigned int* width, unsigned int* height)
{
*width = grid->width;
*height = grid->size/grid->width;
}
void clearGrid(Grid* grid)
{
for (int i = 0; i < grid->size; i++) grid->state[i]=false;
@ -57,9 +62,8 @@ void clearGrid(Grid* grid)
void updateGrid(Grid* grid)
{
unsigned int width = grid->width;
unsigned int height = grid->size/width;
unsigned int width, height;
getDimensions(grid, &width, &height);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
grid->next_state[toIndex(grid, x, y)] = isAliveNext(grid, x, y);
@ -79,8 +83,7 @@ void setPixel(Grid* grid, int x, int y, bool on)
void drawGrid(Grid* grid)
{
unsigned int width, height;
width = grid->width;
height = grid->size/width;
getDimensions(grid, &width, &height);
// Init color pair init_pair(index, fg, bg);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {

2
grid.h
View File

@ -19,6 +19,8 @@ unsigned int toIndex(Grid* grid, int x, int y);
bool getPixel(Grid* grid, int x, int y);
void getDimensions(Grid* grid, unsigned int* width, unsigned int* height);
void clearGrid(Grid* grid);
void updateGrid(Grid* grid);