diff options
-rw-r--r-- | src/main/game/game.c | 97 |
1 files changed, 94 insertions, 3 deletions
diff --git a/src/main/game/game.c b/src/main/game/game.c index 3996cee..00e74cd 100644 --- a/src/main/game/game.c +++ b/src/main/game/game.c @@ -24,10 +24,72 @@ #include "../util.h" #include "../engine/ui/ui.h" +float xmin, xmax, ymin, ymax; +float box_size; +int field_matrix[3][3]; + +float first_row, half_first_row; +float first_col, half_first_col; +float second_row, half_second_row; +float second_col, half_second_col; +float third_row, half_third_row; +float third_col, half_third_col; + +static void +draw_circle(float x, float y) +{ + engine_draw_circle(x, y, 0.20, 120, 1); +} + static void game_mouse_button_pressed(float x, float y) { - printf("Mouse OpenGL: X = %.2f, Y = %.2f\n", x, y); + /* Click inside the field */ + if (x >= xmin && x <= xmax && y >= ymin && y <= ymax) { + if (y >= first_row) { + if (x <= first_col && field_matrix[0][0] == 0) { + draw_circle(half_first_col, half_first_row); + field_matrix[0][0] = 1; + printf("OK\n"); + } else if (x >= first_col && x <= second_col && field_matrix[0][1] == 0) { + draw_circle(half_second_col, half_first_row); + field_matrix[0][1] = 1; + printf("OK\n"); + } else if (x >= second_col && field_matrix[0][2] == 0) { + draw_circle(half_third_col, half_first_row); + field_matrix[0][2] = 1; + printf("OK\n"); + } + } else if (y >= second_row) { + if (x <= first_col && field_matrix[1][0] == 0) { + draw_circle(half_first_col, half_second_row); + field_matrix[1][0] = 1; + printf("OK\n"); + } else if (x >= first_col && x <= second_col && field_matrix[1][1] == 0) { + draw_circle(half_second_col, half_second_row); + field_matrix[1][1] = 1; + printf("OK\n"); + } else if (x >= second_col && field_matrix[1][2] == 0) { + draw_circle(half_third_col, half_second_row); + field_matrix[1][2] = 1; + printf("OK\n"); + } + } else if (y >= third_row) { + if (x <= first_col && field_matrix[2][0] == 0) { + draw_circle(half_first_col, half_third_row); + field_matrix[2][0] = 1; + printf("OK\n"); + } else if (x >= first_col && x <= second_col && field_matrix[2][1] == 0) { + draw_circle(half_second_col, half_third_row); + field_matrix[2][1] = 1; + printf("OK\n"); + } else if (x >= second_col && field_matrix[2][2] == 0) { + draw_circle(half_third_col, half_third_row); + field_matrix[2][2] = 1; + printf("OK\n"); + } + } + } } TrisGame * @@ -44,6 +106,12 @@ game_init(int width, int height) game_draw_field(0.5, 0, 0); + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + field_matrix[i][j] = 0; + } + } + return game; } @@ -54,12 +122,35 @@ game_start() } void -game_draw_field(float box_size, float x, float y) +game_draw_field(float _box_size, float x, float y) { - float half_box = box_size / 2; + 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); + + xmin = x - half_box * 3; + xmax = x + half_box * 3; + ymin = y - half_box * 3; + ymax = y + half_box * 3; + + box_size = _box_size; + + first_row = ymax - box_size; + second_row = ymax - box_size * 2; + third_row = ymax - box_size * 3; + + half_first_row = ymax - box_size / 2; + half_second_row = ymax - (box_size / 2) * 3; + half_third_row = ymax - (box_size / 2) * 5; + + first_col = xmin + box_size; + second_col = xmin + box_size * 2; + third_col = xmin + box_size * 3; + + half_first_col = xmin + box_size / 2; + half_second_col = xmin + (box_size / 2) * 3; + half_third_col = xmin + (box_size / 2) * 5; } |