aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-02-16 20:42:23 -0500
committerSam Anthony <sam@samanthony.xyz>2025-02-16 20:42:23 -0500
commit1d04001a61e9d1660f1c2b07db091a5f971aa2a4 (patch)
treece4a12583123be7d8ff0a9fa70eddf9712800ba6
parentbfff9d70936bcc797de7d5069c2822478b46833b (diff)
downloadvolute-1d04001a61e9d1660f1c2b07db091a5f971aa2a4.zip
move input handling into renderer
-rw-r--r--main.c65
-rw-r--r--renderer.c63
-rw-r--r--renderer.h1
3 files changed, 65 insertions, 64 deletions
diff --git a/main.c b/main.c
index 1d0b3bc..ac6e1a3 100644
--- a/main.c
+++ b/main.c
@@ -32,23 +32,6 @@ static const mu_Color COLOR_BASEFOCUS = COLOR_BASE;
static const mu_Color COLOR_SCROLLBASE = WHITE;
static const mu_Color COLOR_SCROLLTHUMB = WHITE;
-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,
-};
-
/* Function declarations. */
@@ -56,8 +39,6 @@ static void set_style(mu_Context *ctx);
static int text_width(mu_Font font, const char *text, int len);
static int text_height(mu_Font font);
static void main_loop(mu_Context *ctx, UI *ui);
-static void handle_events(mu_Context *ctx);
-static void handle_event(SDL_Event e, mu_Context *ctx);
static void process_frame(mu_Context *ctx, UI *ui);
static void main_window(mu_Context *ctx, UI *ui);
static void render(mu_Context *ctx);
@@ -121,57 +102,13 @@ text_height(mu_Font font) {
static void
main_loop(mu_Context *ctx, UI *ui) {
for (;;) {
- handle_events(ctx);
+ r_handle_input(ctx);
process_frame(ctx, ui);
render(ctx);
}
}
static void
-handle_events(mu_Context *ctx) {
- SDL_Event e;
- while (SDL_PollEvent(&e)) {
- handle_event(e, ctx);
- }
-}
-
-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);
- }
- }
- }
-}
-
-static void
process_frame(mu_Context *ctx, UI *ui) {
mu_begin(ctx);
main_window(ctx, ui);
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; }
diff --git a/renderer.h b/renderer.h
index a9de784..9c0df1f 100644
--- a/renderer.h
+++ b/renderer.h
@@ -4,6 +4,7 @@
#include "microui.h"
void r_init(void);
+void r_handle_input(mu_Context *ctx);
void r_draw_rect(mu_Rect rect, mu_Color color);
void r_draw_text(const char *text, mu_Vec2 pos, mu_Color color);
void r_draw_icon(int id, mu_Rect rect, mu_Color color);