[renderer] Return count of lines printed to char buffer

This allows users to know the number of terminated lines in the printed
text without having to redundantly count the lines.
This commit is contained in:
Bill Currie 2022-09-20 10:27:24 +09:00
parent 3eceb444d3
commit 474f01d321
2 changed files with 11 additions and 6 deletions

View file

@ -59,7 +59,7 @@ void Draw_DestroyBuffer (draw_charbuffer_t *buffer);
void Draw_ClearBuffer (draw_charbuffer_t *buffer);
void Draw_ScrollBuffer (draw_charbuffer_t *buffer, int lines);
void Draw_CharBuffer (int x, int y, draw_charbuffer_t *buffer);
void Draw_PrintBuffer (draw_charbuffer_t *buffer, const char *str);
int Draw_PrintBuffer (draw_charbuffer_t *buffer, const char *str);
extern byte *draw_chars;

View file

@ -91,12 +91,13 @@ Draw_CharBuffer (int x, int y, draw_charbuffer_t *buffer)
#define TAB 8
VISIBLE void
VISIBLE int
Draw_PrintBuffer (draw_charbuffer_t *buffer, const char *str)
{
char *dst = buffer->chars;
char c;
int tab;
int lines = 0;
dst += buffer->cursy * buffer->width + buffer->cursx;
while ((c = *str++)) {
switch (c) {
@ -109,10 +110,12 @@ Draw_PrintBuffer (draw_charbuffer_t *buffer, const char *str)
buffer->cursx = 0;
dst += buffer->width;
buffer->cursy++;
lines++;
break;
case '\f':
dst += buffer->width;
buffer->cursy++;
lines++;
break;
case '\t':
tab = TAB - buffer->cursx % TAB;
@ -127,12 +130,14 @@ Draw_PrintBuffer (draw_charbuffer_t *buffer, const char *str)
if (buffer->cursx >= buffer->width) {
buffer->cursx -= buffer->width;
buffer->cursy++;
lines++;
}
if (buffer->cursy >= buffer->height) {
int lines = buffer->cursy - buffer->height + 1;
Draw_ScrollBuffer (buffer, lines);
dst -= lines * buffer->width;
buffer->cursy -= lines;
int excess = buffer->cursy - buffer->height + 1;
Draw_ScrollBuffer (buffer, excess);
dst -= excess * buffer->width;
buffer->cursy -= excess;
}
}
return lines;
}