diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-12-08 16:11:55 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-12-08 16:11:55 -0500 |
commit | 8b1bb7223af179e0bb6d2321165f4274f23073bd (patch) | |
tree | f9badf1f832ed018591c84cd3268ba55285cfe2f | |
parent | 27ed9515a369d86597d347e377e1353afb6bd975 (diff) |
text rendering working
-rw-r--r-- | sim.cpp | 21 | ||||
-rw-r--r-- | sim.hpp | 2 | ||||
-rw-r--r-- | text.cpp | 56 |
3 files changed, 38 insertions, 41 deletions
@@ -345,7 +345,7 @@ void sim_frame(Frame *frame) { state->platform_thickness = 0.005f; state->bottom_y = 0.1f; - text_font_load(state, &state->font, "assets/font.ttf", 48.0f); + text_font_load(state, &state->font, "assets/font.ttf", 36.0f); ball->radius = 0.02f; ball->pos = V2(0.5f, 0.8f); @@ -408,6 +408,7 @@ void sim_frame(Frame *frame) { #endif b2World *world = state->world; + Font *font = &state->font; // simulate physics { @@ -425,9 +426,10 @@ void sim_frame(Frame *frame) { if (ball->pos.y - ball->radius < state->bottom_y) { // oh no! ball reached bottom line - printf("Score: %f m\n", ball_pos.x); world->DestroyBody(ball->body); ball->body = NULL; + } else { + state->distance_traveled = ball_pos.x; } } for (Platform *platform = state->platforms, *end = platform + state->nplatforms; platform != end; ++platform) { @@ -456,9 +458,22 @@ void sim_frame(Frame *frame) { glVertex2f(-1, line_pos.y); glVertex2f(+1, line_pos.y); glEnd(); + glBegin(GL_QUADS); + glColor4f(1,0,0,0.1f); + glVertex2f(-1, line_pos.y); + glVertex2f(-1, -1); + glVertex2f(+1, -1); + glVertex2f(+1, line_pos.y); + glEnd(); } - text_render2f(state, &state->font, "Hello", 0, 0); + { + char text[256] = {0}; + glColor3f(0.8f,0.5f,1); + snprintf(text, sizeof text - 1, "Distance: %.2f m", state->distance_traveled); + v2 size = text_get_size(state, font, text); + text_render(state, font, text, v2_sub(V2(0.95f, 0.95f), size)); + } #if DEBUG GLuint error = glGetError(); @@ -156,6 +156,8 @@ typedef struct { Ball ball; float bottom_y; // if y goes below here, it's over + float distance_traveled; + Font font; float platform_thickness; @@ -81,20 +81,18 @@ static bool text_font_load(State *state, Font *font, char const *filename, float return success; } -static void text_render_(State *state, Font *font, char const *s, float *xp, float *yp, bool render, float clip_left) { +static void text_render_(State *state, Font *font, char const *s, float *xp, float *yp, bool render) { if (!font->char_height) return; // font not loaded properly stbtt_bakedchar *cdata = font->char_data; int tex_w = font->tex_width, tex_h = font->tex_height; float x = *xp, y = *yp; - // stb_truetype uses a pixel-based top-is-y=0 coordinate system, so we need to convert back and forth - float resolution = (float)state->win_height; - float inv_resolution = 1 / resolution; + float widthf = (float)state->win_width, heightf = (float)state->win_height; + float inv_widthf = 1.0f / widthf, inv_heightf = 1.0f / heightf; - - x *= resolution; - y = 1 - y; - y *= resolution; + // convert between GL and stb_truetype coordinates + x = (x + 1) * 0.5f * widthf; + y = (1 - (y + 1) * 0.5f) * heightf; glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, font->texture); @@ -105,28 +103,10 @@ static void text_render_(State *state, Font *font, char const *s, float *xp, flo stbtt_aligned_quad q = {}; stbtt_GetBakedQuad(cdata, tex_w, tex_h, c - 32, &x, &y, &q, 1); if (render) { - q.x0 *= inv_resolution; - q.y0 *= inv_resolution; - q.x1 *= inv_resolution; - q.y1 *= inv_resolution; - q.y0 = 1 - q.y0; - q.y1 = 1 - q.y1; -#if 1 - if (q.x1 < clip_left) { - // entirety of character is to the left of clip_left. - } else if (q.x0 < clip_left) { - // part of character is to the left of clip_left - float char_width = q.x1 - q.x0; // full width of character - float display_char_width = q.x1 - clip_left; // width of character which is being rendered, after clipping - float char_fraction = display_char_width / char_width; // fraction of character rendered - float s0 = q.s1 - char_fraction * (q.s1 - q.s0); // calculate where to start from - float x0 = clip_left; - glTexCoord2f( s0, q.t0); glVertex2f( x0, q.y0); - glTexCoord2f(q.s1, q.t0); glVertex2f(q.x1, q.y0); - glTexCoord2f(q.s1, q.t1); glVertex2f(q.x1, q.y1); - glTexCoord2f( s0, q.t1); glVertex2f( x0, q.y1); - } else -#endif + q.x0 = (q.x0 * inv_widthf) * 2 - 1; + q.x1 = (q.x1 * inv_widthf) * 2 - 1; + q.y0 = (1 - q.y0 * inv_heightf) * 2 - 1; + q.y1 = (1 - q.y1 * inv_heightf) * 2 - 1; { // no clipping #if 0 @@ -142,25 +122,25 @@ static void text_render_(State *state, Font *font, char const *s, float *xp, flo } glEnd(); glDisable(GL_TEXTURE_2D); - *xp = x * inv_resolution; - *yp = 1 - y * inv_resolution; + *xp = (x * inv_widthf) * 2 - 1; + *yp = (1 - y * inv_heightf) * 2 - 1; } static void text_render2f(State *state, Font *font, char const *s, float x, float y) { - text_render_(state, font, s, &x, &y, true, 0); + text_render_(state, font, s, &x, &y, true); } static void text_render(State *state, Font *font, char const *s, v2 pos) { - text_render_(state, font, s, &pos.x, &pos.y, true, 0); + text_render_(state, font, s, &pos.x, &pos.y, true); } -static float text_font_char_height(Font *font) { - return font->char_height * 0.666667f; +static float text_font_char_height(State *state, Font *font) { + return font->char_height / (float)state->win_width * 1.3333333f; } static v2 text_get_size(State *state, Font *font, char const *s) { float x = 0, y = 0; - text_render_(state, font, s, &x, &y, false, 0); - return V2(x, y + text_font_char_height(font)); + text_render_(state, font, s, &x, &y, false); + return V2(x, y + text_font_char_height(state, font)); } static void text_render_centered(State *state, Font *font, char const *s, v2 center) { |