diff options
author | Hampus <hampus@hampusmat.com> | 2022-01-04 18:55:51 +0100 |
---|---|---|
committer | Hampus <hampus@hampusmat.com> | 2022-01-05 20:09:27 +0100 |
commit | e3690eb85a9456cc1f3ccda751ae7d9fdf2d3b03 (patch) | |
tree | 2fdd32726d753495bf562102a0531101eaa1ddfd /src/grid.c | |
parent | 1bed3ac57906b26ef05b25c2bc5c1dca424dba4a (diff) |
refactor: improve even more
Diffstat (limited to 'src/grid.c')
-rw-r--r-- | src/grid.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/grid.c b/src/grid.c new file mode 100644 index 0000000..7248087 --- /dev/null +++ b/src/grid.c @@ -0,0 +1,55 @@ +#include "grid.h" +#include "utils.h" +#include <stdlib.h> + +Grid grid_create(unsigned int width, unsigned int height, char *fill) +{ + unsigned int mem_height = height * sizeof(char **); + unsigned int mem_width = width * sizeof(char *); + + Dimensions dimens = {.width = width, .height = height}; + + Grid grid = {.grid = malloc_s(mem_height), .dimens = dimens}; + + // Fill the grid + for (unsigned int y = 0; y < height; y++) + { + grid.grid[y] = malloc_s(mem_width); + + for (unsigned int x = 0; x < width; x++) + grid.grid[y][x] = fill; + } + + return grid; +} + +char *grid_get(Grid grid, Position pos) +{ + return grid.grid[pos.y][pos.x]; +} + +void grid_set(Grid grid, Position pos, char *value) +{ + grid.grid[pos.y][pos.x] = value; +} + +void grid_print(Grid grid) +{ + for (unsigned int y = 0; y < grid.dimens.height; y++) + { + for (unsigned int x = 0; x < grid.dimens.width; x++) + printf("%s", grid.grid[y][x]); + + printf("\n"); + } +} + +void grid_destroy(Grid grid) +{ + // Deallocate the memory of the grid + for (unsigned int y = 0; y < grid.dimens.height; y++) + free(grid.grid[y]); + + free(grid.grid); +} + |