From 0e474ad6af0940de52deb6a45c2b04400486aaad Mon Sep 17 00:00:00 2001 From: bjorn Date: Tue, 1 Aug 2017 19:58:24 -0700 Subject: [PATCH] Reorganize lovrFontPrint; --- src/graphics/font.c | 33 ++++++++++----------------------- src/graphics/font.h | 3 +-- src/graphics/graphics.c | 13 ++++++++++++- src/graphics/graphics.h | 2 +- 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/graphics/font.c b/src/graphics/font.c index bb9a976b..173b3447 100644 --- a/src/graphics/font.c +++ b/src/graphics/font.c @@ -30,7 +30,6 @@ Font* lovrFontCreate(FontData* fontData) { font->texture = NULL; font->lineHeight = 1.f; font->pixelDensity = font->fontData->height; - vec_init(&font->vertices); map_init(&font->kerning); // Atlas @@ -62,11 +61,10 @@ void lovrFontDestroy(const Ref* ref) { lovrRelease(&font->texture->ref); map_deinit(&font->atlas.glyphs); map_deinit(&font->kerning); - vec_deinit(&font->vertices); free(font); } -void lovrFontPrint(Font* font, const char* str, mat4 transform, float wrap, HorizontalAlign halign, VerticalAlign valign) { +void lovrFontRender(Font* font, const char* str, float wrap, HorizontalAlign halign, VerticalAlign valign, vec_float_t* vertices, float* offsety) { FontAtlas* atlas = &font->atlas; float cx = 0; @@ -85,14 +83,14 @@ void lovrFontPrint(Font* font, const char* str, mat4 transform, float wrap, Hori int linePtr = 0; int lineCount = 1; - vec_reserve(&font->vertices, len * 30); - vec_clear(&font->vertices); + vec_reserve(vertices, len * 30); + vec_clear(vertices); while ((bytes = utf8_decode(str, end, &codepoint)) > 0) { // Newlines if (codepoint == '\n' || (wrap && cx * scale > wrap && codepoint == ' ')) { - linePtr = lovrFontAlignLine(&font->vertices, linePtr, cx, halign); + linePtr = lovrFontAlignLine(vertices, linePtr, cx, halign); lineCount++; cx = 0; cy -= font->fontData->height * font->lineHeight; @@ -110,7 +108,7 @@ void lovrFontPrint(Font* font, const char* str, mat4 transform, float wrap, Hori // Start over if texture was repacked if (u != atlas->width || v != atlas->height) { - lovrFontPrint(font, start, transform, wrap, halign, valign); + lovrFontRender(font, start, wrap, halign, valign, vertices, offsety); return; } @@ -125,7 +123,7 @@ void lovrFontPrint(Font* font, const char* str, mat4 transform, float wrap, Hori float s2 = (glyph->x + glyph->tw) / u; float t2 = glyph->y / v; - float vertices[30] = { + float quad[30] = { x1, y1, 0, s1, t1, x1, y2, 0, s1, t2, x2, y1, 0, s2, t1, @@ -134,7 +132,7 @@ void lovrFontPrint(Font* font, const char* str, mat4 transform, float wrap, Hori x2, y2, 0, s2, t2 }; - vec_pusharr(&font->vertices, vertices, 30); + vec_pusharr(vertices, quad, 30); } // Advance cursor @@ -143,25 +141,14 @@ void lovrFontPrint(Font* font, const char* str, mat4 transform, float wrap, Hori } // Align the last line - lovrFontAlignLine(&font->vertices, linePtr, cx, halign); + lovrFontAlignLine(vertices, linePtr, cx, halign); // Calculate vertical offset - float offsety = 0; if (valign == ALIGN_MIDDLE) { - offsety = lineCount * font->fontData->height * font->lineHeight * .5f; + *offsety = lineCount * font->fontData->height * font->lineHeight * .5f; } else if (valign == ALIGN_BOTTOM) { - offsety = lineCount * font->fontData->height * font->lineHeight; + *offsety = lineCount * font->fontData->height * font->lineHeight; } - - // Render! - lovrGraphicsPush(); - lovrGraphicsMatrixTransform(transform); - lovrGraphicsScale(scale, scale, scale); - lovrGraphicsTranslate(0, offsety, 0); - lovrGraphicsBindTexture(font->texture); - lovrGraphicsSetShapeData(font->vertices.data, font->vertices.length); - lovrGraphicsDrawPrimitive(GL_TRIANGLES, 0, 1, 0); - lovrGraphicsPop(); } float lovrFontGetWidth(Font* font, const char* str, float wrap) { diff --git a/src/graphics/font.h b/src/graphics/font.h index eb653555..f562bd45 100644 --- a/src/graphics/font.h +++ b/src/graphics/font.h @@ -36,14 +36,13 @@ typedef struct { Texture* texture; FontAtlas atlas; map_int_t kerning; - vec_float_t vertices; float lineHeight; float pixelDensity; } Font; Font* lovrFontCreate(FontData* fontData); void lovrFontDestroy(const Ref* ref); -void lovrFontPrint(Font* font, const char* str, mat4 transform, float wrap, HorizontalAlign halign, VerticalAlign valign); +void lovrFontRender(Font* font, const char* str, float wrap, HorizontalAlign halign, VerticalAlign valign, vec_float_t* vertices, float* offsety); float lovrFontGetWidth(Font* font, const char* string, float wrap); float lovrFontGetHeight(Font* font); float lovrFontGetAscent(Font* font); diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index 75904f64..29f5eac6 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -999,7 +999,18 @@ void lovrGraphicsPrint(const char* str, mat4 transform, float wrap, HorizontalAl } lovrGraphicsEnsureFont(); - lovrFontPrint(state.activeFont, str, transform, wrap, halign, valign); + Font* font = state.activeFont; + float scale = 1 / font->pixelDensity; + float offsety; + lovrFontRender(font, str, wrap, halign, valign, &state.shapeData, &offsety); + + lovrGraphicsPush(); + lovrGraphicsMatrixTransform(transform); + lovrGraphicsScale(scale, scale, scale); + lovrGraphicsTranslate(0, offsety, 0); + lovrGraphicsBindTexture(font->texture); + lovrGraphicsDrawPrimitive(GL_TRIANGLES, 0, 1, 0); + lovrGraphicsPop(); if (lastShader == state.defaultShader) { lovrGraphicsSetShader(lastShader); diff --git a/src/graphics/graphics.h b/src/graphics/graphics.h index 883b66aa..6dc1d884 100644 --- a/src/graphics/graphics.h +++ b/src/graphics/graphics.h @@ -168,4 +168,4 @@ void lovrGraphicsBox(DrawMode mode, Texture* texture, mat4 transform); void lovrGraphicsCylinder(float x1, float y1, float z1, float x2, float y2, float z2, float r1, float r2, int capped, int segments); void lovrGraphicsSphere(Texture* texture, mat4 transform, int segments); void lovrGraphicsSkybox(Skybox* skybox, float angle, float ax, float ay, float az); -void lovrGraphicsPrint(const char* str, mat4 transform, float wrap, HorizontalAlign halign, VerticalAlign valign); +void lovrGraphicsPrint(const char* str, mat4 transform, float wrap, HorizontalAlign halign, VerticalAlign valign);