playbit / docs

Graphics


#include <playbit/draw.h>

PBUIFont

struct PBUIFont {
    PBStrSlice family; // font family name (e.g. PBStrLit("Inter"))
    f32        size;   // font size in pixels (e.g. 16.0)
    f32        weight; // font weight between 100-900
};

PBTextCacheEntry

struct PBTextCacheEntry {
    u64 hash;
    // key
    u8* text; // copied into cache arena
    u32 textLen;
    u8* fontFamily; // copied into cache arena
    u32 fontFamilyLen;
    u32 fontSize; // scaled 26.6 fixed point
    u32 weight;
    u32 color;
    f32 maxWidth;
    f32 maxHeight;
    u32 flags;
    // value
    PBSysHandle plan;
    u64         lastFrame;
};

PBTextCache

struct PBTextCache {
    PBArena*          arena; // owns text copies + entry array
    PBTextCacheEntry* entries;
    u64               capacity; // power of two
    u64               count;
    u64               frame; // incremented each PBTextCacheNextFrame()
    // context required to create plans
    PBWindow window;
};

PBRenderCommand

struct PBRenderCommand {
    PBSysWindowRendererInstructionKind kind;
    union {
        PBSysWindowRendererShapeItem shape;
        PBSysWindowRendererTextItem  text;
        PBSysWindowRendererClipItem  clip;
        PBSysHandle                  texture;
    };
};

PBRender

struct PBRender {
    PBArena* arena;

    struct {
        PBRenderCommand* data;
        u64              count;
        u64              capacity;
    } cmds;
};

PBDrawContext

struct PBDrawContext {
    PBArena*    arena;
    PBRender    render;
    PBTextCache textCache;

    PBWindow window;
    f32      scale;
    u32      clipDepth;
};

PBTexture

struct PBTexture {
    PBSysHandle handle;
};

PBStrokeType

typedef enum PB_ENUM_TYPE(uint32_t) {
    PBStrokeType_CENTER = 0,
    PBStrokeType_INNER = 1,
    PBStrokeType_OUTER = 2,
} PBStrokeType;

PBTextplanWidthOfSize

#define PBTextplanWidthOfSize(x)

PBSysTextplanGetSize result macros

PBTextureCreate

PBTexture PBTextureCreate(PBWindow                      window,
                          PBSysTextureFormat            format,
                          u32                           width,
                          u32                           height,
                          PBSysWindowCreateTextureFlags flags);

PBTextureCreate creates a GPU texture for window with the given format, dimensions, and flags. Returns a texture with an invalid handle if the GPU is not yet ready; check with PBTextureIsValid.

PBTextureCreateFromData

PBTexture PBTextureCreateFromData(PBWindow                      window,
                                  const void*                   data,
                                  u64                           dataSize,
                                  PBSysWindowCreateTextureFlags flags);

PBTextureCreateFromData decodes encoded image bytes (PNG, JPEG, WebP, GIF, BMP, TGA) and creates an upload-ready GPU texture in one call, with no guest-side pixel buffer. Returns a texture with an invalid handle on failure; check with PBTextureIsValid.

PBTextureIsValid

bool PBTextureIsValid(PBTexture texture);

PBTextureIsValid returns true if the texture handle is valid (i.e. successfully created).

PBTextureWrite

void PBTextureWrite(PBTexture   texture,
                    u32         originX,
                    u32         originY,
                    const void* pixels,
                    u64         pixelSize,
                    u32         width,
                    u32         height);

PBTextureWrite uploads a pixel region to the texture starting at (originX, originY). pixels must point to at least pixelSize bytes covering a width x height region.

PBTextureDestroy

void PBTextureDestroy(PBTexture* texture);

PBTextureDestroy releases the texture's GPU resources and invalidates the handle.

PBDrawSetGlobal

void PBDrawSetGlobal(PBDrawContext* ctx);

PBDrawSetGlobal sets the thread-local draw context used by PBDraw* calls.

PBDrawGetGlobal

PBDrawContext* PBDrawGetGlobal();

PBDrawGetGlobal returns the active thread-local draw context.

PBDrawInit

void PBDrawInit(PBDrawContext* ctx,
                PBArena*       arena,
                PBWindow       window);

PBDrawInit initializes draw/render/text-cache state for window.

PBDrawBeginFrame

void PBDrawBeginFrame(PBWindow window);

PBDrawBeginFrame starts a frame and refreshes window scale and per-frame draw state.

PBDrawEndFrame

void PBDrawEndFrame();

PBDrawEndFrame submits queued commands and advances text-cache frame lifetime.

PBDrawSetScale

void PBDrawSetScale(f32 scale);

PBDrawSetScale sets the drawing scale for the current frame. By default, the scale is set to the window's scale (drawing is in dps, not pixels).

PBDrawGetScale

f32 PBDrawGetScale();

PBDrawGetScale gets the drawing scale for the current frame.

PBDrawRect

void PBDrawRect(PBRectangle bounds,
                PBColor     color);

PBDrawRect enqueues a filled rectangle with square corners.

PBDrawRectExt

void PBDrawRectExt(PBRectangle bounds,
                   PBColor     color,
                   PBVector4   cornerRadius);

PBDrawRectExt enqueues a filled rectangle with per-corner radius.

PBDrawRectExt2

void PBDrawRectExt2(PBRectangle bounds,
                    PBColor     colorTopLeft,
                    PBColor     colorTopRight,
                    PBColor     colorBottomRight,
                    PBColor     colorBottomLeft,
                    PBVector4   cornerRadius);

PBDrawRectExt enqueues a filled rectangle with per-corner color and radius.

PBDrawRectOutline

void PBDrawRectOutline(PBRectangle  bounds,
                       PBStrokeType type,
                       PBColor      strokeColor,
                       f32          strokeWidth,
                       PBVector4    cornerRadius);

PBDrawRectInset

void PBDrawRectInset(PBRectangle bounds,
                     PBColor     strokeColor,
                     f32         strokeWidth,
                     PBVector4   cornerRadius);

PBDrawRectInset enqueues an inset rectangle stroke with configurable width and corner radius.

PBDrawRectWithStroke

void PBDrawRectWithStroke(PBRectangle bounds,
                          PBColor     fillColor,
                          PBColor     strokeColor,
                          f32         strokeWidth,
                          PBVector4   cornerRadius);

PBDrawRectWithStroke enqueues an rectangle with both a fill and inset stroke with configurable width and corner radius.

PBDrawText

void PBDrawText(PBUIFont    font,
                PBStrSlice  text,
                PBColor     color,
                PBRectangle bounds);

PBDrawText enqueues text rendering into bounds using font and color. Text is assumed to be single-line text.

PBDrawTextLine

void PBDrawTextLine(PBUIFont   font,
                    PBStrSlice text,
                    PBColor    color,
                    PBVector2  pos,
                    f32        maxWidth);

PBDrawTextLine draws a single line of text. Optionally you can specify maxWidth and maxHeight (or use 0 if none is desired).

PBDrawTextMultiline

void PBDrawTextMultiline(PBUIFont   font,
                         PBStrSlice text,
                         PBColor    color,
                         PBVector2  pos,
                         f32        maxWidth,
                         f32        maxHeight);

PBDrawTextMultiline draws multiple lines of text. Optionally you can specify maxWidth and maxHeight (or use 0 if none is desired).

PBDrawTextWithPlan

void PBDrawTextWithPlan(PBSysHandle plan,
                        PBRectangle bounds);

PBDrawTextWithPlan enqueues text rendering into bounds using an existing text plan. The text plan must not be released until the frame is complete.

PBDrawTextWithPlanAndOverrideColor

void PBDrawTextWithPlanAndOverrideColor(PBSysHandle plan,
                                        PBRectangle bounds,
                                        PBColor     color);

PBDrawTextWithPlanAndOverrideColor enqueues text rendering into bounds using an existing text plan; the text is drawn is the specified color, rather than the color specified when the text plan was created. The text plan must not be released until the frame is complete.

PBDrawMeasureText

PBVector2 PBDrawMeasureText(PBUIFont   font,
                            PBStrSlice text,
                            f32        maxWidth);

PBDrawMeasureText returns measured text size for the given font and max width constraint (in dps).

PBDrawPushClip

void PBDrawPushClip(PBRectangle bounds);

PBDrawPushClip pushes a clip rectangle onto the clip stack, restricting rendering to bounds.

PBDrawPopClip

void PBDrawPopClip();

PBDrawPopClip pops the most recently pushed clip rectangle from the clip stack.

PBDrawResetClip

void PBDrawResetClip();

PBDrawResetClip pops all clip rectangles from the clip stack.

PBDrawTexturedRect

void PBDrawTexturedRect(PBTexture   texture,
                        PBRectangle bounds);

PBDrawTexturedRect draws a texture mapped to bounds, tinted by tintColor. Use PBColorFromRGBA(1,1,1,1) for no tint.

PBDrawTexturedRectExt

void PBDrawTexturedRectExt(PBTexture   texture,
                           PBRectangle bounds,
                           PBRectangle uv,
                           PBColor     tintColor,
                           PBVector4   cornerRadius);

PBDrawTexturedRectExt draws a texture mapped to bounds with per-corner radius and tint color.

PBDrawTexturedRectExt2

void PBDrawTexturedRectExt2(PBTexture   texture,
                            PBRectangle bounds,
                            PBRectangle uv,
                            PBColor     colorTopLeft,
                            PBColor     colorTopRight,
                            PBColor     colorBottomRight,
                            PBColor     colorBottomLeft,
                            PBVector4   cornerRadius);

Same as PBDrawTexturedRectExt, but allows you to specify a color for each of the 4 vertices.