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    uint32_t width, height;
 57    WGPUInstance instance;
 58    WGPUSurface surface;
 59    WGPUAdapter adapter;
 60    WGPUDevice device;
 61    WGPUQueue queue;
 62    WGPUShaderModule shaders;
 63    WGPURenderPipeline pipeline;
 64    WGPUBindGroup bind_group;
 65    // Drawing
 66    int dirty; // if set, the next frame will update the buffers
 67    FloatArray new_vertices;
 68    WGPUBuffer vertices;
 69    FloatArray new_attributes;
 70    WGPUBuffer attributes;
 71    Uint32Array new_indices;
 72    WGPUBuffer indices;
 73    WGPUBuffer uniforms;
 74} AlbaWindow;
 75
 76AlbaWindow* create_window(const AlbaWindowOptions* options);
 77uint32_t window_should_close(const AlbaWindow* window);
 78// Returns window size, in scaled coordinates (e.g. will honor
 79// the windowing system scaling). The point at this coordinate
 80// is at the bottom right of the window.
 81void window_get_size(const AlbaWindow* window, float* width, float* height);
 82void window_render(AlbaWindow* window);
 83void window_release(AlbaWindow* window);
 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(FloatArray* array, uint32_t num_sides, float x, float y, float r);
107void generate_circle(FloatArray* array, float x, float y, float r);
108void generate_rounded_rect_aa(FloatArray* array, float x0, float y0, float x1, float y1, float r);
109
110void draw_concave_polygon(AlbaWindow* window, float x, float y, FloatArray* 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
116#endif // ALBA_H