This guide walks through a complete LWXGL program from scratch. By the end you will have a 640×480 window containing a text label and a clickable button, running a capped frame loop at 60 FPS. No prior Xlib knowledge is required — LWXGL handles the display connection, colormap, double-buffering, and event dispatch internally.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.
Complete example
Walkthrough
Create the window
CreateWindow opens a connection to the X server, allocates the 16-color palette, creates the window, loads the system font, and sets up the off-screen back-buffer. It returns an integer status code:| Code | Meaning |
|---|---|
0 | Success |
1 | Cannot connect to X display ($DISPLAY not set or X server unreachable) |
2 | Font 9x15 could not be loaded |
3 | A window is already open (only one window per process is supported) |
127 + N | XAllocColor failed while allocating palette entry N (display may not support the required colors) |
CLR_BLACK = 0x0). The fifth argument is a bitfield of window flags.Three flags are available for the
f argument:FLAG_NONE(0)— fixed-size window, no canvas. The default for most applications.FLAG_CANVAS(1 << 1)— automatically creates a full-window drawable image at element id0, enabling pixel-level drawing withPrimitiveRect,PrimitiveCircle,PrimitiveLine, and related functions.FLAG_RESIZE(1 << 2)— allows the user to resize the window. Attach a handler withEventAttachResizeto respond to size changes.
FLAG_CANVAS | FLAG_RESIZE.Add a text label
CreateText(id, x, y, text, color) places static text at pixel coordinates (x, y) using the global font. The first argument is the element id — an integer you choose. You can later move, hide, or delete this element using that id.CLR_WHITE (0xF) is one of the 16 palette constants defined in libLWXGL.h.Add a button
CreateButton(id, x, y, w, h, u, hvr, p, label, onclick) creates an interactive button. The three color arguments (u, hvr, p) each encode two palette indices in a single byte:- Low nibble (
& 0x0F) — fill (background) color - High nibble (
>> 4 & 0x0F) — border color
CLR_GRAY | (CLR_LGRAY << 4) means: fill with CLR_GRAY (0x8), border with CLR_LGRAY (0x7), packed as 0x78.The three states are:u— normal (unpressed, not hovered)hvr— mouse hovering over the buttonp— mouse button held down
onclick function pointer is invoked when the button is released after a press. Pass NULL if you only want to style the button without a click action.Attach a global click handler (optional)
EventAttachClick registers a callback that fires on every mouse button press anywhere in the window, regardless of which element was clicked. The callback receives the pointer coordinates and the button index (1 = left, 2 = middle, 3 = right). This is independent of individual button onclick handlers.Pass NULL to disable the global handler.Run the main loop
MainWindowLoop(target_fps, on_every) blocks until the window is closed. Each frame it:- Processes all pending X events (mouse, keyboard, resize, close)
- Calls
on_every(tick, delta_time)— your per-frame callback — wheretickis the frame counter (starts at 0) anddelta_timeis seconds since the last frame as afloat - Renders all visible elements to the back-buffer and presents it
- Runs any queued tasks whose scheduled time has elapsed
- Sleeps for the remainder of the frame budget
NULL as the second argument if you do not need a per-frame callback.The frame budget is
1 000 000 / target_fps microseconds. LWXGL uses std::chrono::steady_clock internally — the target FPS is a cap, not a guarantee. Use the delta_time argument to write frame-rate-independent logic.Full color palette reference
All 16 palette indices are available as named constants:| Constant | Index | Color |
|---|---|---|
CLR_BLACK | 0x0 | Black |
CLR_BLUE | 0x1 | Dark blue |
CLR_GREEN | 0x2 | Dark green |
CLR_CYAN | 0x3 | Dark cyan |
CLR_RED | 0x4 | Dark red |
CLR_MAGENTA | 0x5 | Dark magenta |
CLR_ORANGE | 0x6 | Orange |
CLR_LGRAY | 0x7 | Light gray |
CLR_GRAY | 0x8 | Dark gray |
CLR_LBLUE | 0x9 | Light blue |
CLR_LGREEN | 0xA | Light green |
CLR_LCYAN | 0xB | Light cyan |
CLR_LRED | 0xC | Light red |
CLR_LMAGENTA | 0xD | Light magenta |
CLR_YELLOW | 0xE | Yellow |
CLR_WHITE | 0xF | White |
CLR_NONE (-1) anywhere a color is optional and should be omitted (e.g., a transparent background).