Add newlines to draw text
This commit is contained in:
@ -18,11 +18,27 @@ void drawText(
|
||||
) {
|
||||
if(len == 0) return;
|
||||
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.");
|
||||
|
||||
uint16_t i = y * FRAME_WIDTH + x;
|
||||
memcpy(&FRAME_BUFFER[i], text, rLen);
|
||||
memset(&FRAME_COLOR[i], color, rLen);
|
||||
char_t c;
|
||||
uint16_t xPos = x;
|
||||
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);
|
||||
}
|
@ -89,7 +89,7 @@ void drawUITextbox() {
|
||||
memset(&FRAME_COLOR[DRAW_UI_TEXTBOX_CURSOR_POS], COLOR_WHITE, 3);
|
||||
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 + 2] = blink ? ' ' : '>';
|
||||
}
|
@ -11,6 +11,7 @@
|
||||
#define DRAW_UI_TEXTBOX_WIDTH FRAME_WIDTH
|
||||
#define DRAW_UI_TEXTBOX_HEIGHT 8
|
||||
#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.
|
||||
|
@ -46,7 +46,7 @@ void playerUpdate(entity_t *entity) {
|
||||
|
||||
target->state = ENTITY_STATE_TALKING;
|
||||
entity->state = ENTITY_STATE_TALKING;
|
||||
textboxSetText("NPC", "Hello Player");
|
||||
textboxSetText("NPC", "Hello Player.\nHow are you today?");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -61,6 +61,19 @@ void textboxUpdate() {
|
||||
TEXTBOX.nextChar -= TIME.delta;
|
||||
if(TEXTBOX.nextChar > 0.0f) return;
|
||||
|
||||
TEXTBOX.textIndex++;
|
||||
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++;
|
||||
}
|
@ -14,7 +14,7 @@
|
||||
(DRAW_UI_TEXTBOX_HEIGHT - 2) * (DRAW_UI_TEXTBOX_WIDTH - 2) \
|
||||
)
|
||||
#define TEXTBOX_CHARS_PER_SECOND 25
|
||||
#define TEXTBOX_BLINKS_PER_SECOND 2
|
||||
#define TEXTBOX_CHARS_PAUSE_DELAY 0.15f
|
||||
|
||||
typedef struct {
|
||||
char_t title[TEXTBOX_TITLE_MAX+1];
|
||||
|
Reference in New Issue
Block a user