Skip to main content

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/:

ModuleHeaderSourceCovers
typesccompose/types.hintern.cCC_* aliases of Clay types, CC_StrIntern, element ids
sugarccompose/sugar.hGrow(), PadAll(), Color() and the other shorthands
coreccompose/core.hcore.cCC_Init, CC_Begin, CC_End, window settings, CC_RUN_LOOP
textccompose/text.hfont.cFonts, global text style, Text()
elementccompose/element.helement.cColumn, Row, Box, Element, Box layers
leafccompose/leaf.hleaf.cSpacers and dividers
drawccompose/draw.hdraw.cDraw callbacks and the draw slot pool
shadowccompose/shadow.hshadow.cShadow()
progressccompose/progress.hprogress.cLinearProgress, CircularProgress
text_inputccompose/text_input.htext_input.cTextInput and CC_TextEdit
imageccompose/image.himage.cCC_LoadImage, Image()
buttonccompose/button.hbutton.cButton, CC_Hovered, CC_Clicked
scrollccompose/scroll.hscroll.cCC_ScrollUpdate
transitionccompose/transition.heasing.cDefineTransition 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_BACKEND is defined on the library target (propagates to consumers).
  • The raylib code in each src/*.c file 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:

  1. CC_OpenElement calls Clay__OpenElement and Clay__ConfigureOpenElement. The scope's .active field permits one iteration. The loop step calls CC_CloseScope, which calls Clay__CloseElement. A break skips that step and leaves the element open. See Containers: control-flow caveats.

  2. CC__Str passes the id to CC_StrIntern, which copies its bytes into a process-lifetime pool and returns a CC_String with a stable pointer. Ids hash by content, so any const char * works, including a literal or a buffer filled by snprintf.

  3. __COUNTER__ gives each scope variable a unique name, such as cc_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_EXCEEDED
  • CLAY_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:

TargetWhat it is
ccomposeMain static library (with or without raylib backend depending on CCOMPOSE_BACKEND_RAYLIB).
ccompose::ccomposeAlias of ccompose for subdirectory consumers.
ccompose_demoExample binary, only built when CCOMPOSE_BUILD_EXAMPLES and the raylib backend are both on.
ccompose_headlessSeparate 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_testRegistered 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, not ccompose.

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.

SymbolRole
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) macroOne-character alias for CC_StrIntern(s). Used by the public macros so they read uniformly.
CC_SID macroBuild a Clay_ElementId from a CC_String at runtime (#define CC_SID CLAY_SID).
CC_PointerOverAlias 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) macroIndex-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 as cc__arena_memory, cc__backend_init, and cc__fonts.
  • CC__* names support header macros. Treat hooks such as CC__MousePressedThisFrame and CC__TextStyleWithGlobalFont as internal.

Use CC_* in app code. Treat CC__* and cc__* as internal.