include/alba.h
1#ifndef ALBA_H
2#define ALBA_H
3
4#include "GLFW/glfw3.h"
5#include "webgpu.h"
6
7// Dynamic arrays
8typedef struct
9{
10 uint64_t length;
11 uint64_t capacity;
12 float* data;
13} FloatArray;
14
15void float_array_reserve(FloatArray* array, uint64_t capacity);
16void float_array_append(FloatArray* array, float value);
17void float_array_extend(FloatArray* array, uint64_t size, const float* data);
18void float_array_release(const FloatArray* array);
19void float_array_clear(FloatArray* array);
20
21typedef struct
22{
23 uint64_t length;
24 uint64_t capacity;
25 uint32_t* data;
26} Uint32Array;
27
28void uint32_array_reserve(Uint32Array* array, uint64_t capacity);
29void uint32_array_append(Uint32Array* array, uint32_t value);
30void uint32_array_extend(Uint32Array* array, uint64_t size, const uint32_t* data);
31void uint32_array_release(const Uint32Array* array);
32void uint32_array_clear(Uint32Array* array);
33
34// Misc
35typedef struct
36{
37 float r;
38 float g;
39 float b;
40 float a;
41} AlbaColor;
42
43// Window
44typedef struct
45{
46 char* title;
47 uint32_t initial_width;
48 uint32_t initial_height;
49 AlbaColor clear_color;
50} AlbaWindowOptions;
51
52typedef struct
53{
54 AlbaWindowOptions options;
55 GLFWwindow* glfw_window;
56 WGPUInstance instance;
57 WGPUSurface surface;
58 WGPUAdapter adapter;
59 WGPUDevice device;
60 WGPUQueue queue;
61 WGPUShaderModule shaders;
62 WGPURenderPipeline pipeline;
63 WGPUBindGroup bind_group;
64 // Drawing
65 int dirty; // if set, the next frame will update the buffers
66 FloatArray new_vertices;
67 WGPUBuffer vertices;
68 FloatArray new_attributes;
69 WGPUBuffer attributes;
70 Uint32Array new_indices;
71 WGPUBuffer indices;
72 WGPUBuffer uniforms;
73} AlbaWindow;
74
75AlbaWindow* create_window(const AlbaWindowOptions* options);
76uint32_t window_should_close(const AlbaWindow* window);
77// Returns window size, in scaled coordinates (e.g. will honor
78// the windowing system scaling). The point at this coordinate
79// is at the bottom right of the window.
80void window_get_size(const AlbaWindow* window, float* width, float* height);
81void window_render(AlbaWindow* window);
82void window_release(AlbaWindow* window);
83
84// Low level drawing
85void draw_triangles_indexed(
86 AlbaWindow* window,
87 uint32_t num_vertices,
88 const float* vertices,
89 const float* attributes,
90 uint32_t num_indices,
91 uint32_t* indices
92);
93void draw_triangles(
94 AlbaWindow* window,
95 uint32_t num_vertices,
96 const float* vertices,
97 const float* attributes
98);
99
100// Higher level drawing
101void draw_triangle(AlbaWindow* window, const float vertices[6], AlbaColor color);
102void draw_rect(AlbaWindow* window, const float vertices[8], AlbaColor color);
103void draw_rect_aa(AlbaWindow* window, float x0, float y0, float x1, float y1, AlbaColor color);
104
105void regular_polygon_points(FloatArray* array, uint32_t num_sides, float x, float y, float r);
106// FloatArray circle_points(float x, float y, float r);
107
108void draw_concave_polygon(AlbaWindow* window, float x, float y, FloatArray* contour, AlbaColor color);
109void draw_regular_polygon(AlbaWindow* window, uint32_t num_sides, float x, float y, float r, AlbaColor color);
110// void draw_circle(AlbaColor window, float x, float y, float r);
111
112#endif // ALBA_H