From 79b7aa06baedad381c1897fce9c8607121255ee8 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 25 May 2021 20:04:18 -0700 Subject: [PATCH] Temporarily fixed font wrapping by removing scaling. --- include/dawn/display/gui/font.h | 4 +- src/display/gui/font.c | 92 ++++++--------------------------- src/display/gui/font.h | 10 +--- src/poker/render/talk.c | 21 ++++++-- src/poker/render/talk.h | 3 ++ 5 files changed, 40 insertions(+), 90 deletions(-) diff --git a/include/dawn/display/gui/font.h b/include/dawn/display/gui/font.h index 9cad8aa8..c19678d7 100644 --- a/include/dawn/display/gui/font.h +++ b/include/dawn/display/gui/font.h @@ -16,7 +16,7 @@ #define FONT_NUM_CHARS 96 /** Passed to STBTT for scaling the font nicely */ -#define FONT_TEXTURE_SIZE 64 +#define FONT_TEXTURE_SIZE 24 /** Width of the loaded font texture */ #define FONT_TEXTURE_WIDTH 512 @@ -54,7 +54,7 @@ typedef struct { int32_t lines; /** Dimensions of the string */ - float width, height, scale; + float width, height; /** Array of precalculated quads */ stbtt_aligned_quad *quads; diff --git a/src/display/gui/font.c b/src/display/gui/font.c index 7746e562..0fa6debd 100644 --- a/src/display/gui/font.c +++ b/src/display/gui/font.c @@ -45,66 +45,8 @@ void fontDispose(font_t *font) { } -fonttextinfo_t * fontGetTextInfo(font_t *font, char *text, float scale) { - int32_t i, j; - char c; - float x, y; - stbtt_aligned_quad *quad; - - // Create info buffer. - fonttextinfo_t *info = malloc(sizeof(fonttextinfo_t)); - - info->length = 0; - info->realLength = 0; - - // Count how many "real characters" are in the string. - i = 0; - while(c = text[i++]) { - info->length++; - if(c == FONT_SPACE || c == FONT_NEWLINE) continue; - info->realLength++; - } - - // Now setup the size scales - info->quads = malloc(sizeof(stbtt_aligned_quad) * info->realLength); - info->scale = scale * FONT_SCALE_ADJUST; - info->width = 0, info->height = 0; - info->lines = 1; - - // Begin buffering quads. - x = 0; - y = FONT_INITIAL_LINE; - i = 0, j = 0; - - while(c = text[i++]) { - // Spaces, don't quad just next - if(c == FONT_SPACE) { - x += FONT_SPACE_SIZE; - continue; - } - - // Newlines reset X - if(c == FONT_NEWLINE) { - info->lines++; - y += FONT_LINE_HEIGHT; - x = 0; - continue; - } - - // Calculate the quad of the character, store into the array. - quad = info->quads + j; - stbtt_GetBakedQuad(font->characterData, - FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT, - ((int32_t)c)-FONT_FIRST_CHAR, &x, &y, quad, FONT_FILL_MODE - ); - quad->x0 *= info->scale, quad->x1 *= info->scale; - quad->y0 *= info->scale, quad->y1 *= info->scale; - info->width = mathMax(info->width, quad->x1); - info->height = mathMax(info->height, quad->y1); - j++; - } - - return info; +fonttextinfo_t * fontGetTextInfo(font_t *font, char *text) { + return fontTextClamp(font, text, 999999); } void fontTextInfoDispose(fonttextinfo_t *info) { @@ -135,9 +77,7 @@ void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info) { } -fonttextinfo_t * fontTextClamp(font_t *font, char *text, float scale, - float maxWidth -) { +fonttextinfo_t * fontTextClamp(font_t *font, char *text, float maxWidth) { // This method is similar to fontGetTextInfo, but will modify the text. int32_t i, j, wordIndex; char c; @@ -155,7 +95,6 @@ fonttextinfo_t * fontTextClamp(font_t *font, char *text, float scale, // Setup Scales info->length = 0; info->realLength = 0; - info->scale = scale * FONT_SCALE_ADJUST; info->lines = 1; // Unlike GetTextInfo we can actually calculate the quads at the same time @@ -170,22 +109,24 @@ fonttextinfo_t * fontTextClamp(font_t *font, char *text, float scale, // When space, start of new word about to begin if(c == FONT_SPACE) { x += FONT_SPACE_SIZE; + // Did this space cause a newline? if(x > maxWidth) { info->lines++; - wordStart = i+1; y += FONT_LINE_HEIGHT; x = 0; } + // info->width = mathMax(info->width, x); + // info->height = mathMax(info->height, y); wordX = x; - wordStart = i+1; + wordStart = info->realLength; continue; } // New line. if(c == FONT_NEWLINE) { info->lines++; - wordStart = i+1; + wordStart = info->realLength; y += FONT_LINE_HEIGHT; x = 0; continue; @@ -205,24 +146,21 @@ fonttextinfo_t * fontTextClamp(font_t *font, char *text, float scale, FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT, ((int32_t)c)-FONT_FIRST_CHAR, &x, &y, quad, FONT_FILL_MODE ); - quad->x0 *= info->scale, quad->x1 *= info->scale; - quad->y0 *= info->scale, quad->y1 *= info->scale; - + // Now measure the width of the line (take the right side of that quad) - if(quad->x1 > maxWidth) { + if(x > maxWidth) { // We've exceeded the edge, go back to the start of the word and newline. - info->lines++; - for(j = wordStart; j <= i; j++) { + x = quad->x1 - wordX; + for(j = wordStart; j <= info->realLength; j++) { quad = info->quads + j; quad->x0 -= wordX, quad->x1 -= wordX; quad->y0 += FONT_LINE_HEIGHT, quad->y1 += FONT_LINE_HEIGHT; - x = 0; - y += FONT_LINE_HEIGHT; } + y += FONT_LINE_HEIGHT; + info->lines++; + wordX = 0; } - info->width = mathMax(info->width, quad->x1); - info->height = mathMax(info->height, quad->y1); info->realLength++; } diff --git a/src/display/gui/font.h b/src/display/gui/font.h index 20033acd..c7dacb8c 100644 --- a/src/display/gui/font.h +++ b/src/display/gui/font.h @@ -32,10 +32,9 @@ void fontDispose(font_t *Font); * * @param font Font to use for rendering and measuring * @param text Text to measure/render. - * @param scale Scale of the text. * @returns Font info calculated. */ -fonttextinfo_t * fontGetTextInfo(font_t *font, char *text, float scale); +fonttextinfo_t * fontGetTextInfo(font_t *font, char *text); /** * Disposes a previously calculated font info struct.. @@ -64,17 +63,12 @@ void fontTextBuffer(font_t *font, primitive_t *primitive, fonttextinfo_t *info); void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info); - - /** * Clamps text to a max width, inserting newlines where possible. * * @param font Font to use * @param text Text to clamp - * @param scale Scale of the text to render. * @param maxWidth Max width (in pixels) to clamp the text to. * @returns The calculated text information. */ -fonttextinfo_t * fontTextClamp(font_t *font,char *text,float scale, - float maxWidth -); \ No newline at end of file +fonttextinfo_t * fontTextClamp(font_t *font,char *text, float maxWidth); \ No newline at end of file diff --git a/src/poker/render/talk.c b/src/poker/render/talk.c index 22cb30a6..a32f7146 100644 --- a/src/poker/render/talk.c +++ b/src/poker/render/talk.c @@ -8,10 +8,17 @@ #include "talk.h" #define SCALE 16 +texture_t text; +primitive_t primitive; + void pokerTalkInit(poker_t *poker) { poker->talkText = NULL; poker->talkTextInfo = NULL; poker->lastWidth = 0, poker->lastHeight = 0; + + // Testing + assetTextureLoad(&text, "red.png"); + quadInit(&primitive, 0, 0, 0, 0, 0, 0, 0, 0, 0); } void pokerTalkDispose(poker_t *poker) { @@ -28,12 +35,13 @@ void pokerTalkTextRebuffer(poker_t *poker, render_t *render) { primitiveDispose(&poker->talkPrimitive); } } + + poker->lastWidth = render->width; + poker->lastHeight = render->height; if(poker->talkText == NULL) return; // Rebuffer - poker->talkTextInfo = fontTextClamp(&poker->font, poker->talkText, SCALE, - render->width - ); + poker->talkTextInfo = fontTextClamp(&poker->font,poker->talkText,render->width); fontTextInit(&poker->font, &poker->talkPrimitive, poker->talkTextInfo); } @@ -44,6 +52,13 @@ void pokerTalkRender(poker_t *poker, render_t *render) { shaderUsePosition(&poker->shader, 0,0,0, 0,0,0); shaderUseTexture(&poker->shader, &poker->font.texture); primitiveDraw(&poker->talkPrimitive, 0, -1); + + primitiveDispose(&primitive); + quadInit(&primitive, -0.05, 0, 0, 0.25, 0.25, poker->talkTextInfo->width, poker->talkTextInfo->height, 0.35, 0.35); + + shaderUsePosition(&poker->shader, 0,0,0, 0,0,0); + shaderUseTexture(&poker->shader, &text); + primitiveDraw(&primitive, 0, -1); } void pokerTalk(poker_t *poker, char *text) { diff --git a/src/poker/render/talk.h b/src/poker/render/talk.h index 27f7116b..2e212157 100644 --- a/src/poker/render/talk.h +++ b/src/poker/render/talk.h @@ -11,6 +11,9 @@ #include "../../display/primitive.h" #include "../../display/shader.h" +#include "../../file/asset.h" +#include "../../display/primitives/quad.h" + void pokerTalkInit(poker_t *poker); void pokerTalkDispose(poker_t *poker);