diff --git a/src/main.c b/src/main.c index 92cf94b..02df20b 100644 --- a/src/main.c +++ b/src/main.c @@ -3,14 +3,31 @@ #include #include -#include "pltf/pltf.h" #include "pltf/gl_funcs.h" +#include "pltf/pltf.h" #include "shader.h" -void counter_cb(void* user_data) { - int* counter = (int*) user_data; - printf("Counter: %d\n", *counter); - (*counter)++; +typedef struct { + const char* up_msg; + const char* down_msg; + int up_count; + int down_count; +} MsgState; + +void msg_cb(const PltfKeyAction action, void* user_data) { + MsgState* state = (MsgState*) user_data; + switch (action) { + case PLTF_KEY_DOWN: + printf("Key: %s Count: %d\n", state->down_msg, state->down_count); + state->down_count++; + break; + case PLTF_KEY_UP: + printf("Key: %s Count: %d\n", state->up_msg, state->up_count); + state->up_count++; + break; + default: + printf("INVALID PltfKeyAction. Was %d\n", action); + } } int main() { @@ -22,8 +39,8 @@ int main() { pltf_gl_ctx_info_print(); pltf_key_callback_set(PLTF_KEY_ESC, pltf_window_close, NULL); - int counter = 0; - pltf_key_callback_set(PLTF_KEY_Q, counter_cb, &counter); + MsgState msg_state = { .up_msg = "UP", .down_msg = "DOWN", .up_count = 0, .down_count = 0 }; + pltf_key_callback_set(PLTF_KEY_Q, msg_cb, &msg_state); // clang-format off const float triangle_verts[] = { diff --git a/src/pltf/pltf.h b/src/pltf/pltf.h index 8f4a79b..4457c6d 100644 --- a/src/pltf/pltf.h +++ b/src/pltf/pltf.h @@ -4,7 +4,7 @@ #include typedef enum { - PLTF_WE_NULL, + PLTF_WE_NONE, PLTF_WE_DESTROY, } PltfWindowEvent; @@ -21,7 +21,12 @@ typedef enum { #undef PLTF_KEYS_X -typedef void (*PltfKeyCallback)(void* user_data); +typedef enum { + PLTF_KEY_UP, + PLTF_KEY_DOWN +} PltfKeyAction; + +typedef void (*PltfKeyCallback)(const PltfKeyAction action, void* user_data); void pltf_init(); void pltf_deinit(); @@ -36,7 +41,7 @@ PltfWindowEvent pltf_window_event_handle(); void pltf_key_callback_set( const PltfKey key, const PltfKeyCallback cb, - const void* user_data + void* user_data ); #endif // PLTF_H_ diff --git a/src/pltf/win32/pltf.c b/src/pltf/win32/pltf.c index 176a158..6523074 100644 --- a/src/pltf/win32/pltf.c +++ b/src/pltf/win32/pltf.c @@ -260,7 +260,7 @@ PltfWindowEvent pltf_window_event_handle() { } } - return PLTF_WE_NULL; + return PLTF_WE_NONE; } #define PLTF_KEYS_X(lower, upper) \ @@ -273,7 +273,7 @@ PltfWindowEvent pltf_window_event_handle() { void pltf_key_callback_set( const PltfKey key, const PltfKeyCallback cb, - const void* user_data + void* user_data ) { switch (key) { diff --git a/src/pltf/win32/win_proc.c b/src/pltf/win32/win_proc.c index c1627b1..8d1d804 100644 --- a/src/pltf/win32/win_proc.c +++ b/src/pltf/win32/win_proc.c @@ -6,11 +6,11 @@ #define PLTF_KEYS_X(lower, upper) \ case PLTF_KEY_##upper: \ if (pltf_state.key_##lower##_cb != NULL) { \ - pltf_state.key_##lower##_cb(pltf_state.key_##lower##_cb_user_data); \ + pltf_state.key_##lower##_cb(action, pltf_state.key_##lower##_cb_user_data); \ } \ break; -static void key_down_handle(WPARAM key) { +static void handle_key(const WPARAM key, const PltfKeyAction action) { const PltfKey pltf_key = vk_to_pltf_key(key); switch (pltf_key) { @@ -53,7 +53,11 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) // } case WM_KEYDOWN: - key_down_handle(wParam); + handle_key(wParam, PLTF_KEY_DOWN); + return 0; + + case WM_KEYUP: + handle_key(wParam, PLTF_KEY_UP); return 0; case WM_CLOSE: