diff options
author | 2025-05-18 11:46:26 +0200 | |
---|---|---|
committer | 2025-05-18 11:46:26 +0200 | |
commit | 24339d371c2194405dc151e8de0c319d976a78ba (patch) | |
tree | 8d2f2e2f18f81e89f275322de4267d50c352a11c | |
parent | 77a456b65b3d21213db9a5c88076f511b753e07c (diff) | |
download | tris-24339d371c2194405dc151e8de0c319d976a78ba.tar.gz tris-24339d371c2194405dc151e8de0c319d976a78ba.zip |
Move the process of rendering line from game to engine
-rw-r--r-- | src/main/engine/engine.c | 68 | ||||
-rw-r--r-- | src/main/engine/engine.h | 2 | ||||
-rw-r--r-- | src/main/game.c | 8 | ||||
-rw-r--r-- | src/main/main.c | 4 |
4 files changed, 62 insertions, 20 deletions
diff --git a/src/main/engine/engine.c b/src/main/engine/engine.c index e619dc7..8db206a 100644 --- a/src/main/engine/engine.c +++ b/src/main/engine/engine.c @@ -18,10 +18,12 @@ */ #include <stdio.h> +#include <stdlib.h> #include <time.h> #include <GL/gl.h> -#include "../ui/ui.h" #include "engine.h" +#include "../ui/ui.h" +#include "../util/list.h" #ifdef X11 #include <X11/Xlib.h> @@ -34,6 +36,12 @@ static float ystep; static int width; static int height; +static list_t *lines = NULL; + +typedef struct Line { + int x1, y1, x2, y2; +} Line; + typedef struct Engine { void (*draw_frame)(); } Engine; @@ -57,16 +65,29 @@ engine_init(int w, int h) xstep = RANGE_GL / (float) w; ystep = RANGE_GL / (float) h; + + lines = list_create(); +} + +static Line * +engine_new_line(int x1, int y1, int x2, int y2) +{ + Line *line = malloc(sizeof(Line)); + line->x1 = x1; + line->y1 = y1; + line->x2 = x2; + line->y2 = y2; + + return line; } void engine_draw_line(int x1, int y1, int x2, int y2) { - float _x1 = x1 * xstep - 1; float _y1 = (height - y1) * ystep - 1; - float _x2 = x2 * xstep - 1; float _y2 = (height - y2) * ystep - 1; - - glVertex2f(_x1, _y1); - glVertex2f(_x2, _y2); + if (lines != NULL) { + Line *line = engine_new_line(x1, y1, x2, y2); + list_add(lines, line); + } } static void @@ -83,6 +104,34 @@ engine_calculate_fps() } static void +render_line(Line *line) +{ + float _x1 = line->x1 * xstep - 1; float _y1 = (height - line->y1) * ystep - 1; + float _x2 = line->x2 * xstep - 1; float _y2 = (height - line->y2) * ystep - 1; + + glVertex2f(_x1, _y1); + glVertex2f(_x2, _y2); +} + +static void +draw_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 { + render_line((Line *) current->data); + current = current->next; + } while (current != NULL); + + glEnd(); +} + +static void draw_frames() { glClearColor(0.0f, 0.0f, 0.2f, 1.0f); @@ -91,15 +140,14 @@ draw_frames() glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - engine.draw_frame(); + draw_lines(); + engine_calculate_fps(); } void -engine_loop(void (*draw_frame)()) +engine_loop(void) { - engine.draw_frame = draw_frame; - ui_set_loop_listener(draw_frames); /* FPS calculation */ diff --git a/src/main/engine/engine.h b/src/main/engine/engine.h index db555fc..c6b6502 100644 --- a/src/main/engine/engine.h +++ b/src/main/engine/engine.h @@ -26,7 +26,7 @@ enum EngineInput { void engine_init(int width, int height); void engine_draw_line(int x1, int y1, int x2, int y2); -void engine_loop(void (*draw_frame)()); +void engine_loop(void); void engine_input(void (*f_input)(int engine_input)); void engine_on_mouse_pressed(void (*event)()); diff --git a/src/main/game.c b/src/main/game.c index ea1a935..007bc8c 100644 --- a/src/main/game.c +++ b/src/main/game.c @@ -17,21 +17,13 @@ * along with Tris Game. If not, see <http://www.gnu.org/licenses/>. */ -#include <GL/gl.h> - #include "engine/engine.h" void game_draw_field(int box_size, int x, int y) { - glLineWidth(5.0f); - glBegin(GL_LINES); - glColor3f(1.0f, 1.0f, 0.0f); /* Yellow */ - engine_draw_line(box_size, y, box_size, box_size * 3); engine_draw_line(box_size * 2, y, box_size * 2, box_size * 3); engine_draw_line(x, box_size, box_size * 3, box_size); engine_draw_line(x, box_size * 2, box_size * 3, box_size * 2); - - glEnd(); } diff --git a/src/main/main.c b/src/main/main.c index e102a28..4780e76 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -37,7 +37,9 @@ draw_frame() int main(void) { engine_init(WIDTH, HEIGHT); ui_set_title("Tris Game"); - engine_loop(draw_frame); + draw_frame(); + + engine_loop(); return EXIT_SUCCESS; } |