From 420970c8b83f20a4a2411af687e9d4a38a5fe81a Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Mon, 16 Sep 2024 19:56:36 -0700 Subject: Add function to query mouse buttons. --- app/include/gfx/app.h | 11 ++++++++++- app/src/app.c | 20 +++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/include/gfx/app.h b/app/include/gfx/app.h index 3843d92..77b6ad2 100644 --- a/app/include/gfx/app.h +++ b/app/include/gfx/app.h @@ -31,6 +31,12 @@ typedef struct GfxAppCallbacks { GfxAppResize resize; } GfxAppCallbacks; +typedef enum MouseButton { + LMB, + RMB, + MMB, +} MouseButton; + typedef enum Key { KeyA = 'a', KeyB, @@ -70,8 +76,11 @@ bool gfx_app_run(const GfxAppDesc*, const GfxAppCallbacks*); /// Get the mouse coordinates relative to the app's window. void gfx_app_get_mouse_position(double* x, double* y); +/// Return if the given mouse button is pressed. +bool gfx_app_is_mouse_button_pressed(MouseButton); + /// Return true if the given key is pressed. -bool gfx_is_key_pressed(Key); +bool gfx_app_is_key_pressed(Key); #ifdef __cplusplus } // extern "C" diff --git a/app/src/app.c b/app/src/app.c index 1e636af..9b816ee 100644 --- a/app/src/app.c +++ b/app/src/app.c @@ -149,12 +149,30 @@ void gfx_app_get_mouse_position(double* x, double* y) { glfwGetCursorPos(g_gfx_app.window, x, y); } +static int to_glfw_mouse_button(MouseButton button); + +bool gfx_app_is_mouse_button_pressed(MouseButton button) { + return glfwGetMouseButton(g_gfx_app.window, to_glfw_mouse_button(button)) == + GLFW_PRESS; +} + static int to_glfw_key(Key key); -bool gfx_is_key_pressed(Key key) { +bool gfx_app_is_key_pressed(Key key) { return glfwGetKey(g_gfx_app.window, to_glfw_key(key)) == GLFW_PRESS; } +static int to_glfw_mouse_button(MouseButton button) { + switch (button) { + case LMB: + return GLFW_MOUSE_BUTTON_LEFT; + case RMB: + return GLFW_MOUSE_BUTTON_RIGHT; + case MMB: + return GLFW_MOUSE_BUTTON_MIDDLE; + } +} + static int to_glfw_key(Key key) { switch (key) { case KeyA: -- cgit v1.2.3