v1.4.1Graphics & UIBSD-2-Clause / Apache-2.0

Ultra-portable 2D/3D immediate-mode rendering, bitmap fonts, tilemaps, perspective camera math, and flexbox-style UI layout engine in pure C with OpenGL, GLFW, WebGL2/WASM, and direct Linux framebuffer backends. Canonical header is <ttypt/l

GitHub
git clone https://github.com/tty-pt/liblamina

liblamina logo

liblamina

npm C99 BSD-2-Clause 2D & 3D Graphics OpenGL, WebGL2 & Framebuffer Flexbox UI

Ultra-portable 2D/3D immediate-mode rendering, bitmap fonts, tilemaps, perspective camera math, and flexbox-style UI layout engine in pure C with OpenGL, GLFW, WebGL2/WASM, and direct Linux framebuffer backends. Canonical header is <ttypt/lam.h> and pkg-config package lamina.pc.

liblamina provides an immediate-mode 2D/3D graphics layer and flexbox-style UI layout engine engineered for small engines, tools, kiosks, and 2D/2.5D games. It provides a unified immediate-mode API across desktop environments (OpenGL via GLFW on Linux X11/Wayland, macOS, Windows), browser environments (WebGL2 & HTML5 Canvas via WebAssembly), and direct hardware on embedded Linux (via memory-mapped /dev/fb0 without X11 or Wayland).


1. Quickstart

Initialize the display, obtain window dimensions, fill a rectangle, and present the frame:

#include <ttypt/lam.h>

int main(void)
{
	uint32_t w, h;

	lam_init();
	lam_size(&w, &h);
	lam_fill(0, 0, w, h, 0xFF202020); /* Dark gray background */
	lam_flush();
	return 0;
}

Compiling and Linking

liblamina links via -llamina using the lamina pkg-config definition:

cc my_app.c $(pkg-config --cflags --libs lamina)

Or manually:

cc my_app.c -I/usr/local/include -L/usr/local/lib -llamina -lcorm -lstele -lpng -lxxhash

2. Why liblamina? (2D & 2.5D Graphics & UI Without the Bloat)

Most graphics libraries either drag in massive runtime dependencies (SDL2, Raylib, heavyweight windowing systems) or lock developers into high-level GUI toolkits (GTK, Qt, Electron) that are impossible to run on headless servers, embedded Linux, browser WebAssembly reactors, or bare framebuffer kiosks (/dev/fb0).

liblamina provides an alternative path in pure C:

  • Zero Heavy Frameworks: Compact immediate-mode rendering pipeline with procedural pixel hooks (lam_render), Porter-Duff FBO alpha compositing, built-in PNG and pure-C JPEG decoding (src/jpeg.c), and double-buffered frame presentation (lam_flush).
  • CSS Flexbox & Grid UI Built In: Pure-C CSS parser (src/ui-css.c) supporting Flexbox, CSS Grid tracks, calc(* /), inset rings, SVG properties, box shadows, border radii, DOM-style tree mutation ops (lam-ui.h), and a zero-coupling libbud/.cx host adapter (<ttypt/lam-bud.h>), powered by libcorm.
  • Bitmap & TrueType Font Rendering: Supports both pre-rendered bitmap atlases and built-in TrueType (.ttf) fonts with fallback chains (<ttypt/lam-font.h>).
  • 2.5D / 3D Camera & Unified OpenGL/WebGL2 Pipeline: Perspective and orthographic $4 \times 4$ matrix math, ground-plane ray unprojecting, 3D tilted-camera tile drawing, and an identical <ttypt/webgl.h> API backed natively by OpenGL/GLFW (src/webgl_native.c) or in the browser by htdocs/webgl-cap.js and htdocs/lamina-cap.js.
  • Run on Raw Hardware or Browser WebGL2: Seamlessly falls back to /dev/fb0 when no display server is present, auto-detecting screen geometry and bit depth via ioctl(fb_fd, FBIOGET_VSCREENINFO, ...), or streams directly to WebGL2 in the browser via htdocs/webgl-cap.js and htdocs/lamina-cap.js.
  • Ecosystem Integration: Direct integration with libcorm for UI state caching and libstele for cross-platform system primitives and isomorphic WASM boundaries.

The Five Jobs It Does

  1. The 2D Immediate-Mode Canvas: Primitives, rectangle fills, pixel-by-pixel procedural shaders (lam_render), color tinting, Porter-Duff FBO alpha, and frame presentation via lam_flush.
  2. Textures, Sprites & Tilemaps: Fast PNG and built-in pure-C JPEG loading, pixel picking/painting, spritesheet subdivision (lam_tm_new), and batch 2D/3D tilemap drawing (lam_tile_map_draw, lam_tile_draw_ground_3d, lam_tile_draw_billboard_3d).
  3. 3D Camera & Native/Browser WebGL2: $4 \times 4$ column-major matrix operations, tilted-camera setup (lam_cam_set_3d), screen-to-ground ray casting (lam_unproject_ground), and unified WebGL2 rendering (VAOs, UBOs, glCmdFlush command ring, instancing, 2D/3D texture arrays, half-float FBOs, GPU pass timers) across native OpenGL/GLFW (src/webgl_native.c) and WASM (htdocs/webgl-cap.js).
  4. Bitmap & TrueType Font Rendering: Pre-rendered bitmap font atlases and built-in TrueType (.ttf) font rendering (lam-font.h) with fallback chains, pixel-sized drawing/measurement, word wrapping, and bounding-box calculation.
  5. CSS Flexbox/Grid UI, Bud Adapter & Input Dispatch: Declarative UI trees (lam-ui.h) with pure-C CSS parsing (src/ui-css.c), Grid/Flexbox layout, lam-bud.h reactive host adapter, and non-blocking keyboard/gamepad callbacks (lam-key.h).

3. API Overview & Headers

The API contract lives across eight modular headers in <ttypt/>:

1. Core Rendering & Primitives (<ttypt/lam.h>)

  • Lifecycle & Frame Presentation: lam_init(), lam_deinit(), lam_size(&w, &h), lam_flush(), lam_poll().
  • Screen Clearing & Primitives: lam_clear(color), lam_fill(x, y, w, h, color), lam_rect(x, y, w, h, border_color, border_width), lam_line(x0, y0, x1, y1, color, thickness).
  • Color Helpers: LAM_RGBA(r, g, b, a), LAM_RGB(r, g, b), LAM_COLOR_R/G/B/A(c), and standard palette constants (LAM_COLOR_WHITE, LAM_COLOR_BLACK, LAM_COLOR_RED, etc.).
  • Procedural Shading: lam_render(lambda, x, y, w, h, ctx) for custom pixel-by-pixel generators.
  • Texture Operations: lam_tex_load(filename) (PNG and built-in pure-C JPEG via src/jpeg.c), lam_tex_draw(tex, x, y, dw, dh), lam_tex_draw_x(...), lam_tex_pick(tex, x, y), lam_tex_paint(tex, x, y, color), lam_tex_save(tex), lam_tint(color).
  • WebGL2 / WASM Streaming Hooks: lam_canvas_flush(), lam_shader_register(), lam_shader_bind(), lam_shader_uniforms(), lam_terrain_mesh_flush(), lam_billboard_batch_flush().

2. CSS Flexbox & Grid UI Layout (<ttypt/lam-ui.h>)

  • Declarative Element Constructors & Mutation Ops:
  • lam_ui_flex(parent, dir): Create a flex container with display: flex in row or column orientation.
  • lam_ui_box(parent, w, h, bg_color): Create a sized container with background color.
  • lam_ui_label(parent, text, font_ref, color): Create a text label node with font and color.
  • lam_ui_new(parent, style): Low-level element creation, plus DOM-style tree mutation ops for reactive reconcilers.
  • Direct Style Setters: lam_ui_set_size(div, w, h), lam_ui_set_bg(div, color), lam_ui_set_padding(div, left, top, right, bottom), lam_ui_set_flex(div, grow, shrink, basis), lam_ui_set_align(div, align_items, justify_content).
  • Layout Accessors: lam_ui_get_x(), lam_ui_get_y(), lam_ui_get_width(), lam_ui_get_height(), lam_ui_get_text(), lam_ui_get_class().
  • Pure-C CSS Parser & Styling (src/ui-css.c): lam_ui_stylesheet_init() and lam_ui_stylesheet_add() supporting Flexbox, CSS Grid tracks, inline/replaced flow layout, form control sizing, calc(* /), inset rings, SVG properties, borders, margins, padding, border radii (lam_border_radius), and drop shadows (lam_box_shadow).
  • Caching Engine: Render cache powered by libcorm for high-performance layout reuse.

3. Zero-Coupling Bud Reactive Host Adapter (<ttypt/lam-bud.h>)

  • Header-only adapter (lam_bud_host_init(), lam_bud_host_ops(), lam_bud_sync_layout(), lam_bud_dispatch_event(), lam_bud_dispatch_click()) bridging libbud reactive reconciler and .cx components directly onto lam_ui trees without coupling libbud to liblamina.

4. Bitmap & TrueType Fonts (<ttypt/lam-font.h>)

  • Bitmap Atlas Loading: lam_font_open(path) loads pre-rendered bitmap font atlases (e.g. converted from BDF files via bdf2tm.py).
  • TrueType (TTF) Fonts & Color Emoji: lam_font_open_ttf(path, px_size), lam_font_add_fallback(font_ref, fallback_ref), lam_font_is_ttf(), lam_font_line_height(), lam_font_ascent(), browser-parity serif/sans-serif face selection, and color emoji priority in fallback chains.
  • Text Rendering & Measurement: lam_font_draw(...) / lam_font_draw_px(...) / lam_font_draw_px_ex(...) and lam_font_measure(...) / lam_font_measure_px(...) / lam_font_measure_px_ex(...) with letter-spacing, leading-space preservation flags, word wrapping, and color tinting.

5. Sprite Tilemaps & 3D Sprites (<ttypt/lam-tm.h>)

  • Atlas Subdivision: lam_tm_new(tex, tile_w, tile_h) segments a spritesheet into a 2D tile grid.
  • Tile Presentation: Draw individual tiles by index (lam_tile_draw(...)).
  • Batch Grid Map Drawing: lam_tile_map_draw(tm_ref, tiles, map_w, map_h, x, y, scale) renders entire 2D map grids from a tile index buffer in a single call.
  • 3D Ground & Billboard Tiles: lam_tile_draw_ground_3d(tm_ref, idx, wx, wy, wz, tw, th) and lam_tile_draw_billboard_3d(tm_ref, idx, wx, wy, wz, sw, sh, anchor_y).

6. 3D Camera & Matrix Math (<ttypt/lam-cam.h>)

  • Matrix Math (lam_mat4_t): lam_mat4_identity, lam_mat4_mul, lam_mat4_translate, lam_mat4_scale, lam_mat4_rotate_x/y/z, lam_mat4_perspective, lam_mat4_ortho, lam_mat4_look_at, lam_mat4_invert.
  • Camera Control: lam_cam_set_3d(...), lam_cam_set_2d(), lam_cam_is_3d(), lam_cam_get(), lam_cam_get_vp(), lam_unproject_ground().

7. Input Mapping & Gamepads (<ttypt/lam-key.h>)

  • Callbacks: lam_key_reg(key, callback) and lam_key_default_reg(callback) for non-blocking raw key events.
  • Key Code Translation: lam_key_val(code) and lam_key_parse(target, len, code, flags) map hardware keycodes to ASCII/UTF-8 characters.
  • Gamepads & Hats: Constants for directional pads and buttons (LAM_HAT_*, LAM_GAMEPAD_*).

8. Unified Native & WebGL2 Hardware Bridge (<ttypt/webgl.h>, src/webgl_native.c, htdocs/webgl-cap.js, htdocs/lamina-cap.js)

  • Portable WebGL2-shaped C API backed natively by desktop OpenGL/GLFW (src/webgl_native.c), mobile OpenGL ES 3.0 (LAM_NO_GLFW), and in the browser by htdocs/webgl-cap.js (createWebGlBridge) and htdocs/lamina-cap.js (createLaminaBridge).
  • Supports WebGL2 VAOs (glCreateVertexArray, glBindVertexArray, LAM_GL_CMD_BIND_VAO), UBOs (glBindBufferBase, glGetUniformBlockIndex, glUniformBlockBinding), flat uint32_t command ring submission (glCmdFlush), zero-alloc glBufferSubData, hardware instancing (glDrawArraysInstanced, glDrawElementsInstanced, glVertexAttribDivisor), 3D / 2D-array textures (glTexStorage3D, glTexImage3D, glTexSubImage3D, glGenerateMipmap), half-float formats (GL_R16F, GL_RGBA16F, GL_HALF_FLOAT), offscreen FBO passes with swapchain viewport isolation, GPU pass timers (EXT_disjoint_timer_query_webgl2 via glTimerBegin/glTimerEnd), and parallel non-blocking shader compilation/linking (KHR_parallel_shader_compile natively and in the browser, with 1-per-frame fallback link pacing in htdocs/webgl-cap.js).

4. Display Backends

  • Linux Desktop: Automatically uses GLFW when running under an active X11 or Wayland session (DISPLAY or WAYLAND_DISPLAY set), including native OpenGL execution of <ttypt/webgl.h> via src/webgl_native.c.
  • Embedded Linux Framebuffer: Falls back to direct /dev/fb0 rendering when no display server is present, accessing screen buffers without X11.
  • macOS & Windows: Native GLFW backend with OpenGL acceleration (Windows builds auto-bundle static GLFW 3.4).
  • Android & iOS (OpenGL ES 3.0): Headless/mobile GLES3 backend (LAM_NO_GLFW) with cross-compiled static libpng and freetype sysroots.
  • Browser (WebGL2 / Canvas): Hardware-accelerated WebGL2 and 2D Canvas presentation via htdocs/webgl-cap.js, htdocs/lamina-cap.js, and @tty-pt/lamina.

Linux Framebuffer Setup (/dev/fb0)

To run directly on the Linux framebuffer without an X11 or Wayland display server:

  1. Device Permissions: Ensure your user has read/write permissions to the framebuffer and input event devices:

`bash sudo usermod -aG video,input $USER `

  1. TTY Console: Switch to a raw virtual console (e.g. Ctrl+Alt+F3) or run in an embedded environment (e.g. Raspberry Pi or kiosk).
  2. Auto-Detection: liblamina automatically reads screen resolution, row stride, and bit depth via ioctl(fb_fd, FBIOGET_VSCREENINFO, ...) supporting standard 16bpp and 32bpp RGBA/BGRA modes.

5. Examples

Six complete example programs demonstrate features from raw pixels to styled UI trees:

# Generate test fixtures and build examples
python3 tests/generate_fixtures.py
make examples

# Run an example
LD_LIBRARY_PATH=./lib ./examples/01_basic_rendering
ExampleSource FileDescription
01 Basic Renderingexamples/01_basic_rendering.cWindow creation, color fills, and double-buffered presentation
02 Texturesexamples/02_textures.cLoading PNG images, drawing sprites, and sub-region blitting
03 Fontsexamples/03_fonts.cBitmap font rendering, scaling, measurement, and text wrapping
04 Tilemapsexamples/04_tilemaps.cSegmenting sprite atlases into grid tilemaps
05 UI Layoutexamples/05_ui_layout.cConstructing flexbox container trees with lam_ui_div
06 Advanced UIexamples/06_ui_advanced.cAdvanced UI styling, borders, layout inspection, and caching

See examples/README.md for detailed walkthroughs.


6. Testing & Test Suite

The library includes an extensive test suite covering all public subsystems:

make test-build       # builds the C unit and integration test binaries
make test             # executes unit tests across core, textures, fonts, CSS, WebGL, Bud/.cx, and UI
make npm-test         # builds and executes the Node.js & WebAssembly test suite

Test coverage spans:

  • Core rendering and viewport clipping (test_core)
  • PNG/JPEG texture loading and sub-region blitting (test_textures)
  • Bitmap and TrueType (.ttf) font rendering and wrapping math (test_fonts, test_fonts_ttf)
  • Tilemap indexing and bounds checking (test_tilemaps)
  • 3D camera matrix math, perspective projection, and ground-plane ray unprojecting (test_cam_3d)
  • Pure-C CSS parser, Flexbox/Grid layout calculations, styling, and libcorm cache eviction (test_css_parser, test_ui_layout, test_ui_style, test_ui_render)
  • Native OpenGL/GLFW WebGL2 surface (test_webgl) and libbud / .cx reactive UI host integration (test_bud_lamina, test_cx_lamina, tests/music_panel.cx)
  • WebGL2 & WebAssembly surface/bridge integration (npm/tests/lamina.test.mjs)

7. Installation & Build

Prebuilt Packages

Prebuilt packages across all 9 platforms β€” Linux (APT / Alpine / Arch / Fedora-RHEL), macOS (Homebrew), Windows (winget / Scoop / MSYS2), OpenBSD, plus static mobile sysroots for Android (https://tty.pt/android/ β€” arm64-v8a, x86_64) and iOS (https://tty.pt/ios/ β€” liblamina.xcframework + SwiftPM) β€” are distributed via tty.pt packages. Use liblamina as the package name.

Dependencies

  • Core: libcorm, libstele, libpng, libxxhash
  • Desktop (GLFW backend): libglfw3-dev, libglew-dev, OpenGL
  • Embedded (Framebuffer backend): Linux /dev/fb0 (standard in Linux kernel)

On Debian / Ubuntu:

sudo apt-get install build-essential pkg-config libglfw3-dev libglew-dev libpng-dev libxxhash-dev

Build from Source

Builds use shared libstele rules (include ttypt/include.mk; WASM reactor builds honor WASM_GLOBAL_BASE for collision-free multi-module shared linear memory):

make                  # builds lib/liblamina.so
make test             # runs the C unit test suite
make npm-test         # builds and runs the Node.js / WASM test suite
make examples         # builds example programs in examples/
sudo make install     # installs headers to /usr/local/include/ttypt and lib to /usr/local/lib

8. JavaScript & WebAssembly Bindings (@tty-pt/lamina)

For browser, Node.js, and WebGL2 applications, liblamina ships a full TypeScript, WebGL2 bridge (with workerOwned capability metadata for libstele Web Worker routing), and WebAssembly package on npm:

npm config set @tty-pt:registry https://tty.pt/npm/
npm install @tty-pt/lamina

See npm/README.md for the full TypeScript, WebGL2 renderer, and WebAssembly API reference.


License

Mostly BSD 2-Clause License. Copyright (c) 2025–2026, Paulo AndrΓ© Azevedo Quirino. See LICENSE. The Linux framebuffer device initialization is derived from pedroth's linux-framebuffer and remains under the Apache License, Version 2.0.

Maintained by:tty-pt