summaryrefslogtreecommitdiff
path: root/text.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'text.cpp')
-rw-r--r--text.cpp56
1 files changed, 18 insertions, 38 deletions
diff --git a/text.cpp b/text.cpp
index c3c3de4..1db0226 100644
--- a/text.cpp
+++ b/text.cpp
@@ -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) {