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