Every LWXGL application follows the same three-phase lifecycle: create a window and allocate its X11 resources, loop to process events and render frames, then terminate to free all resources. Understanding this flow is essential before using any other part of the API.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.
GCreateWindow must be called before any other G* function. All LWXGL
state — the X11 display connection, graphics context, back-buffer, font, and
color palette — is initialised inside GCreateWindow.Creating a Window
| Parameter | Type | Description |
|---|---|---|
w | int | Window width in pixels |
h | int | Window height in pixels |
name | const char* | Title bar string |
bgcol | int | Background color — a palette index (0–15) |
GCreateWindow opens a connection to the X11 display, loads the 9x15 bitmap font, allocates all 16 palette colors from the default colormap, creates the X11 window, and sets up the Pixmap back-buffer used for double buffering.
Return codes:
| Code | Meaning |
|---|---|
0 | Success |
1 | Could not open X11 display (XOpenDisplay returned NULL) |
2 | Font 9x15 not found on the server |
3 | A window is already open (call GTerminateWindow first) |
w × h, so the window cannot be resized by the user.
The Rendering Loop
After creating a window you need a loop that pumps X11 events and issues draw calls every frame. LWXGL provides a managed loop helper and also supports a fully manual pattern.Managed loop — GSimpleWindowLoop
GSimpleWindowLoop runs until GWindowShouldClose() returns non-zero. On each frame it:
- Calls
GHandleWindowEvents()to process queued X11 events (input, expose, close) - Calls
GRenderWindow()to draw all elements to the back-buffer and blit to the screen - Calls your
on_everycallback (if non-NULL), passing the current tick counter (a monotonically increasingintstarting at 0)
target_fps frames per second using a high-resolution std::chrono timer. Between frames it either sleeps or yields depending on how much headroom remains.
GSimpleWindowLoop also enables the debug overlay — press F12 at runtime to toggle it on and off (see Debug overlay below).
Manual loop
For cases that need custom timing, integration with an external event loop, or full control over when events and rendering happen, you can drive the loop yourself:GHandleWindowEvents and GRenderWindow must be called together each iteration; calling one without the other will result in stale input state or a frozen display.
Closing the Window
GDeleteWindow sets the internal closing flag to 1, which causes GWindowShouldClose() to return non-zero and breaks the loop on the next iteration.
The X11 close button (the window manager’s ✕) is intercepted via the WM_DELETE_WINDOW atom and internally calls GDeleteWindow, so both code-initiated and user-initiated closes follow the same path.
Custom close logic: attach a handler with GEventAttachDelete before entering the loop:
GDeleteWindow calls it and sets closing to whatever value the handler returns. Returning 0 cancels the close.
Cleanup
GTerminateWindow must be called after the loop exits. It performs a full teardown in order:
- Calls
GDeleteElementfor every non-NULLslot in the element vector - Frees the X11 font, graphics context, back-buffer Pixmap, and stipple Pixmap
- Releases all 16 allocated palette colors from the colormap
- Destroys the X11 window and closes the display connection
GTerminateWindow will leak X11 resources.
Double Buffering
LWXGL renders every frame entirely into an off-screenPixmap (the back-buffer, bb) before copying it to the visible window in one XCopyArea call. This eliminates screen tearing — the display is only updated once the complete frame is ready.
GRenderWindow follows this sequence each frame:
- Fill the back-buffer with the background color
- Iterate the element vector and render each visible element onto the back-buffer
- Draw any active modal dialog on top
- Draw the debug overlay if enabled
XCopyArea— blit the back-buffer to the windowXSync— flush the display connection
Debug Overlay
When usingGSimpleWindowLoop, the debug overlay is armed automatically. Press F12 at any time to toggle it. The overlay renders a small panel in the top-left corner of the window showing:
- FPS — the current measured frame rate
- FT — the rolling average frame time in microseconds (averaged over the last 60 frames)