abstract input into platform layer
This commit is contained in:
@@ -2,10 +2,11 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "base/base_core.h"
|
||||
#include "base/base_math.h"
|
||||
|
||||
////////////////////////////////
|
||||
// Input event buffer
|
||||
// Accumulated per frame, consumed by the app each tick.
|
||||
// Input — accumulated per frame, consumed by the app each tick.
|
||||
|
||||
#define PLATFORM_MAX_CHARS_PER_FRAME 64
|
||||
#define PLATFORM_MAX_KEYS_PER_FRAME 32
|
||||
@@ -29,7 +30,7 @@ enum {
|
||||
PKEY_X = 0x58,
|
||||
};
|
||||
|
||||
struct PlatformInputEvents {
|
||||
struct PlatformInput {
|
||||
// Typed characters (UTF-16 code units, printable only)
|
||||
uint16_t chars[PLATFORM_MAX_CHARS_PER_FRAME];
|
||||
int32_t char_count;
|
||||
@@ -41,6 +42,12 @@ struct PlatformInputEvents {
|
||||
// Modifier state at time of last key event
|
||||
bool ctrl_held;
|
||||
bool shift_held;
|
||||
|
||||
// Mouse state (polled per frame)
|
||||
Vec2F32 mouse_pos;
|
||||
Vec2F32 scroll_delta;
|
||||
B32 mouse_down;
|
||||
B32 was_mouse_down;
|
||||
};
|
||||
|
||||
struct PlatformWindow;
|
||||
@@ -77,8 +84,8 @@ void platform_set_frame_callback(PlatformWindow *window, PlatformFram
|
||||
void platform_set_menu(PlatformWindow *window, PlatformMenu *menus, int32_t menu_count);
|
||||
int32_t platform_poll_menu_command(PlatformWindow *window);
|
||||
|
||||
// Returns accumulated input events since last call, then clears the buffer.
|
||||
PlatformInputEvents platform_get_input_events(PlatformWindow *window);
|
||||
// Returns accumulated input since last call (keyboard events + polled mouse state), then clears the buffer.
|
||||
PlatformInput platform_get_input(PlatformWindow *window);
|
||||
|
||||
// Clipboard operations (null-terminated UTF-8 strings).
|
||||
// platform_clipboard_set copies text to the system clipboard.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user