Implemented draw circle

Francesco Pasa 10 months ago

demo.c   M +7

33     draw_rect(window, rect_vertices, blue);
34 
35     draw_regular_polygon(window, 5, 320, 240, 20, blue);
36+    draw_circle(window, 400, 240, 5, blue);
37+    draw_circle(window, 420, 240, 6, blue);
38+    draw_circle(window, 440, 240, 7, blue);
39+    draw_circle(window, 460, 240, 10, blue);
40+    draw_circle(window, 480, 240, 15, blue);
41+    draw_circle(window, 500, 240, 20, blue);
42+    draw_circle(window, 400, 540, 200, blue);
43 
44     while (!window_should_close(window))
45     {

include/alba.h   M +3 -2

53 {
54     AlbaWindowOptions options;
55     GLFWwindow* glfw_window;
56+    uint32_t width, height;
57     WGPUInstance instance;
58     WGPUSurface surface;
59     WGPUAdapter adapter;
104 void draw_rect_aa(AlbaWindow* window, float x0, float y0, float x1, float y1, AlbaColor color);
105 
106 void regular_polygon_points(FloatArray* array, uint32_t num_sides, float x, float y, float r);
107-// FloatArray circle_points(float x, float y, float r);
108+void circle_points(FloatArray* array, float x, float y, float r);
109 
110 void draw_concave_polygon(AlbaWindow* window, float x, float y, FloatArray* contour, AlbaColor color);
111 void draw_regular_polygon(AlbaWindow* window, uint32_t num_sides, float x, float y, float r, AlbaColor color);
112-// void draw_circle(AlbaColor window, float x, float y, float r);
113+void draw_circle(AlbaWindow* window, float x, float y, float r, AlbaColor color);
114 
115 #endif // ALBA_H

src/drawing.c   M +19 -11

113     }
114 }
115 
116+void circle_points(FloatArray* array, const float x, const float y, const float r)
117+{
118+    const uint32_t num_sides = 8 + 2 * PI * r / 10;
119+    regular_polygon_points(array, num_sides, x, y, r);
120+}
121+
122 void draw_concave_polygon(AlbaWindow* window, const float x, const float y, FloatArray* contour, AlbaColor color)
123 {
124     const uint32_t num_sides = contour->length / 2;
135     }
136 
137     uint32_t* indices = malloc(num_sides * 3 * sizeof(uint32_t));
138-    for (uint32_t i = 0; i < num_vertices; i++)
139+    for (uint32_t i = 0; i < num_sides; i++)
140     {
141-        indices[i * 3] = 0;
142-        indices[i * 3 + 1] = i + 1;
143-        indices[i * 3 + 2] = i + 2;
144+        indices[i * 3] = num_vertices - 1;
145+        indices[i * 3 + 1] = i;
146+        indices[i * 3 + 2] = i + 1;
147     }
148-    indices[num_sides * 3 - 1] = 1;
149+    indices[num_sides * 3 - 1] = 0;
150 
151     draw_triangles_indexed(window, num_sides + 1, contour->data, colors.data, num_sides * 3, indices);
152 
160                "A polygon must have at least 3 sides\n", num_sides);
161         exit(1);
162     }
163+
164     FloatArray polyon = {0};
165     regular_polygon_points(&polyon, num_sides, x, y, r);
166     draw_concave_polygon(window, x, y, &polyon, color);
167-    printf("%p\n", &polyon.data);
168     float_array_release(&polyon);
169 }
170 
171-// FloatArray circle_points(float x, float y, float r)
172-// {
173-//     uint32_t num_points = 10;
174-//
175-// }
176+void draw_circle(AlbaWindow* window, const float x, const float y, const float r, const AlbaColor color)
177+{
178+    FloatArray circle = {0};
179+    circle_points(&circle, x, y, r);
180+    draw_concave_polygon(window, x, y, &circle, color);
181+    float_array_release(&circle);
182+}

src/dynarray.c   M +2

1+#include <stdio.h>
2+
3 #include "alba.h"
4 
5 #include <stdlib.h>

src/window.c   M +6 -3

104     return pipeline_layout;
105 }
106 
107-void configure_surface(const AlbaWindow* window)
108+void configure_surface(AlbaWindow* window)
109 {
110     int width, height;
111     float x_scale, y_scale;
113 
114     if (width == 0 || height == 0) return;
115 
116+    window->width = width;
117+    window->height = height;
118+
119     // Update uniforms
120     const float scale[2] = {width / (2. * x_scale), height / (2. * y_scale)};
121     wgpuQueueWriteBuffer(window->queue, window->uniforms, 0, &scale, 2 * sizeof(uint32_t));
421     texture_options.usage = WGPUTextureUsage_RenderAttachment;
422     texture_options.format = WGPUTextureFormat_BGRA8UnormSrgb; // TODO: avoid hardcoding
423     texture_options.dimension = WGPUTextureDimension_2D;
424-    texture_options.size.width = 800;
425-    texture_options.size.height = 600;
426+    texture_options.size.width = window->width;
427+    texture_options.size.height = window->height;
428     texture_options.size.depthOrArrayLayers = 1;
429     texture_options.sampleCount = 4;
430     texture_options.mipLevelCount = 1;