raylib integration
The default backend includes raylib.h through ccompose.h. You can use raylib drawing, input, audio, file I/O, shaders, and 3D rendering alongside ccompose.
Choose where to call raylib based on the work it needs to do:
- Draw inside a
Draw,DrawRow, orDrawColumncallback when Clay should position and size the content. See Custom drawing. - Poll input or call non-drawing functions between
CC_Begin()andCC_End(). - Manage the raylib drawing frame yourself when ccompose is one layer of a larger scene.
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
Declare a Draw element to receive its computed bounding box in a callback. The callback runs in Clay's render command stream, with the element's children drawn 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()
Call non-drawing raylib functions outside the element macros. This includes input, audio, file I/O, math, and state updates. For example:
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 forwards mouse position, the primary button, and the scroll wheel to Clay. Use raylib directly for keyboard, gestures, gamepad, file drops, clipboard, and window-state queries.
CC_End() owns the BeginDrawing() and EndDrawing() pair. For custom drawing, use a Draw callback or manage the entire drawing frame as shown below. Separate drawing calls between CC_Begin() and CC_End() are outside that pair.
Pattern 3: owning BeginDrawing() / EndDrawing() yourself
To combine a 3D scene, fullscreen shader, or render target with UI, replace CC_End() with its underlying steps and insert your drawing passes in the required order:
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 gives you control of pass order. You also need to provide the renderer declaration and font table:
Clay_Raylib_Renderis compiled intolibccomposebut not declared inccompose.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 ownFont my_fonts[](slot 0 =GetFontDefault()), load fonts with raylib'sLoadFontEx, and use their array index as.fontId. Fonts loaded withCC_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. |
Use raylib's LoadTexture, LoadFontEx, or LoadShader directly for resources outside these wrappers.
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
- Load GPU resources after
CC_Init()and unload them beforeCC_Shutdown(). - Use one
BeginDrawing()andEndDrawing()pair per frame. If you manage that pair, omitCC_End(). - Headless builds exclude
raylib.h, andDrawcallbacks never run. Guard raylib-specific code with#if CCOMPOSE_BACKEND_RAYLIB. - Both raylib and Clay use a top-left origin with y increasing downward. Use
CC_BoundingBoxcoordinates directly. - Raw
IsMouseButtonPressed(MOUSE_BUTTON_LEFT)andCC_Clickedcan both react to the same press. Choose which handler owns each action.
See also
- Custom drawing: the
Draw/DrawRow/DrawColumncallback pattern. - Lifecycle: the
CC_Init,CC_Begin,CC_End, andCC_Shutdowncalls. - Headless mode: building without raylib.
- Images:
CC_LoadImageand theImage()macro. - Text:
CC_LoadFontand the default raylib font at slot 0.