Skip to main content

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.

Button elements are interactive rectangles with a centered text label. LWXGL automatically tracks mouse position and button state to switch between three visual appearances: the default (unpressed) state, a hover state when the pointer is inside the button, and a pressed state while the left mouse button is held down. Each of the three state parameters is a packed color integer that encodes both the fill (background) and border (foreground) colors in a single value. The low nibble is the fill color index and the high nibble is the border color index.

Packed Color Format

Many LWXGL element parameters use a packed integer to store two CLR_* indices:
packed = fill_color | (border_color << 4)
For example, a button with a light-gray fill and a white border:
int state = CLR_LGRAY | (CLR_WHITE << 4);
The renderer extracts the low nibble (L(x) = x & 0xF) for the fill and the high nibble (H(x) = (x >> 4) & 0xF) for the border.

Functions

CreateButton

Creates an interactive button element. The button is drawn as a filled rectangle with an outline; the label is centered horizontally and vertically inside the bounds.
void CreateButton(int id, int x, int y, int w, int h,
                  int u, int hvr, int p,
                  const char* label, void (*onclick)(void));
id
int
required
Unique integer identifier for this element. Reusing an existing ID replaces the old element.
x
int
required
Left edge of the button in pixels.
y
int
required
Top edge of the button in pixels.
w
int
required
Width of the button in pixels.
h
int
required
Height of the button in pixels.
u
int
required
Packed color for the unpressed (default) state. Low nibble = fill color, high nibble = border color.
hvr
int
required
Packed color for the hover state (pointer inside button, no click held). Same packing as u.
p
int
required
Packed color for the pressed state (left mouse button held while inside button). Same packing as u.
label
const char*
required
Text label drawn centered inside the button. Centering is calculated using a fixed character width of 9 px. The pointer is stored directly — keep the string valid for the element’s lifetime.
onclick
void (*)(void)
required
Callback invoked when the user releases the left mouse button while the pointer is inside the button. Pass NULL to create a non-interactive (display-only) button.

State Transitions

StateCondition
Unpressed (u)Pointer is outside the button bounds, or no mouse button is held.
Hover (hvr)Pointer is inside the button bounds and no mouse button is pressed.
Pressed (p)Left mouse button is held (mouse_down == 1) while pointer is inside bounds.
The onclick callback fires on mouse release (not press), consistent with standard button behavior. The callback is suppressed while a modal dialog is open — onclick will not fire until the modal is dismissed.
The label pointer is stored without copying. If your label string is a stack variable, ensure it remains in scope for the element’s lifetime, or use a static/global string.

Code Example

#include "libLWXGL.h"
#include <stdio.h>

static void on_save_clicked(void) {
    printf("Save button clicked!\n");
}

static void on_cancel_clicked(void) {
    DeleteWindow();
}

static void on_frame(int tick, float dt) {
    (void)tick; (void)dt;
}

int main(void) {
    CreateWindow(400, 150, "Button Demo", CLR_GRAY, FLAG_NONE);

    /*
     * Save button:
     *   unpressed — dark-gray fill, white border
     *   hover     — light-gray fill, white border
     *   pressed   — black fill, light-gray border
     */
    int u_save   = CLR_GRAY   | (CLR_WHITE << 4);
    int hvr_save = CLR_LGRAY  | (CLR_WHITE << 4);
    int p_save   = CLR_BLACK  | (CLR_LGRAY  << 4);
    CreateButton(1, 50, 50, 120, 40, u_save, hvr_save, p_save, "Save", on_save_clicked);

    /*
     * Cancel button:
     *   unpressed — dark-red fill, light-red border
     *   hover     — light-red fill, white border
     *   pressed   — red fill, white border
     */
    int u_cancel   = CLR_RED   | (CLR_LRED   << 4);
    int hvr_cancel = CLR_LRED  | (CLR_WHITE  << 4);
    int p_cancel   = CLR_RED   | (CLR_WHITE  << 4);
    CreateButton(2, 220, 50, 120, 40, u_cancel, hvr_cancel, p_cancel, "Cancel", on_cancel_clicked);

    MainWindowLoop(60, on_frame);
    TerminateWindow();
    return 0;
}

Build docs developers (and LLMs) love