An image element is a fixed-size pixel canvas backed by anDocumentation 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.
XImage. You write palette-index bytes into the image’s pixel buffer, call GUpdateImage to push changes to the X11 surface, and the canvas is blitted to the window on every GRenderWindow call. Image canvases are ideal for custom rendering, procedural graphics, tilemaps, or any content that doesn’t fit the standard widget model.
All primitive functions (
GPrimitiveRect, GPrimitiveCircle, GPrimitiveLine, GPrimitiveSprite) and GDrawString operate in image-local coordinates: (0, 0) is the top-left pixel of the image canvas, not the window. The image element’s (x, y) position only controls where the finished canvas is composited into the window.Creating and Managing Image Canvases
GCreateImage
w × h pixels at window position (x, y). The backing XImage is allocated via XCreateImage at the display’s default visual and depth. Both the user-facing pixel buffer and an internal “previous frame” dirty-tracking buffer are zero-initialised (calloc), so every pixel starts at palette index 0 (black by default).
Element slot to create the image at.
X position of the image’s top-left corner within the window (pixels).
Y position of the image’s top-left corner within the window (pixels).
Width of the image canvas in pixels.
Height of the image canvas in pixels.
GGetImageData
w * h byte pixel buffer for image element id. Each byte holds a palette index in the range 0–15. The buffer is laid out in row-major order: the pixel at column x, row y is at data[y * w + x].
Write palette indices directly into this buffer to paint pixels, then call GUpdateImage to push the changes to the X11 surface.
The image element whose pixel buffer to retrieve.
unsigned char*: pointer to the writable pixel buffer. Valid until the element is deleted or recreated. Do not free.
Example
GUpdateImage
XImage using the display’s put_pixel function. Unchanged pixels are skipped, making incremental updates efficient even for large canvases.
You must call GUpdateImage after modifying the pixel buffer before calling GRenderWindow — the renderer always blits whatever pixel data is currently in the XImage; it does not call GUpdateImage automatically.
The image element to synchronise.
GClearImage
c using memset. This is faster than looping over the buffer manually. Call GUpdateImage after clearing if you want the change to appear on screen.
The image element to clear.
Palette index
0–15 to fill every pixel with. Use 0 for black.GRedrawAllImages
255, which never equals a valid 0–15 palette index) and then calling GUpdateImage on each image. This causes every pixel to be rewritten regardless of whether it changed.
Call this after GPaletteModify with redraw = 0 to apply colour changes to image canvases, or after restoring a saved pixel buffer from external storage.
Example
Bitmap Font Rendering
GImageSetFont
id for use with GDrawString. The font is described as a flat byte array of 256 × h bytes: one entry per ASCII code (0–255), each entry h bytes tall. Each byte represents one row of 8 pixels with the MSB mapping to the leftmost pixel.
The library copies the font buffer internally, so you can free your source buffer immediately after this call.
The image element that will use this font.
Pointer to a
256 * h byte array. Index into it as font[char_code * h + row]. Each byte is an 8-bit bitmask: bit 7 (MSB) = leftmost pixel, bit 0 = rightmost.Character height in pixels. Also controls the Y advance for
\n in GDrawString.GDrawString
txt onto image element id starting at image-local pixel position (x, y) using the bitmap font loaded by GImageSetFont. Each character is 8 pixels wide with a 1-pixel horizontal gap, giving a stride of 9 pixels per character. Pixels with a 0 bit in the font bitmap are not written (transparent background).
\n (ASCII 10) resets the X position back to the original x and advances y by h pixels (the font character height).
The image element to draw text onto.
Starting X position in image-local coordinates (pixels from the image’s left edge).
Starting Y position in image-local coordinates (pixels from the image’s top edge).
Null-terminated string to render. Supports
\n for line breaks.Palette index
0–15 for the text foreground color. Background pixels are left unchanged.Drawing Primitives
All primitives write directly into the pixel buffer of the target image element. CallGUpdateImage after your drawing operations to push the result to the X11 surface.
GPrimitiveRect
id. The 1-pixel outline uses fg; the interior is filled with bg. Pass -1 for either to skip that part. If fg == -1, the border is treated as bg (the outer edge is drawn with the fill colour instead of being skipped).
Pixels outside the image bounds are clipped silently.
Target image element.
Left edge of the rectangle in image-local coordinates.
Top edge of the rectangle in image-local coordinates.
Width in pixels.
Height in pixels.
Palette index for the 1-pixel border. Pass
-1 to skip the border (border falls back to bg).Palette index for the fill. Pass
-1 to leave the interior unchanged (border only).GPrimitiveCircle
cx, cy) with radius r pixels. Pixels whose squared distance from the center satisfies (r-1)² ≤ dx²+dy² ≤ r² are treated as the border and painted with fg. Pixels strictly inside that ring (dx²+dy² < (r-1)²) are painted with bg. Pass -1 for either to skip that layer.
Pixels outside the image bounds are clipped silently.
Target image element.
X coordinate of the circle center in image-local coordinates.
Y coordinate of the circle center in image-local coordinates.
Radius in pixels.
Palette index for the 1-pixel ring border. Pass
-1 to draw a solid disc with no distinct border.Palette index for the filled interior. Pass
-1 for an outline-only circle.GPrimitiveLine
x1, y1) to (x2, y2) using palette index color. The algorithm computes steps = max(|dx|, |dy|) and advances by fractional increments, rounding to the nearest integer pixel at each step. This produces a connected, single-pixel-wide line regardless of angle.
Pixels outside the image bounds are clipped silently.
Target image element.
X coordinate of the start point (image-local).
Y coordinate of the start point (image-local).
X coordinate of the end point (image-local).
Y coordinate of the end point (image-local).
Palette index
0–15 for the line color.GPrimitiveSprite
id, placing the sprite’s top-left pixel at image-local position (sx, sy). The color parameter sets the foreground palette index used for # pixels. Transparent pixels (.) write palette index 0. The scale parameter multiplies each logical pixel into a scale × scale block.
Target image element.
X position of the sprite’s top-left corner in image-local coordinates.
Y position of the sprite’s top-left corner in image-local coordinates.
Palette index
0–15 used to draw # pixels.Null-terminated RLE sprite string. See the Sprite Format section below.
Pixel scale factor.
1 = 1:1 (each logical pixel = 1 screen pixel), 2 = 2×2 block per pixel, etc.Sprite Format
The RLE sprite string is a compact ASCII encoding for pixel art. The cursor starts at (sx, sy) and advances left-to-right, row by row.
| Token | Meaning |
|---|---|
# | Draw one foreground pixel (color) and advance right by scale px. |
. | Draw one transparent pixel (palette index 0) and advance right by scale px. |
$ | Move to the start of the next row (x resets to sx, y advances by scale px). |
> | Skip right by scale px without writing a pixel. |
N# / N. | Repeat # or . exactly N times (e.g., 3# = three foreground pixels). |
N$ | Advance N rows at once. |
N> | Skip N pixels right. |
N[…] | Repeat the group […] exactly N times. Groups can be nested. |