Separate grid border and cells border parameter

This commit is contained in:
Amine B. Hassouna
2018-12-01 12:09:18 +01:00
parent 9ab54e520f
commit d3c9c890d4
3 changed files with 35 additions and 31 deletions

View File

@@ -59,20 +59,21 @@ struct Grid
// x, y, width, height // x, y, width, height
SDL_Rect rect; 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 // Number of cells over the x axis
int xCells; int xCells;
// Number of cells over the y axis // Number of cells over the y axis
int yCells; int yCells;
// Interspace between cells over the x axis // Cells boder thickness and color
int xInterspace; int cellsBorder;
// Interspace between cells over the y axis SDL_Color cellsBorderColor;
int yInterspace;
// Grid background color
SDL_Color backgroundColor;
// Grid border color
SDL_Color borderColor;
// Matrix of Cells // Matrix of Cells
Cell cells[GRID_MAX_X_CELLS][GRID_MAX_Y_CELLS]; Cell cells[GRID_MAX_X_CELLS][GRID_MAX_Y_CELLS];

View File

@@ -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.w = MIN(w - margin * 2, h - margin * 2);
grid.rect.h = grid.rect.w; 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 // Set number of cells
grid.xCells = 10; grid.xCells = 10;
grid.yCells = 10; grid.yCells = 10;
// Set cells interspace // Set cells border thickness and color
grid.xInterspace = 6; grid.cellsBorder = grid.border;
grid.yInterspace = 6; grid.cellsBorderColor = grid.borderColor;
// Set backgroud and border color
grid.backgroundColor = COLOR_DARK_GRAY;
grid.borderColor = COLOR_GRAY;
// Ajust size and center // Ajust size and center
Grid_ajustSize(&grid); Grid_ajustSize(&grid);

View File

@@ -40,11 +40,11 @@ int Grid_ajustSize(Grid *grid)
} }
// Init rect // Init rect
int interspaceWidth = (grid->xCells + 2) * grid->xInterspace; int interspaceWidth = grid->xCells * grid->cellsBorder * 2;
grid->rect.w -= (grid->rect.w - interspaceWidth) % grid->xCells; grid->rect.w -= (grid->rect.w - (grid->border * 2) - interspaceWidth) % grid->xCells;
int interspaceHeigth = (grid->yCells + 2) * grid->yInterspace; int interspaceHeigth = grid->yCells * grid->cellsBorder * 2;
grid->rect.h -= (grid->rect.h - interspaceHeigth) % grid->yCells; grid->rect.h -= (grid->rect.h - (grid->border * 2) - interspaceHeigth) % grid->yCells;
return true; return true;
} }
@@ -80,7 +80,7 @@ bool Grid_init(Grid *grid)
&(grid->cells[i][j]), &(grid->cells[i][j]),
i, j, i, j,
grid->backgroundColor, 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) void Grid_initCell(Grid *grid, Cell *cell, int i, int j, SDL_Color color, SDL_Color borderColor)
{ {
// Init rect // Init rect
int interspaceWidth = (grid->xCells + 2) * grid->xInterspace; int interspaceWidth = grid->xCells * grid->cellsBorder * 2;
cell->rect.w = (grid->rect.w - interspaceWidth) / grid->xCells; cell->rect.w = (grid->rect.w - (grid->border * 2) - interspaceWidth) / grid->xCells;
int interspaceHeigth = (grid->yCells + 2) * grid->yInterspace; int interspaceHeigth = grid->yCells * grid->cellsBorder * 2;
cell->rect.h = (grid->rect.h - interspaceHeigth) / grid->yCells; 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.x = grid->rect.x + grid->border + grid->cellsBorder + (grid->cellsBorder * 2 + cell->rect.w) * i;
cell->rect.y = grid->rect.y + grid->yInterspace * (j+1.5) + cell->rect.h * j; cell->rect.y = grid->rect.y + grid->border + grid->cellsBorder + (grid->cellsBorder * 2 + cell->rect.h) * j;
// Init rectColor // Init rectColor
cell->rectColor = color; cell->rectColor = color;
// Init border // Init border
cell->border.w = cell->rect.w + grid->xInterspace; cell->border.w = cell->rect.w + grid->cellsBorder * 2;
cell->border.h = cell->rect.h + grid->yInterspace; cell->border.h = cell->rect.h + grid->cellsBorder * 2;
cell->border.x = cell->rect.x - grid->xInterspace/2; cell->border.x = cell->rect.x - grid->cellsBorder;
cell->border.y = cell->rect.y - grid->yInterspace/2; cell->border.y = cell->rect.y - grid->cellsBorder;
// Init borderColor // Init borderColor
cell->borderColor = borderColor; cell->borderColor = borderColor;