commit 49ced8d
dalem
·
2026-02-07 22:52:47 +0000 UTC
parent 4e7e043
add circle rendering and shit
4 files changed,
+56,
-0
+2,
-0
1@@ -33,6 +33,7 @@ static void renderer_copy_rectangle(struct wld_renderer *renderer,
2 int32_t dst_x, int32_t dst_y,
3 int32_t src_x, int32_t src_y,
4 uint32_t width, uint32_t height);
5+
6 #ifdef RENDERER_IMPLEMENTS_REGION
7 static void renderer_fill_region(struct wld_renderer *base, uint32_t color,
8 pixman_region32_t *region);
9@@ -61,6 +62,7 @@ static const struct wld_renderer_impl wld_renderer_impl = {
10 .fill_region = &default_fill_region,
11 .copy_region = &default_copy_region,
12 #endif
13+ .draw_circle = &wld_draw_circle,
14 .draw_text = &renderer_draw_text,
15 .flush = &renderer_flush,
16 .destroy = &renderer_destroy
+49,
-0
1@@ -139,6 +139,55 @@ wld_copy_region(struct wld_renderer *renderer,
2 dst_x, dst_y, region);
3 }
4
5+
6+/* https://en.wikipedia.org/wiki/Midpoint_circle_algorithm :wq*/
7+static void
8+circle_points(struct wld_renderer *renderer, uint32_t color,
9+ int32_t x1, int32_t y1, int32_t x2, int32_t y2, bool fill)
10+{
11+ /* hacky */
12+ if (fill) {
13+ renderer->impl->fill_rectangle(renderer, color, x1-x2, y1+y2, 2*x2 + 1, 1);
14+ renderer->impl->fill_rectangle(renderer, color, x1-x2, y1-y2, 2*x2 + 1, 1);
15+ renderer->impl->fill_rectangle(renderer, color, x1-y2, y1+x2, 2*y2 + 1, 1);
16+ renderer->impl->fill_rectangle(renderer, color, x1-y2, y1-x2, 2*y2 + 1, 1);
17+ }
18+
19+ else {
20+ renderer->impl->fill_rectangle(renderer, color, x1+x2, y1+y2, 1, 1);
21+ renderer->impl->fill_rectangle(renderer, color, x1-x2, y1+y2, 1, 1);
22+ renderer->impl->fill_rectangle(renderer, color, x1+x2, y1-y2, 1, 1);
23+ renderer->impl->fill_rectangle(renderer, color, x1-x2, y1-y2, 1, 1);
24+ renderer->impl->fill_rectangle(renderer, color, x1+y2, y1+x2, 1, 1);
25+ renderer->impl->fill_rectangle(renderer, color, x1-y2, y1+x2, 1, 1);
26+ renderer->impl->fill_rectangle(renderer, color, x1+y2, y1-x2, 1, 1);
27+ renderer->impl->fill_rectangle(renderer, color, x1-y2, y1-x2, 1, 1);
28+ }
29+}
30+
31+EXPORT
32+void
33+wld_draw_circle(struct wld_renderer *renderer, uint32_t color,
34+ int32_t x, int32_t y, uint32_t r, bool fill)
35+{
36+ int32_t x1 = 0, y1 = r;
37+ int32_t d = 3 - 2 * r;
38+ circle_points(renderer, color, x, y, x1, y1, fill);
39+
40+ while (y1 >= x1) {
41+ if (d > 0) {
42+ y1--;
43+ d = d + 4 * (x1 - y1) + 10;
44+ }
45+ else
46+ d = d + 4 * x1 + 6;
47+
48+ x1++;
49+
50+ circle_points(renderer, color, x, y, x1, y1, fill);
51+ }
52+}
53+
54 EXPORT
55 void
56 wld_draw_text(struct wld_renderer *renderer,
+2,
-0
1@@ -108,6 +108,8 @@ struct wld_renderer_impl {
2 void (*copy_region)(struct wld_renderer *renderer, struct buffer *src,
3 int32_t dst_x, int32_t dst_y,
4 pixman_region32_t *region);
5+ void (*draw_circle)(struct wld_renderer *renderer, uint32_t color,
6+ int32_t x, int32_t y, uint32_t r, bool fill);
7 void (*draw_text)(struct wld_renderer *renderer,
8 struct font *font, uint32_t color,
9 int32_t x, int32_t y, const char *text, uint32_t length,
M
wld.h
+3,
-0
1@@ -253,6 +253,9 @@ void wld_copy_region(struct wld_renderer *renderer,
2 struct wld_buffer *buffer,
3 int32_t dst_x, int32_t dst_y, pixman_region32_t *region);
4
5+void wld_draw_circle(struct wld_renderer *renderer, uint32_t color,
6+ int32_t x, int32_t y, uint32_t r, bool fill);
7+
8 /**
9 * Draw a UTF-8 text string to the given buffer.
10 *