Skip to main content

Headless mode

Build without raylib to run layout code in CI, tests, benchmarks, or tools that do not need a display.

Building headless​

cmake -S . -B build -DCCOMPOSE_BACKEND_RAYLIB=OFF
cmake --build build

This configuration:

  • defines CCOMPOSE_NO_BACKEND on the ccompose target (public, so it propagates to consumers)
  • skips the ccompose_demo executable
  • does not link against raylib

The headless library builds the same src/*.c files with the backend code excluded by preprocessor guards.

The project also builds a separate ccompose_headless static library when the raylib backend is enabled. Link tests to that target to keep them headless regardless of the main build configuration.

What changes at runtime​

FunctionRaylib backendHeadless
CC_SetWindowsets window size/titleno-op
CC_SetWindowFlagssets raylib flagsno-op
CC_SetBackgroundsets clear color (drawn each frame)stores the value (no draw)
CC_LoadFontloads font, returns slot idalways returns -1
CC_InitFontsets global font id + default sizeworks: no font loads, but both values flow into emitted text commands
CC_GetGlobalFontIdreturns configured idreturns configured id (0 unless CC_InitFont set one)
CC_GetGlobalFontSizereturns configured sizereturns configured size (defaults to 16)
CC_SetGlobalFontColorstores global text colorstores the value (still flows into emitted text commands)
CC_GetGlobalFontColorreturns stored colorreturns stored color (defaults to white)
CC_Running!WindowShouldClose()always true
CC_Beginreads mouse/keyboard/wheel into Clayjust calls Clay_BeginLayout()
CC_Endrenders via Clay_Raylib_Renderreturns commands without drawing
CC__MousePressedThisFrameIsMouseButtonPressed(LEFT)always false
LinearProgresslays out a track and fill, infinite mode animates off GetTime()works: same layout and rectangle commands, clock fixed at 0 so an infinite bar shows only its track
CircularProgresspaints a ring with raylibreserves its .sizing box, draws nothing
CC_SetViewportoverridden by CC_Begin from window sizeused: drives Clay's layout dimensions

CC_Running() always returns true in headless mode. Run a bounded loop or a single frame.

Clay emits configured colors and font ids into the CC_RenderCommandArray from CC_End() even without a renderer. Tests can inspect those values in the returned commands.

Minimal headless program​

#include "ccompose.h"
#include <assert.h>

int main(void) {
CC_SetViewport(800.0f, 600.0f); // required: no window to read size from
CC_Init();

CC_Begin();
Column("Root",
.layout = { .sizing = { Grow(), Grow() }, .padding = PadAll(8) },
.backgroundColor = Color(24, 24, 24, 255)) {
Text("headless",
.textColor = Color(255,255,255,255),
.fontSize = 16);
}
CC_RenderCommandArray commands = CC_End();

// Inspect commands: they describe the layout even though nothing was drawn.
int rects = 0, texts = 0;
for (int32_t i = 0; i < commands.length; ++i) {
CC_RenderCommand *cmd = CC_RenderCommandArray_Get(&commands, i);
if (cmd->commandType == CLAY_RENDER_COMMAND_TYPE_RECTANGLE) ++rects;
if (cmd->commandType == CLAY_RENDER_COMMAND_TYPE_TEXT) ++texts;
}
assert(rects >= 1);
assert(texts >= 1);

CC_Shutdown();
return 0;
}

Text measurement in headless​

Headless builds use a fallback text measurement function that estimates width as font_size * 0.5 * char_count. It uses 14.0 when .fontSize is 0. This gives deterministic test layouts, but the widths differ from rendered text.

If you need accurate measurement in headless mode (e.g. snapshot tests against rendered baselines), install your own measure function after CC_Init():

static CC_Dimensions my_measure(Clay_StringSlice text,
CC_TextElementConfig *cfg,
void *user) {
// ... your own font metrics ...
return (CC_Dimensions){ width, height };
}

CC_Init();
Clay_SetMeasureTextFunction(my_measure, NULL);

Pointer input in tests​

Since the raylib backend isn't providing mouse state, CC_Begin() doesn't touch the pointer. To drive hover/click tests, call Clay_SetPointerState() directly between CC_Init and CC_Begin:

CC_Init();

// First frame: just to establish layout
CC_Begin();
build_ui();
CC_End();

// Push the mouse cursor over "Save" and click
Clay_SetPointerState((CC_Vector2){ 100.0f, 200.0f }, /*is_down=*/true);

// Second frame: now CC_Hovered("Save") will be true
CC_Begin();
build_ui();
assert(CC_Hovered("Save")); // Uses the previous frame's layout
CC_End();

CC_Clicked always returns false in headless mode because CC__MousePressedThisFrame() has no mouse input source. For simulated presses, combine CC_PointerOver(CLAY_ID(...)) with your own press state, or use Clay_OnHover with a pointer-data state check.

CTest example​

Use tests/smoke_test.c as a headless example. Register a test in CMakeLists.txt:

if(CCOMPOSE_BUILD_TESTS)
enable_testing()

# Builds every src/*.c file with CCOMPOSE_NO_BACKEND defined.
ccompose_define_library(ccompose_headless HEADLESS)

add_executable(my_test tests/my_test.c)
target_link_libraries(my_test PRIVATE ccompose_headless)
add_test(NAME my_test COMMAND my_test)
endif()

Run with ctest --test-dir build --output-on-failure.

Uses​

  • Run CI tests without X11 or GL dependencies.
  • Measure Clay's layout cost without rendering.
  • Snapshot a CC_RenderCommandArray and compare it across commits.
  • Integrate a custom renderer by iterating the returned command array.