Still working on my scrolling text and bug fixing.

This commit is contained in:
2021-07-16 22:26:04 -07:00
parent 06202f6f7e
commit f619b7c9d2
26 changed files with 495 additions and 115 deletions

View File

@ -7,17 +7,12 @@
#include "timeline.h"
void onDone(timeline_t *test) {
}
void timelineInit(timeline_t *timeline) {
timeline->current = 0;
timeline->diff = 0;
timeline->previous = 0;
timeline->user = 0;
timeline->actionCount = 0;
timeline->actions[0].onStart = &onDone;
}
void timelineUpdate(timeline_t *timeline, float delta) {
@ -38,7 +33,7 @@ void timelineUpdate(timeline_t *timeline, float delta) {
// Did we start this frame?
if(action->start > timeline->previous && action->onStart != NULL) {
action->onStart(timeline);
action->onStart(timeline, action, i);
}
// Durations of 0 only fire starts, never ends or durations.
@ -47,9 +42,10 @@ void timelineUpdate(timeline_t *timeline, float delta) {
// Is the end still in the future? Durations in negatives go forever
full = action->start+action->duration;
if(action->duration < 0 || full > timeline->current) {
if(action->onDuration != NULL) action->onDuration(timeline);
if(action->onDuration != NULL) action->onDuration(timeline, action, i);
} else if(full > timeline->previous) {// Did we end this frame?
if(action->onEnd != NULL) action->onEnd(timeline);
if(action->onEnd != NULL) action->onEnd(timeline, action, i);
if(action->loop) action->start = timeline->current;
}
}
}
@ -74,6 +70,7 @@ timelineaction_t * timelineAddAction(timeline_t *timeline, float start,
) {
if(timeline->actionCount == TIMELINE_ACTION_COUNT_MAX) return NULL;
timelineaction_t *action = timeline->actions + (timeline->actionCount++);
action->loop = false;
action->start = start, action->duration = duration;
action->onStart = action->onEnd = action->onDuration = NULL;
return action;

View File

@ -44,17 +44,6 @@ void fontDispose(font_t *font) {
textureDispose(&font->texture);
}
fonttextinfo_t * fontGetTextInfo(font_t *font, char *text) {
return fontTextClamp(font, text, 999999);
}
void fontTextInfoDispose(fonttextinfo_t *info) {
free(info->quads);
free(info);
}
void fontTextBuffer(font_t *font,primitive_t *primitive,fonttextinfo_t *info) {
stbtt_aligned_quad *quad;
int32_t i;
@ -76,29 +65,27 @@ void fontTextInit(font_t *font, primitive_t *primitive, fonttextinfo_t *info) {
fontTextBuffer(font, primitive, info);
}
fonttextinfo_t * fontTextClamp(font_t *font, char *text, float maxWidth) {
// This method is similar to fontGetTextInfo, but will modify the text.
void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
float maxWidth
) {
int32_t i, j, wordIndex;
char c;
float x, y, wordX;
stbtt_aligned_quad *quad;
fonttextinfo_t *info = malloc(sizeof(fonttextinfo_t));
/** Which index in the original text var is the current word from */
int32_t wordStart = 0;
/** Buffer of the quads for the word (currently) */
int32_t bufferTextSize = 64;
info->quads = malloc(sizeof(stbtt_aligned_quad) * bufferTextSize);
// Setup Scales
info->length = 0;
info->realLength = 0;
info->lines = 1;
info->lineCount = 0;
// Prepare the line counters
info->lines[0].start = 0;
info->lines[0].length = 0;
// Unlike GetTextInfo we can actually calculate the quads at the same time
// that we determine the string length.
// Setup the initial loop state, and X/Y coords for the quad.
i = 0;
x = 0;
y = FONT_INITIAL_LINE;
@ -112,12 +99,13 @@ fonttextinfo_t * fontTextClamp(font_t *font, char *text, float maxWidth) {
// Did this space cause a newline?
if(x > maxWidth) {
info->lines++;
info->lineCount++;
info->lines[info->lineCount].start = info->realLength;
info->lines[info->lineCount].length = 0;
y += FONT_LINE_HEIGHT;
x = 0;
}
// info->width = mathMax(info->width, x);
// info->height = mathMax(info->height, y);
wordX = x;
wordStart = info->realLength;
continue;
@ -125,21 +113,16 @@ fonttextinfo_t * fontTextClamp(font_t *font, char *text, float maxWidth) {
// New line.
if(c == FONT_NEWLINE) {
info->lines++;
info->lineCount++;
info->lines[info->lineCount].start = info->realLength;
info->lines[info->lineCount].length = 0;
wordStart = info->realLength;
y += FONT_LINE_HEIGHT;
x = 0;
continue;
}
// Actual letter. We need to start by resizing the buffer
if(info->realLength+1 >= bufferTextSize) {
info->quads = memBufferResize(info->quads,
bufferTextSize, bufferTextSize * 2, sizeof(stbtt_aligned_quad)
);
bufferTextSize *= 2;
}
// Generate the quad.
quad = info->quads + info->realLength;
stbtt_GetBakedQuad(font->characterData,
@ -156,20 +139,28 @@ fonttextinfo_t * fontTextClamp(font_t *font, char *text, float maxWidth) {
quad->x0 -= wordX, quad->x1 -= wordX;
quad->y0 += FONT_LINE_HEIGHT, quad->y1 += FONT_LINE_HEIGHT;
}
// Go back to the previous (still current) line and remove the chars
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;
info->lines++;
wordX = 0;
}
info->lines[info->lineCount].length++;
info->realLength++;
}
info->lineCount++;
for(j = 0; j < info->realLength; j++) {
quad = info->quads + j;
info->width = mathMax(info->width, quad->x1);
info->height = mathMax(info->height, quad->y1);
}
return info;
}

View File

@ -26,23 +26,6 @@ void fontInit(font_t *font, char *data);
*/
void fontDispose(font_t *Font);
/**
* Measures (and calculates the quads for) a text prior to rendering it.
*
* @param font Font to use for rendering and measuring
* @param text Text to measure/render.
* @returns Font info calculated.
*/
fonttextinfo_t * fontGetTextInfo(font_t *font, char *text);
/**
* Disposes a previously calculated font info struct..
* @param info Info to dispose.
*/
void fontTextInfoDispose(fonttextinfo_t *info);
/**
* 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.
@ -67,8 +50,10 @@ 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 info Font text info to clamp into.
* @param text Text to clamp.
* @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 maxWidth);
void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
float maxWidth
);

View File

@ -9,6 +9,7 @@
#define QUAD_VERTICE_COUNT 4
#define QUAD_INDICE_COUNT 6
#define QUAD_INDICE_PER_QUAD 2
/**
* Buffers the vertices of a quad onto a primitive.