[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_ClearBuffer (draw_charbuffer_t *buffer);
void Draw_ScrollBuffer (draw_charbuffer_t *buffer, int lines); void Draw_ScrollBuffer (draw_charbuffer_t *buffer, int lines);
void Draw_CharBuffer (int x, int y, draw_charbuffer_t *buffer); 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; extern byte *draw_chars;

View file

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