89 lines
2.0 KiB
C
89 lines
2.0 KiB
C
#include "term.h"
|
|
|
|
#include <gint/display.h>
|
|
#include <stdio.h>
|
|
|
|
extern font_t uf5x7;
|
|
|
|
struct tcell {
|
|
char chr[4];
|
|
int fg;
|
|
int bg;
|
|
};
|
|
|
|
static struct tcell tgrid[UNS_TERM_ROWS][UNS_TERM_COLS];
|
|
|
|
static void tgrid_set(int row, int col, int fg, int bg, char c[4]) {
|
|
tgrid[row][col].chr[0] = c[0];
|
|
tgrid[row][col].chr[1] = c[1];
|
|
tgrid[row][col].chr[2] = c[2];
|
|
tgrid[row][col].chr[3] = c[3];
|
|
tgrid[row][col].fg = fg;
|
|
tgrid[row][col].bg = bg;
|
|
}
|
|
|
|
void tgrid_sets(int row, int col, int fg, int bg, const char *s) {
|
|
int _row = row;
|
|
int _col = col;
|
|
|
|
for (int i = 0; s[i] != '\0';) {
|
|
if (_col >= UNS_TERM_COLS) {
|
|
_col = col;
|
|
_row++;
|
|
}
|
|
|
|
// boundary check
|
|
if (_row >= UNS_TERM_ROWS)
|
|
return;
|
|
|
|
// detech utf-8 char length
|
|
unsigned char lb = s[i];
|
|
int charlen = 0;
|
|
if ((lb & 0x80) == 0) // lead bit is zero, must be a single ascii
|
|
charlen = 1;
|
|
else if ((lb & 0xE0) == 0xC0) // 110x xxxx
|
|
charlen = 2;
|
|
else if ((lb & 0xF0) == 0xE0) // 1110 xxxx
|
|
charlen = 3;
|
|
else if ((lb & 0xF8) == 0xF0) // 1111 0xxx
|
|
charlen = 4;
|
|
|
|
char unichar[4] = "";
|
|
for (int j = 0; j < charlen; j++)
|
|
unichar[j] = s[i + j];
|
|
|
|
tgrid_set(_row, _col, fg, bg, unichar);
|
|
|
|
_col++;
|
|
i += charlen;
|
|
}
|
|
}
|
|
|
|
void tgrid_display(void) {
|
|
dclear(C_BLACK);
|
|
dfont(&uf5x7);
|
|
|
|
int y = 3;
|
|
for (int row = 0; row < UNS_TERM_ROWS; row++) {
|
|
int x = 2;
|
|
for (int col = 0; col < UNS_TERM_COLS; col++) {
|
|
struct tcell cell = tgrid[row][col];
|
|
|
|
char buf[5];
|
|
buf[0] = cell.chr[0];
|
|
buf[1] = cell.chr[1];
|
|
buf[2] = cell.chr[2];
|
|
buf[3] = cell.chr[3];
|
|
buf[4] = '0';
|
|
|
|
drect(x - 1, y - 1, x + 6, y + 8, cell.bg);
|
|
dtext_opt(x, y, cell.fg, C_NONE, DTEXT_LEFT, DTEXT_TOP, (char *)&buf, -1);
|
|
|
|
x += 1 + 5;
|
|
}
|
|
|
|
y += 1 + 7 + 2;
|
|
}
|
|
|
|
dupdate();
|
|
} |