wasm events to list, wm_char

This commit is contained in:
krzosa
2025-01-03 08:24:40 +01:00
parent 6933566a86
commit 3cdfbd1957
7 changed files with 68 additions and 59 deletions

View File

@@ -19,9 +19,6 @@ HWND w32_window_handle;
HDC w32_dc;
b32 w32_quit_app;
app_event_list_t w32_event_list;
ma_arena_t *w32_event_arena;
fn v2f64_t w32_get_window_size(HWND window) {
RECT window_rect;
GetClientRect(window, &window_rect);
@@ -46,6 +43,11 @@ fn f64 w32_get_dpr(HWND window_handle) {
return result;
}
///////////////////////////////
// event processing
app_event_list_t w32_event_list;
ma_arena_t *w32_event_arena;
fn void w32_push_event(app_event_t event) {
app_event_t *ev = ma_push_type(w32_event_arena, app_event_t);
*ev = event;
@@ -61,6 +63,25 @@ fn void w32_push_event(app_event_t event) {
w32_event_list.len += 1;
}
fn void w32_push_wm_char_event(WPARAM wparam) {
if (wparam >= 32 || wparam == 127) return;
s8_t string = s8_lit("?");
utf32_result_t encode = utf16_to_utf32((u16 *)&wparam, 1);
if (!encode.error) {
utf8_result_t encode8 = utf32_to_utf8(encode.out_str);
if (!encode8.error) {
string = s8(encode8.out_str, encode8.len);
string = s8_copy(w32_event_arena, string);
}
}
w32_push_event((app_event_t){
.kind = app_event_kind_text,
.text = string,
});
}
fn LRESULT CALLBACK w32_window_proc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam) {
switch (msg) {
case WM_CLOSE: {
@@ -134,13 +155,11 @@ fn LRESULT CALLBACK w32_window_proc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lp
});
} break;
case WM_CHAR: {
//https://handmade.network/forums/t/2011-keyboard_inputs_-_scancodes,_raw_input,_text_input,_key_names
// WM_UNICHAR
/*
I looked closer to how you process WM_CHAR in example code and I believe it is very wrong. wParam doesn't contain two ushorts joined in one 32-bit value. It is always one UTF-16 value. That means 16-bits. If Windows wants to send you unicode codepoint with value >0xFFFF, then it sends two UTF-16 WM_CHAR messages. Each with one part of UTF-16 surrogate pair. So your first WM_CHAR needs to remember it, and second one construct full unicode codepoint. For example, this is mentioned here: http://www.catch22.net/tuts/unicode-text-editing
*/
} break
// @todo:
// WM_CHAR sends 2byte codepoints in 2 messages??????
case WM_CHAR: {
w32_push_wm_char_event(wparam);
} break;
default: return DefWindowProcW(wnd, msg, wparam, lparam);
@@ -296,8 +315,14 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n
// w32_canvas_t canvas = w32_create_canvas(w32_window_handle);
f64 refresh_rate = 60;
DEVMODEW devmodew = {0};
if (EnumDisplaySettingsW(0, ENUM_CURRENT_SETTINGS, &devmodew)) {
refresh_rate = (f64)devmodew.dmDisplayFrequency;
}
f64 time_frame_start = os_seconds_now();
f64 time_delta = 0.01666666666666;
f64 time_delta = 1.0 / refresh_rate;
f64 time_total = 0.0;
f64 time_update = 0.0;