replace font scale parameters with standardized font size

This commit is contained in:
2023-12-11 23:00:31 +02:00
committed by Illusion
parent 3ab33f3f84
commit 307b44d7e4
3 changed files with 15 additions and 14 deletions
+3 -3
View File
@@ -59,9 +59,9 @@ void draw_win() {
/* set_height_constraint(&constraints, window.height, UI_CONSTRAINT_FIXED); */ /* set_height_constraint(&constraints, window.height, UI_CONSTRAINT_FIXED); */
Alignment align = ALIGN_TOP_RIGHT; Alignment align = ALIGN_TOP_RIGHT;
float scale = 1.0f; int font_size = 72.0f;
Sizef text_size = calculate_text_size(win_text, scale); Sizef text_size = calculate_text_size(win_text, font_size);
set_x_constraint(&constraints, 0, 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);
@@ -70,7 +70,7 @@ void draw_win() {
draw_quad(constraints, &bg_color, align); draw_quad(constraints, &bg_color, align);
draw_text(win_text, scale, (Vec2f){0.0, 0.0}, &text_color, align); draw_text(win_text, font_size, (Vec2f){0.0, 0.0}, &text_color, align);
} }
void set_scale_factor(int width, int height, float *x, float *y) { void set_scale_factor(int width, int height, float *x, float *y) {
+10 -9
View File
@@ -212,7 +212,8 @@ Size zephr_get_window_size() {
return zephr_context.window_size; return zephr_context.window_size;
} }
Sizef calculate_text_size(const char *text, float scale) { Sizef calculate_text_size(const char *text, int font_size) {
float scale = (float)font_size / 128.f;
Sizef size = { .width = 0, .height = 0 }; Sizef size = { .width = 0, .height = 0 };
int w = 0; int w = 0;
int h = 0; int h = 0;
@@ -396,8 +397,7 @@ void draw_quad(UIConstraints constraints, Color *color, Alignment align) {
glBindVertexArray(0); glBindVertexArray(0);
} }
// TODO: remove scaling and replace it with standardized font sizes with 16.0f being the default void draw_text(const char* text, int font_size, Vec2f pos, Color *color, Alignment alignment) {
void draw_text(const char* text, float scale, Vec2f pos, Color *color, Alignment alignment) {
use_shader(font_shader); use_shader(font_shader);
if (color) { if (color) {
set_vec4f(font_shader, "textColor", color->r / 255.f, color->g / 255.f, color->b / 255.f, color->a / 255.f); set_vec4f(font_shader, "textColor", color->r / 255.f, color->g / 255.f, color->b / 255.f, color->a / 255.f);
@@ -414,7 +414,8 @@ void draw_text(const char* text, float scale, Vec2f pos, Color *color, Alignment
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindVertexArray(font_vao); glBindVertexArray(font_vao);
Sizef text_size = calculate_text_size(text, 1.0); Sizef text_size = calculate_text_size(text, 128.0);
float font_scale = (float)font_size / 128.f;
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++) {
@@ -431,15 +432,15 @@ void draw_text(const char* text, float scale, Vec2f pos, Color *color, Alignment
// 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)) * scale; float xpos = (x + (ch.bearing.width - first_char_bearing_w)) * font_scale;
float ypos = (text_size.height - ch.bearing.height - (text_size.height - max_bearing_h)) * scale; float ypos = (text_size.height - ch.bearing.height - (text_size.height - max_bearing_h)) * font_scale;
Vec2f final_pos = { xpos, ypos }; Vec2f final_pos = { xpos, ypos };
float w = (ch.size.width * scale); float w = (ch.size.width * font_scale);
float h = (ch.size.height * scale); float h = (ch.size.height * font_scale);
apply_alignment(alignment, &final_pos, (Sizef){ text_size.width * scale, text_size.height * 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] = {
// top left tri // top left tri
+2 -2
View File
@@ -82,5 +82,5 @@ void set_y_constraint(UIConstraints *constraints, float value, UIConstraint type
void set_width_constraint(UIConstraints *constraints, float value, UIConstraint type); void set_width_constraint(UIConstraints *constraints, float value, UIConstraint type);
void set_height_constraint(UIConstraints *constraints, float value, UIConstraint type); void set_height_constraint(UIConstraints *constraints, float value, UIConstraint type);
void draw_quad(UIConstraints constraints, Color *color, Alignment align); void draw_quad(UIConstraints constraints, Color *color, Alignment align);
void draw_text(const char* text, float scale, Vec2f pos, Color *color, Alignment align); void draw_text(const char* text, int font_size, Vec2f pos, Color *color, Alignment align);
Sizef calculate_text_size(const char *text, float scale); Sizef calculate_text_size(const char *text, int font_size);