From 1d04001a61e9d1660f1c2b07db091a5f971aa2a4 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sun, 16 Feb 2025 20:42:23 -0500 Subject: move input handling into renderer --- renderer.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'renderer.c') diff --git a/renderer.c b/renderer.c index 13ea671..663d469 100644 --- a/renderer.c +++ b/renderer.c @@ -17,6 +17,23 @@ static int buf_idx; static SDL_Window *window; +static const char button_map[256] = { + [ SDL_BUTTON_LEFT & 0xff ] = MU_MOUSE_LEFT, + [ SDL_BUTTON_RIGHT & 0xff ] = MU_MOUSE_RIGHT, + [ SDL_BUTTON_MIDDLE & 0xff ] = MU_MOUSE_MIDDLE, +}; + +static const char key_map[256] = { + [ SDLK_LSHIFT & 0xff ] = MU_KEY_SHIFT, + [ SDLK_RSHIFT & 0xff ] = MU_KEY_SHIFT, + [ SDLK_LCTRL & 0xff ] = MU_KEY_CTRL, + [ SDLK_RCTRL & 0xff ] = MU_KEY_CTRL, + [ SDLK_LALT & 0xff ] = MU_KEY_ALT, + [ SDLK_RALT & 0xff ] = MU_KEY_ALT, + [ SDLK_RETURN & 0xff ] = MU_KEY_RETURN, + [ SDLK_BACKSPACE & 0xff ] = MU_KEY_BACKSPACE, +}; + void r_init(void) { /* init SDL window */ @@ -49,6 +66,52 @@ void r_init(void) { } +static void +handle_event(SDL_Event e, mu_Context *ctx) { + switch (e.type) { + case SDL_QUIT: { + exit(EXIT_SUCCESS); + } + break; case SDL_MOUSEMOTION: { + mu_input_mousemove(ctx, e.motion.x, e.motion.y); + } + break; case SDL_MOUSEWHEEL: { + mu_input_scroll(ctx, 0, e.wheel.y * -30); + } + break; case SDL_TEXTINPUT: { + mu_input_text(ctx, e.text.text); + } + break; case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: { + int b = button_map[e.button.button & 0xff]; + if (b && e.type == SDL_MOUSEBUTTONDOWN) { + mu_input_mousedown(ctx, e.button.x, e.button.y, b); + } + if (b && e.type == SDL_MOUSEBUTTONUP) { + mu_input_mouseup(ctx, e.button.x, e.button.y, b); + } + } + break; case SDL_KEYDOWN: case SDL_KEYUP: { + int c = key_map[e.key.keysym.sym & 0xff]; + if (c && e.type == SDL_KEYDOWN) { + mu_input_keydown(ctx, c); + } + if (c && e.type == SDL_KEYUP) { + mu_input_keyup(ctx, c); + } + } + } +} + + +void +r_handle_input(mu_Context *ctx) { + SDL_Event e; + while (SDL_PollEvent(&e)) { + handle_event(e, ctx); + } +} + + static void flush(void) { if (buf_idx == 0) { return; } -- cgit v1.2.3