Fix drawing functions to use new structs and UV attribute
Francesco Pasa 10 months ago
Francesco Pasa 10 months ago
examples/shapes.c M +21 -21
9 AlbaWindow* window = create_window(&options);
10
11 const AlbaVector tri_vertices[] = {
12- {100, 100},
13- {100, 400},
14- {200, 100},
15+ 100, 100,
16+ 100, 400,
17+ 200, 100,
18 //
19- {300, 150},
20- {150, 300}
21+ 300, 150,
22+ 150, 300
23 };
24 const AlbaAttribute attributes[] = {
25 {{1, 0, 0, 1}, {-1, -1}},
26 uint32_t indices[] = {0, 1, 2, 0, 3, 4};
27 draw_triangles_indexed(window, 5, tri_vertices, attributes, 6, indices);
28
29- // const AlbaColor blue = {0.0, 0.4, 1.0, 1.0};
30- // draw_rect_aa(window, 350, 100, 500, 150, blue);
31- //
32- // const float rect_vertices[] = {350, 300, 500, 350, 450, 400, 300, 350};
33- // draw_quad(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, 640, 200, blue);
43- //
44- // draw_rounded_rect_aa(window, 350, 30, 500, 80, 10, blue);
45+ const AlbaColor blue = {0.0, 0.4, 1.0, 1.0};
46+ draw_rect_aa(window, 350, 100, 500, 150, blue);
47+
48+ const float rect_vertices[] = {350, 300, 500, 350, 450, 400, 300, 350};
49+ draw_quad(window, rect_vertices, blue);
50+
51+ draw_regular_polygon(window, 5, 320, 240, 20, blue);
52+ draw_circle(window, 400, 240, 5, blue);
53+ draw_circle(window, 420, 240, 6, blue);
54+ draw_circle(window, 440, 240, 7, blue);
55+ draw_circle(window, 460, 240, 10, blue);
56+ draw_circle(window, 480, 240, 15, blue);
57+ draw_circle(window, 500, 240, 20, blue);
58+ draw_circle(window, 400, 640, 200, blue);
59+
60+ draw_rounded_rect_aa(window, 350, 30, 500, 80, 10, blue);
61
62 while (!window_should_close(window))
63 {
include/alba.h M +7 -8
97 void draw_triangles_indexed(
98 AlbaWindow* window,
99 uint32_t num_vertices,
100- const float* vertices,
101- const float* attributes,
102+ const AlbaVector* vertices,
103+ const AlbaAttribute* attributes,
104 uint32_t num_indices,
105 uint32_t* indices
106 );
107 void draw_triangles(
108 AlbaWindow* window,
109 uint32_t num_vertices,
110- const float* vertices,
111- const float* attributes
112+ const AlbaVector* vertices,
113+ const AlbaAttribute* attributes
114 );
115
116 // Higher level drawing
117-void draw_triangle(AlbaWindow* window, const float vertices[6], AlbaColor color);
118-void draw_quad(AlbaWindow* window, const float vertices[8], AlbaColor color);
119+void draw_triangle(AlbaWindow* window, const AlbaVector vertices[3], AlbaColor color);
120+void draw_quad(AlbaWindow* window, const AlbaVector vertices[4], AlbaColor color);
121 void draw_rect_aa(AlbaWindow* window, float x0, float y0, float x1, float y1, AlbaColor color);
122
123 void generate_regular_polygon(DynArray* array, uint32_t num_sides, float x, float y, float r);
124 void generate_circle(DynArray* array, float x, float y, float r);
125 void generate_rounded_rect_aa(DynArray* array, float x0, float y0, float x1, float y1, float r);
126
127-void draw_concave_polygon(AlbaWindow* window, float x, float y, DynArray* contour, AlbaColor color);
128+void draw_concave_polygon(AlbaWindow* window, float x, float y, const DynArray* contour, AlbaColor color);
129 void draw_regular_polygon(AlbaWindow* window, uint32_t num_sides, float x, float y, float r, AlbaColor color);
130 void draw_circle(AlbaWindow* window, float x, float y, float r, AlbaColor color);
131-void draw_quad(AlbaWindow* window, const float vertices[8], AlbaColor color);
132 void draw_rounded_rect_aa(AlbaWindow* window, float x0, float y0, float x1, float y1, float r, AlbaColor color);
133
134 void draw_text(AlbaWindow* window, const float x, const float y, uint64_t length, const char* text);
src/drawing.c M +66 -59
7 #define PI 3.14159265358979323846
8 #define MAX_ERROR 0.25
9
10+AlbaVector NO_TEXTURE = {-1, -1};
11+
12 void draw_triangles_indexed(
13 AlbaWindow* window,
14 const uint32_t num_vertices,
15- const float* vertices,
16- const float* attributes,
17+ const AlbaVector* vertices,
18+ const AlbaAttribute* attributes,
19 const uint32_t num_indices,
20 uint32_t* indices
21 )
39 void draw_triangles(
40 AlbaWindow* window,
41 const uint32_t num_vertices,
42- const float* vertices,
43- const float* attributes
44+ const AlbaVector* vertices,
45+ const AlbaAttribute* attributes
46 )
47 {
48 uint32_t* indices = malloc(num_vertices * sizeof(uint32_t));
63 free(indices);
64 }
65
66-void draw_triangle(AlbaWindow* window, const float vertices[6], const AlbaColor color)
67+void draw_triangle(AlbaWindow* window, const AlbaVector vertices[3], const AlbaColor color)
68 {
69- const float attributes[] = {
70- color.r, color.g, color.b, color.a,
71- color.r, color.g, color.b, color.a,
72- color.r, color.g, color.b, color.a,
73+ const AlbaAttribute attributes[] = {
74+ color.r, color.g, color.b, color.a, NO_TEXTURE,
75+ color.r, color.g, color.b, color.a, NO_TEXTURE,
76+ color.r, color.g, color.b, color.a, NO_TEXTURE,
77 };
78 // Does not try to consider winding order
79 uint32_t indices[] = {0, 1, 2};
80 draw_triangles_indexed(window, 3, vertices, attributes, 3, indices);
81 }
82
83-void draw_quad(AlbaWindow* window, const float vertices[8], const AlbaColor color)
84+void draw_quad(AlbaWindow* window, const AlbaVector vertices[4], const AlbaColor color)
85 {
86- const float attributes[] = {
87- color.r, color.g, color.b, color.a,
88- color.r, color.g, color.b, color.a,
89- color.r, color.g, color.b, color.a,
90- color.r, color.g, color.b, color.a,
91+ const AlbaAttribute attributes[] = {
92+ color.r, color.g, color.b, color.a, NO_TEXTURE,
93+ color.r, color.g, color.b, color.a, -1, -1,
94+ color.r, color.g, color.b, color.a, -1, -1,
95+ color.r, color.g, color.b, color.a, -1, -1,
96 };
97 // Does not try to consider winding order
98 uint32_t indices[] = {0, 1, 2, 0, 2, 3};
105 exit(1);
106 }
107
108- const float vertices[] = {
109+ const AlbaVector vertices[] = {
110 x0, y0,
111 x0, y1,
112 x1, y1,
118 DynArray* array, const uint32_t num_sides,
119 const float x, const float y, const float r)
120 {
121- dynarray_reserve(array, array->length + num_sides * 2);
122+ dynarray_reserve(array, array->length + num_sides);
123
124 const float step_angle = 2 * PI / num_sides;
125 for (uint32_t i = 0; i < num_sides; i++)
126 {
127- dynarray_extend(
128- array, 2,
129- &(float[]){
130+ dynarray_append(
131+ array,
132+ &(AlbaVector){
133 x + r * sin(step_angle * i),
134 y - r * cos(step_angle * i)
135 }
154 if (x1 - x0 < r) r = (x1 - x0) / 2;
155 if (y1 - y0 < r) r = (y1 - y0) / 2;
156
157- dynarray_reserve(array, array->length + 4 + num_sides);
158+ dynarray_reserve(array, array->length + num_sides);
159
160 uint32_t i = 0;
161 for (; i < num_sides_per_corner + 1; i++)
162 {
163- dynarray_extend(
164- array, 2,
165- &(float[]){
166+ dynarray_append(
167+ array,
168+ &(AlbaVector){
169 x0 + r - r * sin(step_angle * i),
170 y0 + r - r * cos(step_angle * i)
171- });
172+ }
173+ );
174 }
175 i -= 1;
176
177 for (; i < 2 * num_sides_per_corner + 1; i++)
178 {
179- dynarray_extend(
180- array, 2,
181- &(float[]){
182+ dynarray_append(
183+ array,
184+ &(AlbaVector){
185 x0 + r - r * sin(step_angle * i),
186 y1 - r - r * cos(step_angle * i)
187- });
188+ }
189+ );
190 }
191 i -= 1;
192
193 for (; i < 3 * num_sides_per_corner + 1; i++)
194 {
195- dynarray_extend(
196- array, 2,
197- &(float[]){
198+ dynarray_append(
199+ array,
200+ &(AlbaVector){
201 x1 - r - r * sin(step_angle * i),
202 y1 - r - r * cos(step_angle * i)
203- });
204+ }
205+ );
206 }
207 i -= 1;
208
209 for (; i < 4 * num_sides_per_corner + 1; i++)
210 {
211- dynarray_extend(
212- array, 2,
213- &(float[]){
214+ dynarray_append(
215+ array,
216+ &(AlbaVector){
217 x1 - r - r * sin(step_angle * i),
218 y0 + r - r * cos(step_angle * i)
219- });
220+ }
221+ );
222 }
223 }
224
225-void draw_concave_polygon(AlbaWindow* window, const float x, const float y, DynArray* contour, AlbaColor color)
226+void draw_concave_polygon(AlbaWindow* window, const float x, const float y, const DynArray* contour,
227+ const AlbaColor color)
228 {
229- const uint32_t num_sides = contour->length / 2;
230- const uint32_t num_vertices = num_sides + 1;
231-
232- dynarray_append(contour, &x);
233- dynarray_append(contour, &y);
234+ const uint32_t num_sides = contour->length;
235+ const uint32_t num_triangles = num_sides - 2;
236
237- DynArray colors = dynarray_new(sizeof(uint32_t), num_vertices * 4);
238- for (uint32_t i = 0; i < num_vertices; i++)
239+ DynArray attributes = dynarray_new(sizeof(AlbaAttribute), num_sides);
240+ for (uint32_t i = 0; i < num_sides; i++)
241 {
242- dynarray_extend(&colors, 4, (float*)&color);
243+ dynarray_append(&attributes, &(AlbaAttribute){color, NO_TEXTURE});
244 }
245
246- uint32_t* indices = malloc(num_sides * 3 * sizeof(uint32_t));
247- for (uint32_t i = 0; i < num_sides; i++)
248+ uint32_t* indices = malloc(num_triangles * 3 * sizeof(uint32_t));
249+ for (uint32_t i = 0; i < num_triangles; i++)
250 {
251- indices[i * 3] = num_vertices - 1;
252- indices[i * 3 + 1] = i;
253- indices[i * 3 + 2] = i + 1;
254+ indices[i * 3] = 0;
255+ indices[i * 3 + 1] = i + 1;
256+ indices[i * 3 + 2] = i + 2;
257 }
258- indices[num_sides * 3 - 1] = 0;
259
260- draw_triangles_indexed(window, num_sides + 1, contour->data, colors.data, num_sides * 3, indices);
261+ draw_triangles_indexed(
262+ window,
263+ num_sides, contour->data, attributes.data,
264+ num_triangles * 3, indices
265+ );
266
267 free(indices);
268 }
247 exit(1);
248 }
249
250- DynArray polyon = dynarray_new(sizeof(float), 0);
251- generate_regular_polygon(&polyon, num_sides, x, y, r);
252- draw_concave_polygon(window, x, y, &polyon, color);
253- dynarray_release(&polyon);
254+ DynArray polygon = dynarray_new(sizeof(AlbaVector), 0);
255+ generate_regular_polygon(&polygon, num_sides, x, y, r);
256+ draw_concave_polygon(window, x, y, &polygon, color);
257+ dynarray_release(&polygon);
258 }
259
260 void draw_circle(AlbaWindow* window, const float x, const float y, const float r, const AlbaColor color)
261 {
262- DynArray circle = dynarray_new(sizeof(float), 0);
263+ DynArray circle = dynarray_new(sizeof(AlbaVector), 0);
264 generate_circle(&circle, x, y, r);
265 draw_concave_polygon(window, x, y, &circle, color);
266 dynarray_release(&circle);
273 return;
274 }
275
276- DynArray contour = dynarray_new(sizeof(float), 0);
277+ DynArray contour = dynarray_new(sizeof(AlbaVector), 0);
278 generate_rounded_rect_aa(&contour, x0, y0, x1, y1, r);
279 draw_concave_polygon(window, (x0 + x1) / 2, (y0 + y1) / 2, &contour, color);
280 }
TODO.md A +1