Basic interface to plot triangles
Francesco Pasa 11 months ago
Francesco Pasa 11 months ago
demo.c M +18
8 options.clear_color.b = 0.02;
9 AlbaWindow* window = create_window(&options);
10
11+ const float vertices[] = {
12+ -0.5, -0.5,
13+ +0.5, -0.5,
14+ +0.5, +0.5,
15+ //
16+ -1.0, 0.0,
17+ -0.5, 0.75
18+ };
19+ const float color[] = {
20+ 1.0, 0.0, 0.0, 1.0,
21+ 0.0, 1.0, 0.0, 1.0,
22+ 0.0, 0.0, 1.0, 1.0,
23+ 1.0, 1.0, 1.0, 1.0,
24+ 1.0, 1.0, 1.0, 1.0,
25+ };
26+ const uint32_t indices[] = {0, 1, 2, 0, 3, 4};
27+ draw_triangles(window, 5, 6, vertices, color, indices);
28+
29 while (!window_should_close(window))
30 {
31 window_render(window);
include/alba.h M +10 -1
14
15 void float_array_reserve(FloatArray* array, uint64_t capacity);
16 void float_array_append(FloatArray* array, float value);
17+void float_array_extend(FloatArray* array, uint64_t size, const float* data);
18 void float_array_release(const FloatArray* array);
19 void float_array_clear(FloatArray* array);
20
27
28 void uint32_array_reserve(Uint32Array* array, uint64_t capacity);
29 void uint32_array_append(Uint32Array* array, uint32_t value);
30+void uint32_array_extend(Uint32Array* array, uint64_t size, const uint32_t* data);
31 void uint32_array_release(const Uint32Array* array);
32 void uint32_array_clear(Uint32Array* array);
33
75 void window_render(AlbaWindow* window);
76 void window_release(AlbaWindow* window);
77
78-void draw_triangles(AlbaWindow* window, uint64_t num_vertices, const float* vertices);
79+void draw_triangles(
80+ AlbaWindow* window,
81+ uint64_t num_vertices,
82+ uint64_t num_indices,
83+ const float* vertices,
84+ const float* attributes,
85+ const uint32_t* indices
86+);
87
88 #endif // ALBA_H
src/drawing.c M +12 -2
1 #include "alba.h"
2
3-void draw_triangles(AlbaWindow* window, uint64_t num_vertices, float* vertices)
4+void draw_triangles(
5+ AlbaWindow* window,
6+ const uint64_t num_vertices,
7+ const uint64_t num_indices,
8+ const float* vertices,
9+ const float* attributes,
10+ const uint32_t* indices
11+)
12 {
13- // dynarray_append(window->current_frame_buffers, );
14+ float_array_extend(&window->new_vertices, num_vertices * 2, vertices);
15+ float_array_extend(&window->new_attributes, num_vertices * 4, attributes);
16+ uint32_array_extend(&window->new_indices, num_indices, indices);
17+ window->dirty = 1;
18 }
src/dynarray.c M +15
1 #include "alba.h"
2
3 #include <stdlib.h>
4+#include <string.h>
5
6 void float_array_reserve(FloatArray* array, const uint64_t capacity)
7 {
26 array->length += 1;
27 }
28
29+void float_array_extend(FloatArray* array, const uint64_t size, const float* data)
30+{
31+ float_array_reserve(array, array->length + size);
32+ memcpy(array->data + array->length, data, size * sizeof(float));
33+ array->length += size;
34+}
35+
36 void float_array_release(const FloatArray* array)
37 {
38 free(array->data);
67 array->length += 1;
68 }
69
70+void uint32_array_extend(Uint32Array* array, const uint64_t size, const uint32_t* data)
71+{
72+ uint32_array_reserve(array, array->length + size);
73+ memcpy(array->data + array->length, data, size * sizeof(uint32_t));
74+ array->length += size;
75+}
76+
77 void uint32_array_release(const Uint32Array* array)
78 {
79 free(array->data);
src/window.c M -16
205
206 // This is necessary to initialize buffers
207 window->dirty = 1;
208- float_array_append(&window->new_vertices, -0.5);
209- float_array_append(&window->new_vertices, -0.5);
210- float_array_append(&window->new_vertices, +0.5);
211- float_array_append(&window->new_vertices, -0.5);
212- float_array_append(&window->new_vertices, +0.5);
213- float_array_append(&window->new_vertices, +0.5);
214- for (int i = 0; i < 3; i++)
215- {
216- float_array_append(&window->new_attributes, i / 2);
217- float_array_append(&window->new_attributes, (2 - i) / 2);
218- float_array_append(&window->new_attributes, 0.0);
219- float_array_append(&window->new_attributes, 1.0);
220- }
221- uint32_array_append(&window->new_indices, 0);
222- uint32_array_append(&window->new_indices, 1);
223- uint32_array_append(&window->new_indices, 2);
224
225 return window;
226 }