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