aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--font.c12
-rw-r--r--graphics.c43
-rw-r--r--state.h1
3 files changed, 40 insertions, 16 deletions
diff --git a/font.c b/font.c
index dd65a6a..dc8f03c 100644
--- a/font.c
+++ b/font.c
@@ -70,6 +70,7 @@ void atlas_init(client_state* state) {
if (FT_Load_Char(state->ft_face, i, state->load_flags)) {
fprintf(stderr, "Failed to render character %c\n", (char)i);
}
+
state->atlas.width += state->ft_face->glyph->bitmap.width;
state->atlas.height = MAX(state->atlas.height, state->ft_face->glyph->bitmap.rows);
@@ -80,6 +81,11 @@ void atlas_init(client_state* state) {
metric->bitmap_height = state->ft_face->glyph->bitmap.rows;
metric->bearing_x = state->ft_face->glyph->bitmap_left;
metric->bearing_y = state->ft_face->glyph->bitmap_top;
+
+ state->atlas.vert_shift = MAX(state->atlas.vert_shift, metric->bearing_y - metric->bitmap_height);
+
+ // printf("%c: advance: (%f, %f), bitmap: (%u, %u), bearing: (%d, %d)\n",
+ // (char)i, metric->advance_x, metric->advance_y, metric->bitmap_width, metric->bitmap_height, metric->bearing_x, metric->bearing_y);
}
}
@@ -120,12 +126,12 @@ void atlas_create_texture(client_state* state) {
0,
x,
0,
- state->ft_face->glyph->bitmap.width,
- state->ft_face->glyph->bitmap.rows,
+ metric->bitmap_width,
+ metric->bitmap_height,
GL_RED,
GL_UNSIGNED_BYTE,
state->ft_face->glyph->bitmap.buffer);
- x += state->ft_face->glyph->bitmap.width;
+ x += metric->bitmap_width;
metric->texture_x_end = (float)x / state->atlas.width;
}
}
diff --git a/graphics.c b/graphics.c
index d4fcb10..03122a7 100644
--- a/graphics.c
+++ b/graphics.c
@@ -23,9 +23,13 @@ uniform vec2 u_offset;\n\
\n\
out vec2 o_uv;\n\
\n\
+vec2 project(vec2 point) { \n\
+ return (((2.0 * point) / u_screen_size) - vec2(1)); \n\
+} \n\
+\n\
void main() {\n\
o_uv = uv;\n\
- gl_Position = vec4((((2.0 * (pos + u_offset)) / u_screen_size) - vec2(1.0)), 0.0, 1.0);\n\
+ gl_Position = vec4(project(pos + u_offset), 0.0, 1.0);\n\
}";
const char* fragmentShader = "#version 330 core\n\
@@ -136,7 +140,7 @@ void render_frame(client_state *state) {
// bind shader and draw
glUseProgram(state->text_shader);
glUniform2f(state->screen_size_uniform, state->width, state->height);
- glUniform2f(state->offset_uniform, 0.0f, state->line_height * (state->lines + 0.15f));
+ glUniform2f(state->offset_uniform, 0.0f, state->line_height * state->lines - state->atlas.vert_shift);
glDrawElements(GL_TRIANGLES, state->input_buffer_text.num_elements, GL_UNSIGNED_INT, 0);
// present screen
@@ -171,20 +175,33 @@ void init_text_buffer(client_state* state, text_buffer_t* buffer, char* text, si
uint32_t base_idx = n * 6;
// bottom left vert
- vert_t v = { .x=start_x, .y=start_y, .u=m->texture_x_start, .v=0.0f};
- vertices[base_vert + 0] = v;
+ vertices[base_vert + 0] = (vert_t){
+ .x=start_x,
+ .y=start_y,
+ .u=m->texture_x_start,
+ .v=0.0f,
+ };
// top left vert
- v.y -= m->bitmap_height;
- v.v = ((float)m->bitmap_height / state->atlas.height);
- vertices[base_vert + 1] = v;
+ vertices[base_vert + 1] = (vert_t){
+ .x=start_x,
+ .y=start_y - m->bitmap_height,
+ .u=m->texture_x_start,
+ .v=(m->bitmap_height / (float)state->atlas.height),
+ };
// top right vert
- v.x += m->bitmap_width;
- v.u = m->texture_x_end;
- vertices[base_vert + 2] = v;
+ vertices[base_vert + 2] = (vert_t){
+ .x=start_x + m->bitmap_width,
+ .y=start_y - m->bitmap_height,
+ .u=m->texture_x_end,
+ .v=(m->bitmap_height / (float)state->atlas.height),
+ };
// bottom right vert
- v.y = start_y;
- v.v = 0.0f;
- vertices[base_vert + 3] = v;
+ vertices[base_vert + 3] = (vert_t){
+ .x=start_x + m->bitmap_width,
+ .y=start_y,
+ .u=m->texture_x_end,
+ .v=0,
+ };
// indices
indices[base_idx + 0] = base_vert + 0;
diff --git a/state.h b/state.h
index 242970e..87c3126 100644
--- a/state.h
+++ b/state.h
@@ -35,6 +35,7 @@ typedef struct {
typedef struct {
uint32_t width, height;
GLuint texture;
+ int32_t vert_shift;
metric_t metrics[128];
} atlas_t;