From 8296fa3a8fa773adbf7544910fd12f84c278cfc3 Mon Sep 17 00:00:00 2001 From: Jack Jamison Date: Tue, 14 Oct 2025 15:32:16 -0400 Subject: text rendering which doesn't work --- array.c | 24 +++++++++++++++++++++--- array.h | 4 ++-- font.c | 1 + font.h | 1 - graphics.c | 28 ++++++++++++++++++++++++---- main.c | 6 ++++++ state.h | 1 + 7 files changed, 55 insertions(+), 10 deletions(-) diff --git a/array.c b/array.c index aa25e93..8e3a64f 100644 --- a/array.c +++ b/array.c @@ -4,9 +4,26 @@ #include "array.h" -array_info* array_new_(size_t item_size, size_t capacity) { - array_info* info = calloc(1, sizeof(array_info) + item_size * capacity); - info->capacity = capacity; +array_info* array_new_(size_t item_size, size_t size) { + // find capacity + size_t cap = 8; + if (size > cap) { + cap = size; + + // round up to next power of 2 + cap--; + cap |= cap >> 1; + cap |= cap >> 2; + cap |= cap >> 4; + cap |= cap >> 8; + cap |= cap >> 16; + cap++; + } + + // make array + array_info* info = calloc(1, sizeof(array_info) + item_size * cap); + info->size = size; + info->capacity = cap; return info; } @@ -41,3 +58,4 @@ void array_pop(void* array) { if (info->size) info->size--; } + diff --git a/array.h b/array.h index c2d908a..2383d9b 100644 --- a/array.h +++ b/array.h @@ -8,7 +8,7 @@ typedef struct { char array[]; } array_info; -array_info* array_new_(size_t item_size, size_t capacity); +array_info* array_new_(size_t item_size, size_t size); void array_print(void* array); void array_pop(void* array); void* array_add_(void* array, size_t item_size); @@ -16,7 +16,7 @@ void* array_add_(void* array, size_t item_size); #define array_clear(array) (((array_info*)(array))[-1].size = 0) #define array_size(array) (((array_info*)(array))[-1].size) -#define array_new(type, capacity) ((type*)(array_new_(sizeof(type), capacity)->array)) +#define array_new(type, size) ((type*)(array_new_(sizeof(type), size)->array)) #define array_free(array) do { \ free(((array_info*)(array)) - 1); \ diff --git a/font.c b/font.c index 371a0f9..ef95d79 100644 --- a/font.c +++ b/font.c @@ -122,6 +122,7 @@ void atlas_create_texture(client_state* state) { GL_UNSIGNED_BYTE, state->ft_face->glyph->bitmap.buffer); x += state->ft_face->glyph->bitmap.width; + metric->texture_size = (float)x / state->atlas.width; } } diff --git a/font.h b/font.h index 762fe53..71c1a12 100644 --- a/font.h +++ b/font.h @@ -2,7 +2,6 @@ #define FONT_H_ #include "state.h" - char* get_font(const char* font_name); bool freetype_init(client_state* state, const char* font); void atlas_init(client_state* state); diff --git a/graphics.c b/graphics.c index 02a6c9f..99cb457 100644 --- a/graphics.c +++ b/graphics.c @@ -1,4 +1,5 @@ #include "graphics.h" +#include "array.h" bool init_gl(client_state* state) { EGLint attrs[] = { @@ -79,11 +80,30 @@ void render_frame(client_state *state) { glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, state->atlas.texture); + // set up matrices + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0.0, (double)state->width, (double)state->height, 0.0, -1.0, 1.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + // draw text glBegin(GL_QUADS); - glTexCoord2f(0, 0); glVertex2f(-1,1); - glTexCoord2f(0, 1); glVertex2f(-1,-1); - glTexCoord2f(1, 1); glVertex2f(1,-1); - glTexCoord2f(1, 0); glVertex2f(1,1); + float pen_x = 300; + float pen_y = 200; + // TODO for unicode - check for missing characters, iterate over "unicode characters" + for ( int n = 0; n < array_size(state->input_buffer); n++ ) + { + char c = state->input_buffer[n]; + metric_t* metric = &state->atlas.metrics[(int)c]; + + glTexCoord2f(metric->texture_offset, 0.0f); glVertex2f(pen_x, pen_y); + glTexCoord2f(metric->texture_offset, 1.0f); glVertex2f(pen_x, pen_y + metric->bitmap_height); + glTexCoord2f(metric->texture_size, 1.0f); glVertex2f(pen_x + metric->bitmap_width, pen_y + metric->bitmap_height); + glTexCoord2f(metric->texture_size, 0.0f); glVertex2f(pen_x + metric->bitmap_width, pen_y); + + pen_x += metric->advance_x; + } glEnd(); glDisable(GL_TEXTURE_2D); diff --git a/main.c b/main.c index 0f7a0cb..90c2257 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include #include +#include "array.h" #include "state.h" #include "wayland.h" #include "graphics.h" @@ -13,15 +14,19 @@ void poll_events(client_state* state); int main() { client_state state = {0}; state.running = true; + state.input_buffer = array_new(char, 0); + // find font char* font = get_font("Monospace"); if (!freetype_init(&state, font)) { fprintf(stderr, "Failed to Initalize FreeType\n"); } free(font); + // create font atlas atlas_init(&state); + // start wayland state.display = wl_display_connect(NULL); if (!state.display) { fprintf(stderr, "Failed to Connect to wayland display\n"); @@ -43,6 +48,7 @@ int main() { return 1; } + // create font altas textures atlas_create_texture(&state); // event loop diff --git a/state.h b/state.h index b3f8600..3089233 100644 --- a/state.h +++ b/state.h @@ -22,6 +22,7 @@ typedef struct { float advance_x, advance_y; uint32_t bitmap_width, bitmap_height; float texture_offset; + float texture_size; } metric_t; typedef struct { -- cgit v1.2.3