From 609b57193310bb6fe37bb7c1ef5d44a706020f84 Mon Sep 17 00:00:00 2001 From: IllusionMan1212 Date: Tue, 28 Nov 2023 23:19:44 +0200 Subject: [PATCH] x11 window depthbits and render text immediately --- cudoku.c | 8 ++++++-- main.c | 1 - todo.txt | 13 ------------- ui.c | 47 ++++++++++++++++++++++------------------------- ui.h | 5 ++--- x11.c | 1 + 6 files changed, 31 insertions(+), 44 deletions(-) diff --git a/cudoku.c b/cudoku.c index 32d02a1..3bd7843 100644 --- a/cudoku.c +++ b/cudoku.c @@ -115,11 +115,15 @@ void draw_board(Cudoku *game, Size window_size) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { + if (!game->board[i][j].value) { + continue; + } Sizef text_size = calculate_text_size("7", 72.f); + char num[2]; + snprintf(num, sizeof(num), "%d", game->board[i][j].value); set_y_constraint(&constraints, i * 100 + cell_size.height / 2.f - text_size.height / 2.f, UI_CONSTRAINT_FIXED); set_x_constraint(&constraints, j * 100 + cell_size.width / 2.f - text_size.width / 2.f, UI_CONSTRAINT_FIXED); - // TODO: - draw_text("7", 72.f, constraints, &color, ALIGN_TOP_LEFT); + draw_text(num, 72.f, constraints, &color, ALIGN_TOP_LEFT); } } } diff --git a/main.c b/main.c index 8374b66..43cbf5b 100644 --- a/main.c +++ b/main.c @@ -169,7 +169,6 @@ int main(int argc, char *argv[]) { draw_win(&game); } - zephr_batch_text_draw(); zephr_swap_buffers(); } /* while (!quit) { */ diff --git a/todo.txt b/todo.txt index 0e88f11..e3a299e 100644 --- a/todo.txt +++ b/todo.txt @@ -3,19 +3,6 @@ - some kind of ui layout/hierarchy system (for later, when we actually need it) - grid texture (draw_image? or draw_texture? func) -- sooo a bit of a problem... since we render all our text in one draw call - at the end of the frame. this means that we can't have text that's rendered - in behind anything else. This needs to be fixed because drawing any overlay - will result in all the text being drawn OVER everything else, which might not be - the desired behavior at times (e.g. help/win overlay where the board numbers need to be - rendered under the overlay(s)) - - possible solutions :Clueless: - - z-index based on the order of creation (e.g. the first drawable created has the lowest z-index) - I _think_ this works because we can just chuck a global z-index whenever we call - draw_text or draw_quad/etc.. and the shader _should_ take care of everything - - every draw_text call actually makes a single draw call (compromises on performance but way easier to use) - - text rendering improvements - [ ] unicode support (allows for non-ascii characters) - unicode support removed for the foreseeable future diff --git a/ui.c b/ui.c index cb96c53..e9e664b 100644 --- a/ui.c +++ b/ui.c @@ -26,23 +26,23 @@ unsigned int ui_vao; unsigned int font_instance_vbo; unsigned int ui_vbo; -void new_text_instance_list(TextInstanceList *list, uint capacity) { +void new_glyph_instance_list(GlyphInstanceList *list, uint capacity) { list->size = 0; list->capacity = capacity; list->data = malloc(list->capacity * sizeof(TextInstance)); } -void clear_text_instance_list(TextInstanceList *list) { +void clear_glyph_instance_list(GlyphInstanceList *list) { list->size = 0; memset(list->data, 0, list->size * sizeof(TextInstance)); } -void add_text_instance(TextInstanceList *list, TextInstance instance) { +void add_glyph_instance(GlyphInstanceList *list, TextInstance instance) { if (list->size >= list->capacity) { list->capacity *= 2; TextInstance* temp = realloc(list->data, list->capacity * sizeof(TextInstance)); if (!temp) { - printf("[FATAL] Failed to reallocate memory for text instance list\n"); + printf("[FATAL] Failed to reallocate memory for glyph instance list\n"); exit(1); } list->data = temp; @@ -178,7 +178,6 @@ int init_ui(const char* font_path, Size window_size) { zephr_context.window_size = window_size; zephr_context.projection = orthographic_projection_2d(0.f, window_size.width, window_size.height, 0.f); - new_text_instance_list(&zephr_context.texts, 16); int res = init_fonts(font_path); if (res == -1) { @@ -356,25 +355,6 @@ bool zephr_should_quit() { return zephr_context.should_quit; } -/* This MUST be called at the end of the frame - if you wish to draw text */ -void zephr_batch_text_draw() { - use_shader(font_shader); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, zephr_context.font.atlas_texture_id); - glBindVertexArray(font_vao); - - glBindBuffer(GL_ARRAY_BUFFER, font_instance_vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(TextInstance) * zephr_context.texts.size, zephr_context.texts.data, GL_DYNAMIC_DRAW); - - glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL, zephr_context.texts.size); - - glBindVertexArray(0); - glBindTexture(GL_TEXTURE_2D, 0); - - clear_text_instance_list(&zephr_context.texts); -} - // This MUST be called at the end of the frame void zephr_swap_buffers() { glXSwapBuffers(display, window); @@ -676,6 +656,9 @@ void draw_text(const char* text, int font_size, UIConstraints constraints, const float first_char_bearing_w = zephr_context.font.characters[(int)text[0]].bearing.width; + GlyphInstanceList glyph_instance_list; + new_glyph_instance_list(&glyph_instance_list, 16); + // we use the original text and character sizes in the loop and then we just // scale up or down the model matrix to get the desired font size. // this way everything works out fine and we get to transform the text using the @@ -698,11 +681,25 @@ void draw_text(const char* text, int font_size, UIConstraints constraints, const memcpy(instance.model, model.m, sizeof(float[4][4])); - add_text_instance(&zephr_context.texts, instance); + add_glyph_instance(&glyph_instance_list, instance); x += (ch.advance >> 6); c++; } + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, zephr_context.font.atlas_texture_id); + glBindVertexArray(font_vao); + + glBindBuffer(GL_ARRAY_BUFFER, font_instance_vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(TextInstance) * glyph_instance_list.size, glyph_instance_list.data, GL_DYNAMIC_DRAW); + + glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL, glyph_instance_list.size); + + glBindVertexArray(0); + glBindTexture(GL_TEXTURE_2D, 0); + + free(glyph_instance_list.data); } /* enum Element { */ diff --git a/ui.h b/ui.h index f0aab3f..e334527 100644 --- a/ui.h +++ b/ui.h @@ -24,11 +24,11 @@ typedef struct TextInstance { float model[4][4]; } TextInstance; -typedef struct TextInstanceList { +typedef struct GlyphInstanceList { TextInstance *data; int size; int capacity; -} TextInstanceList; +} GlyphInstanceList; typedef struct Context { bool should_quit; @@ -36,7 +36,6 @@ typedef struct Context { Size screen_size; Size window_size; ZephFont font; - TextInstanceList texts; Matrix4x4 projection; } Context; diff --git a/x11.c b/x11.c index 81b9638..8fb99fd 100644 --- a/x11.c +++ b/x11.c @@ -64,6 +64,7 @@ int init_x11(Display **display, Window *window, const char* title, int window_wi GLint visual_attributes[] = { GLX_RENDER_TYPE, GLX_RGBA_BIT, + GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, 1, GLX_SAMPLES, 4, // MSAA None