ZenitUI is rendering- and windowing-agnostic.
To integrate it into a custom engine or a different graphics library (GLFW + bgfx, Sokol, Vulkan, DirectX, WebGPU, etc.), implement three lightweight interfaces and attach them to a ZenitUI::UIContext (optionally wrapped by ZenitUI::IBackend for standalone apps):
| Interface | Responsibility |
| IPlatform | Window size, DPI scale, monotonic clock, pointer/keyboard polling, safe area, clipboard, optional OS cursors (setCursor) and IME rect (setTextInputRect) |
| IRenderer | Single mandatory indexed geometry primitive (drawGeometry), dynamic RGBA textures (createTexture/updateTexture/destroyTexture for TextEngine's glyph atlas), clip/transform/blend/shader stacks, optional off-screen targets |
| IAssetProvider | Name-based loading, caching, lookup, and unloading of textures and shaders (including language-aware loadEffectAsset) |
| IBackend (optional) | Unified window/context lifecycle (init, initAndBind, pollFrame, beginDraw, endDraw, shutdown) for portable standalone apps |
All reference classes are grouped under Backends, TextEngine, UTF-8 & Debugging.
1. Wiring a Backend into UIContext
Embedding in an existing engine loop — attach your three instances directly to a UIContext, mount your root tree, and drive runFrame / renderFrame from your host loop:
MyPlatform platform;
MyRenderer renderer;
MyAssetProvider assets(renderer);
while (running) {
float dt = computeDeltaTime();
}
Per-instance UI context: backends, theme, text engine, frame state, and routing.
Definition UIContext.hpp:725
void mount(Layout &root)
Attach a root Layout subtree to this context and set it as the primary root.
void detachBackend()
Flush pooled render targets while the renderer is still valid and clear backend pointers.
Definition UIContext.hpp:765
void renderFrame()
Render the mounted primary root (beginFrame, draw, endFrame).
void runFrame(float dt)
Run one full update/measure/arrange frame on the mounted primary root.
void attachBackend(IRenderer *r, IPlatform *p, IAssetProvider *a, UIContext **ownerSlot=nullptr)
Attach renderer, platform, and asset backends to this context.
Definition UIContext.hpp:747
Standalone apps via IBackend — implement ZenitUI::IBackend to get single-call initialization and teardown (backend.initAndBind(cfg, ctx)):
MyBackend backend;
if (!backend.initAndBind(cfg, ctx))
return 1;
while (backend.pollFrame()) {
backend.beginDraw({15, 17, 23, 255});
backend.endDraw();
}
backend.shutdown();
void unmount(Layout &root)
Detach a previously mounted root Layout subtree from this context.
2. Minimum Viable Backend vs Optional Features
You do not need custom shape rasterizers, shaders, or off-screen framebuffers to get a complete UI running:
- Geometry helpers included: IRenderer provides default tessellated implementations for fillRect, fillRoundedRect, fillCircle, strokeRect, strokeRoundedRect, fillBorder, drawTexture, and drawNineSlice via Geometry2D.hpp. Implementing drawGeometry() alone brings up the entire 2D rendering pipeline.
- Incremental GPU features: IRenderer::supports(Feature) lets a backend opt into advanced capabilities one at a time:
- Feature::Effects: fragment shaders (createEffect, pushEffect, popEffect, setEffect*, shaderLanguage()).
- Feature::NestedTargets: off-screen render targets for Canvas and post-processing filters (createTarget, pushTarget, popTarget, drawTarget, setBlendMode).
- Feature::TargetSubRect: sub-rectangle blitting from bucket-pooled render targets (drawTarget(t, src, dst, tint)).
Returning false from supports() causes ZenitUI to skip shader/filter passes gracefully while keeping layout, widgets, text, animations, and clipping fully functional. Start from ZenitUI::Test::MockRenderer for the smallest valid skeleton, then consult ZenitUI::SDL3Renderer or ZenitUI::RaylibRenderer for full GPU implementations.
3. Critical Behavioral Invariants
Keep these rules in mind when porting to avoid subtle layout or rendering bugs:
- Logical vs physical coordinates (IPlatform & IRenderer): viewportSize(), pointer().pos, and layout rects all operate in logical pixels. Apply dpiScale() only when configuring the GPU projection/scissor or sampling the physical framebuffer.
- Edge-triggered input (IPlatform): ButtonState::pressed, ButtonState::released, PointerState::wheelY, InputEvents::keys, and InputEvents::chars must fire for one frame only, whereas ButtonState::down and InputEvents::held remain true while held.
- Intersected clip stack (IRenderer): pushClip(r) must transform r through the active transform stack, intersect it with the current clip rectangle, and expose the resulting screen-space clip via getClipRect() so off-screen culling stays accurate.
- Blend modes (IRenderer::setBlendMode): Backends supporting Feature::NestedTargets should flush the current batch on mode changes and implement BlendMode::Alpha (separate alpha: SRC_ALPHA, ONE_MINUS_SRC_ALPHA / ONE, ONE_MINUS_SRC_ALPHA), BlendMode::Premultiplied (ONE, ONE_MINUS_SRC_ALPHA), and BlendMode::Replace (blending disabled) so off-screen filter targets and LayerCache composite cleanly without double-premultiplication.
- Dynamic glyph atlas uploads (IRenderer::updateTexture): Flush any pending geometry batch referencing texture t before mutating sub-rectangle pixels so earlier draw calls in the same frame do not sample overwritten atlas slots.
- Zero-allocation asset lookup (IAssetProvider): getTexture() and getEffect() are called during layout and draw; prefer transparent std::string_view map lookups.
- Cursor & IME hooks (IPlatform): setCursor(CursorShape) and setTextInputRect(Rect, float) are optional (default no-ops) and already deduplicated by UIContext.
- Safe GPU shutdown: Call ctx.detachBackend() before destroying the graphics context so TextEngine's glyph atlas and TargetPool's off-screen targets are released while GPU handles are still valid.
4. Registering a New Backend in CMake
Drop your backend under backends/<name>/ and call zenitui_register_backend() in its CMakeLists.txt:
zenitui_register_backend(
NAME mybackend
TARGET ZenitUI::MyBackend
CLASS ZenitUI::MyBackend
HEADER MyBackend.hpp
)
Every backend-agnostic example in examples/ will immediately build and run against it via make examples BACKEND=mybackend and make run-01_hello_markup BACKEND=mybackend, and docs/gen_backends.cmake will index it automatically from its header metadata annotations.
5. Built-in Reference Implementations
Every backend shipped under backends/ is indexed automatically with its CMake target, supported optional features, and class references: