commit 7fe31a5
uint
·
2026-05-04 12:50:37 +0000 UTC
parent 8e9d59f
add drawing, create RGFW_surface, draw cells to surface
6 files changed,
+146,
-7
M
Makefile
+2,
-1
1@@ -4,7 +4,8 @@ CPPFLAGS = -Isource/external -Isource/include
2 LDFLAGS = -framework Cocoa -framework CoreVideo -framework IOKit -framework CoreGraphics -framework CoreFoundation -framework Carbon
3
4 SRCS = source/cterm.c \
5- source/util.c
6+ source/util.c \
7+ source/draw.c
8 OUT = cterm
9
10 all:
+33,
-6
1@@ -1,38 +1,65 @@
2 #define RGFW_IMPLEMENTATION
3+#include <stdint.h>
4 #include <stdlib.h>
5
6 #include <RGFW.h>
7 #include <grapheme.h>
8 #include <schrift.h>
9
10+#include "draw.h"
11 #include "util.h"
12
13+#define WIDTH 800
14+#define HEIGHT 600
15+#define CELLW 9
16+#define CELLH 16
17+
18 RGFW_window* win = NULL;
19+RGFW_surface* surf = NULL;
20+uint32_t* pixels = NULL;
21
22-void init(void);
23-void run(void);
24+static void init(void);
25+static void run(void);
26
27 /**
28 * @brief initialise: window (TODO)
29 */
30-void init(void)
31+static void init(void)
32 {
33 if (!(win = RGFW_createWindow("cterm", 0, 0, 800, 600, 0)))
34 die(1, "failed to create window");
35+
36+ pixels = calloc(WIDTH*HEIGHT, sizeof(*pixels));
37+ if (!pixels)
38+ die(1, "failed to alloc pixel buffer");
39+
40+ surf = RGFW_window_createSurface(
41+ win, (u8*)pixels, WIDTH, HEIGHT, RGFW_formatRGBA8
42+ );
43+ if (!surf)
44+ die(1, "failed to create window surface");
45 }
46
47 /**
48 * @brief program event loop (TODO)
49 */
50-void run(void)
51+static void run(void)
52 {
53 RGFW_event ev;
54+
55 while (!RGFW_window_shouldClose(win)) {
56 while (RGFW_window_checkEvent(win, &ev))
57 ;
58- }
59
60- RGFW_window_close(win);
61+ draw_clear(pixels, WIDTH, HEIGHT, rgba(255, 0, 0, 255)); /* TODO: opacity */
62+
63+ draw_cell(pixels, WIDTH, HEIGHT, 0, 0, CELLW, CELLH, rgba(255, 0, 0, 255));
64+ draw_cell(pixels, WIDTH, HEIGHT, 1, 0, CELLW, CELLH, rgba(0, 255, 0, 255));
65+ draw_cell(pixels, WIDTH, HEIGHT, 2, 0, CELLW, CELLH, rgba(0, 0, 255, 255));
66+ draw_cursor(pixels, WIDTH, HEIGHT, 3, 0, CELLW, CELLH, rgba(255, 255, 255, 255));
67+
68+ RGFW_window_blitSurface(win, surf);
69+ }
70 }
71
72 int main(int argc, char* argv[])
+37,
-0
1@@ -0,0 +1,37 @@
2+#include "draw.h"
3+
4+void draw_clear(uint32_t* dst, int w, int h, uint32_t col)
5+{
6+ for (int i = 0; i < w*h; i++)
7+ dst[i] = col;
8+}
9+
10+void draw_cell(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
11+ uint32_t bg)
12+{
13+ int x0 = cl * cw; /* x origin of cell */
14+ int y0 = rw * ch; /* y origin of cell */
15+ int x;
16+ int y;
17+
18+ /* iterate through columns */
19+ for (y = y0; y < y0+ch && y < h; y++) {
20+ if (y < 0)
21+ continue;
22+
23+
24+ /* through rows */
25+ for (x = x0; x < x0+cw && x < w; x++) {
26+ if (x < 0)
27+ continue;
28+ dst[y * w + x] = bg;
29+ }
30+ }
31+}
32+
33+void draw_cursor(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
34+ uint32_t fg)
35+{
36+ draw_cell(dst, w, h, cl, rw, cw, ch, fg);
37+}
38+
+51,
-0
1@@ -0,0 +1,51 @@
2+#ifndef DRAW_H
3+#define DRAW_H
4+
5+#include <stdint.h>
6+
7+/* TODO: proper structs and faster drawing with damage for drawing shaepes*/
8+
9+/**
10+ * @brief clear a buffer size wxh
11+ *
12+ * @param dst buffer to clear
13+ * @param w width of buffer
14+ * @param h height of buffer
15+ * @param col colour to clear buffer with
16+ */
17+void draw_clear(uint32_t* dst, int w, int h, uint32_t col);
18+
19+/**
20+ * @brief fill the background of a cell
21+ *
22+ * @param dst buffer to draw to
23+ * @param w width of buffer
24+ * @param h height of buffer
25+ * @param cl cell column
26+ * @param cr cell row
27+ * @param cw cell width
28+ * @param ch cell height
29+ * @param bg cell background colour
30+ */
31+void draw_cell(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
32+ uint32_t bg);
33+
34+/**
35+ * @brief fill the background of a cell
36+ *
37+ * @param dst buffer to draw to
38+ * @param w width of buffer
39+ * @param h height of buffer
40+ * @param cl cell column
41+ * @param cr cell row
42+ * @param cw cell width
43+ * @param ch cell height
44+ * @param fg cell foreground colour
45+ *
46+ * (TODO: glyphs)
47+ */
48+void draw_cursor(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
49+ uint32_t fg);
50+
51+#endif /* DRAW_H */
52+
+14,
-0
1@@ -1,6 +1,8 @@
2 #ifndef UTIL_H
3 #define UTIL_H
4
5+#include <stdint.h>
6+
7 /**
8 * @brief exit program after printing a message
9 *
10@@ -9,5 +11,17 @@
11 */
12 void die(int ec, const char* msg);
13
14+/**
15+ * @brief convert red, green, blue, alpha values to a packed value
16+ *
17+ * @param r red value
18+ * @param g green value
19+ * @param b blue value
20+ * @param a alpha value
21+ *
22+ * @return packed rbga value
23+ */
24+uint32_t rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
25+
26 #endif /* UTIL_H */
27
+9,
-0
1@@ -9,3 +9,12 @@ void die(int ec, const char* msg)
2 exit(ec);
3 }
4
5+uint32_t rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
6+{
7+ return
8+ ((uint32_t)a << 24) |
9+ ((uint32_t)b << 16) |
10+ ((uint32_t)g << 8) |
11+ ((uint32_t)r);
12+}
13+