Image elements are LWXGL’s primary tool for custom 2D rendering. An image element is aDocumentation 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.
width × height pixel buffer where every byte holds a palette color index from 0 to 15. You can write to this buffer directly, use the built-in primitive drawing functions to compose shapes and sprites, or mix both approaches. Changes become visible on screen only after you call GUpdateImage, which efficiently transfers only the pixels that changed since the last flush.
Coordinates used inside primitive functions are relative to the image element’s own pixel buffer — the top-left corner of the buffer is always
(0, 0) — not to the window coordinate space.Creating an Image Element
XImage backed by two byte arrays of size w * h: one for the current pixel data and one to track the previous state for dirty-pixel optimization. The image is placed at window position (x, y) and rendered every frame by GRenderWindow.
Writing Pixel Data
GGetImageData returns a pointer to the raw pixel buffer. The buffer is stored in row-major order: pixel at column x, row y lives at index y * width + x. Each byte is a palette color index (0–15).
GUpdateImage.
Flushing to Screen
XImage::put_pixel and updates the snapshot. Only changed pixels are written to the XImage, making it efficient to call GUpdateImage every frame even when most pixels are static.
GRenderWindow copies the finalized XImage to the screen back-buffer, but it does not call GUpdateImage for you. You must call GUpdateImage explicitly after modifying pixel data.Clearing an Image
c using a single memset. This is much faster than looping over the buffer yourself and is the recommended way to clear the canvas at the start of each frame.
Drawing Primitives
All primitive functions write palette indices directly into an image element’s pixel buffer. None of them callGUpdateImage — remember to flush after drawing.
GPrimitiveRect — Filled Rectangle with Border
GPrimitiveRect — Filled Rectangle with Border
fg is the border/outline color and bg is the interior fill color. Pass -1 for either to skip that layer.GPrimitiveCircle — Filled Circle with Border
GPrimitiveCircle — Filled Circle with Border
(cx, cy) with radius r. fg is the 1-pixel outline color; bg is the interior fill. Pass -1 to skip fill or border. Pixels outside the image bounds are silently clipped.GPrimitiveLine — Straight Line
GPrimitiveLine — Straight Line
(x1, y1) to (x2, y2) using incremental floating-point stepping (similar to Bresenham’s algorithm). color is a single palette index. Out-of-bounds pixels are skipped.GPrimitiveSprite — RLE Monochrome Sprite
GPrimitiveSprite — RLE Monochrome Sprite
(sx, sy). color is the palette index used for filled pixels. scale is the pixel magnification factor (1 = 1:1, 2 = double-size, etc.).Sprite Format
Sprites are described by compact ASCII strings. The renderer processes characters left-to-right, advancingx for each pixel drawn and resetting to sx on a newline.
| Character | Meaning |
|---|---|
# | Draw one filled pixel using the color argument |
. | Draw one pixel in palette color 0 (black) |
$ | Newline — move to the next row and reset x to sx |
> | Skip one pixel to the right without drawing |
N<char> | Repeat <char> exactly N times (e.g. 5# draws 5 filled pixels) |
N[...] | Repeat the group inside [...] exactly N times |
! | End of sprite — stop processing |
N always precedes the character or group it modifies. For example, 3> skips 3 pixels right and 2[3#2.] repeats the group 3#2. twice.
Example — 5×5 diamond:
Redrawing All Images
XImage. This is useful after a palette modification, since changing a color index affects how every existing pixel with that index appears but does not change the stored byte values in the pixel buffers.