/*- * Copyright (C) 2025 Alessandro Iezzi * * This file is part of Tris Game. * * Tris Game is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Tris Game is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Tris Game. If not, see . */ #include #include #include #include "../engine/engine.h" #include "game.h" #include "cell.h" #include "../engine/ui/ui.h" const char SIGN_CIRCLE = 'o'; const char SIGN_CROSS = 'x'; static Log *log = NULL; static void init_log(void) { if (log != NULL) return; log = log_create("Game"); } static void game_draw_field(TrisGame *game, float box_size); static char game_next_sign(TrisGame *game); static void draw_sign(TrisGame *game, float x, float y) { float l = 0.20f; if (game_next_sign(game) == SIGN_CIRCLE) { engine_draw_circle(x, y, l, 120, 1); game->sign = SIGN_CROSS; } else if (game_next_sign(game) == SIGN_CROSS) { engine_draw_line(x - l, y + l, x + l, y - l); engine_draw_line(x + l, y + l, x - l, y - l); game->sign = SIGN_CIRCLE; } game->moves++; } static void loop_cells(TrisGame *game, float x, float y) { init_log(); list_node_t *current = game->cells->head; do { Cell *cell = current->data; if (!cell_is_filled(cell) && cell_within_bounds(cell, x, y)) { draw_sign(game, cell->cx, cell->cy); cell_set_filled(cell, true); } current = current->next; } while (current != NULL); } static void game_mouse_button_pressed(float x, float y, void *data) { loop_cells(data, x, y); } TrisGame * game_init(int width, int height) { init_log(); /* Init of the TrisGame */ TrisGame *game = malloc(sizeof(TrisGame)); if (game == NULL) { log_error(log, "Error allocating memory for the game"); exit(EXIT_FAILURE); } game->width = width; game->height = height; game->cells = list_create(); game->sign = SIGN_CIRCLE; game->moves = 0; engine_init(width, height); ui_set_title("Tris Game"); engine_set_mouse_button_listener(game_mouse_button_pressed, game); game_draw_field(game, 0.5f); return game; } void game_start() { engine_loop(); } static void game_draw_field(TrisGame *game, float box_size) { float x = 0.0f; 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; float first_row = ymax - box_size; float second_row = ymax - box_size * 2; float first_col = xmin + box_size; float second_col = xmin + box_size * 2; /* First row */ list_add(game->cells, cell_new(xmin, first_col, ymax, first_row)); list_add(game->cells, cell_new(first_col, second_col, ymax, first_row)); list_add(game->cells, cell_new(second_col, xmax, ymax, first_row)); /* Second row */ list_add(game->cells, cell_new(xmin, first_col, first_row, second_row)); list_add(game->cells, cell_new(first_col, second_col, first_row, second_row)); list_add(game->cells, cell_new(second_col, xmax, first_row, second_row)); /* Third row */ list_add(game->cells, cell_new(xmin, first_col, second_row, ymin)); list_add(game->cells, cell_new(first_col, second_col, second_row, ymin)); list_add(game->cells, cell_new(second_col, xmax, second_row, ymin)); } static char game_next_sign(TrisGame *game) { return game->sign; }