From 32897d35fa724e46e4195927d385da4e35ac7c76 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 8 Feb 2024 07:55:30 -0800 Subject: Add function to check for key press. --- gfx-app/include/gfx/gfx_app.h | 32 ++++++++++++++++++++++ gfx-app/src/gfx_app.c | 63 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/gfx-app/include/gfx/gfx_app.h b/gfx-app/include/gfx/gfx_app.h index 3c544fa..86c502a 100644 --- a/gfx-app/include/gfx/gfx_app.h +++ b/gfx-app/include/gfx/gfx_app.h @@ -28,12 +28,44 @@ typedef struct GfxAppCallbacks { GfxAppShutdown shutdown; } GfxAppCallbacks; +typedef enum Key { + KeyA = 'a', + KeyB, + KeyC, + KeyD, + KeyE, + KeyF, + KeyG, + KeyH, + KeyI, + KeyJ, + KeyK, + KeyL, + KeyM, + KeyN, + KeyO, + KeyP, + KeyQ, + KeyR, + KeyS, + KeyT, + KeyU, + KeyV, + KeyW, + KeyX, + KeyY, + KeyZ, +} Key; + /// Create a window with an OpenGL context and run the main loop. 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 true if the given key is pressed. +bool gfx_is_key_pressed(Key); + /// Define a main function that initializes and puts the application in a loop. /// See also: gfx_app_run(). #define GFX_APP_MAIN(WIDTH, HEIGHT, MAX_FPS) \ diff --git a/gfx-app/src/gfx_app.c b/gfx-app/src/gfx_app.c index a313ce8..e0211b1 100644 --- a/gfx-app/src/gfx_app.c +++ b/gfx-app/src/gfx_app.c @@ -138,3 +138,66 @@ cleanup: void gfx_app_get_mouse_position(double* x, double* y) { glfwGetCursorPos(g_gfx_app.window, x, y); } + +static int to_glfw_key(Key key); + +bool gfx_is_key_pressed(Key key) { + return glfwGetKey(g_gfx_app.window, to_glfw_key(key)) == GLFW_PRESS; +} + +static int to_glfw_key(Key key) { + switch (key) { + case KeyA: + return GLFW_KEY_A; + case KeyB: + return GLFW_KEY_B; + case KeyC: + return GLFW_KEY_C; + case KeyD: + return GLFW_KEY_D; + case KeyE: + return GLFW_KEY_E; + case KeyF: + return GLFW_KEY_F; + case KeyG: + return GLFW_KEY_G; + case KeyH: + return GLFW_KEY_H; + case KeyI: + return GLFW_KEY_I; + case KeyJ: + return GLFW_KEY_J; + case KeyK: + return GLFW_KEY_K; + case KeyL: + return GLFW_KEY_L; + case KeyM: + return GLFW_KEY_M; + case KeyN: + return GLFW_KEY_N; + case KeyO: + return GLFW_KEY_O; + case KeyP: + return GLFW_KEY_P; + case KeyQ: + return GLFW_KEY_Q; + case KeyR: + return GLFW_KEY_R; + case KeyS: + return GLFW_KEY_S; + case KeyT: + return GLFW_KEY_T; + case KeyU: + return GLFW_KEY_U; + case KeyV: + return GLFW_KEY_V; + case KeyW: + return GLFW_KEY_W; + case KeyX: + return GLFW_KEY_X; + case KeyY: + return GLFW_KEY_Y; + case KeyZ: + return GLFW_KEY_Z; + } +} -- cgit v1.2.3