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.
Layers
The ccompose library statically links three layers:
┌──────────────────────────────────────────┐
│ your app │
├──────────────────────────────────────────┤
│ ccompose macros & lifecycle │
│ (Column, Row, Box, Button, Text, CC_*) │
│ include/ccompose/*.h + src/*.c │
├──────────────────────────────────────────┤
│ Clay (layout engine, vendored) │
│ include/clay.h, external/clay/ │
├──────────────────────────────────────────┤
│ raylib backend (optional, bundled) │
│ renderer/raylib/clay_renderer_raylib.c │
└──────────────────────────────────────────┘
Clay
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.
ccompose core
The core wraps Clay's element-open, configure, and close calls in C block macros. include/ccompose.h is the umbrella header apps include. Each module has its own public header in include/ccompose/ and its own source file in src/:
| Module | Header | Source | Covers |
|---|---|---|---|
| types | ccompose/types.h | intern.c | CC_* aliases of Clay types, CC_StrIntern, element ids |
| sugar | ccompose/sugar.h | Grow(), PadAll(), Color() and the other shorthands | |
| core | ccompose/core.h | core.c | CC_Init, CC_Begin, CC_End, window settings, CC_RUN_LOOP |
| text | ccompose/text.h | font.c | Fonts, global text style, Text() |
| element | ccompose/element.h | element.c | Column, Row, Box, Element, Box layers |
| leaf | ccompose/leaf.h | leaf.c | Spacers and dividers |
| draw | ccompose/draw.h | draw.c | Draw callbacks and the draw slot pool |
| shadow | ccompose/shadow.h | shadow.c | Shadow() |
| progress | ccompose/progress.h | progress.c | LinearProgress, CircularProgress |
| text_input | ccompose/text_input.h | text_input.c | TextInput and CC_TextEdit |
| image | ccompose/image.h | image.c | CC_LoadImage, Image() |
| button | ccompose/button.h | button.c | Button, CC_Hovered, CC_Clicked |
| scroll | ccompose/scroll.h | scroll.c | CC_ScrollUpdate |
| transition | ccompose/transition.h | easing.c | DefineTransition and the easing functions |
src/clay_impl.c defines CLAY_IMPLEMENTATION, includes the raylib renderer and holds the backend glue that calls it. src/internal.h declares the cc__* state the modules share. It is private and is not installed.
Each module header includes what it needs, so #include "ccompose/text_input.h" works on its own. Apps normally include ccompose.h.
raylib backend
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 raylib code in each
src/*.cfile is#ifdef'd out. - raylib is not linked.
See Headless mode for the runtime implications.
How the container macros expand
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,
(CC_ElementDeclaration){
.backgroundColor = (CC_Color){18,18,20,255},
});
cc_scope_42_.active;
CC_CloseScope(&cc_scope_42_)) {
Clay__OpenTextElement(
CC_StrIntern("hi"),
(CC_TextElementConfig){
.fontSize = 14,
.textColor = (CC_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.
Frame loop
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()) → CC_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.
Memory model
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.
Font management
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 src/font.c and have no per-frame allocation cost.
Build targets
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 the same src/*.c files 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.
Warning discipline
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 the src/*.c files in normal builds.
Low-level escape hatches
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.
Symbol prefixes
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.