Skip to content

Raylib Integration

rizuki edited this page Nov 30, 2026 · 3 revisions

Raylib Integration

ccompose's default backend is raylib, and the public header (ccompose.h) re-includes raylib.h whenever the raylib backend is active. That means every raylib function, type, and flag is available to your code with no extra setup — you can mix raylib draw calls, input polling, audio, file I/O, shaders, and 3D rendering freely with ccompose UI.

This page covers the three places where you'd call raylib directly:

  1. Inside a Draw / DrawRow / DrawColumn callback — for primitives positioned by Clay's layout (see Custom Drawing for the canonical pattern).
  2. Between CC_Begin() and CC_End() — for input polling, audio, or anything that doesn't paint.
  3. Wrapped around CC_Begin() / CC_End() — for a full raylib frame where ccompose UI is just one layer.

What's exposed

When CCOMPOSE_BACKEND_RAYLIB=ON (the default), including ccompose.h transitively includes raylib.h. No extra include or link step:

#include "ccompose.h"   // pulls in raylib.h, Clay, ccompose API

int main(void) {
    CC_SetWindow(1280, 720, "App");
    CC_SetWindowFlags(FLAG_WINDOW_RESIZABLE | FLAG_MSAA_4X_HINT);
    CC_Init();          // opens the raylib window + GL context

    Texture2D tex = LoadTexture("assets/sprite.png");   // raylib, fine
    Sound boop    = LoadSound("assets/boop.wav");       // raylib, fine

    while (CC_Running()) {
        CC_Begin();
        Column("Root", ...) { ... }
        CC_End();
    }

    UnloadTexture(tex);
    UnloadSound(boop);
    CC_Shutdown();
}

CC_Init() opens the window and creates the GL context, so any raylib call that needs the GL context (LoadTexture, LoadFont, LoadShader, …) must come after CC_Init() and before CC_Shutdown().

If you need to compile against the headless backend (-DCCOMPOSE_BACKEND_RAYLIB=OFF), raylib.h is not included and any direct raylib call will fail to compile. Gate raylib-only code with #if CCOMPOSE_BACKEND_RAYLIB.

Pattern 1 — inside a Draw callback

The cleanest way to mix raylib primitives with ccompose layout: declare a Draw element, get the computed bounding box, and paint inside it. The callback fires inline with Clay's render command stream, so children of the Draw element stack on top.

typedef struct { float pct; } Bar;

static void paint_bar(CC_BoundingBox bb, void *user) {
    Bar *b = (Bar *)user;
    DrawRectangle((int)bb.x, (int)bb.y,
                  (int)(bb.width * b->pct), (int)bb.height,
                  (Color){ 80, 180, 255, 255 });
}

static Bar bar = { .pct = 0.42f };

Column("Root", .layout = { .padding = PadAll(16), .childGap = 8 }) {
    Text("Loading...", .fontSize = 14);
    Draw("bar", paint_bar, &bar,
         .layout          = { .sizing = { Grow(), Fixed(6) } },
         .backgroundColor = Color(40,40,50,255),
         .cornerRadius    = RadiusAll(3));
}

See Custom Drawing for the full callback contract, slot-pool sizing, and the .custom gotcha. Use this pattern when you want Clay to position and size your raylib primitives.

Pattern 2 — direct raylib between CC_Begin() and CC_End()

For things that don't paint into a Clay rectangle — input polling, audio, file I/O, math, building state used by the next frame — call raylib directly anywhere outside the macros. The simplest case is reading input:

while (CC_Running()) {
    // Raw raylib input — no layout, no Clay
    if (IsKeyPressed(KEY_F1))         toggle_debug_overlay();
    if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) open_context_menu();
    if (IsFileDropped()) {
        FilePathList dropped = LoadDroppedFiles();
        handle_drop(dropped);
        UnloadDroppedFiles(dropped);
    }

    CC_Begin();
    Column("Root", ...) { ... }
    CC_End();
}

ccompose only forwards a subset of input (mouse position, primary button, wheel) into Clay. For anything beyond that — keyboard, gestures, gamepad, file drop, clipboard, window-state queries — call the raylib API directly.

Don't draw between CC_Begin() and CC_End(). CC_End() calls BeginDrawing() / EndDrawing() itself; any Draw* calls outside that pair are either ignored or split your frame across two BeginDrawing() blocks, which raylib does not support.

Pattern 3 — owning BeginDrawing() / EndDrawing() yourself

If you want a 3D scene, a fullscreen shader, or a render-target composition where ccompose UI is just an overlay, you need control of the raylib frame. The escape hatch is: don't use CC_End() — replicate what it does manually, and inject your own draw calls in the order you want.

while (CC_Running()) {
    UpdateCamera(&camera, CAMERA_ORBITAL);

    CC_Begin();                                   // builds Clay layout
    Column("HUD", .floating = FloatOnRoot(10)) {
        Text("FPS", .fontSize = 14);
    }
    CC_RenderCommandArray commands = Clay_EndLayout(GetFrameTime());

    BeginDrawing();
        ClearBackground(BLACK);

        // 3D pass — your raylib code
        BeginMode3D(camera);
            DrawModel(model, (Vector3){0}, 1.0f, WHITE);
            DrawGrid(20, 1.0f);
        EndMode3D();

        // UI pass — ccompose render
        Clay_Raylib_Render(commands, my_fonts);

        DrawFPS(10, 10);                          // raylib HUD on top
    EndDrawing();
}

This is more rope than CC_End(), but you get to interleave passes however you want. Two things the public header doesn't give you here:

  • Clay_Raylib_Render is compiled into libccompose but not declared in ccompose.h. Declare it yourself: void Clay_Raylib_Render(Clay_RenderCommandArray commands, Font *fonts);
  • The font table CC_LoadFont() fills is private to ccompose. In this pattern, keep your own Font my_fonts[] (slot 0 = GetFontDefault()), load fonts with raylib's LoadFontEx, and use their array index as .fontId. Fonts loaded with CC_LoadFont() won't render correctly in this pattern.

Loading raylib assets through ccompose

ccompose wraps the two raylib loaders most UI code needs:

ccompose raylib equivalent What you get
CC_LoadFont(path, size) LoadFontEx An int font id you pass into .fontId. Up to 15 custom fonts (slot 0 is the default). Returns -1 on failure.
CC_LoadImage(path) LoadTexture A raylib Texture2D. Pass its address to ImgFill / ImgFit / ImgCrop inside Image("id", ...), and free it with CC_UnloadImage.

You can still call LoadTexture / LoadFontEx / LoadShader directly — ccompose only wraps the two paths it has macros for.

Window flags, vsync, MSAA, resizable

Set raylib window flags before CC_Init() via CC_SetWindowFlags. They map 1:1 to raylib's flags:

CC_SetWindow(1280, 720, "App");
CC_SetWindowFlags(FLAG_WINDOW_RESIZABLE
                  | FLAG_VSYNC_HINT
                  | FLAG_MSAA_4X_HINT
                  | FLAG_WINDOW_HIGHDPI);
CC_Init();

Changing flags after CC_Init() has no effect (raylib limitation).

Gotchas

  • GL context lifetime. Any raylib resource that needs the GPU (Texture2D, Font, Shader, render targets) must be loaded after CC_Init() and unloaded before CC_Shutdown().
  • Single BeginDrawing() per frame. raylib does not support two BeginDrawing / EndDrawing pairs in the same frame. If you take Pattern 3, don't also call CC_End().
  • Headless build. With -DCCOMPOSE_BACKEND_RAYLIB=OFF, raylib.h is not included and Draw callbacks never fire. Gate platform-specific code with #if CCOMPOSE_BACKEND_RAYLIB.
  • Coordinate system. raylib uses top-left origin, y grows down — same as Clay's bounding boxes. No flip needed when painting from a CC_BoundingBox.
  • No automatic input dedup. If you're polling IsMouseButtonPressed(MOUSE_BUTTON_LEFT) yourself, ccompose's CC_Clicked may also fire on the same frame. Decide one owner per input.

See also

  • Custom Drawing — the Draw / DrawRow / DrawColumn callback pattern.
  • Lifecycle — what CC_Init / CC_Begin / CC_End / CC_Shutdown actually do.
  • Headless Mode — building without raylib.
  • Images — CC_LoadImage and the Image() macro.
  • Text — CC_LoadFont and the default raylib font at slot 0.

Clone this wiki locally