diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-02-16 20:42:23 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-02-16 20:42:23 -0500 |
| commit | 1d04001a61e9d1660f1c2b07db091a5f971aa2a4 (patch) | |
| tree | ce4a12583123be7d8ff0a9fa70eddf9712800ba6 /renderer.c | |
| parent | bfff9d70936bcc797de7d5069c2822478b46833b (diff) | |
| download | volute-1d04001a61e9d1660f1c2b07db091a5f971aa2a4.zip | |
move input handling into renderer
Diffstat (limited to 'renderer.c')
| -rw-r--r-- | renderer.c | 63 |
1 files changed, 63 insertions, 0 deletions
@@ -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; } |