x11 window depthbits and render text immediately

This commit is contained in:
2023-11-28 23:19:44 +02:00
parent 1ed67cb62a
commit 5dd067e03b
6 changed files with 31 additions and 44 deletions
+6 -2
View File
@@ -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);
}
}
}
-1
View File
@@ -169,7 +169,6 @@ int main(int argc, char *argv[]) {
draw_win(&game);
}
zephr_batch_text_draw();
zephr_swap_buffers();
}
/* while (!quit) { */
-13
View File
@@ -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
+22 -25
View File
@@ -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 { */
+2 -3
View File
@@ -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;
+1
View File
@@ -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