include/alba.h
1#ifndef ALBA_H
2#define ALBA_H
3
4#include "GLFW/glfw3.h"
5#include "webgpu.h"
6#include "ft2build.h"
7#include FT_FREETYPE_H
8
9// Dynamic arrays
10typedef struct
11{
12 uint64_t element_size;
13 uint64_t length;
14 uint64_t capacity;
15 void* data;
16} DynArray;
17
18DynArray dynarray_new(uint64_t element_size, uint64_t capacity);
19void dynarray_reserve(DynArray* array, uint64_t request);
20void dynarray_append(DynArray* array, const void* value);
21void dynarray_extend(DynArray* array, uint64_t size, const void* data);
22void dynarray_release(const DynArray* array);
23void dynarray_clear(DynArray* array);
24void dynarray_sort(const DynArray* array, uint32_t (*is_before_pivot)(void* pivot, void* elem));
25void dynarray_print(const DynArray* array, void (*print_element)(void* element));
26
27// Misc
28typedef struct
29{
30 float r, g, b, a;
31} AlbaColor;
32
33#define RED {1, 0, 0, 1}
34#define GREEN {0, 1, 0, 1}
35#define BLUE {0, 0, 1, 1}
36#define BLACK {0, 0, 0, 1}
37#define WHITE {1, 1, 1, 1}
38#define TRANSPARENT {0, 0, 0, 1}
39#define YELLOW {1, 1, 0, 1}
40#define MAGENTA {1, 0, 1, 1}
41#define CYAN {0, 1, 1, 1}
42
43void color_print(const AlbaColor* color);
44
45typedef struct
46{
47 float x, y;
48} AlbaVector;
49
50void vector_print(const AlbaVector* vector);
51
52inline AlbaVector vector_add(const AlbaVector a, const AlbaVector b)
53{
54 return (AlbaVector){a.x + b.x, a.y + b.y};
55}
56
57inline AlbaVector vector_sub(const AlbaVector a, const AlbaVector b)
58{
59 return (AlbaVector){a.x - b.x, a.y - b.y};
60}
61
62inline AlbaVector vector_mul(const AlbaVector a, const AlbaVector b)
63{
64 return (AlbaVector){a.x * b.x, a.y * b.y};
65}
66
67inline AlbaVector vector_div(const AlbaVector a, const AlbaVector b)
68{
69 return (AlbaVector){a.x / b.x, a.y / b.y};
70}
71
72typedef struct
73{
74 AlbaColor color;
75 AlbaVector uv;
76} AlbaAttribute;
77
78void attribute_print(const AlbaAttribute* attribute);
79
80// Window
81typedef struct
82{
83 char* title;
84 uint32_t initial_width;
85 uint32_t initial_height;
86 AlbaColor clear_color;
87} AlbaWindowOptions;
88
89// TODO: make opaque
90typedef struct
91{
92 // if set, the next frame will update the buffers
93 int dirty;
94 DynArray new_vertices;
95 WGPUBuffer vertices;
96 DynArray new_attributes;
97 WGPUBuffer attributes;
98 DynArray new_indices;
99 WGPUBuffer indices;
100 WGPUBuffer uniforms;
101 WGPUTexture texture;
102 WGPURenderPipeline pipeline;
103 WGPUBindGroup bind_group;
104} RenderPassData;
105
106// TODO: make opaque
107typedef struct
108{
109 AlbaWindowOptions options;
110 GLFWwindow* glfw_window;
111 AlbaVector size;
112 AlbaVector scale;
113 WGPUInstance instance;
114 WGPUSurface surface;
115 WGPUAdapter adapter;
116 WGPUDevice device;
117 WGPUQueue queue;
118 WGPUShaderModule shaders;
119 RenderPassData drawing;
120 RenderPassData text;
121} AlbaWindow;
122
123AlbaWindow* create_window(const AlbaWindowOptions* options);
124uint32_t window_should_close(const AlbaWindow* window);
125// Returns window size, in scaled coordinates (e.g. will honor
126// the windowing system scaling). The point at this coordinate
127// is at the bottom right of the window.
128void window_get_size(const AlbaWindow* window, float* width, float* height);
129void window_render(AlbaWindow* window);
130void window_release(AlbaWindow* window);
131
132void alba_release();
133
134// Low level drawing
135void draw_triangles_indexed(
136 AlbaWindow* window,
137 uint32_t num_vertices,
138 const AlbaVector* vertices,
139 const AlbaAttribute* attributes,
140 uint32_t num_indices,
141 uint32_t* indices
142);
143void draw_triangles(
144 AlbaWindow* window,
145 uint32_t num_vertices,
146 const AlbaVector* vertices,
147 const AlbaAttribute* attributes
148);
149
150// Higher level drawing
151void draw_triangle(AlbaWindow* window, const AlbaVector vertices[3], AlbaColor color);
152void draw_quad(AlbaWindow* window, const AlbaVector vertices[4], AlbaColor color);
153void draw_rect_aa(AlbaWindow* window, float x0, float y0, float x1, float y1, AlbaColor color);
154
155void generate_regular_polygon(DynArray* array, uint32_t num_sides, float x, float y, float r);
156void generate_circle(DynArray* array, float x, float y, float r);
157void generate_rounded_rect_aa(DynArray* array, float x0, float y0, float x1, float y1, float r);
158
159void draw_concave_polygon(AlbaWindow* window, float x, float y, const DynArray* contour, AlbaColor color);
160void draw_regular_polygon(AlbaWindow* window, uint32_t num_sides, float x, float y, float r, AlbaColor color);
161void draw_circle(AlbaWindow* window, float x, float y, float r, AlbaColor color);
162void draw_rounded_rect_aa(AlbaWindow* window, float x0, float y0, float x1, float y1, float r, AlbaColor color);
163
164void draw_text(
165 AlbaWindow* window,
166 AlbaVector pos, AlbaColor color,
167 uint64_t length, const char* text
168);
169
170#endif // ALBA_H