Repository navigation
Architecture
ccompose combines Clay's layout engine, a C macro layer, and an optional raylib backend. The sections below describe the internals for debugging and contributing.
The ccompose library statically links three layers:
┌──────────────────────────────────────────┐
│ your app │
├──────────────────────────────────────────┤
│ ccompose macros & lifecycle │
│ (Column, Row, Box, Button, Text, CC_*) │
│ include/ccompose.h + src/ccompose.c │
├──────────────────────────────────────────┤
│ Clay (layout engine, vendored) │
│ include/clay.h, external/clay/ │
├──────────────────────────────────────────┤
│ raylib backend (optional, bundled) │
│ renderer/raylib/clay_renderer_raylib.c │
└──────────────────────────────────────────┘
Clay is vendored at include/clay.h, with a mirror at external/clay/ for the FetchContent vendor layout. Clay owns the per-frame layout state in its arena. ccompose adds no allocations after CC_Init().
Treat include/clay.h and external/clay/ as read-only vendor code. Upgrades should be atomic and documented.
The core wraps Clay's element-open, configure, and close calls in C block macros. The public API lives in include/ccompose.h, with its implementation in src/ccompose.c.
The backend handles windows, input, and rendering in renderer/raylib/, using the raylib renderer from Clay's source tree. The ccompose CMake target exposes renderer/raylib/raylib.h publicly. Including ccompose.h gives your app access to raylib types and flags such as Vector2, Font, and FLAG_*.
When you build with -DCCOMPOSE_BACKEND_RAYLIB=OFF:
- The raylib headers are not added to the public include path.
-
CCOMPOSE_NO_BACKENDis defined on the library target (propagates to consumers). - The whole raylib block in
src/ccompose.cis#ifdef'd out. - raylib is not linked.
See Headless mode for the runtime implications.
A call like:
Column("Root", .backgroundColor = Color(18,18,20,255)) {
Text("hi", .fontSize = 14, .textColor = Color(255,255,255,255));
}expands (roughly) to:
for (CC_Scope cc_scope_42_ = CC_OpenElement(
CC_StrIntern("Root"), // interned CC_String
CC_TOP_TO_BOTTOM,
(Clay_ElementDeclaration){
.backgroundColor = (Clay_Color){18,18,20,255},
});
cc_scope_42_.active;
CC_CloseScope(&cc_scope_42_)) {
Clay__OpenTextElement(
CC_StrIntern("hi"),
(Clay_TextElementConfig){
.fontSize = 14,
.textColor = (Clay_Color){255,255,255,255},
});
}The expansion has three parts:
-
CC_OpenElementcallsClay__OpenElementandClay__ConfigureOpenElement. The scope's.activefield permits one iteration. The loop step callsCC_CloseScope, which callsClay__CloseElement. Abreakskips that step and leaves the element open. See Containers: control-flow caveats. -
CC__Strpasses the id toCC_StrIntern, which copies its bytes into a process-lifetime pool and returns aCC_Stringwith a stable pointer. Ids hash by content, so anyconst char *works, including a literal or a buffer filled bysnprintf. -
__COUNTER__gives each scope variable a unique name, such ascc_scope_42_, so nested blocks do not trigger-Wshadow.
CC_Begin()
├─ (raylib) read window size, mouse, wheel, frame time
├─ Clay_SetLayoutDimensions
├─ Clay_SetPointerState
├─ Clay_UpdateScrollContainers
└─ Clay_BeginLayout
your UI code
└─ Column/Row/Box/Button/Text macros → Clay__OpenElement/Configure/Close
CC_End()
├─ Clay_EndLayout(GetFrameTime()) → Clay_RenderCommandArray
└─ (raylib)
├─ BeginDrawing
├─ ClearBackground
├─ Clay_Raylib_Render(commands, fonts)
└─ EndDrawing
Your code describes the layout again each frame. Clay retains the layout state it needs in its arena.
Exactly one heap allocation happens inside ccompose, in CC_Init:
uint32_t memory_size = Clay_MinMemorySize();
cc__arena_memory = malloc(memory_size);
Clay_Arena arena = Clay_CreateArenaWithCapacityAndMemory(memory_size, cc__arena_memory);
Clay_Initialize(arena, cc__viewport, /* error handler */);This arena holds Clay's per-frame element pool, text measurement cache, and internal bookkeeping. It's freed in CC_Shutdown (also via atexit).
If the layout exceeds Clay's element or text capacity, Clay reports one of these errors:
CLAY_ERROR_TYPE_ELEMENTS_CAPACITY_EXCEEDEDCLAY_ERROR_TYPE_TEXT_MEASUREMENT_CAPACITY_EXCEEDED
Increase the relevant limit by calling Clay's own setters after CC_Init:
CC_Init();
Clay_SetMaxElementCount(16384);
Clay_SetMaxMeasureTextCacheWordCount(32768);The limits take effect at the next arena reset, on the next CC_Begin.
The font table uses CC_MAX_FONTS (16) slots, with raylib's default font in slot 0. Call CC_LoadFont after CC_Init so raylib has an active GL context. Clay_Raylib_Render resolves each fontId through the font array.
Load a font with CC_LoadFont, then select it with CC_InitFont(fontId, base_size). Text uses that id when .fontId is zero and that size when .fontSize is zero. CC_InitFont uses the same implementation in both backends, with a backend-specific bounds check.
CC_SetGlobalFontColor and CC_GetGlobalFontColor manage the default text color, initially ColorHex(0xFFFFFF). Text uses it when .textColor is omitted. The font id, size, and color live in file-scope statics in src/ccompose.c and have no per-frame allocation cost.
From CMakeLists.txt:
| Target | What it is |
|---|---|
ccompose |
Main static library (with or without raylib backend depending on CCOMPOSE_BACKEND_RAYLIB). |
ccompose::ccompose |
Alias of ccompose for subdirectory consumers. |
ccompose_demo |
Example binary, only built when CCOMPOSE_BUILD_EXAMPLES and the raylib backend are both on. |
ccompose_headless |
Separate static library that always builds src/ccompose.c with -DCCOMPOSE_NO_BACKEND. Only used by the smoke test, so headless tests run regardless of the main target's backend config. |
ccompose_smoke_test |
Registered with CTest, links against ccompose_headless. |
The split between ccompose and ccompose_headless means:
- Top-level builds can have the raylib backend on and still run headless tests.
- Tests never accidentally link against raylib.
- Adding new tests requires linking them to
ccompose_headless, notccompose.
The Clay and raylib renderer translation units build with -w to suppress vendor warnings. Downstream targets can enable their own warning flags. This also suppresses pedantic warnings from src/ccompose.c in normal builds.
Use Column, Row, Box, Element, Button, Text, CC_Hovered, and CC_Clicked for app code. Their underlying functions and aliases are also available for custom block macros, raw Clay integration, and runtime ids.
| Symbol | Role |
|---|---|
CC_String CC_StrIntern(const char *s) |
Intern a NUL-terminated C string into a process-lifetime pool and return a CC_String (alias for Clay_String) with a stable pointer + length. Used internally by every Column/Row/Text/Image/CC_Hovered id argument. Empty / NULL input returns a zero-length string. |
CC__Str(s) macro |
One-character alias for CC_StrIntern(s). Used by the public macros so they read uniformly. |
CC_SID macro |
Build a Clay_ElementId from a CC_String at runtime (#define CC_SID CLAY_SID). |
CC_PointerOver |
Alias for Clay_PointerOver. CC_Hovered("id") expands to CC_PointerOver(CC_SID(CC__Str("id"))). |
CC_OpenElement(CC_String id, CC_LayoutDirection direction, CC_ElementDeclaration decl) |
Open a Clay element and push it onto the stack. Returns a CC_Scope whose .active field is 1 for the body iteration. |
CC_CloseScope(CC_Scope *) |
Closes the open element via Clay__CloseElement and flips .active to 0 so the for-loop terminates. |
CC_IDI(label, index) macro |
Index-offset element id: hash a (label, index) pair into a CC_ElementId. Useful for list rows / grid cells. Alias for CLAY_IDI. |
These primitives let custom macros use the same operations as the built-in macros.
-
CC_*names are public functions, types, and macros for app code. -
cc__*names are file-local helpers and state, such ascc__arena_memory,cc__backend_init, andcc__fonts. -
CC__*names support header macros. Treat hooks such asCC__MousePressedThisFrameandCC__TextStyleWithGlobalFontas internal.
Use CC_* in app code. Treat CC__* and cc__* as internal.