summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/main/engine/engine.c50
-rw-r--r--src/main/engine/engine.h14
-rw-r--r--src/main/engine/shape/circle.c17
-rw-r--r--src/main/engine/shape/circle.h1
-rw-r--r--src/main/engine/ui/ui.h10
-rw-r--r--src/main/engine/ui/x11/ui.c11
-rw-r--r--src/main/game/cell.c1
-rw-r--r--src/main/game/cell.h1
-rw-r--r--src/main/game/game.c25
10 files changed, 81 insertions, 50 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index efb94f9..870379d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -37,7 +37,6 @@ add_executable(tris
src/main/util/list.h
src/main/util/list.c
src/main/engine/ui/types.h
- src/main/engine/types.h
src/main/game/cell.h
src/main/game/cell.c
src/main/game/domain/board.c
diff --git a/src/main/engine/engine.c b/src/main/engine/engine.c
index 3abfbcc..a555a89 100644
--- a/src/main/engine/engine.c
+++ b/src/main/engine/engine.c
@@ -73,7 +73,6 @@ engine_new(int w, int h)
log_error(_log, "Error allocating memory for engine");
exit(EXIT_FAILURE);
}
- engine->circles = list_create();
engine->lines = list_create();
engine->ui = ui_new(w, h);
@@ -88,13 +87,6 @@ engine_set_window_title(Engine *engine, const char *title)
ui_set_title(engine->ui, title);
}
-void
-engine_draw_circle(float cx, float cy, float r, int num_segments, int outline)
-{
- if (engine == NULL || engine->circles == NULL) return;
- list_add(engine->circles, engine_circle_new(cx, cy, r, num_segments, outline));
-}
-
static Line *
engine_new_line(float x1, float y1, float x2, float y2)
{
@@ -157,35 +149,11 @@ draw_lines()
}
static void
-render_circle(Circle *circle)
-{
- glBegin(GL_LINE_LOOP);
-
- for (int i = 0; i < circle->num_segments; i++) {
- float theta = 2.0f * M_PI * i / circle->num_segments;
- float x = circle->r * cosf(theta);
- float y = circle->r * sinf(theta);
- glVertex2f(circle->cx + x, circle->cy + y);
- }
-
- glEnd();
-}
-
-static void
-draw_circles()
+draw_frames(void *data)
{
- if (engine == NULL || engine->circles == NULL || engine->circles->size <= 0) return;
+ if (data == NULL) return;
- list_node_t *current = engine->circles->head;
- do {
- render_circle(current->data);
- current = current->next;
- } while (current != NULL);
-}
-
-static void
-draw_frames()
-{
+ Engine *engine = data;
Color *bk = engine->rendering_background;
glClearColor(bk->r, bk->g, bk->b, bk->a);
@@ -195,7 +163,7 @@ draw_frames()
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
draw_lines();
- draw_circles();
+ engine->draw_frame_listener->draw_frames(engine->draw_frame_listener->data);
engine_calculate_fps();
}
@@ -239,7 +207,7 @@ void
engine_loop(void)
{
ui_set_resize_listener(engine_on_ui_expose);
- ui_set_loop_listener(draw_frames);
+ ui_set_loop_listener(engine->ui, draw_frames, engine);
/* FPS calculation */
start = time(NULL);
@@ -313,3 +281,11 @@ engine_set_rendering_background_c(Engine *engine, Color *color)
{
engine_set_rendering_background(engine, color->r, color->g, color->b, color->a);
}
+
+void
+engine_set_rendering_listener(Engine *engine, void (*draw_frames)(void *data), void *data)
+{
+ engine->draw_frame_listener = malloc(sizeof(DrawFrameListener));
+ engine->draw_frame_listener->draw_frames = draw_frames;
+ engine->draw_frame_listener->data = data;
+}
diff --git a/src/main/engine/engine.h b/src/main/engine/engine.h
index f3f817b..24dbe61 100644
--- a/src/main/engine/engine.h
+++ b/src/main/engine/engine.h
@@ -24,24 +24,32 @@
#include "../util/list.h"
#include "domain/color.h"
+#include "shape/circle.h"
+
enum EngineInput {
ENGINE_MOUSE_PRESSED = 4
};
+typedef struct DrawFrameListener {
+ void (*draw_frames)(void *data);
+ void *data;
+} DrawFrameListener;
+
typedef struct {
UI *ui;
- list_t *circles;
+ list_t *circles1;
list_t *lines;
- void (*draw_frame)();
float ortho_left, ortho_right, ortho_top, ortho_bottom;
Color *rendering_background;
+ /* Listeners */
+ DrawFrameListener *draw_frame_listener;
} Engine;;
Engine *engine_new(int width, int height);
void engine_set_rendering_background(Engine *engine, float r, float g, float b, float a);
void engine_set_rendering_background_c(Engine *engine, Color *color);
void engine_set_window_title(Engine *engine, const char *title);
-void engine_draw_circle(float cx, float cy, float r, int num_segments, int outline);
+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));
diff --git a/src/main/engine/shape/circle.c b/src/main/engine/shape/circle.c
index 9cc79c6..f60e917 100644
--- a/src/main/engine/shape/circle.c
+++ b/src/main/engine/shape/circle.c
@@ -18,6 +18,8 @@
*/
#include <stdlib.h>
+#include <GL/gl.h>
+#include <math.h>
#include "circle.h"
Circle *
@@ -32,3 +34,18 @@ engine_circle_new(float cx, float cy, float r, int num_segments, int outline)
return circle;
}
+
+void
+engine_render_circle(Circle *circle)
+{
+ glBegin(GL_LINE_LOOP);
+
+ for (int i = 0; i < circle->num_segments; i++) {
+ float theta = 2.0f * M_PI * i / circle->num_segments;
+ float x = circle->r * cosf(theta);
+ float y = circle->r * sinf(theta);
+ glVertex2f(circle->cx + x, circle->cy + y);
+ }
+
+ glEnd();
+}
diff --git a/src/main/engine/shape/circle.h b/src/main/engine/shape/circle.h
index f8bb91d..72e69e1 100644
--- a/src/main/engine/shape/circle.h
+++ b/src/main/engine/shape/circle.h
@@ -29,5 +29,6 @@ typedef struct {
} Circle;
Circle *engine_circle_new(float cx, float cy, float r, int num_segments, int outline);
+void engine_render_circle(Circle *circle);
#endif /* __CIRCLE_H__ */
diff --git a/src/main/engine/ui/ui.h b/src/main/engine/ui/ui.h
index e1c4283..53040ed 100644
--- a/src/main/engine/ui/ui.h
+++ b/src/main/engine/ui/ui.h
@@ -22,11 +22,17 @@
#include "types.h"
+typedef struct LoopEvent {
+ void (*on_loop_event)(void *data);
+ void *data;
+} LoopEvent;
+
typedef struct UI {
int width;
int height;
void *extra;
int close_window;
+ LoopEvent *loop_event;
} UI;
UI *ui_new(int w, int h);
@@ -34,8 +40,8 @@ void ui_loop(UI *ui);
void ui_set_title(UI *ui, const char *title);
void ui_set_expose_listener(void (*expose_event)());
void ui_set_resize_listener(void (*resize_event)(UIEventResize *));
-void ui_set_loop_listener(void (*event)());
-void ui_set_mouse_press_listener(void (*event)(UIMouseButtonPressed *));
+void ui_set_loop_listener(UI *ui, void (*event)(void *data), void *data);
+void ui_set_mouse_press_listener(void (*event)(UIMouseButtonPressed *), void *data);
void ui_set_generic_listener(void (*generic_event)(int type));
#endif /* __UI_H__ */
diff --git a/src/main/engine/ui/x11/ui.c b/src/main/engine/ui/x11/ui.c
index fc7e0b7..07db3a6 100644
--- a/src/main/engine/ui/x11/ui.c
+++ b/src/main/engine/ui/x11/ui.c
@@ -35,7 +35,6 @@ typedef struct {
int close_window;
} X11UI;
-void (*on_loop_event)();
void (*on_expose_event)();
void (*on_resize_event)(UIEventResize *);
void (*on_mouse_press_event)(UIMouseButtonPressed *);
@@ -253,9 +252,11 @@ ui_on_generic_event(XEvent event)
}
void
-ui_set_loop_listener(void (*loop_event)())
+ui_set_loop_listener(UI *ui, void (*loop_event)(void *data), void *data)
{
- on_loop_event = loop_event;
+ ui->loop_event = malloc(sizeof(LoopEvent));
+ ui->loop_event->on_loop_event = loop_event;
+ ui->loop_event->data = data;
}
void
@@ -271,7 +272,7 @@ ui_set_resize_listener(void (*resize_event)(UIEventResize *))
}
void
-ui_set_mouse_press_listener(void (*mouse_press_event)(UIMouseButtonPressed *))
+ui_set_mouse_press_listener(void (*mouse_press_event)(UIMouseButtonPressed *), void *data)
{
on_mouse_press_event = mouse_press_event;
}
@@ -301,7 +302,7 @@ ui_loop(UI *ui)
ui_on_mouse_release(event);
}
- on_loop_event();
+ ui->loop_event->on_loop_event(ui->loop_event->data);
glXSwapBuffers(display, window);
}
diff --git a/src/main/game/cell.c b/src/main/game/cell.c
index e8322ff..cf5c11c 100644
--- a/src/main/game/cell.c
+++ b/src/main/game/cell.c
@@ -51,6 +51,7 @@ cell_new(float left, float right, float top, float bottom)
cell->cy = (cell->top + cell->bottom) / 2;
cell->sign = '\0';
+ cell->shape = NULL;
return cell;
}
diff --git a/src/main/game/cell.h b/src/main/game/cell.h
index e912ec0..8f52c91 100644
--- a/src/main/game/cell.h
+++ b/src/main/game/cell.h
@@ -27,6 +27,7 @@ typedef struct Cell {
float cx, cy;
bool filled;
char sign;
+ void *shape;
} Cell;
Cell *cell_new(float left, float right, float top, float bottom);
diff --git a/src/main/game/game.c b/src/main/game/game.c
index 31c9780..c72f81a 100644
--- a/src/main/game/game.c
+++ b/src/main/game/game.c
@@ -23,7 +23,6 @@
#include "../engine/engine.h"
#include "game.h"
#include "cell.h"
-#include "../engine/ui/ui.h"
#define WIN_PATTERNS 8
@@ -59,6 +58,20 @@ static void game_draw_field(TrisGame *game, float box_size);
static char game_next_sign(TrisGame *game);
static void
+draw_frames(void *data)
+{
+ if (data == NULL) return;
+ TrisGame *game = data;
+
+ for (int i = 0; i < BOARD_SIZE; i++) {
+ Cell *cell = game->cells[i];
+ if (cell->sign == SIGN_CIRCLE) {
+ engine_render_circle(cell->shape);
+ }
+ }
+}
+
+static void
game_restart(TrisGame *game)
{
game->ended = false;
@@ -67,7 +80,13 @@ game_restart(TrisGame *game)
Cell *cell = game->cells[i];
cell->filled = false;
cell->sign = '\0';
+
+ if (cell->shape != NULL) {
+ free(cell->shape);
+ }
}
+
+ engine_set_rendering_background_c(game->engine, game->board->default_color);
}
static void
@@ -107,7 +126,7 @@ draw_sign(TrisGame *game, Cell *cell)
cell_set_sign(cell, sign);
if (sign == SIGN_CIRCLE) {
- engine_draw_circle(x, y, l, 120, 1);
+ 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);
@@ -166,6 +185,8 @@ game_init(int width, int height)
engine_set_window_title(game->engine, "Tris Game");
engine_set_mouse_button_listener(game_mouse_button_pressed, game);
+ engine_set_rendering_listener(game->engine, draw_frames, game);
+
game_draw_field(game, 0.5f);
return game;