In LWXGL, a window is the top-level X11 surface your application renders into. LWXGL follows a single-window-per-process model — only one window may exist at a time. The library manages the X11 display connection, graphics context, color allocation, font loading, and back-buffer pixmap internally, so you interact with a straightforward C API rather than raw Xlib.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
CallCreateWindow to open a window with a given pixel size, title, and background color index:
| Return Value | Meaning |
|---|---|
0 | Success — the window is open and ready |
1 | XOpenDisplay failed — no X server is reachable |
2 | Font load failed — the built-in 9x15 bitmap font could not be loaded |
3 | A window already exists in this process |
127 + i | Color allocation failed for palette entry i (0–15) |
CreateWindow returns 0, the X11 window is mapped, the back-buffer pixmap is allocated, and the window’s WM size hints are set to fix its dimensions. The default cursor (X cursor font glyph 68) is applied automatically.
The Double-Buffered Back-Buffer
LWXGL maintains an off-screen X11Pixmap called the back-buffer (bb) that is the same size as the window (or taller when scroll mode is enabled). Every drawing operation — element rendering, immediate-mode calls, and the debug overlay — targets the back-buffer. At the end of each frame the completed pixmap is copied to the visible window in a single XCopyArea call, preventing flickering.
When scroll mode is active (see ReserveScroll), the back-buffer height is set to the virtual scroll height rather than the physical window height. The back-buffer is recreated automatically whenever the window is resized. You do not need to manage the pixmap directly.
MainWindowLoop
MainWindowLoop is LWXGL’s frame driver. It blocks until the window is closed, executing the following work on every frame tick:
- Drain the X11 event queue — all pending
XEvents are dispatched to built-in and user-registered handlers. - Call
on_every(tick, delta_time)— your per-frame callback, wheretickis a frame counter (starting at 0) anddelta_timeis afloatrepresenting elapsed seconds since the previous frame. - Render all visible elements to the back-buffer, then blit the back-buffer to the window.
- Run queued tasks — any tasks scheduled with
NewQueuedTaskwhose target time has been reached are executed. - Sleep the remainder of the frame budget so that the loop runs at approximately
target_fpsframes per second.
By default,
on_every is called before elements are rendered (ORDER_ELEM_SECOND), meaning anything you draw with immediate-mode functions inside on_every appears beneath retained elements. Call SetRenderingOrder(ORDER_ELEM_FIRST) to flip the order so elements render first and on_every draws on top.on_every callback can be changed with SetRenderingOrder. By default, on_every runs first and elements are rendered on top (ORDER_ELEM_SECOND).
Closing and Cleanup
There are two distinct concepts: requesting a close and freeing resources.DeleteWindow() signals that the window should close. If a delete callback was registered with EventAttachDelete, that function is called and its return value decides whether the close proceeds (return 1 to close, return 0 to cancel). If no callback is registered, the window closes immediately.
TerminateWindow() performs the actual resource teardown. It must be called after MainWindowLoop returns:
- Frees all elements (calling
DeleteElementon each non-null slot) - Frees all allocated TGA image assets
- Frees any active modal state
- Releases the X11 font, GC, back-buffer pixmap, and all 16 palette colors
- Destroys the X11 window and closes the display connection
TerminateWindow() must be called after MainWindowLoop returns to free all resources. Do not call it before the loop exits — doing so while the loop is still running will leave dangling pointers and result in undefined behaviour.Resizable Windows
By default,CreateWindow locks the window to the requested dimensions by setting both the minimum and maximum WM size hints to (w, h). To allow the user to resize the window, call EnableResizing after CreateWindow: