summaryrefslogtreecommitdiff
path: root/src/main/game
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/game')
-rw-r--r--src/main/game/domain/board.c13
-rw-r--r--src/main/game/domain/board.h6
-rw-r--r--src/main/game/game.c33
3 files changed, 41 insertions, 11 deletions
diff --git a/src/main/game/domain/board.c b/src/main/game/domain/board.c
index e7ae40f..82b1784 100644
--- a/src/main/game/domain/board.c
+++ b/src/main/game/domain/board.c
@@ -20,6 +20,7 @@
#include <stdlib.h>
#include <log.h>
#include "board.h"
+#include "../../util/list.h"
static Log *log = NULL;
@@ -31,7 +32,7 @@ init_log(void)
}
Board *
-board_new(void)
+board_new(float left, float right, float top, float bottom, float box_size)
{
init_log();
@@ -45,5 +46,15 @@ board_new(void)
board->wining_color = color_new(0.0f, 0.5f, 0.0f, 1.0f);
board->draft_color = color_new(0.1f, 0.3f, 0.5f, 1.0f);
+ board->lines = list_create();
+
+ /* Vertical lines */
+ list_add(board->lines, engine_line_new(left + box_size, top, left + box_size, bottom));
+ list_add(board->lines, engine_line_new(right - box_size, top, right - box_size, bottom));
+
+ /* Horizontal lines */
+ list_add(board->lines, engine_line_new(left, top + box_size, right, top + box_size));
+ list_add(board->lines, engine_line_new(left, bottom - box_size, right, bottom - box_size));
+
return board;
}
diff --git a/src/main/game/domain/board.h b/src/main/game/domain/board.h
index 2d63809..5ed1bf2 100644
--- a/src/main/game/domain/board.h
+++ b/src/main/game/domain/board.h
@@ -20,14 +20,16 @@
#ifndef __GAME_BOARD_H__
#define __GAME_BOARD_H__
-#include "../../engine/domain/color.h"
+#include "../../util/list.h"
+#include "../../engine/engine.h"
typedef struct {
Color *default_color;
Color *wining_color;
Color *draft_color;
+ list_t *lines;
} Board;
-Board *board_new(void);
+Board *board_new(float left, float right, float top, float bottom, float box_size);
#endif /* __GAME_BOARD_H__ */
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;