better text positioning which fixes rotations and scaling

This commit is contained in:
2023-12-11 23:00:31 +02:00
committed by Illusion
parent 212c7b9d5a
commit e44f7fb078
3 changed files with 22 additions and 28 deletions
+5 -5
View File
@@ -58,15 +58,15 @@ void draw_win() {
/* set_width_constraint(&constraints, window.width, UI_CONSTRAINT_FIXED); */ /* set_width_constraint(&constraints, window.width, UI_CONSTRAINT_FIXED); */
/* set_height_constraint(&constraints, window.height, UI_CONSTRAINT_FIXED); */ /* set_height_constraint(&constraints, window.height, UI_CONSTRAINT_FIXED); */
Alignment align = ALIGN_CENTER; Alignment align = ALIGN_TOP_RIGHT;
int font_size = 72.0f; int font_size = 72.0f;
Sizef text_size = calculate_text_size(win_text, font_size); Sizef text_size = calculate_text_size(win_text, font_size);
set_x_constraint(&constraints, 50, UI_CONSTRAINT_FIXED); set_x_constraint(&constraints, 0, UI_CONSTRAINT_FIXED);
set_y_constraint(&constraints, 0, UI_CONSTRAINT_FIXED); set_y_constraint(&constraints, 0, UI_CONSTRAINT_FIXED);
set_width_constraint(&constraints, 50, UI_CONSTRAINT_FIXED); set_width_constraint(&constraints, text_size.width, UI_CONSTRAINT_FIXED);
set_height_constraint(&constraints, 50, UI_CONSTRAINT_FIXED); set_height_constraint(&constraints, text_size.height, UI_CONSTRAINT_FIXED);
UIConstraints circle_const; UIConstraints circle_const;
set_x_constraint(&circle_const, 50, UI_CONSTRAINT_FIXED); set_x_constraint(&circle_const, 50, UI_CONSTRAINT_FIXED);
@@ -75,7 +75,7 @@ void draw_win() {
set_height_constraint(&circle_const, 90, UI_CONSTRAINT_FIXED); set_height_constraint(&circle_const, 90, UI_CONSTRAINT_FIXED);
/* draw_circle(circle_const, NULL, ALIGN_LEFT_CENTER); */ /* draw_circle(circle_const, NULL, ALIGN_LEFT_CENTER); */
/* draw_quad(constraints, &bg_color, 0.0, ALIGN_TOP_LEFT); */ draw_quad(constraints, &bg_color, 0.0, align);
/* set_x_constraint(&constraints, -50, UI_CONSTRAINT_FIXED); */ /* set_x_constraint(&constraints, -50, UI_CONSTRAINT_FIXED); */
/* draw_quad(constraints, &bg_color, 25.0, ALIGN_TOP_RIGHT); */ /* draw_quad(constraints, &bg_color, 25.0, ALIGN_TOP_RIGHT); */
/* set_x_constraint(&constraints, 0, UI_CONSTRAINT_FIXED); */ /* set_x_constraint(&constraints, 0, UI_CONSTRAINT_FIXED); */
-2
View File
@@ -1,5 +1,3 @@
- rotations/scaling for text (fix the code for it)
- maybe force the window to be a specific resolution - maybe force the window to be a specific resolution
- border decorations for shapes - border decorations for shapes
- look into SDF/vector text - look into SDF/vector text
+17 -21
View File
@@ -469,15 +469,21 @@ void draw_text(const char* text, int font_size, Vec2f pos, Color *color, Alignme
set_vec4f(font_shader, "textColor", 0.f, 0.f, 0.f, 1.f); set_vec4f(font_shader, "textColor", 0.f, 0.f, 0.f, 1.f);
} }
set_mat4f(font_shader, "projection", (float *)zephr_context.projection.m); set_mat4f(font_shader, "projection", (float *)zephr_context.projection.m);
Sizef text_size = calculate_text_size(text, FONT_PIXEL_SIZE);
float font_scale = (float)font_size / FONT_PIXEL_SIZE;
apply_alignment(alignment, &pos, (Sizef){ text_size.width * font_scale, text_size.height * font_scale });
Matrix4x4 model = identity(); Matrix4x4 model = identity();
model.m[0][0] = font_scale;
model.m[1][1] = font_scale;
apply_rotation(&model, to_radians(0));
model.m[3][0] = pos.x; model.m[3][0] = pos.x;
model.m[3][1] = pos.y; model.m[3][1] = pos.y;
set_mat4f(font_shader, "model", (float *)model.m); set_mat4f(font_shader, "model", (float *)model.m);
Sizef text_size = calculate_text_size(text, FONT_PIXEL_SIZE);
float font_scale = (float)font_size / FONT_PIXEL_SIZE;
int max_bearing_h = 0; int max_bearing_h = 0;
for (uint i = 0; i < strlen(text); i++) { for (uint i = 0; i < strlen(text); i++) {
Character ch = characters[(int)text[i]]; Character ch = characters[(int)text[i]];
@@ -493,29 +499,19 @@ void draw_text(const char* text, int font_size, Vec2f pos, Color *color, Alignme
int x = 0; int x = 0;
while (text[c] != '\0') { while (text[c] != '\0') {
Character ch = characters[(int)text[c]]; Character ch = characters[(int)text[c]];
// subtract the bearing width of the first character to remove the extra space // subtract the bearing width of the first character to remove the extra space
// at the start of the text and move every char to the left by that width // at the start of the text and move every char to the left by that width
float xpos = (x + (ch.bearing.width - first_char_bearing_w)) * font_scale; float xpos = (x + (ch.bearing.width - first_char_bearing_w));
float ypos = (text_size.height - ch.bearing.height - (text_size.height - max_bearing_h));
float ypos = (text_size.height - ch.bearing.height - (text_size.height - max_bearing_h)) * font_scale;
Vec2f final_pos = { xpos, ypos };
float w = (ch.size.width * font_scale);
float h = (ch.size.height * font_scale);
apply_alignment(alignment, &final_pos, (Sizef){ text_size.width * font_scale, text_size.height * font_scale });
float vertices[6][4] = { float vertices[6][4] = {
// bottom left tri {xpos, ypos + ch.size.height, 0.0, 1.0},
{final_pos.x, final_pos.y + h, 0.0, 1.0}, {xpos, ypos, 0.0, 0.0},
{final_pos.x, final_pos.y, 0.0, 0.0}, {xpos + ch.size.width, ypos, 1.0, 0.0},
{final_pos.x + w, final_pos.y, 1.0, 0.0},
// top right tri {xpos, ypos + ch.size.height, 0.0, 1.0},
{final_pos.x, final_pos.y + h, 0.0, 1.0}, {xpos + ch.size.width, ypos, 1.0, 0.0},
{final_pos.x + w, final_pos.y, 1.0, 0.0}, {xpos + ch.size.width, ypos + ch.size.height, 1.0, 1.0},
{final_pos.x + w, final_pos.y + h, 1.0, 1.0},
}; };
glBindTexture(GL_TEXTURE_2D, ch.texture_id); glBindTexture(GL_TEXTURE_2D, ch.texture_id);