diff options
author | 2025-05-20 22:12:17 +0200 | |
---|---|---|
committer | 2025-05-20 22:12:17 +0200 | |
commit | a1329de937f033a7c2cca35211307e391f1873e4 (patch) | |
tree | 22d000ebba7394750cb25915599d6bd1eb44a94b | |
parent | 693d830f69b9a4ce6f956f4a688f41180156473e (diff) | |
download | tris-a1329de937f033a7c2cca35211307e391f1873e4.tar.gz tris-a1329de937f033a7c2cca35211307e391f1873e4.zip |
Add logic to get mouse position
-rw-r--r-- | src/main/engine/engine.c | 27 | ||||
-rw-r--r-- | src/main/engine/engine.h | 2 | ||||
-rw-r--r-- | src/main/game/game.c | 4 |
3 files changed, 23 insertions, 10 deletions
diff --git a/src/main/engine/engine.c b/src/main/engine/engine.c index 443ee93..3dc3cc5 100644 --- a/src/main/engine/engine.c +++ b/src/main/engine/engine.c @@ -34,8 +34,6 @@ #define RANGE_GL 2.0f -static float xstep; -static float ystep; static int width; static int height; @@ -49,6 +47,7 @@ typedef struct Engine { UI *ui; list_t *circles; void (*draw_frame)(); + float ortho_left, ortho_right, ortho_top, ortho_bottom; } Engine; static Engine *engine; @@ -59,7 +58,7 @@ static int frames; static time_t end; void (*dispatch_ui_event)(int); -void (*on_mouse_button_pressed)(int x, int y); +void (*on_mouse_button_pressed)(float x, float y); void engine_init(int w, int h) @@ -199,6 +198,17 @@ draw_frames() } static void +engine_set_ortho(float left, float right, float bottom, float top) +{ + engine->ortho_left = left; + engine->ortho_right = right; + engine->ortho_bottom = bottom; + engine->ortho_top = top; + + glOrtho(left, right, bottom, top, -1, 1); +} + +static void engine_on_ui_expose(UIEventResize *er) { /* Set the viewport to the window size */ @@ -211,10 +221,10 @@ engine_on_ui_expose(UIEventResize *er) float aspect; if (er->width >= er->height) { aspect = (float)er->width / (float)er->height; - glOrtho(-aspect, aspect, -1, 1, -1, 1); + engine_set_ortho(-aspect, aspect, -1, 1); } else { aspect = (float)er->height / (float)er->width; - glOrtho(-1, 1, -aspect, aspect, -1, 1); + engine_set_ortho(-1, 1, -aspect, aspect); } /* Returns to the model view */ @@ -238,11 +248,14 @@ engine_loop(void) void mouse_button_press_event(UIMouseButtonPressed *mbp) { - on_mouse_button_pressed(mbp->x, mbp->y); + 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); + + on_mouse_button_pressed(oglX, oglY); } void -engine_set_mouse_button_listener(void (*event)(int x, int y)) +engine_set_mouse_button_listener(void (*event)(float x, float y)) { on_mouse_button_pressed = event; ui_set_mouse_press_listener(mouse_button_press_event); diff --git a/src/main/engine/engine.h b/src/main/engine/engine.h index 73d5dd5..624bc44 100644 --- a/src/main/engine/engine.h +++ b/src/main/engine/engine.h @@ -29,6 +29,6 @@ void engine_draw_circle(float cx, float cy, float r, int num_segments, int outli 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)(int x, int y)); +void engine_set_mouse_button_listener(void (*event)(float x, float y)); #endif /* __ENGINE_H__ */ diff --git a/src/main/game/game.c b/src/main/game/game.c index 2bd0bb5..3996cee 100644 --- a/src/main/game/game.c +++ b/src/main/game/game.c @@ -25,9 +25,9 @@ #include "../engine/ui/ui.h" static void -game_mouse_button_pressed(int x, int y) +game_mouse_button_pressed(float x, float y) { - printf("mouse pressed %d %d\n", x, y); + printf("Mouse OpenGL: X = %.2f, Y = %.2f\n", x, y); } TrisGame * |