Trying to optimize memory.
This commit is contained in:
@ -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) {
|
||||
|
Reference in New Issue
Block a user