summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/engine/engine.c15
-rw-r--r--src/main/engine/ui/ui.h2
-rw-r--r--src/main/engine/ui/x11/ui.c28
3 files changed, 21 insertions, 24 deletions
diff --git a/src/main/engine/engine.c b/src/main/engine/engine.c
index 36bb352..fb0875a 100644
--- a/src/main/engine/engine.c
+++ b/src/main/engine/engine.c
@@ -43,9 +43,6 @@ init_log(void)
_log = log_create("Engine");
}
-static int width;
-static int height;
-
typedef struct Line {
float x1, y1, x2, y2;
} Line;
@@ -78,12 +75,10 @@ engine_new(int w, int h)
}
engine->circles = list_create();
engine->lines = list_create();
- engine_set_rendering_background(engine, 0.0f, 0.0f, 0.2f, 1.0f);
-
- width = w;
- height = h;
engine->ui = ui_init(w, h);
+ engine_set_rendering_background(engine, 0.0f, 0.0f, 0.2f, 1.0f);
+
return engine;
}
@@ -257,14 +252,14 @@ engine_loop(void)
start = time(NULL);
frames = 0;
- ui_loop();
+ ui_loop(engine->ui);
}
void
mouse_button_press_event(UIMouseButtonPressed *mbp)
{
- float oglX = engine->ortho_left + ((float) mbp->x / width) * (engine->ortho_right - engine->ortho_left);
- float oglY = engine->ortho_top - ((float) mbp->y / height) * (engine->ortho_top - engine->ortho_bottom);
+ float oglX = engine->ortho_left + ((float) mbp->x / engine->ui->width) * (engine->ortho_right - engine->ortho_left);
+ float oglY = engine->ortho_top - ((float) mbp->y / engine->ui->height) * (engine->ortho_top - engine->ortho_bottom);
mouse_button_event->on_mouse_button_pressed(oglX, oglY, mouse_button_event->data);
}
diff --git a/src/main/engine/ui/ui.h b/src/main/engine/ui/ui.h
index 432d158..c01591c 100644
--- a/src/main/engine/ui/ui.h
+++ b/src/main/engine/ui/ui.h
@@ -23,7 +23,7 @@
#include "types.h"
UI *ui_init(int w, int h);
-void ui_loop(void);
+void ui_loop(UI *ui);
void ui_set_title(const char *title);
void ui_set_expose_listener(void (*expose_event)());
void ui_set_resize_listener(void (*resize_event)(UIEventResize *));
diff --git a/src/main/engine/ui/x11/ui.c b/src/main/engine/ui/x11/ui.c
index b1086a9..2501f5c 100644
--- a/src/main/engine/ui/x11/ui.c
+++ b/src/main/engine/ui/x11/ui.c
@@ -38,7 +38,15 @@ void (*on_resize_event)(UIEventResize *);
void (*on_mouse_press_event)(UIMouseButtonPressed *);
void (*on_generic_event)(int);
-void cleanup(void);
+static void
+cleanup(UI *ui) {
+ glXMakeCurrent(display, None, NULL);
+ glXDestroyContext(display, gl_context);
+ XDestroyWindow(display, window);
+ XCloseDisplay(display);
+
+ free(ui);
+}
static Log *log = NULL;
@@ -148,7 +156,7 @@ ui_on_expose(XEvent event)
}
static void
-ui_on_resize(XEvent event)
+ui_on_resize(UI *ui, XEvent event)
{
init_log();
@@ -161,6 +169,8 @@ ui_on_resize(XEvent event)
}
er->width = event.xconfigure.width;
er->height = event.xconfigure.height;
+ ui->width = er->width;
+ ui->height = er->height;
on_resize_event(er);
}
}
@@ -244,7 +254,7 @@ ui_set_generic_listener(void (*generic_event)(int type))
}
void
-ui_loop(void)
+ui_loop(UI *ui)
{
XEvent event;
while (!close_window) {
@@ -252,7 +262,7 @@ ui_loop(void)
XNextEvent(display, &event);
ui_on_generic_event(event);
ui_on_expose(event);
- ui_on_resize(event);
+ ui_on_resize(ui, event);
ui_on_keypress(event);
ui_on_mouse_press(event);
ui_on_mouse_release(event);
@@ -262,13 +272,5 @@ ui_loop(void)
glXSwapBuffers(display, window);
}
- cleanup();
-}
-
-void
-cleanup(void) {
- glXMakeCurrent(display, None, NULL);
- glXDestroyContext(display, gl_context);
- XDestroyWindow(display, window);
- XCloseDisplay(display);
+ cleanup(ui);
}