summaryrefslogtreecommitdiff
path: root/src/main/game/game.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/game/game.c')
-rw-r--r--src/main/game/game.c33
1 files changed, 25 insertions, 8 deletions
diff --git a/src/main/game/game.c b/src/main/game/game.c
index c72f81a..35d5c21 100644
--- a/src/main/game/game.c
+++ b/src/main/game/game.c
@@ -26,6 +26,22 @@
#define WIN_PATTERNS 8
+typedef struct CrossShape {
+ list_t *lines;
+} CrossShape;
+
+static CrossShape *
+cross_shape_new(float left, float right, float top, float bottom)
+{
+ CrossShape *shape = malloc(sizeof(CrossShape));
+ shape->lines = list_create();
+
+ list_add(shape->lines, engine_line_new(left, top, right, bottom));
+ list_add(shape->lines, engine_line_new(right, top, left, bottom));
+
+ return shape;
+}
+
static const char SIGN_CIRCLE = 'o';
static const char SIGN_CROSS = 'x';
@@ -63,10 +79,15 @@ draw_frames(void *data)
if (data == NULL) return;
TrisGame *game = data;
+ engine_render_lines(game->board->lines);
+
for (int i = 0; i < BOARD_SIZE; i++) {
Cell *cell = game->cells[i];
if (cell->sign == SIGN_CIRCLE) {
engine_render_circle(cell->shape);
+ } else if (cell->sign == SIGN_CROSS) {
+ CrossShape *shape = cell->shape;
+ engine_render_lines(shape->lines);
}
}
}
@@ -75,6 +96,7 @@ static void
game_restart(TrisGame *game)
{
game->ended = false;
+ game->moves = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
Cell *cell = game->cells[i];
@@ -129,8 +151,7 @@ draw_sign(TrisGame *game, Cell *cell)
cell->shape = engine_circle_new(x, y, l, 120, 1);
game->sign = SIGN_CROSS;
} else if (sign == SIGN_CROSS) {
- engine_draw_line(x - l, y + l, x + l, y - l);
- engine_draw_line(x + l, y + l, x - l, y - l);
+ cell->shape = cross_shape_new(x - l, x + l, y + l, y - l);
game->sign = SIGN_CIRCLE;
}
@@ -179,7 +200,6 @@ game_init(int width, int height)
game->sign = SIGN_CIRCLE;
game->moves = 0;
game->engine = engine_new(width, height);
- game->board = board_new();
game->ended = false;
engine_set_window_title(game->engine, "Tris Game");
@@ -205,17 +225,14 @@ game_draw_field(TrisGame *game, float box_size)
float y = 0.0f;
float half_box = box_size / 2;
- engine_draw_line(x - half_box, y + half_box * 3, x - half_box, y - half_box * 3);
- engine_draw_line(x + half_box, y + half_box * 3, x + half_box, y - half_box * 3);
-
- engine_draw_line(x - half_box * 3, y + half_box, x + half_box * 3, y + half_box);
- engine_draw_line(x - half_box * 3, y - half_box, x + half_box * 3, y - half_box);
float xmin = x - half_box * 3;
float xmax = x + half_box * 3;
float ymin = y - half_box * 3;
float ymax = y + half_box * 3;
+ game->board = board_new(xmin, xmax, ymin, ymax, box_size);
+
float first_row = ymax - box_size;
float second_row = ymax - box_size * 2;
float first_col = xmin + box_size;