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