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 (*smaller_than_pivot)(void* pivot, void* elem));
25
26// Misc
27typedef struct
28{
29 float r;
30 float g;
31 float b;
32 float a;
33} AlbaColor;
34
35// Window
36typedef struct
37{
38 char* title;
39 uint32_t initial_width;
40 uint32_t initial_height;
41 AlbaColor clear_color;
42} AlbaWindowOptions;
43
44typedef struct
45{
46 // if set, the next frame will update the buffers
47 int dirty;
48 DynArray new_vertices;
49 WGPUBuffer vertices;
50 DynArray new_attributes;
51 WGPUBuffer attributes;
52 DynArray new_indices;
53 WGPUBuffer indices;
54 WGPUBuffer uniforms;
55} RenderPassData;
56
57typedef struct
58{
59 AlbaWindowOptions options;
60 GLFWwindow* glfw_window;
61 uint32_t width, height;
62 WGPUInstance instance;
63 WGPUSurface surface;
64 WGPUAdapter adapter;
65 WGPUDevice device;
66 WGPUQueue queue;
67 WGPUShaderModule shaders;
68 WGPURenderPipeline pipeline;
69 WGPUBindGroup bind_group;
70 RenderPassData drawing;
71 RenderPassData text;
72} AlbaWindow;
73
74AlbaWindow* create_window(const AlbaWindowOptions* options);
75uint32_t window_should_close(const AlbaWindow* window);
76// Returns window size, in scaled coordinates (e.g. will honor
77// the windowing system scaling). The point at this coordinate
78// is at the bottom right of the window.
79void window_get_size(const AlbaWindow* window, float* width, float* height);
80void window_render(AlbaWindow* window);
81void window_release(AlbaWindow* window);
82
83void alba_release();
84
85// Low level drawing
86void draw_triangles_indexed(
87 AlbaWindow* window,
88 uint32_t num_vertices,
89 const float* vertices,
90 const float* attributes,
91 uint32_t num_indices,
92 uint32_t* indices
93);
94void draw_triangles(
95 AlbaWindow* window,
96 uint32_t num_vertices,
97 const float* vertices,
98 const float* attributes
99);
100
101// Higher level drawing
102void draw_triangle(AlbaWindow* window, const float vertices[6], AlbaColor color);
103void draw_quad(AlbaWindow* window, const float vertices[8], AlbaColor color);
104void draw_rect_aa(AlbaWindow* window, float x0, float y0, float x1, float y1, AlbaColor color);
105
106void generate_regular_polygon(DynArray* array, uint32_t num_sides, float x, float y, float r);
107void generate_circle(DynArray* array, float x, float y, float r);
108void generate_rounded_rect_aa(DynArray* array, float x0, float y0, float x1, float y1, float r);
109
110void draw_concave_polygon(AlbaWindow* window, float x, float y, DynArray* contour, AlbaColor color);
111void draw_regular_polygon(AlbaWindow* window, uint32_t num_sides, float x, float y, float r, AlbaColor color);
112void draw_circle(AlbaWindow* window, float x, float y, float r, AlbaColor color);
113void draw_quad(AlbaWindow* window, const float vertices[8], AlbaColor color);
114void draw_rounded_rect_aa(AlbaWindow* window, float x0, float y0, float x1, float y1, float r, AlbaColor color);
115
116void draw_text(AlbaWindow* window, const float x, const float y, uint64_t length, const char* text);
117
118#endif // ALBA_H