Select Git revision
flash-all.gdb
Forked from
card10 / firmware
Source project has a limited visibility.
textbuffer.c 4.70 KiB
#include "textbuffer.h"
#include "gfx.h"
#include <string.h>
void txt_init(struct txt_buffer *txtb, struct gfx_region *reg, sFONT *f)
{
txtb->reg = reg;
txtb->font = f;
txtb->cursor_column = 0;
txtb->cursor_row = 0;
txtb->fg_color = gfx_color_rgb_f(reg, 1.0f, 1.0f, 1.0f);
txtb->bg_color = gfx_color_rgb_f(reg, .0f, .0f, .0f);
txtb->draw_cursor = 1;
txtb->auto_update = 1;
}
static inline size_t width_(struct txt_buffer *tm)
{
return tm->reg->width / tm->font->Width;
}
static inline size_t height_(struct txt_buffer *tm)
{
return tm->reg->height / tm->font->Height;
}
size_t txt_width(struct txt_buffer *tm)
{
return width_(tm);
}
size_t txt_height(struct txt_buffer *tm)
{
return height_(tm);
}
static void scrollup(struct txt_buffer *tm)
{
const int last_row = height_(tm) - 1;
const size_t line_size = width_(tm) * sizeof(struct txt_glyph);
for (int row = 0; row < last_row; row++)
memcpy(&tm->text[row][0], &tm->text[row + 1][0], line_size);
for (int col = 0; col < width_(tm); col++) {
struct txt_glyph *g = &tm->text[last_row][col];
g->ch = ' ';
g->fg_color = tm->fg_color;
g->bg_color = tm->bg_color;
}
}
static void newline(struct txt_buffer *tm)
{
const int last_row = height_(tm) - 1;
scrollup(tm);
tm->cursor_row = last_row;
tm->cursor_column = 0;
}
static inline void advance_cursor(struct txt_buffer *tm)
{
const int last_row = height_(tm) - 1;
tm->cursor_column++;
if (tm->cursor_column >= width_(tm)) {
tm->cursor_column = 0;
tm->cursor_row++;
if (tm->cursor_row > last_row)
newline(tm);