The frame loop#

Every CEUI program has the same shape: ui_init, a loop around ui_frame, and ui_exit.

ui_init();
while (ui_frame()) {
  ui_titlebar("Settings", NULL);
  ui_toggle("Sound", &sound);
  if (ui_button("Save")) save();
}
ui_exit();

Immediate mode#

There are no widget objects to create, keep or free. Each frame, your code calls one function per widget, top to bottom, and the calls themselves describe the screen. A widget that you stop calling is gone. A widget behind an if appears when the condition is true.

Widgets read and write your own variables. ui_toggle("Sound", &sound) draws the toggle from sound and flips sound when the user presses [enter] on it. Widgets that report an event return true in the frame it happens: ui_button when it is pressed, value widgets when their value changed. There are no callbacks.

static bool sound = true;
static int volume = 5;

while (ui_frame()) {
  ui_titlebar("Audio", NULL);
  ui_toggle("Sound", &sound);
  ui_enabled(sound);
  if (ui_slider("Volume", &volume, 0, 10, 1)) set_volume(volume);
  ui_enabled(true);
}

Your variables must outlive the frame when they hold state between frames, so make them static, globals, or locals of main declared before the loop. Strings you pass to widgets are copied when the frame is recorded, so text built on the stack is safe.

Behind the calls, CEUI records draw commands into a buffer, compares them with the frame before, and redraws only the 64 by 16 pixel tiles that changed. You never draw or refresh the screen yourself.

When frames run#

ui_frame ends the frame before it, waits for something to happen, and starts the next frame. Something happening is one of:

  • a key press, or an auto repeat of a held arrow or [del];

  • the time asked for with ui_wake running out;

  • a toast expiring.

When nothing happens, the program sits in ui_frame and uses no time on drawing.

The settle frame#

Widgets act on keys in the middle of the frame. Code above a widget may already have drawn with the old value, for example a caption that shows the slider’s value above the slider. So CEUI runs the frame once more right away (the settle frame) when:

  • a widget or your code used a key,

  • focus moved at the end of the frame,

  • a modal or menu opened or closed,

  • a scroll area had to move.

The settle frame draws the consistent result, and when nothing changed the comparison draws nothing. You never need a refresh call. A frame runs at most 6 times in a row before CEUI waits for input again.

Animation and background work#

ui_wake(ms) asks for a frame after ms milliseconds even if no key is pressed. Call it in every frame that needs a next one. ui_wake(0) asks for the next frame at once. ui_ms() returns milliseconds since ui_init.

static void download_step(void)
{
  unsigned long now = ui_ms();

  downloaded += (now - tick) * BYTES_PER_SECOND / 1000;
  tick = now;
  if (downloaded < TOTAL) ui_wake(50);
}

ui_spinner and ui_toast ask for their own frames, so you do not need ui_wake for them.

Long work between widgets#

CEUI reads the keypad while it waits, every fourth widget, and before every rectangle it draws, so a short press during a long frame is not lost. If your own code runs for long between widgets, such as sorting or loading a file, call ui_listen() now and then in it. The showcase calls it from its sort comparison:

static int compare(const void *a, const void *b)
{
  ui_listen();
  return strcmp(((const file_t *)a)->name, ((const file_t *)b)->name);
}

Quitting#

ui_quit() makes the next ui_frame return false, so the loop ends after the current frame. Breaking out of the loop yourself and calling ui_exit is also fine.

ui_exit() waits until every key is released, so the key that quit does not reach the OS, puts the keypad back the way it was, and ends graphx. Call it once, after the loop.

static const char *const keys[UI_SOFTKEYS] = {NULL, NULL, NULL, NULL, "Quit"};

while (ui_frame()) {
  if (ui_softkeys(keys) == UI_F5) ui_quit();
  ui_titlebar("Hello", NULL);
  if (ui_key_pressed(sk_Clear)) ui_quit();
}
ui_exit();

Drawing outside CEUI#

If your program draws with graphx outside the frame loop, for example a full screen game between two CEUI screens, call ui_invalidate() when you come back. The next frame then redraws the whole screen instead of only what changed.

Order inside a frame#

  • Call ui_titlebar and ui_softkeys first, so the content area between them is known before other widgets are placed.

  • Check your own shortcuts with ui_key_pressed after the widgets, so a text field gets [clear] before your code treats it as “back”.

  • Call overlays (ui_modal_begin, ui_menu, ui_message) anywhere. They are drawn on top no matter where in the frame they are called.

The showcase home screen

The showcase home screen: a title bar with a right label, a list, and four softkeys.#