randrew/layout logo

randrew/layout

Free

Single-file library for calculating 2D UI layouts using stacking boxes. Compiles as C99 or C++.

FreeFree tier
Type
Open Source

About randrew/layout

Layout is a single-file C library for calculating 2D user interface layouts using a stacking box model. It compiles as both C99 and C++, has no external dependencies (by default uses standard library functions that can be replaced via preprocessor definitions), and supports integer (int16) or floating-point (float) coordinates. Based on the oui library, Layout focuses solely on layout calculation and does not handle user input, focus, or UI state. It is tested with GCC, Clang/LLVM, and MSVC (MSVC requires C++ compilation). The library is designed to be easily integrated into any project by copying one header file and defining LAY_IMPLEMENTATION in a single translation unit.

Key Features

Single-file library (layout.h) – just copy and include
No external dependencies (stdlib, assert, string used by default but replaceable)
Supports integer (int16) and floating-point (float) coordinates via LAY_FLOAT option
Compiles as C99 or C++ (C++ required for MSVC)
Customizable memory allocation: define LAY_REALLOC, LAY_FREE, LAY_ASSERT, LAY_MEMSET
Based on oui but simplified – no input handling or UI state
Tested with GCC, Clang/LLVM, and MSVC
Lightweight and fast: suitable for real-time 2D UIs

Pros & Cons

Pros
  • Extremely easy to integrate: one header file, no build system required
  • No external dependencies – self-contained and portable
  • Supports both integer and floating-point coordinate systems
  • Compiles as both C and C++ for broad compatibility
  • Customizable memory functions allow use in constrained environments
  • Fast and lightweight; suitable for real-time use
Cons
  • Limited to stacking box layout; no flexbox or grid support
  • Does not handle user input, focus, or UI state (must be integrated with other code)
  • MSVC requires C++ compilation due to lack of vector_size extension
  • No built-in support for advanced layout features like scrolling or constraints

Best For

2D user interface layout calculation in games or applicationsEmbedded UI rendering where a simple stacking layout is sufficientPrototyping and small-scale UI systems needing minimal dependenciesTools requiring a fast, portable layout engine

FAQ

How do I use Layout in my project?
Copy layout.h into your project, then define LAY_IMPLEMENTATION in exactly one .c or .cpp file that includes layout.h. All other files can include layout.h normally without that define.
Can I use floating-point coordinates instead of integers?
Yes, define LAY_FLOAT before including layout.h to use float instead of int16 for coordinates.
What are the compiler requirements?
Layout compiles as C99 or C++ with GCC and Clang. For MSVC, it must be compiled as C++. It has been tested with GCC (mingw64), VS2015, and Clang/LLVM.
Can I replace the default memory functions?
Yes. Define LAY_REALLOC, LAY_FREE, LAY_ASSERT, and LAY_MEMSET to use your own implementations. If you define LAY_REALLOC, you must also define LAY_FREE.
Does Layout handle user input or UI state?
No. Layout only calculates layout positions. It is based on oui but does not include any input handling, focus management, or UI state.