From 4e9422b6727d1fbb698ceca752e13f76583f4d70 Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Sat, 4 Jul 2020 00:02:55 +0100 Subject: [PATCH] Added function to get dimensions. --- grid.c | 17 ++++++++++------- grid.h | 2 ++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/grid.c b/grid.c index b65c417..99034c3 100644 --- a/grid.c +++ b/grid.c @@ -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++) { diff --git a/grid.h b/grid.h index 5990206..fbe822a 100644 --- a/grid.h +++ b/grid.h @@ -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);