commit 5780131
uint
·
2026-05-12 09:38:01 +0000 UTC
parent 43f39da
rename Cell->Rune, Font->Fontface, Cursor->Caret portability is a pain
8 files changed,
+70,
-69
+15,
-15
1@@ -17,15 +17,15 @@
2 #define COLS 80
3 #define ROWS 24
4
5-static Font font;
6-static Cursor cur;
7+static Fontface font;
8+static Caret car;
9 static RGFW_window* win = NULL;
10 static RGFW_surface* surf = NULL;
11 static uint32_t* pixels = NULL;
12
13-static struct Cell termcells[COLS*ROWS];
14+static Rune runes[COLS*ROWS]; /* TODO: dynamic */
15 static Term term = {
16- .cells = termcells,
17+ .runes = runes,
18 .cols = COLS,
19 .rows = ROWS,
20 .ptyfd = -1, /* TODO: pty */
21@@ -73,17 +73,17 @@ static void init(void)
22
23 /* font */
24 /* TODO: paths */
25- if (font_load(&font, "/System/Library/Fonts/Supplemental/Andale Mono.ttf", 24.0) < 0)
26+ if (font_load(&font, "./Xanh.ttf", 24.0) < 0)
27 die(1, "failed to load font");
28 font.aa = false;
29
30 /* tmp */
31- term_clear(&term, &cur);
32+ term_clear(&term, &car);
33 const char* s =
34 "cterm test\n"
35 "terminal buffer!";
36 for (const char* p = s; *p; p++)
37- term_putc(&term, &cur, (unsigned char)*p);
38+ term_putc(&term, &car, (unsigned char)*p);
39 }
40
41 /**
42@@ -101,25 +101,25 @@ static void run(void)
43
44 for (int y = 0; y < term.rows; y++) {
45 for (int x = 0; x < term.cols; x++) {
46- struct Cell* cell = &CELL(&term, x, y);
47+ Rune* rune = &RUNE(&term, x, y);
48
49- draw_cell(pixels, WIDTH, HEIGHT,
50+ draw_rune(pixels, WIDTH, HEIGHT,
51 x, y,
52 font.cellw, font.cellh,
53- cell->bg);
54+ rune->bg);
55
56- if (cell->cp != ' ') {
57+ if (rune->cp != ' ') {
58 draw_codepoint(&font, pixels, WIDTH, HEIGHT,
59 x * font.cellw,
60 y * font.cellh + (int)font.asc,
61- cell->cp,
62- cell->fg);
63+ rune->cp,
64+ rune->fg);
65 }
66 }
67 }
68
69- draw_cursor(pixels, WIDTH, HEIGHT,
70- cur.x, cur.y,
71+ draw_caret(pixels, WIDTH, HEIGHT,
72+ car.x, car.y,
73 font.cellw, font.cellh,
74 rgba(255, 255, 255, 255));
75
+4,
-4
1@@ -48,7 +48,7 @@ void draw_clear(uint32_t* dst, int w, int h, uint32_t col)
2 dst[i] = col;
3 }
4
5-void draw_cell(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
6+void draw_rune(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
7 uint32_t bg)
8 {
9 int x0 = cl * cw; /* x origin of cell */
10@@ -71,13 +71,13 @@ void draw_cell(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
11 }
12 }
13
14-void draw_cursor(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
15+void draw_caret(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
16 uint32_t fg)
17 {
18- draw_cell(dst, w, h, cl, rw, cw, ch, fg);
19+ draw_rune(dst, w, h, cl, rw, cw, ch, fg);
20 }
21
22-void draw_codepoint(Font* f, uint32_t* dst, int w, int h, int x,
23+void draw_codepoint(Fontface* f, uint32_t* dst, int w, int h, int x,
24 int baseline, uint32_t cp, uint32_t fg)
25 {
26 SFT_Glyph glyph;
+2,
-2
1@@ -4,7 +4,7 @@
2 #include <stdlib.h>
3 #include <string.h>
4
5-int font_load(Font* f, const char* path, double size)
6+int font_load(Fontface* f, const char* path, double size)
7 {
8 SFT_LMetrics lm;
9 SFT_Glyph glyph;
10@@ -53,7 +53,7 @@ fail:
11 return -1;
12 }
13
14-void font_free(Font* f)
15+void font_free(Fontface* f)
16 {
17 if (!f)
18 return;
+17,
-17
1@@ -18,41 +18,41 @@
2 void draw_clear(uint32_t* dst, int w, int h, uint32_t col);
3
4 /**
5- * @brief fill the background of a cell
6+ * @brief fill the background of a rune
7 *
8 * @param dst buffer to draw to
9 * @param w width of buffer
10 * @param h height of buffer
11- * @param cl cell column
12- * @param rw cell row
13- * @param cw cell width
14- * @param ch cell height
15- * @param bg cell background colour
16+ * @param cl rune column
17+ * @param rw rune row
18+ * @param cw rune width
19+ * @param ch rune height
20+ * @param bg rune background colour
21 */
22-void draw_cell(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
23+void draw_rune(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
24 uint32_t bg);
25
26 /**
27- * @brief fill the background of a cell
28+ * @brief fill the background of a rune
29 *
30 * @param dst buffer to draw to
31 * @param w width of buffer
32 * @param h height of buffer
33- * @param cl cell column
34- * @param rw cell row
35- * @param cw cell width
36- * @param ch cell height
37- * @param fg cell foreground colour
38+ * @param cl rune column
39+ * @param rw rune row
40+ * @param cw rune width
41+ * @param ch rune height
42+ * @param fg rune foreground colour
43 *
44- * (TODO: glyphs)
45+ * (TODO: runes)
46 */
47-void draw_cursor(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
48+void draw_caret(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
49 uint32_t fg);
50
51 /**
52 * @brief draw one codepoint to buffer
53 *
54- * @param f font to draw from
55+ * @param face fontface to draw from
56 * @param dst destination buffer
57 * @param w buffer width
58 * @param h buffer height
59@@ -61,7 +61,7 @@ void draw_cursor(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
60 * @param cp (unicode) codepoint to draw
61 * @param fg foreground colour
62 */
63-void draw_codepoint(Font* f, uint32_t* dst, int w, int h, int x,
64+void draw_codepoint(Fontface* face, uint32_t* dst, int w, int h, int x,
65 int baseline, uint32_t cp, uint32_t fg);
66
67 #endif /* DRAW_H */
+3,
-3
1@@ -14,7 +14,7 @@ typedef struct {
2 int cellw;
3 int cellh;
4 bool aa; /* anti-aliasing */
5-} Font;
6+} Fontface;
7
8 /**
9 * @brief load font from path
10@@ -25,14 +25,14 @@ typedef struct {
11 *
12 * @return 1=success, -1=failed
13 */
14-int font_load(Font* f, const char* path, double size);
15+int font_load(Fontface* f, const char* path, double size);
16
17 /**
18 * @brief free a loaded font
19 *
20 * @param f font struct to free
21 */
22-void font_free(Font* f);
23+void font_free(Fontface* f);
24
25 #endif /* FONT_H */
26
+10,
-10
1@@ -4,23 +4,23 @@
2 #include <stdbool.h>
3 #include <stdint.h>
4
5-/* resolve 1D cell from 2D input */
6-#define CELL(t, x, y) ((t)->cells[(y) * (t)->cols + (x)])
7+/* resolve 1D rune from 2D input */
8+#define RUNE(t, x, y) ((t)->runes[(y) * (t)->cols + (x)])
9
10-struct Cell {
11+typedef struct {
12 uint32_t cp; /* codepoint */
13 uint32_t fg;
14 uint32_t bg;
15- bool dmg; /* cell damaged */
16-};
17+ bool dmg; /* rune damaged */
18+} Rune;
19
20 typedef struct {
21 int x;
22 int y;
23-} Cursor;
24+} Caret;
25
26 typedef struct {
27- struct Cell* cells;
28+ Rune* runes;
29 uint32_t fg;
30 uint32_t bg;
31 int cols;
32@@ -29,13 +29,13 @@ typedef struct {
33 } Term;
34
35 /**
36- * @brief clear screen by filling cells with ' ' and reseting
37+ * @brief clear screen by filling runes with ' ' and reseting
38 * to default terminal foreground and background
39 *
40 * @param t terminal instance to scroll
41 * @param c cursor instance to position
42 */
43-void term_clear(Term* t, Cursor* c);
44+void term_clear(Term* t, Caret* c);
45
46 /**
47 * @brief handle placing special and normal characters
48@@ -44,7 +44,7 @@ void term_clear(Term* t, Cursor* c);
49 * @param c cursor instance
50 * @param cp codepoint to place
51 */
52-void term_putc(Term* t, Cursor* c, uint32_t cp);
53+void term_putc(Term* t, Caret* c, uint32_t cp);
54
55 /**
56 * @brief scroll viewport down by one
+18,
-18
1@@ -3,15 +3,15 @@
2
3 #include "term.h"
4
5-void term_clear(Term* t, Cursor* c)
6+void term_clear(Term* t, Caret* c)
7 {
8 for (int y = 0; y < t->rows; y++) {
9 for (int x = 0; x < t->cols; x++) {
10- struct Cell* cell = &CELL(t, x, y);
11- cell->cp = ' ';
12- cell->fg = t->fg;
13- cell->bg = t->bg;
14- cell->dmg = true;
15+ Rune* rune = &RUNE(t, x, y);
16+ rune->cp = ' ';
17+ rune->fg = t->fg;
18+ rune->bg = t->bg;
19+ rune->dmg = true;
20 }
21 }
22
23@@ -19,7 +19,7 @@ void term_clear(Term* t, Cursor* c)
24 c->y = 0;
25 }
26
27-void term_putc(Term* t, Cursor* c, uint32_t cp)
28+void term_putc(Term* t, Caret* c, uint32_t cp)
29 {
30 switch (cp) {
31 case '\r':
32@@ -47,10 +47,10 @@ void term_putc(Term* t, Cursor* c, uint32_t cp)
33 if (cp < 32)
34 break;
35
36- CELL(t, c->x, c->y).cp = cp;
37- CELL(t, c->x, c->y).fg = t->fg;
38- CELL(t, c->x, c->y).bg = t->bg;
39- CELL(t, c->x, c->y).dmg = true;
40+ RUNE(t, c->x, c->y).cp = cp;
41+ RUNE(t, c->x, c->y).fg = t->fg;
42+ RUNE(t, c->x, c->y).bg = t->bg;
43+ RUNE(t, c->x, c->y).dmg = true;
44 c->x++;
45 if (c->x >= t->cols)
46 term_putc(t, c, '\n');
47@@ -61,19 +61,19 @@ void term_putc(Term* t, Cursor* c, uint32_t cp)
48
49 void term_scroll(Term* t)
50 {
51- struct Cell* cells = t->cells;
52+ Rune* runes = t->runes;
53 int cols = t->cols;
54 int rows = t->rows;
55
56 /* TODO: inefficient */
57- memmove(cells, cells + cols, sizeof(struct Cell)*cols * (rows - 1));
58+ memmove(runes, runes + cols, sizeof(Rune)*cols * (rows - 1));
59
60 for (int x = 0; x < t->cols; x++) {
61- struct Cell* cell = &CELL(t, x, t->rows - 1);
62- cell->cp = ' ';
63- cell->fg = t->fg;
64- cell->bg = t->bg;
65- cell->dmg = true;
66+ Rune* rune = &RUNE(t, x, t->rows - 1);
67+ rune->cp = ' ';
68+ rune->fg = t->fg;
69+ rune->bg = t->bg;
70+ rune->dmg = true;
71 }
72 }
73
+1,
-0
1@@ -3,6 +3,7 @@
2
3 #include "util.h"
4
5+/* TODO: stdarg */
6 void die(int ec, const char* msg)
7 {
8 perror(msg);