diff options
author | 2025-05-22 18:59:46 +0200 | |
---|---|---|
committer | 2025-05-22 18:59:46 +0200 | |
commit | a22c967a59a1e17663790db72f2d24edafc6199a (patch) | |
tree | 5aa0a60a4cba2b314f019bb0499a091e79f3fc13 | |
parent | cec27feccd1d3232ceae2bf284f66071d36cc96a (diff) | |
download | tris-a22c967a59a1e17663790db72f2d24edafc6199a.tar.gz tris-a22c967a59a1e17663790db72f2d24edafc6199a.zip |
Remove the rendiring process of the lines
I've moved that inside the game rendering function.
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/main/engine/engine.c | 54 | ||||
-rw-r--r-- | src/main/engine/engine.h | 3 | ||||
-rw-r--r-- | src/main/engine/shape/circle.h | 3 | ||||
-rw-r--r-- | src/main/engine/shape/line.c | 69 | ||||
-rw-r--r-- | src/main/engine/shape/line.h | 35 | ||||
-rw-r--r-- | src/main/game/domain/board.c | 13 | ||||
-rw-r--r-- | src/main/game/domain/board.h | 6 | ||||
-rw-r--r-- | src/main/game/game.c | 33 |
9 files changed, 152 insertions, 68 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 870379d..e50217d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,7 +44,9 @@ add_executable(tris src/main/engine/domain/color.c src/main/engine/domain/color.h src/main/engine/shape/circle.c - src/main/engine/shape/circle.h) + src/main/engine/shape/circle.h + src/main/engine/shape/line.c + src/main/engine/shape/line.h) target_include_directories(tris PRIVATE log/src) diff --git a/src/main/engine/engine.c b/src/main/engine/engine.c index a555a89..495b960 100644 --- a/src/main/engine/engine.c +++ b/src/main/engine/engine.c @@ -21,12 +21,10 @@ #include <stdlib.h> #include <time.h> #include <GL/gl.h> -#include <math.h> #include <log.h> #include "engine.h" #include "ui/ui.h" #include "../util/list.h" -#include "shape/circle.h" #ifdef X11 #include <X11/Xlib.h> @@ -43,10 +41,6 @@ init_log(void) _log = log_create("Engine"); } -typedef struct Line { - float x1, y1, x2, y2; -} Line; - static Engine *engine; /* FPS */ @@ -73,7 +67,6 @@ engine_new(int w, int h) log_error(_log, "Error allocating memory for engine"); exit(EXIT_FAILURE); } - engine->lines = list_create(); engine->ui = ui_new(w, h); engine_set_rendering_background(engine, 0.0f, 0.0f, 0.2f, 1.0f); @@ -87,27 +80,6 @@ engine_set_window_title(Engine *engine, const char *title) ui_set_title(engine->ui, title); } -static Line * -engine_new_line(float x1, float y1, float x2, float y2) -{ - Line *line = malloc(sizeof(Line)); - line->x1 = x1; - line->y1 = y1; - line->x2 = x2; - line->y2 = y2; - - return line; -} - -void -engine_draw_line(float x1, float y1, float x2, float y2) -{ - if (engine->lines != NULL) { - Line *line = engine_new_line(x1, y1, x2, y2); - list_add(engine->lines, line); - } -} - static void engine_calculate_fps() { @@ -124,31 +96,6 @@ engine_calculate_fps() } static void -render_line(Line *line) -{ - glVertex2f(line->x1, line->y1); - glVertex2f(line->x2, line->y2); -} - -static void -draw_lines() -{ - if (engine->lines == NULL || engine->lines->size <= 0) return; - - glLineWidth(5.0f); - glBegin(GL_LINES); - glColor3f(1.0f, 1.0f, 0.0f); /* Yellow */ - - list_node_t *current = engine->lines->head; - do { - render_line(current->data); - current = current->next; - } while (current != NULL); - - glEnd(); -} - -static void draw_frames(void *data) { if (data == NULL) return; @@ -162,7 +109,6 @@ draw_frames(void *data) glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - draw_lines(); engine->draw_frame_listener->draw_frames(engine->draw_frame_listener->data); engine_calculate_fps(); diff --git a/src/main/engine/engine.h b/src/main/engine/engine.h index 24dbe61..1d36632 100644 --- a/src/main/engine/engine.h +++ b/src/main/engine/engine.h @@ -25,6 +25,7 @@ #include "domain/color.h" #include "shape/circle.h" +#include "shape/line.h" enum EngineInput { ENGINE_MOUSE_PRESSED = 4 @@ -38,7 +39,6 @@ typedef struct DrawFrameListener { typedef struct { UI *ui; list_t *circles1; - list_t *lines; float ortho_left, ortho_right, ortho_top, ortho_bottom; Color *rendering_background; /* Listeners */ @@ -50,7 +50,6 @@ void engine_set_rendering_background(Engine *engine, float r, float g, float void engine_set_rendering_background_c(Engine *engine, Color *color); void engine_set_window_title(Engine *engine, const char *title); void engine_set_rendering_listener(Engine *engine, void (*draw_frames)(void *data), void *data); -void engine_draw_line(float x1, float y1, float x2, float y2); void engine_loop(void); void engine_input(void (*f_input)(int engine_input)); void engine_set_mouse_button_listener(void (*event)(float x, float y, void *data), void *data); diff --git a/src/main/engine/shape/circle.h b/src/main/engine/shape/circle.h index 72e69e1..67c3a46 100644 --- a/src/main/engine/shape/circle.h +++ b/src/main/engine/shape/circle.h @@ -20,12 +20,15 @@ #ifndef __CIRCLE_H__ #define __CIRCLE_H__ +#include "../domain/color.h" + typedef struct { int outline; float cx; float cy; float r; int num_segments; + Color *color; } Circle; Circle *engine_circle_new(float cx, float cy, float r, int num_segments, int outline); diff --git a/src/main/engine/shape/line.c b/src/main/engine/shape/line.c new file mode 100644 index 0000000..7c0bc18 --- /dev/null +++ b/src/main/engine/shape/line.c @@ -0,0 +1,69 @@ +/*- + * Copyright (C) 2025 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <GL/gl.h> +#include "line.h" + +Line * +engine_line_new(float x1, float y1, float x2, float y2) +{ + Line *line = malloc(sizeof(Line)); + line->x1 = x1; + line->y1 = y1; + line->x2 = x2; + line->y2 = y2; + + return line; +} + +void +engine_render_line(Line *line) +{ + glLineWidth(5.0f); + glBegin(GL_LINES); + glColor3f(1.0f, 1.0f, 0.0f); /* Yellow */ + + glVertex2f(line->x1, line->y1); + glVertex2f(line->x2, line->y2); + + glEnd(); +} + +void +engine_render_lines(list_t *lines) +{ + if (lines == NULL || lines->size <= 0) return; + + glLineWidth(5.0f); + glBegin(GL_LINES); + glColor3f(1.0f, 1.0f, 0.0f); /* Yellow */ + + list_node_t *current = lines->head; + do { + Line *line = current->data; + + glVertex2f(line->x1, line->y1); + glVertex2f(line->x2, line->y2); + + current = current->next; + } while (current != NULL); + + glEnd(); +} diff --git a/src/main/engine/shape/line.h b/src/main/engine/shape/line.h new file mode 100644 index 0000000..c1a933d --- /dev/null +++ b/src/main/engine/shape/line.h @@ -0,0 +1,35 @@ +/*- + * Copyright (C) 2025 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef __ENGINE_LINE_H__ +#define __ENGINE_LINE_H__ + +#include "../domain/color.h" +#include "../../util/list.h" + +typedef struct Line { + float x1, y1, x2, y2; + Color *color; +} Line; + +Line *engine_line_new(float x1, float y1, float x2, float y2); +void engine_render_line(Line *line); +void engine_render_lines(list_t *lines); + +#endif /* __ENGINE_LINE_H__ */ 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; |