improved keymapping

This commit is contained in:
2026-07-22 13:43:55 +03:00
parent 6a77546313
commit 5e7ed489af
4 changed files with 41 additions and 15 deletions

View File

@@ -3,14 +3,31 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "pltf/pltf.h"
#include "pltf/gl_funcs.h" #include "pltf/gl_funcs.h"
#include "pltf/pltf.h"
#include "shader.h" #include "shader.h"
void counter_cb(void* user_data) { typedef struct {
int* counter = (int*) user_data; const char* up_msg;
printf("Counter: %d\n", *counter); const char* down_msg;
(*counter)++; 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() { int main() {
@@ -22,8 +39,8 @@ int main() {
pltf_gl_ctx_info_print(); pltf_gl_ctx_info_print();
pltf_key_callback_set(PLTF_KEY_ESC, pltf_window_close, NULL); pltf_key_callback_set(PLTF_KEY_ESC, pltf_window_close, NULL);
int counter = 0; MsgState msg_state = { .up_msg = "UP", .down_msg = "DOWN", .up_count = 0, .down_count = 0 };
pltf_key_callback_set(PLTF_KEY_Q, counter_cb, &counter); pltf_key_callback_set(PLTF_KEY_Q, msg_cb, &msg_state);
// clang-format off // clang-format off
const float triangle_verts[] = { const float triangle_verts[] = {

View File

@@ -4,7 +4,7 @@
#include <stdbool.h> #include <stdbool.h>
typedef enum { typedef enum {
PLTF_WE_NULL, PLTF_WE_NONE,
PLTF_WE_DESTROY, PLTF_WE_DESTROY,
} PltfWindowEvent; } PltfWindowEvent;
@@ -21,7 +21,12 @@ typedef enum {
#undef PLTF_KEYS_X #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_init();
void pltf_deinit(); void pltf_deinit();
@@ -36,7 +41,7 @@ PltfWindowEvent pltf_window_event_handle();
void pltf_key_callback_set( void pltf_key_callback_set(
const PltfKey key, const PltfKey key,
const PltfKeyCallback cb, const PltfKeyCallback cb,
const void* user_data void* user_data
); );
#endif // PLTF_H_ #endif // PLTF_H_

View File

@@ -260,7 +260,7 @@ PltfWindowEvent pltf_window_event_handle() {
} }
} }
return PLTF_WE_NULL; return PLTF_WE_NONE;
} }
#define PLTF_KEYS_X(lower, upper) \ #define PLTF_KEYS_X(lower, upper) \
@@ -273,7 +273,7 @@ PltfWindowEvent pltf_window_event_handle() {
void pltf_key_callback_set( void pltf_key_callback_set(
const PltfKey key, const PltfKey key,
const PltfKeyCallback cb, const PltfKeyCallback cb,
const void* user_data void* user_data
) { ) {
switch (key) { switch (key) {

View File

@@ -6,11 +6,11 @@
#define PLTF_KEYS_X(lower, upper) \ #define PLTF_KEYS_X(lower, upper) \
case PLTF_KEY_##upper: \ case PLTF_KEY_##upper: \
if (pltf_state.key_##lower##_cb != NULL) { \ 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; 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); const PltfKey pltf_key = vk_to_pltf_key(key);
switch (pltf_key) { switch (pltf_key) {
@@ -53,7 +53,11 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
// } // }
case WM_KEYDOWN: 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; return 0;
case WM_CLOSE: case WM_CLOSE: