abstract input into platform layer

This commit is contained in:
2026-03-02 23:01:40 -05:00
parent 8d56ea7217
commit 46f636a9ac
5 changed files with 48 additions and 60 deletions

View File

@@ -14,7 +14,8 @@ struct PlatformWindow {
int32_t pending_menu_cmd;
PlatformFrameCallback frame_callback;
void *frame_callback_user_data;
PlatformInputEvents input_events;
PlatformInput input;
B32 prev_mouse_down;
};
static PlatformWindow *g_current_window = nullptr;
@@ -34,7 +35,7 @@ static LRESULT CALLBACK win32_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM
return 0;
case WM_CHAR:
if (g_current_window && wparam >= 32 && wparam < 0xFFFF) {
PlatformInputEvents *ev = &g_current_window->input_events;
PlatformInput *ev = &g_current_window->input;
if (ev->char_count < PLATFORM_MAX_CHARS_PER_FRAME)
ev->chars[ev->char_count++] = (uint16_t)wparam;
}
@@ -42,13 +43,19 @@ static LRESULT CALLBACK win32_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (g_current_window) {
PlatformInputEvents *ev = &g_current_window->input_events;
PlatformInput *ev = &g_current_window->input;
if (ev->key_count < PLATFORM_MAX_KEYS_PER_FRAME)
ev->keys[ev->key_count++] = (uint8_t)wparam;
ev->ctrl_held = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
ev->shift_held = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
}
break; // fall through to DefWindowProc for system keys
case WM_MOUSEWHEEL:
if (g_current_window) {
int16_t wheel_delta = (int16_t)HIWORD(wparam);
g_current_window->input.scroll_delta.y += (F32)wheel_delta / (F32)WHEEL_DELTA;
}
return 0;
case WM_COMMAND:
if (g_current_window && HIWORD(wparam) == 0)
g_current_window->pending_menu_cmd = (int32_t)LOWORD(wparam);
@@ -201,9 +208,22 @@ int32_t platform_poll_menu_command(PlatformWindow *window) {
return cmd;
}
PlatformInputEvents platform_get_input_events(PlatformWindow *window) {
PlatformInputEvents result = window->input_events;
window->input_events = {};
PlatformInput platform_get_input(PlatformWindow *window) {
PlatformInput result = window->input;
// Poll mouse position
POINT cursor;
GetCursorPos(&cursor);
ScreenToClient(window->hwnd, &cursor);
result.mouse_pos = v2f32((F32)cursor.x, (F32)cursor.y);
// Poll mouse button
result.was_mouse_down = window->prev_mouse_down;
result.mouse_down = (GetAsyncKeyState(VK_LBUTTON) & 0x8000) != 0;
window->prev_mouse_down = result.mouse_down;
// Clear accumulated events for next frame
window->input = {};
return result;
}