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 <string.h>
#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[] = {

View File

@@ -4,7 +4,7 @@
#include <stdbool.h>
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_

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) \
@@ -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) {

View File

@@ -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: