From 40159931521cc606a03e69420f63d8acd47138cb Mon Sep 17 00:00:00 2001 From: Maxime Chevalier-Boisvert Date: Tue, 3 Sep 2024 13:47:12 -0400 Subject: [PATCH] Fix examples/textedit.c --- ncc/examples/textedit.c | 42 +++++++++++++++++++++++++++++------------ vm/src/window.rs | 3 ++- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/ncc/examples/textedit.c b/ncc/examples/textedit.c index ce3cf9a..6734506 100644 --- a/ncc/examples/textedit.c +++ b/ncc/examples/textedit.c @@ -1,6 +1,8 @@ #include +#include #include #include +#include #include #include @@ -30,7 +32,7 @@ size_t row_len(size_t row_idx) return NUM_COLS; } -void textinput(u64 window_id, char ch) +void textinput(char ch) { //print_i64(ch); //print_endl(); @@ -50,7 +52,7 @@ void textinput(u64 window_id, char ch) redraw(); } -void keydown(u64 window_id, u16 keycode) +void keydown(u16 keycode) { if (keycode == KEY_ESCAPE) { @@ -131,12 +133,7 @@ void redraw() window_draw_frame(0, frame_buffer); } -void anim_callback() -{ - benchmark(redraw()); - - time_delay_cb(400, anim_callback); -} +Event event; void main() { @@ -144,12 +141,33 @@ void main() redraw(); - window_on_keydown(0, keydown); - window_on_textinput(0, textinput); + for (;;) + { + while (window_poll_event(&event)) + { + if (event.kind == EVENT_QUIT) + { + exit(0); + } + + if (event.kind == EVENT_KEYDOWN) + { + keydown(event.key); + } - time_delay_cb(0, anim_callback); + if (event.kind == EVENT_TEXTINPUT) + { + size_t len = strlen(event.text); + for (size_t i = 0; i < len; ++i) + { + textinput(event.text[i]); + } + } + } - enable_event_loop(); + benchmark(redraw()); + thread_sleep(20); + } } //=========================================================================== diff --git a/vm/src/window.rs b/vm/src/window.rs index 964f2dd..8c53720 100644 --- a/vm/src/window.rs +++ b/vm/src/window.rs @@ -287,11 +287,12 @@ fn translate_event(sdl_event: Event, c_event: &mut CEvent) -> bool Event::TextInput { window_id, text, .. } => { c_event.kind = EVENT_TEXTINPUT; c_event.window_id = 0; + c_event.text.fill(0); let text_bytes = text.bytes(); // This should never happen - if text_bytes.len() > EVENT_TEXT_MAX_BYTES { + if text_bytes.len() > EVENT_TEXT_MAX_BYTES - 1 { panic!(); }