Trying to optimize memory.

This commit is contained in:
2021-10-16 15:52:16 -07:00
parent b110bab954
commit e21ba4858f
20 changed files with 132 additions and 117 deletions

View File

@ -48,36 +48,31 @@ float fontGetScale(float fontSize) {
return fontSize / FONT_SIZE_DEFAULT * FONT_GLOBAL_SCALE;
}
void fontTextBuffer(font_t *font, primitive_t *primitive, fonttextinfo_t *info){
stbtt_aligned_quad *quad;
int32_t i;
bool _fontTextBufferAddLine(fonttextinfo_t *info, int32_t start, int32_t len) {
info->lineCount++;
for(i = 0; i < info->realLength; i++) {
quad = info->quads + i;
quadBuffer(primitive, 0,
quad->x0, quad->y0, quad->s0, quad->t0,
quad->x1, quad->y1, quad->s1, quad->t1,
i * QUAD_VERTICE_COUNT, i * QUAD_INDICE_COUNT
);
if(info->lineCount >= FONT_TEXT_INFO_LINES_MAX) {
return false;
}
info->lines[info->lineCount].start = start;
info->lines[info->lineCount].length = len;
return true;
}
void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info) {
primitiveInit(primitive,
QUAD_VERTICE_COUNT * info->realLength,
QUAD_INDICE_COUNT * info->realLength
);
fontTextBuffer(font, primitive, info);
}
void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
void fontTextBuffer(
font_t *font, primitive_t *primitive, fonttextinfo_t *info, char *text,
float maxWidth, float fontSize
) {
int32_t i, j;
int32_t i, j, wordStart;
char c;
float x, y, wordX, scale;
stbtt_aligned_quad *quads;
stbtt_aligned_quad *quad;
// Make some space
quads = malloc(sizeof(stbtt_aligned_quad) * strlen(text));
// Get the font scale
scale = fontGetScale(fontSize);
@ -85,7 +80,7 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
maxWidth = maxWidth == -1 ? 9999999 : maxWidth * (1 / scale);
/** Which index in the original text var is the current word from */
int32_t wordStart = 0;
wordStart = 0;
// Setup Scales
info->length = 0;
@ -105,6 +100,7 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
y = FONT_INITIAL_LINE;
wordX = 0;
while(c = text[i++]) {
if(info->lineCount >= FONT_TEXT_INFO_LINES_MAX) break;
info->length++;
// When space, start of new word about to begin
@ -113,10 +109,7 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
// Did this space cause a newline?
if(x > maxWidth) {
info->lineCount++;
info->lines[info->lineCount].start = info->realLength;
info->lines[info->lineCount].length = 0;
_fontTextBufferAddLine(info, info->realLength, 0);
y += FONT_LINE_HEIGHT;
x = 0;
}
@ -127,10 +120,7 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
// New line.
if(c == FONT_NEWLINE) {
info->lineCount++;
info->lines[info->lineCount].start = info->realLength;
info->lines[info->lineCount].length = 0;
_fontTextBufferAddLine(info, info->realLength, 0);
wordStart = info->realLength;
y += FONT_LINE_HEIGHT;
x = 0;
@ -138,19 +128,18 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
}
// Generate the quad.
quad = info->quads + info->realLength;
quad = quads + info->realLength;
stbtt_GetBakedQuad(font->characterData,
FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT,
((int32_t)c)-FONT_FIRST_CHAR, &x, &y, quad, FONT_FILL_MODE
);
// Now measure the width of the line (take the right side of that quad)
// if(x > maxWidth) {
if(quad->x1 > maxWidth) {
// We've exceeded the edge, go back to the start of the word and newline.
x = quad->x1 - wordX;
for(j = wordStart; j <= info->realLength; j++) {
quad = info->quads + j;
quad = quads + j;
quad->x0 -= wordX;
quad->x1 -= wordX;
quad->y0 += FONT_LINE_HEIGHT;
@ -161,11 +150,10 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
info->lines[info->lineCount].length -= info->realLength - wordStart;
// Next line begins with this word
info->lineCount++;
info->lines[info->lineCount].start = wordStart;
info->lines[info->lineCount].length = info->realLength - wordStart;
y += FONT_LINE_HEIGHT;
if(!_fontTextBufferAddLine(info, wordStart, info->realLength-wordStart)) {
continue;
}
wordX = 0;
}
@ -174,9 +162,15 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
}
info->lineCount++;
// Initialize primitive
primitiveInit(primitive,
QUAD_VERTICE_COUNT * info->realLength,
QUAD_INDICE_COUNT * info->realLength
);
for(j = 0; j < info->realLength; j++) {
quad = info->quads + j;
quad = quads + j;
// Scale the Quad
if(scale != 1.0) {
@ -189,7 +183,17 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
// Update the dimensions.
info->width = mathMax(info->width, quad->x1);
info->height = mathMax(info->height, quad->y1);
// Buffer the quad.
quadBuffer(primitive, 0,
quad->x0, quad->y0, quad->s0, quad->t0,
quad->x1, quad->y1, quad->s1, quad->t1,
j * QUAD_VERTICE_COUNT, j * QUAD_INDICE_COUNT
);
}
// Free up
free(quads);
}
int32_t fontGetLineCharCount(fonttextinfo_t *info,int32_t start,int32_t count) {

View File

@ -46,11 +46,8 @@
#define FONT_INITIAL_LINE FONT_LINE_HEIGHT * 0.75f
#define FONT_SPACE_SIZE FONT_TEXTURE_SIZE * 0.45f
/** Maximum number of characters a font text info can hold. */
#define FONT_TEXT_INFO_CHARS_MAX 1024
/** Maximum number of newlines that a font text info can hold. */
#define FONT_TEXT_INFO_LINES_MAX FONT_TEXT_INFO_CHARS_MAX/50
#define FONT_TEXT_INFO_LINES_MAX 16
/** Representation of a font that can be used to render text */
typedef struct {
@ -80,9 +77,6 @@ typedef struct {
/** Dimensions of the string */
float width, height;
/** Array of precalculated quads */
stbtt_aligned_quad quads[FONT_TEXT_INFO_CHARS_MAX];
} fonttextinfo_t;
/**
@ -98,25 +92,6 @@ void fontInit(font_t *font, char *data);
*/
void fontDispose(font_t *Font);
/**
* Buffers the vertices of a font text onto a primitive. Requires some info
* about how the font is meant to render from the text info section.
*
* @param font Font to render.
* @param primitive Primitive to render into.
* @param info The precalculated text info.
*/
void fontTextBuffer(font_t *font, primitive_t *primitive, fonttextinfo_t *info);
/**
* Initializes an uninitialized primitive for rendering.
*
* @param font Font to render.
* @param primitive Primitive to render into.
* @param info Text Info to use while rendering.
*/
void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info);
/**
* Convert a Font's Size into a Font Scale. The scale is based on the font's
* default size for the given texture size (Refer to FONT_SIZE_DEFAULT).
@ -126,16 +101,28 @@ void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info);
*/
float fontGetScale(float fontSize);
/**
* Internal method that adds a line to the text buffer process.
*
* @param info Info to add to.
* @param start Start character index for the next line.
* @param len Length of the next line.
* @return True if added a line successfully, otherwise false.
*/
bool _fontTextBufferAddLine(fonttextinfo_t *info, int32_t start, int32_t len);
/**
* Clamps text to a max width, inserting newlines where possible.
*
* @param font Font to use
* @param primitive Primitive to buffer the text to.
* @param info Font text info to clamp into.
* @param text Text to clamp.
* @param maxWidth Max width (pixels) to clamp the text to, -1 for no max width.
* @param fontSize Font Size of the text.
*/
void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
void fontTextBuffer(
font_t *font, primitive_t *primitive, fonttextinfo_t *info, char *text,
float maxWidth, float fontSize
);