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    double r;
 38    double g;
 39    double b;
 40    double 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(
 86    AlbaWindow* window,
 87    uint64_t num_vertices,
 88    const float* vertices,
 89    const float* attributes
 90);
 91void draw_triangles_indexed(
 92    AlbaWindow* window,
 93    uint64_t num_vertices,
 94    const float* vertices,
 95    const float* attributes,
 96    uint64_t num_indices,
 97    const uint32_t* indices
 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
105#endif // ALBA_H