LWXGL is a single-window library — each process owns exactly one window at a time. The lifecycle is always linear: create → loop → terminate. Attempting to create a second window while one is already open returns an error instead of spawning a new one.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.
Creating a Window
GCreateWindow opens an X11 window and allocates all internal resources including the 16-color palette, a double-buffer pixmap, and the default 9x15 bitmap font.
| Parameter | Description |
|---|---|
w, h | Width and height of the window in pixels. By default the window is non-resizable — these values are locked as both the minimum and maximum size hints sent to the WM. |
name | The window title string shown by the window manager. |
bgcol | Palette index (0–15) used as the background fill color on every GRenderWindow call. |
| Code | Meaning |
|---|---|
0 | Success — window is ready to use. |
1 | XOpenDisplay failed — no X11 display available (check $DISPLAY). |
2 | Font load failed — the 9x15 bitmap font could not be found on the X server. |
3 | A window already exists in this process — destroy it first. |
127+i | Color allocation failed for palette index i. The X11 colormap may be exhausted. |
The Manual Loop
For full control over timing and per-frame logic, drive the loop yourself with three calls each iteration:GWindowShouldClose returns 1 after GDeleteWindow() is called, the user closes the window via the WM, or Ctrl+Escape is pressed.
The manual loop does not cap the frame rate. You are responsible for any sleep or timing logic. Use
GSimpleWindowLoop if you want automatic FPS capping.GSimpleWindowLoop
GSimpleWindowLoop is a batteries-included loop that caps frame rate, tracks timing, and exposes a per-frame callback:
| Parameter | Description |
|---|---|
target_fps | Target frames per second. The loop sleeps between frames to stay at this rate. |
on_every | Callback invoked once per frame after GHandleWindowEvents and GRenderWindow. Receives the current tick count (increments each frame) and the elapsed time since the last frame in seconds. Pass NULL to skip. |
std::chrono microsecond timers and yields the thread (rather than sleeping) when the remaining time is under 2 ms. If the process falls more than two full frames behind, the timer resets to prevent spiral lag.
on_every receives:
tick— anintcounter that starts at 0 and increments every frame.delta_time— actual elapsed seconds since the last frame (afloat). Use this for time-based motion rather than assuming a fixed timestep.
Debug Overlay
GSimpleWindowLoop enables the built-in debug overlay automatically. Press F12 at runtime to toggle it. The overlay shows:
- FT — average frame work time in microseconds (rolling 60-frame average).
- FPS — current measured frames per second.
GSimpleWindowLoop; it cannot be enabled in the manual loop.
Window Configuration
After the window is created, several properties can be changed at any time:GSetWindowTitle calls XStoreName to update the title bar immediately.
GSetWindowColor changes the background palette index used by GRenderWindow. The change takes effect on the next rendered frame.
GEnableResizing removes the min/max size hints that lock the window dimensions, allowing the window manager to resize the window freely. The Resize callback fires with the new w and h values whenever the window is resized.
By default,
GCreateWindow sets PMinSize | PMaxSize hints so that min == max == (w, h), making the window non-resizable. Calling GEnableResizing clears all size hints to enable WM resizing.Cleanup
Always callGTerminateWindow after the loop exits:
GDeleteElement on each), releases the font, GC, double-buffer pixmap, and all 16 allocated palette colors, then destroys the X11 window and closes the display connection. Skipping this call will leak X resources.
To programmatically trigger a close from within your code:
GDeleteWindow sets the close flag (equivalent to the user clicking the WM close button). If a Delete event handler has been attached with GEventAttachDelete, it is called first — returning 0 from that handler blocks the close. If no handler is set, the close always proceeds.
The keyboard shortcut Ctrl+Escape calls GDeleteWindow internally regardless of whether any event handler is attached.
Capturing a Region
GCaptureRegion copies a rectangular region of the current back-buffer and returns it as a heap-allocated PPM (P6) binary image:
| Parameter | Description |
|---|---|
x, y | Top-left corner of the region to capture, in window pixels. |
w, h | Width and height of the capture region. |
size | Output — filled with the total byte size of the returned buffer. |
P6\n<w> <h>\n255\n) followed by packed 24-bit RGB pixels. The caller is responsible for calling free() on the returned pointer.
GCaptureRegion reads from the double-buffer pixmap (bb), not the live X11 window. Call it after GRenderWindow has composited the latest frame to ensure the capture is up to date.