RmlUi Documentation

repository·master·Indexed 26 days ago

https://github.com/mikke89/rmlui

A lightweight, high-performance C++ UI library for real-time applications and games. RmlUi uses HTML/CSS-like syntax (RML/RCSS) to define interfaces and provides a renderer-agnostic layout engine. It supports Flexbox, animations, transitions, and data bindings, requiring a C++17 compatible compiler and FreeType for font rendering. The library includes various backends for OpenGL, Vulkan, DirectX 12, SDL, GLFW, and Win32.

Tokens
6.7K
Snippets
15
Records
37
Agent score
88%

What's inside RmlUi

  1. Overview of RmlUi features and architecture

    master

    RmlUi is a lightweight C++ user interface library based on XHTML1 and CSS2 standards, with support for many HTML5 and CSS3 features. It is designed for real-time applications like games.

    Key Characteristics:

    • Renderer-Agnostic: RmlUi generates vertices, indices, and draw commands; the user is responsible for implementing the rendering logic via a RenderInterface.
    • User-Controlled Loop: The library does not run in the background; it only executes when the user explicitly calls its API.
    • Extensible: Supports custom decorators, a generic event system, and a Lua scripting plugin.
    • Standard Support: Supports Flexbox layout, animations, transitions, transforms, media queries, and various CSS3 features like border radius and gradients.

    Dependencies:

    • FreeType: Used for font rendering (can be replaced with a custom font engine).
    • C++17: Requires a C++17 compatible compiler.
  2. Understand RmlUi architecture: Renderers, Platforms, and Backends

    master

    RmlUi uses a modular architecture to separate rendering, system interaction, and windowing logic:

    • Renderer: Implements the render interface for a specific rendering API (e.g., OpenGL, Vulkan, DirectX 12). It handles drawing geometry, textures, and shaders.
    • Platform: Implements the system interface for a specific operating system or windowing library (e.g., Win32, GLFW, SDL). It handles windowing, input, and clipboard operations.
    • Backend: A combination of a Renderer and a Platform.

    Integration Strategy:

    • Use provided Renderers and Platforms directly in your project to avoid writing custom interfaces.
    • Do not use the provided Backends directly. Instead, copy and modify them to suit your specific windowing framework needs.
  3. Explore RmlUi Sample Applications

    master
    The Samples/ directory provides a collection of small, easy-to-understand applications designed to demonstrate various RmlUi features and integration patterns. These samples range from basic initialization to complex implementations like a Space Invaders clone.
  4. Explore RmlUi capabilities and samples

    master

    RmlUi supports a wide range of advanced UI features including:

    • Layout: Flexbox layout support.
    • Animations: Transitions and transforms (handled via RCSS), and complex animations.
    • Data Binding: Demonstrating how to bind UI elements to data sources.
    • Visual Effects: Advanced filters and effects applied to UI elements.
    • Vector Graphics: Support for Lottie animations (via Lottie plugin) and SVG images (via SVG plugin).
    • Testing: A built-in visual testing framework for automated layout testing.

    Various samples are available in the repository to demonstrate these features, such as the data_binding sample, effects sample, demo sample (form controls and sandbox), animation sample, lottie sample, and svg sample.

  5. Initialize RmlUi and create a context

    master

    To use RmlUi, follow these initialization steps:

    1. Install your custom RenderInterface using Rml::SetRenderInterface.
    2. Call Rml::Initialise().
    3. Create an Rml::Context using Rml::CreateContext(name, size) to manage documents.
    4. Load required fonts using Rml::LoadFontFace(path) or Rml::LoadFontFace(path, is_fallback).
    5. Shutdown the library using Rml::Shutdown() when the application exits.
    MyRenderInterface render_interface;
    
    Rml::SetRenderInterface(&render_interface);
    
    Rml::Initialise();
    
    Rml::Context* context = Rml::CreateContext("main", Rml::Vector2i(window_width, window_height));
    
    Rml::LoadFontFace("LatoLatin-Regular.ttf");
    Rml::LoadFontFace("NotoEmoji-Regular.ttf", true);
    
    // ... application loop ...
    
    Rml::Shutdown();
  6. Run the RmlUi update and render loop

    master

    To keep the UI responsive and rendered, your application loop must perform the following steps:

    1. Process Input: Submit mouse or keyboard events to the context (e.g., context->ProcessMouseMove).
    2. Update: Call context->Update() to process animations, data bindings, and input changes.
    3. Render: Call context->Render() to submit geometry to the RenderInterface.
    4. Present: Use your backend to swap buffers/present the frame.
    while (!exit_application)
    {
        // 1. Process Input
        if (my_input->MouseMoved())
            context->ProcessMouseMove(mouse_pos.x, mouse_pos.y, 0);
    
        // 2. Update
        context->Update();
    
        // 3. Render
        Backend::BeginFrame();
        context->Render();
    
        // 4. Present
        Backend::PresentFrame();
    }
  7. Integrate RmlUi into a C++ application

    master

    Integrating RmlUi follows these general steps:

    1. Build or Fetch: Build RmlUi using CMake/vcpkg or fetch the pre-built binaries and link them to your application.
    2. Implement Renderer: Implement the abstract RenderInterface (defined in Include/RmlUi/Core/RenderInterface.h) or use one of the provided backends.
    3. Initialize: Initialize RmlUi with your implemented interfaces, create a context, provide font files, and load an RML document.
    4. Main Loop: In your application loop, call the context's update and render methods, and submit input events to the library.
    5. Run: Compile and execute your application.
  8. Build RmlUi with samples using CMake and vcpkg

    master

    To build RmlUi along with its included samples, use CMake with a vcpkg toolchain. This example installs the necessary dependencies (freetype and glfw3), clones the repository, and configures the build using the samples preset and the GLFW_GL3 backend.

    Note: You must replace <path-to-vcpkg> with the actual path to your vcpkg installation.

    vcpkg install freetype glfw3
    git clone https://github.com/mikke89/RmlUi.git
    cd RmlUi
    cmake -B Build -S . --preset samples -DRMLUI_BACKEND=GLFW_GL3 -DCMAKE_TOOLCHAIN_FILE="<path-to-vcpkg>/scripts/buildsystems/vcpkg.cmake"
    cmake --build Build
  9. Build RmlUi with all samples and extra features

    master
    To make all samples available, including those requiring additional features, install lua, lunasvg, rlottie, and harfbuzz on your system, then use the --preset samples-all flag during the CMake configuration step.
  10. Build the RmlUi Test Suite

    master

    The RmlUi test suite consists of three separate projects located under the Source directory. To include them in your build, enable the BUILD_TESTING CMake option. Once enabled, you can build the following targets:

    • rmlui_visual_tests: For testing the layout engine via visual comparison.
    • rmlui_unit_tests: For verifying individual library units.
    • rmlui_benchmarks: For performance tracking and identifying hotspots.