diff options
author | 2025-05-17 14:10:39 +0200 | |
---|---|---|
committer | 2025-05-17 14:10:39 +0200 | |
commit | 268790307bde4859ac6ee5ac6c3d19ada9ed5a11 (patch) | |
tree | d0f0a0574cf33526668307bafc269a1a3d7df6e6 | |
parent | a3027cce6f5f7753147edc49aa4403a013338c3d (diff) | |
download | tris-268790307bde4859ac6ee5ac6c3d19ada9ed5a11.tar.gz tris-268790307bde4859ac6ee5ac6c3d19ada9ed5a11.zip |
Add the maanagement of mouse press event
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/main/engine/engine.c | 31 | ||||
-rw-r--r-- | src/main/engine/engine.h | 5 | ||||
-rw-r--r-- | src/main/ui/ui.h | 1 | ||||
-rw-r--r-- | src/main/ui/x11/ui.c | 16 |
6 files changed, 57 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6815afb..f2087c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,8 @@ project(tris) set(OpenGL_GL_PREFERENCE GLVND) find_package(OpenGL REQUIRED) +add_definitions(-DX11) + add_executable(tris src/main/main.c src/main/ui/ui.h @@ -29,6 +29,8 @@ OBJ = ${SRC:.c=.o} CFLAGS != pkg-config --cflags ${LIBS} LFLAGS != pkg-config --libs ${LIBS} +CFLAGS += -DX11 + ${PROG}: ${OBJ} ${CC} ${OBJ} -o $@ ${LFLAGS} diff --git a/src/main/engine/engine.c b/src/main/engine/engine.c index e45909a..e0da00c 100644 --- a/src/main/engine/engine.c +++ b/src/main/engine/engine.c @@ -21,6 +21,11 @@ #include <time.h> #include <GL/gl.h> #include "../ui/ui.h" +#include "engine.h" + +#ifdef X11 +#include <X11/Xlib.h> +#endif #define RANGE_GL 2.0f @@ -40,6 +45,8 @@ static time_t start; static int frames; static time_t end; +void (*dispatch_ui_event)(int); + void engine_init(int w, int h) { @@ -100,3 +107,27 @@ engine_loop(void (*draw_frame)()) ui_loop(); } + +static void +engine_dispatch_ui_events(int type) +{ + int event_type = 0; + switch (type) { +#ifdef X11 + case ButtonPress: + event_type = ENGINE_MOUSE_PRESSED; + break; +#endif + default: + event_type = 0; + break; + } + dispatch_ui_event(event_type); +} + +void +engine_input(void (*f_input)(int engine_input)) +{ + ui_set_generic_listener(engine_dispatch_ui_events); + dispatch_ui_event = f_input; +} diff --git a/src/main/engine/engine.h b/src/main/engine/engine.h index 691d8ec..fc4c5b9 100644 --- a/src/main/engine/engine.h +++ b/src/main/engine/engine.h @@ -20,8 +20,13 @@ #ifndef __ENGINE_H__ #define __ENGINE_H__ +enum EngineInput { + ENGINE_MOUSE_PRESSED = 4 +}; + 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_input(void (*f_input)(int engine_input)); #endif /* __ENGINE_H__ */ diff --git a/src/main/ui/ui.h b/src/main/ui/ui.h index fcdd99f..06cd515 100644 --- a/src/main/ui/ui.h +++ b/src/main/ui/ui.h @@ -25,5 +25,6 @@ void ui_loop(void); void ui_set_title(const char *title); void ui_set_loop_listener(void (*event)()); void ui_set_mouse_press_listener(void (*event)()); +void ui_set_generic_listener(void (*generic_event)(int type)); #endif /* __UI_H__ */ diff --git a/src/main/ui/x11/ui.c b/src/main/ui/x11/ui.c index fc59e4e..69bd5be 100644 --- a/src/main/ui/x11/ui.c +++ b/src/main/ui/x11/ui.c @@ -33,6 +33,7 @@ static int close_window = 0; void (*on_loop_event)(); void (*on_mouse_press_event)(); +void (*on_generic_event)(int); void cleanup(void); @@ -143,6 +144,14 @@ ui_on_mouse_release(XEvent event) if (event.type != ButtonRelease) return; } +static void +ui_on_generic_event(XEvent event) +{ + if (on_generic_event != NULL) { + on_generic_event(event.type); + } +} + void ui_set_loop_listener(void (*loop_event)()) { @@ -156,12 +165,19 @@ ui_set_mouse_press_listener(void (*mouse_press_event)()) } void +ui_set_generic_listener(void (*generic_event)(int type)) +{ + on_generic_event = generic_event; +} + +void ui_loop(void) { XEvent event; while (!close_window) { while (XPending(display)) { XNextEvent(display, &event); + ui_on_generic_event(event); ui_on_expose(event); ui_on_resize(event); ui_on_keypress(event); |