Add newlines to draw text

This commit is contained in:
2024-10-06 17:30:04 -05:00
parent 5256b32055
commit e14445680f
6 changed files with 38 additions and 8 deletions

View File

@ -18,11 +18,27 @@ void drawText(
) { ) {
if(len == 0) return; if(len == 0) return;
size_t rLen = mathMin(len, strlen(text)); size_t rLen = mathMin(len, strlen(text));
if(rLen == 0) return;
assertTrue(x + rLen <= FRAME_WIDTH, "Text is too long."); assertTrue(x < FRAME_WIDTH, "Text is too far right.");
assertTrue(y < FRAME_HEIGHT, "Text is too low."); assertTrue(y < FRAME_HEIGHT, "Text is too low.");
uint16_t i = y * FRAME_WIDTH + x; char_t c;
memcpy(&FRAME_BUFFER[i], text, rLen); uint16_t xPos = x;
memset(&FRAME_COLOR[i], color, rLen); uint16_t yPos = y * FRAME_WIDTH;
size_t i = 0;
do {
c = text[i++];
if(c == '\n') {
yPos += FRAME_WIDTH;
xPos = x;
continue;
}
assertTrue(xPos < FRAME_WIDTH, "Text overflows right.");
FRAME_BUFFER[yPos + xPos] = c;
FRAME_COLOR[yPos + xPos] = color;
xPos++;
} while(i < rLen);
} }

View File

@ -89,7 +89,7 @@ void drawUITextbox() {
memset(&FRAME_COLOR[DRAW_UI_TEXTBOX_CURSOR_POS], COLOR_WHITE, 3); memset(&FRAME_COLOR[DRAW_UI_TEXTBOX_CURSOR_POS], COLOR_WHITE, 3);
if(TEXTBOX.textIndex < TEXTBOX.textLength) return; if(TEXTBOX.textIndex < TEXTBOX.textLength) return;
int32_t blink = (int32_t)(TIME.time * TEXTBOX_BLINKS_PER_SECOND) % 2; int32_t blink = (int32_t)(TIME.time * DRAW_UI_TEXTBOX_BLINKS_PER_SECOND) % 2;
FRAME_BUFFER[DRAW_UI_TEXTBOX_CURSOR_POS + 1] = blink ? '>' : ' '; FRAME_BUFFER[DRAW_UI_TEXTBOX_CURSOR_POS + 1] = blink ? '>' : ' ';
FRAME_BUFFER[DRAW_UI_TEXTBOX_CURSOR_POS + 2] = blink ? ' ' : '>'; FRAME_BUFFER[DRAW_UI_TEXTBOX_CURSOR_POS + 2] = blink ? ' ' : '>';
} }

View File

@ -11,6 +11,7 @@
#define DRAW_UI_TEXTBOX_WIDTH FRAME_WIDTH #define DRAW_UI_TEXTBOX_WIDTH FRAME_WIDTH
#define DRAW_UI_TEXTBOX_HEIGHT 8 #define DRAW_UI_TEXTBOX_HEIGHT 8
#define DRAW_UI_TEXTBOX_CURSOR_POS ((FRAME_HEIGHT * FRAME_WIDTH) - 4) #define DRAW_UI_TEXTBOX_CURSOR_POS ((FRAME_HEIGHT * FRAME_WIDTH) - 4)
#define DRAW_UI_TEXTBOX_BLINKS_PER_SECOND 2
/** /**
* Draws a UI box to the frame buffer. * Draws a UI box to the frame buffer.

View File

@ -46,7 +46,7 @@ void playerUpdate(entity_t *entity) {
target->state = ENTITY_STATE_TALKING; target->state = ENTITY_STATE_TALKING;
entity->state = ENTITY_STATE_TALKING; entity->state = ENTITY_STATE_TALKING;
textboxSetText("NPC", "Hello Player"); textboxSetText("NPC", "Hello Player.\nHow are you today?");
return; return;
} }

View File

@ -61,6 +61,19 @@ void textboxUpdate() {
TEXTBOX.nextChar -= TIME.delta; TEXTBOX.nextChar -= TIME.delta;
if(TEXTBOX.nextChar > 0.0f) return; if(TEXTBOX.nextChar > 0.0f) return;
switch(TEXTBOX.text[TEXTBOX.textIndex]) {
// Exaggerate the pause for these characters
case '\n':
case '.':
case ',':
case ';':
case ':':
TEXTBOX.nextChar = TEXTBOX_CHARS_PAUSE_DELAY;
break;
default:
TEXTBOX.nextChar = (1.0f / TEXTBOX_CHARS_PER_SECOND);
break;
}
TEXTBOX.textIndex++; TEXTBOX.textIndex++;
TEXTBOX.nextChar = (1.0f / TEXTBOX_CHARS_PER_SECOND);
} }

View File

@ -14,7 +14,7 @@
(DRAW_UI_TEXTBOX_HEIGHT - 2) * (DRAW_UI_TEXTBOX_WIDTH - 2) \ (DRAW_UI_TEXTBOX_HEIGHT - 2) * (DRAW_UI_TEXTBOX_WIDTH - 2) \
) )
#define TEXTBOX_CHARS_PER_SECOND 25 #define TEXTBOX_CHARS_PER_SECOND 25
#define TEXTBOX_BLINKS_PER_SECOND 2 #define TEXTBOX_CHARS_PAUSE_DELAY 0.15f
typedef struct { typedef struct {
char_t title[TEXTBOX_TITLE_MAX+1]; char_t title[TEXTBOX_TITLE_MAX+1];