summaryrefslogtreecommitdiff
path: root/src/main/engine
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/engine')
-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
6 files changed, 56 insertions, 47 deletions
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);
}