-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathmain.c
129 lines (121 loc) · 4.32 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "fenster.h"
#define W 320
#define H 240
/* ============================================================
* An example of using raster graphics with Fenster
* - Line: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
* - Circle: https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
* - Rectangle: well, it's obvious
* - Flood fill: very inefficient, using recursion
* - Text: small 5x3 bitmap font is used to render glyps
* ============================================================ */
static void fenster_line(struct fenster *f, int x0, int y0, int x1, int y1,
uint32_t c) {
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = (dx > dy ? dx : -dy) / 2, e2;
for (;;) {
fenster_pixel(f, x0, y0) = c;
if (x0 == x1 && y0 == y1) {
break;
}
e2 = err;
if (e2 > -dx) {
err -= dy;
x0 += sx;
}
if (e2 < dy) {
err += dx;
y0 += sy;
}
}
}
static void fenster_rect(struct fenster *f, int x, int y, int w, int h,
uint32_t c) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
fenster_pixel(f, x + col, y + row) = c;
}
}
}
static void fenster_circle(struct fenster *f, int x, int y, int r, uint32_t c) {
for (int dy = -r; dy <= r; dy++) {
for (int dx = -r; dx <= r; dx++) {
if (dx * dx + dy * dy <= r * r) {
fenster_pixel(f, x + dx, y + dy) = c;
}
}
}
}
static void fenster_fill(struct fenster *f, int x, int y, uint32_t old,
uint32_t c) {
if (x < 0 || y < 0 || x >= f->width || y >= f->height) {
return;
}
if (fenster_pixel(f, x, y) == old) {
fenster_pixel(f, x, y) = c;
fenster_fill(f, x - 1, y, old, c);
fenster_fill(f, x + 1, y, old, c);
fenster_fill(f, x, y - 1, old, c);
fenster_fill(f, x, y + 1, old, c);
}
}
// clang-format off
static uint16_t font5x3[] = {0x0000,0x2092,0x002d,0x5f7d,0x279e,0x52a5,0x7ad6,0x0012,0x4494,0x1491,0x017a,0x05d0,0x1400,0x01c0,0x0400,0x12a4,0x2b6a,0x749a,0x752a,0x38a3,0x4f4a,0x38cf,0x3bce,0x12a7,0x3aae,0x49ae,0x0410,0x1410,0x4454,0x0e38,0x1511,0x10e3,0x73ee,0x5f7a,0x3beb,0x624e,0x3b6b,0x73cf,0x13cf,0x6b4e,0x5bed,0x7497,0x2b27,0x5add,0x7249,0x5b7d,0x5b6b,0x3b6e,0x12eb,0x4f6b,0x5aeb,0x388e,0x2497,0x6b6d,0x256d,0x5f6d,0x5aad,0x24ad,0x72a7,0x6496,0x4889,0x3493,0x002a,0xf000,0x0011,0x6b98,0x3b79,0x7270,0x7b74,0x6750,0x95d6,0xb9ee,0x5b59,0x6410,0xb482,0x56e8,0x6492,0x5be8,0x5b58,0x3b70,0x976a,0xcd6a,0x1370,0x38f0,0x64ba,0x3b68,0x2568,0x5f68,0x54a8,0xb9ad,0x73b8,0x64d6,0x2492,0x3593,0x03e0};
// clang-format on
static void fenster_text(struct fenster *f, int x, int y, char *s, int scale,
uint32_t c) {
while (*s) {
char chr = *s++;
if (chr > 32) {
uint16_t bmp = font5x3[chr - 32];
for (int dy = 0; dy < 5; dy++) {
for (int dx = 0; dx < 3; dx++) {
if (bmp >> (dy * 3 + dx) & 1) {
fenster_rect(f, x + dx * scale, y + dy * scale, scale, scale, c);
}
}
}
}
x = x + 4 * scale;
}
}
static int run() {
uint32_t buf[W * H];
struct fenster f = {
.title = "hello",
.width = W,
.height = H,
.buf = buf,
};
fenster_open(&f);
uint32_t t = 0;
int64_t now = fenster_time();
while (fenster_loop(&f) == 0) {
t++;
fenster_rect(&f, 0, 0, W, H, 0x00333333);
fenster_rect(&f, W / 4, H / 2, W / 2, H / 3, 0x00ff0000);
fenster_rect(&f, W / 2, H / 2 + H / 12, W / 6, H / 3 - H / 12, 0x00ffffff);
fenster_circle(&f, W / 2 - W / 8, H / 2 + H / 6, W / 20, 0x00ffffff);
fenster_line(&f, W / 4 - 25, H / 2, W / 2, H / 4, 0x0000ffff);
fenster_line(&f, W - W / 4 + 25, H / 2, W / 2, H / 4, 0x0000ffff);
fenster_line(&f, W - W / 4 + 25, H / 2, W / 4 - 25, H / 2, 0x0000ffff);
fenster_fill(&f, W / 2, H / 3, 0x00333333, 0x00ff00ff);
fenster_text(&f, 10, 10, "House", 8, 0x00ffffff);
int64_t time = fenster_time();
if (time - now < 1000 / 60)
fenster_sleep(time - now);
now = time;
}
fenster_close(&f);
return 0;
}
#if defined(_WIN32)
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
int nCmdShow) {
(void)hInstance, (void)hPrevInstance, (void)pCmdLine, (void)nCmdShow;
return run();
}
#else
int main() { return run(); }
#endif