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
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];

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.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);

View File

@@ -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;