From 28b06c375215cf71213fa211965dc87244c95875 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sun, 16 Feb 2025 20:50:58 -0500 Subject: simplify renderer api --- renderer.c | 92 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) (limited to 'renderer.c') diff --git a/renderer.c b/renderer.c index f8aad9e..038425e 100644 --- a/renderer.c +++ b/renderer.c @@ -114,36 +114,6 @@ r_handle_input(mu_Context *ctx) { } -static void -render_command(mu_Command *cmd) { - switch (cmd->type) { - case MU_COMMAND_TEXT: { - r_draw_text(cmd->text.str, cmd->text.pos, cmd->text.color); - } - break; case MU_COMMAND_RECT: { - r_draw_rect(cmd->rect.rect, cmd->rect.color); - } - break; case MU_COMMAND_ICON: { - r_draw_icon(cmd->icon.id, cmd->icon.rect, cmd->icon.color); - } - break; case MU_COMMAND_CLIP: { - r_set_clip_rect(cmd->clip.rect); - } - } -} - - -void -r_render(mu_Context *ctx) { - r_clear(COLOR_BG); - mu_Command *cmd = NULL; - while (mu_next_command(ctx, &cmd)) { - render_command(cmd); - } - r_present(); -} - - static void flush(void) { if (buf_idx == 0) { return; } @@ -219,12 +189,12 @@ static void push_quad(mu_Rect dst, mu_Rect src, mu_Color color) { } -void r_draw_rect(mu_Rect rect, mu_Color color) { +static void draw_rect(mu_Rect rect, mu_Color color) { push_quad(rect, atlas[ATLAS_WHITE], color); } -void r_draw_text(const char *text, mu_Vec2 pos, mu_Color color) { +static void draw_text(const char *text, mu_Vec2 pos, mu_Color color) { mu_Rect dst = { pos.x, pos.y, 0, 0 }; for (const char *p = text; *p; p++) { if ((*p & 0xc0) == 0x80) { continue; } @@ -238,7 +208,7 @@ 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) { +static void draw_icon(int id, mu_Rect rect, mu_Color color) { mu_Rect src = atlas[id]; int x = rect.x + (rect.w - src.w) / 2; int y = rect.y + (rect.h - src.h) / 2; @@ -246,6 +216,49 @@ void r_draw_icon(int id, mu_Rect rect, mu_Color color) { } +static void set_clip_rect(mu_Rect rect) { + flush(); + glScissor(rect.x, height - (rect.y + rect.h), rect.w, rect.h); +} + + +static void +render_command(mu_Command *cmd) { + switch (cmd->type) { + case MU_COMMAND_TEXT: { + draw_text(cmd->text.str, cmd->text.pos, cmd->text.color); + } + break; case MU_COMMAND_RECT: { + draw_rect(cmd->rect.rect, cmd->rect.color); + } + break; case MU_COMMAND_ICON: { + draw_icon(cmd->icon.id, cmd->icon.rect, cmd->icon.color); + } + break; case MU_COMMAND_CLIP: { + set_clip_rect(cmd->clip.rect); + } + } +} + + +static void clear(mu_Color clr) { + flush(); + glClearColor(clr.r / 255., clr.g / 255., clr.b / 255., clr.a / 255.); + glClear(GL_COLOR_BUFFER_BIT); +} + + +void +r_render(mu_Context *ctx) { + clear(COLOR_BG); + mu_Command *cmd = NULL; + while (mu_next_command(ctx, &cmd)) { + render_command(cmd); + } + r_present(); +} + + int r_get_text_width(const char *text, int len) { int res = 0; for (const char *p = text; *p && len--; p++) { @@ -267,19 +280,6 @@ void r_get_window_size(int *w, int *h) { } -void r_set_clip_rect(mu_Rect rect) { - flush(); - glScissor(rect.x, height - (rect.y + rect.h), rect.w, rect.h); -} - - -void r_clear(mu_Color clr) { - flush(); - glClearColor(clr.r / 255., clr.g / 255., clr.b / 255., clr.a / 255.); - glClear(GL_COLOR_BUFFER_BIT); -} - - void r_present(void) { flush(); SDL_GL_SwapWindow(window); -- cgit v1.2.3