aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-02-16 21:13:58 -0500
committerSam Anthony <sam@samanthony.xyz>2025-02-16 21:13:58 -0500
commitac6ef66cc8f60ecc9d7ee8691bf7a73fcc24f4b7 (patch)
tree43228b509adf544468f4f35747bba8130e2ea133
parent40b2fc59394a2bedd1097fde107ce54b5773d649 (diff)
downloadvolute-ac6ef66cc8f60ecc9d7ee8691bf7a73fcc24f4b7.zip
renderer: set font size functions on init
-rw-r--r--main.c21
-rw-r--r--renderer.c47
-rw-r--r--renderer.h2
3 files changed, 32 insertions, 38 deletions
diff --git a/main.c b/main.c
index 1f57612..3a90419 100644
--- a/main.c
+++ b/main.c
@@ -36,8 +36,6 @@ static const mu_Color COLOR_SCROLLTHUMB = WHITE;
/* Function declarations. */
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 process_frame(mu_Context *ctx, UI *ui);
static void main_window(mu_Context *ctx, UI *ui);
@@ -47,14 +45,10 @@ static void main_window(mu_Context *ctx, UI *ui);
int
main(void) {
- /* Init renderer. */
- r_init();
-
/* Init microui. */
static mu_Context ctx;
mu_init(&ctx);
- ctx.text_width = text_width;
- ctx.text_height = text_height;
+ r_init(&ctx);
set_style(&ctx);
/* Init data structures. */
@@ -84,19 +78,6 @@ set_style(mu_Context *ctx) {
ctx->style->colors[MU_COLOR_SCROLLTHUMB] = COLOR_SCROLLTHUMB;
}
-static int
-text_width(mu_Font font, const char *text, int len) {
- if (len < 0) {
- len = strlen(text);
- }
- return r_get_text_width(text, len);
-}
-
-static int
-text_height(mu_Font font) {
- return r_get_text_height();
-}
-
static void
main_loop(mu_Context *ctx, UI *ui) {
for (;;) {
diff --git a/renderer.c b/renderer.c
index 4720ac0..5bbfbcb 100644
--- a/renderer.c
+++ b/renderer.c
@@ -12,6 +12,10 @@ enum window {
HEIGHT = 600,
};
+enum text {
+ TEXT_HEIGHT = 18,
+};
+
static const mu_Color COLOR_BG = {0, 0, 0, 255};
static const char button_map[256] = {
@@ -42,6 +46,27 @@ static int buf_idx;
static SDL_Window *window;
+static int text_width(mu_Font font, const char *text, int len) {
+ if (len < 0) {
+ len = strlen(text);
+ }
+
+ int res = 0;
+ for (const char *p = text; *p && len--; p++) {
+ if ((*p & 0xc0) == 0x80) { continue; }
+ int chr = mu_min((unsigned char) *p, 127);
+ res += atlas[ATLAS_FONT + chr].w;
+ }
+ return res;
+}
+
+
+static int
+text_height(mu_Font font) {
+ return TEXT_HEIGHT;
+}
+
+
static void flush(void) {
if (buf_idx == 0) { return; }
@@ -211,7 +236,7 @@ static void render_command(mu_Command *cmd) {
}
-void r_init(void) {
+void r_init(mu_Context *ctx) {
/* init SDL window */
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow(
@@ -239,6 +264,10 @@ void r_init(void) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
assert(glGetError() == 0);
+
+ /* init microui */
+ ctx->text_width = text_width;
+ ctx->text_height = text_height;
}
@@ -266,22 +295,6 @@ void r_present(void) {
}
-int r_get_text_width(const char *text, int len) {
- int res = 0;
- for (const char *p = text; *p && len--; p++) {
- if ((*p & 0xc0) == 0x80) { continue; }
- int chr = mu_min((unsigned char) *p, 127);
- res += atlas[ATLAS_FONT + chr].w;
- }
- return res;
-}
-
-
-int r_get_text_height(void) {
- return 18;
-}
-
-
void r_get_window_size(int *w, int *h) {
SDL_GetWindowSize(window, w, h);
}
diff --git a/renderer.h b/renderer.h
index 759bfd6..7d52a8a 100644
--- a/renderer.h
+++ b/renderer.h
@@ -3,7 +3,7 @@
#include "microui.h"
-void r_init(void);
+void r_init(mu_Context *ctx);
void r_handle_input(mu_Context *ctx);
void r_render(mu_Context *ctx);
void r_present(void);