This guide walks through creating a minimal LWXGL application with a window, a text label, a button, and a frame loop. By the end you will have a working program that opens a 640×480 window, greets the user, and responds to a button click — all in a single C source file.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.
Include the header
libLWXGL.h is the only LWXGL header you need. It exposes the full public API and is compatible with both C and C++ translation units.hello.c
Define a button callback and create the window
Declare any element callbacks before
main, then call GCreateWindow to open the X11 window. The function takes a width, height, title string, and a background color as a palette index.hello.c
GCreateWindow return codes:| Return value | Meaning |
|---|---|
0 | Success |
1 | Could not open X display ($DISPLAY not set or X server unavailable) |
2 | Could not load the built-in 9x15 bitmap font |
3 | A window is already open |
Add UI elements
Elements are created by integer ID. IDs are chosen by your code and used later to read values, reposition, hide, or delete the element. Here we create a text label (ID The three color arguments to
0) and a button (ID 1).hello.c
GCreateButton (u, hvr, p) each encode two palette indices in a single byte using packed nibbles: the high nibble (bits 7–4) is the border/foreground color and the low nibble (bits 3–0) is the fill/background color. This applies to all three button states — unpressed, hovered, and pressed.Enter the frame loop
GSimpleWindowLoop drives the application. It blocks the calling thread, running event handling and rendering every frame at the requested rate, and optionally invoking a per-frame callback.hello.c
GSimpleWindowLoop blocks until the window is closed — either by the user clicking the window manager’s close button, by pressing Ctrl+Escape, or by your code calling GDeleteWindow(). Passing NULL as the second argument is valid when you have no per-frame logic; the loop still handles input and redraws every frame.GTerminateWindow frees all elements, releases palette colors, destroys the back-buffer pixmap, and closes the X11 connection. Always call it before your program exits.Adding a per-frame callback
When you need to update state every frame — animate a canvas, poll the keyboard, update a timer — pass a callback as the second argument toGSimpleWindowLoop:
delta_time reflects actual elapsed wall-clock time, so it is slightly larger than the nominal frame duration (e.g. ~0.01667 at 60 FPS) whenever a frame runs a little late. Use it to keep motion and animations frame-rate independent.
LWXGL provides many more element types beyond text and buttons, including
single-line input fields, checkboxes, scrollable consoles, and writable image
canvases with primitive drawing support. See the UI Elements
guide for a full walkthrough of each element type.