Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/DRESsedAlarm184/LWXGL/llms.txt

Use this file to discover all available pages before exploring further.

Text elements render a string at a fixed position in the window each frame using LWXGL’s bitmap font. Two creation functions are provided: CreateText holds a pointer to your string without copying it, while CreateCopiedText duplicates the string onto the heap so the original can be discarded immediately after creation. Both variants support multi-line strings — lines are split on \n characters and rendered 15 pixels apart. The active font (9x15 or 9x15bold) applies to all text elements and can be switched globally with SetGlobalBold.

Rendering Details

  • Font: 9x15 (or 9x15bold after SetGlobalBold(1)). Each character is 9 px wide; lines are 15 px tall.
  • Rendered via XDrawString onto the back-buffer pixmap each frame.
  • The y coordinate passed to Create* maps to the top of the first line; the baseline is drawn at y + 11 internally (matching ImmediateText behavior).
  • Newline characters (\n) are supported; every subsequent line steps 15 px down.
  • SetGlobalBold(int bold) — pass 1 to switch to 9x15bold, 0 to revert. Returns 1 on success. Affects all text elements rendered from the next frame onward.

Functions

CreateText

Creates a text label element that renders text every frame. The pointer text is stored as-is — LWXGL does not copy the string. The string must remain valid and unchanged for the entire lifetime of the element.
void CreateText(int id, int x, int y, const char* text, int color);
id
int
required
Unique integer identifier for this element. Used with DeleteElement, ElemModifyBounds, ElemSetVisible, etc. If an element already exists at this ID it will be overwritten.
x
int
required
Horizontal position in pixels from the left edge of the window.
y
int
required
Vertical position in pixels from the top of the window. The rendered baseline sits at y + 11.
text
const char*
required
Pointer to the null-terminated string to display. Must remain valid until DeleteElement(id) is called. Supports \n for multi-line output.
color
int
required
Text color, one of the CLR_* palette constants (e.g., CLR_WHITE).
CreateText stores the raw pointer. If the string lives on the stack or is freed before DeleteElement is called, rendering that element will produce undefined behaviour. Use CreateCopiedText if you cannot guarantee the pointer’s lifetime.

CreateCopiedText

Creates a text label element like CreateText, but internally calls strdup to copy the string onto the heap. The original pointer can be freed or go out of scope immediately after this call. LWXGL frees the copy when DeleteElement is called.
void CreateCopiedText(int id, int x, int y, const char* text, int color);
id
int
required
Unique integer identifier for this element.
x
int
required
Horizontal position in pixels from the left edge of the window.
y
int
required
Vertical position in pixels from the top of the window.
text
const char*
required
Null-terminated string to display. A heap copy is made immediately; the original may be freed after this call returns.
color
int
required
Text color, one of the CLR_* palette constants.
Use CreateCopiedText when building label strings with snprintf into a local buffer, or when displaying data that changes infrequently and you want a simple “set and forget” API. For frequently updating text, consider recreating the element each frame or using ImmediateText instead.

Code Example

#include "libLWXGL.h"
#include <stdio.h>

/* A static string — pointer is valid for the program's lifetime */
static const char* STATIC_LABEL = "LWXGL Text Demo\nLine two of the label";

static void on_frame(int tick, float dt) {
    (void)tick; (void)dt;
}

int main(void) {
    CreateWindow(400, 200, "Text Demo", CLR_BLACK, FLAG_NONE);

    /* CreateText: pointer must stay valid — safe here because STATIC_LABEL is in .rodata */
    CreateText(1, 20, 20, STATIC_LABEL, CLR_WHITE);

    /* CreateCopiedText: safe with a stack buffer */
    char version_buf[64];
    snprintf(version_buf, sizeof version_buf, "Build: %s %s", __DATE__, __TIME__);
    CreateCopiedText(2, 20, 70, version_buf, CLR_LGREEN);
    /* version_buf can now go out of scope safely */

    /* Switch to bold font for both elements */
    SetGlobalBold(1);

    MainWindowLoop(60, on_frame);
    TerminateWindow();
    return 0;
}

Build docs developers (and LLMs) love