From d3c9c890d46adef196cae11e0c178875891549ee Mon Sep 17 00:00:00 2001 From: "Amine B. Hassouna" Date: Sat, 1 Dec 2018 12:09:18 +0100 Subject: [PATCH] Separate grid border and cells border parameter --- include/grid.h | 19 ++++++++++--------- src/game.c | 17 ++++++++++------- src/grid.c | 30 +++++++++++++++--------------- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/include/grid.h b/include/grid.h index bab54d4..5d7e781 100644 --- a/include/grid.h +++ b/include/grid.h @@ -59,20 +59,21 @@ struct Grid // x, y, width, height SDL_Rect rect; + // Grid background color + SDL_Color backgroundColor; + + // Grid border thickness and color + int border; + SDL_Color borderColor; + // Number of cells over the x axis int xCells; // Number of cells over the y axis int yCells; - // Interspace between cells over the x axis - int xInterspace; - // Interspace between cells over the y axis - int yInterspace; - - // Grid background color - SDL_Color backgroundColor; - // Grid border color - SDL_Color borderColor; + // Cells boder thickness and color + int cellsBorder; + SDL_Color cellsBorderColor; // Matrix of Cells Cell cells[GRID_MAX_X_CELLS][GRID_MAX_Y_CELLS]; diff --git a/src/game.c b/src/game.c index b948fa2..7421ba7 100644 --- a/src/game.c +++ b/src/game.c @@ -40,17 +40,20 @@ bool Game_start(SDL_Renderer *renderer, int w, int h) grid.rect.w = MIN(w - margin * 2, h - margin * 2); grid.rect.h = grid.rect.w; + // Set grid backgroud + grid.backgroundColor = COLOR_DARK_GRAY; + + // Set grid border thickness and color + grid.border = 3; + grid.borderColor = COLOR_GRAY; + // Set number of cells grid.xCells = 10; grid.yCells = 10; - // Set cells interspace - grid.xInterspace = 6; - grid.yInterspace = 6; - - // Set backgroud and border color - grid.backgroundColor = COLOR_DARK_GRAY; - grid.borderColor = COLOR_GRAY; + // Set cells border thickness and color + grid.cellsBorder = grid.border; + grid.cellsBorderColor = grid.borderColor; // Ajust size and center Grid_ajustSize(&grid); diff --git a/src/grid.c b/src/grid.c index 77f25a4..f02dfaf 100644 --- a/src/grid.c +++ b/src/grid.c @@ -40,11 +40,11 @@ int Grid_ajustSize(Grid *grid) } // Init rect - int interspaceWidth = (grid->xCells + 2) * grid->xInterspace; - grid->rect.w -= (grid->rect.w - interspaceWidth) % grid->xCells; + int interspaceWidth = grid->xCells * grid->cellsBorder * 2; + grid->rect.w -= (grid->rect.w - (grid->border * 2) - interspaceWidth) % grid->xCells; - int interspaceHeigth = (grid->yCells + 2) * grid->yInterspace; - grid->rect.h -= (grid->rect.h - interspaceHeigth) % grid->yCells; + int interspaceHeigth = grid->yCells * grid->cellsBorder * 2; + grid->rect.h -= (grid->rect.h - (grid->border * 2) - interspaceHeigth) % grid->yCells; return true; } @@ -80,7 +80,7 @@ bool Grid_init(Grid *grid) &(grid->cells[i][j]), i, j, grid->backgroundColor, - grid->borderColor); + grid->cellsBorderColor); } } @@ -90,23 +90,23 @@ bool Grid_init(Grid *grid) void Grid_initCell(Grid *grid, Cell *cell, int i, int j, SDL_Color color, SDL_Color borderColor) { // Init rect - int interspaceWidth = (grid->xCells + 2) * grid->xInterspace; - cell->rect.w = (grid->rect.w - interspaceWidth) / grid->xCells; + int interspaceWidth = grid->xCells * grid->cellsBorder * 2; + cell->rect.w = (grid->rect.w - (grid->border * 2) - interspaceWidth) / grid->xCells; - int interspaceHeigth = (grid->yCells + 2) * grid->yInterspace; - cell->rect.h = (grid->rect.h - interspaceHeigth) / grid->yCells; + int interspaceHeigth = grid->yCells * grid->cellsBorder * 2; + cell->rect.h = (grid->rect.h - (grid->border * 2) - interspaceHeigth) / grid->yCells; - cell->rect.x = grid->rect.x + grid->xInterspace * (i+1.5) + cell->rect.w * i; - cell->rect.y = grid->rect.y + grid->yInterspace * (j+1.5) + cell->rect.h * j; + cell->rect.x = grid->rect.x + grid->border + grid->cellsBorder + (grid->cellsBorder * 2 + cell->rect.w) * i; + cell->rect.y = grid->rect.y + grid->border + grid->cellsBorder + (grid->cellsBorder * 2 + cell->rect.h) * j; // Init rectColor cell->rectColor = color; // Init border - cell->border.w = cell->rect.w + grid->xInterspace; - cell->border.h = cell->rect.h + grid->yInterspace; - cell->border.x = cell->rect.x - grid->xInterspace/2; - cell->border.y = cell->rect.y - grid->yInterspace/2; + cell->border.w = cell->rect.w + grid->cellsBorder * 2; + cell->border.h = cell->rect.h + grid->cellsBorder * 2; + cell->border.x = cell->rect.x - grid->cellsBorder; + cell->border.y = cell->rect.y - grid->cellsBorder; // Init borderColor cell->borderColor = borderColor;