6 ex__gui__display__fallback_8c
gullradriel edited this page 2026-10-09 00:55:18 +02:00

examples/ex_gui_display_fallback.c

Functions

Name
int process_args(int argc, char ** argv)
parse -V LOG_LEVEL
int main(int argc, char ** argv)
void expect_true(const char * what, int ok)
record one check
void draw_into(N_GUI_CTX * gui, int w, int h)
draw one pass of the context into a memory bitmap of the given size

Attributes

Name
int log_level
int failures

Defines

Name
ALLEGRO_UNSTABLE

Functions Documentation

function process_args

static int process_args(
    int argc,
    char ** argv
)

parse -V LOG_LEVEL

function main

int main(
    int argc,
    char ** argv
)

function expect_true

static void expect_true(
    const char * what,
    int ok
)

record one check

function draw_into

static void draw_into(
    N_GUI_CTX * gui,
    int w,
    int h
)

draw one pass of the context into a memory bitmap of the given size

Attributes Documentation

variable log_level

static int log_level = 3;

variable failures

static int failures = 0;

Macros Documentation

define ALLEGRO_UNSTABLE

#define ALLEGRO_UNSTABLE 1

Source code



#define ALLEGRO_UNSTABLE 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <allegro5/allegro_primitives.h>

#include "nilorea/n_common.h"
#include "nilorea/n_log.h"
#include "nilorea/n_gui.h"

static int log_level = LOG_ERR;
static int failures = 0;

static void expect_true(const char* what, int ok) {
    if (!ok) {
        n_log(LOG_ERR, "FAIL: %s", what);
        failures++;
    } else {
        n_log(LOG_INFO, "ok: %s", what);
    }
}

static int process_args(int argc, char** argv) {
    int opt = 0;

    while ((opt = getopt(argc, argv, "V:")) != -1) {
        if (opt == 'V') {
            log_level = atoi(optarg);
        } else {
            return -1;
        }
    }
    return 0;
}

static void draw_into(N_GUI_CTX* gui, int w, int h) {
    int old_flags = al_get_new_bitmap_flags();

    al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
    ALLEGRO_BITMAP* target = al_create_bitmap(w, h);
    al_set_new_bitmap_flags(old_flags);
    if (!target) {
        expect_true("a memory bitmap to draw into", 0);
        return;
    }
    al_set_target_bitmap(target);
    al_clear_to_color(al_map_rgb(0, 0, 0));
    n_gui_draw(gui);
    al_set_target_bitmap(NULL);
    al_destroy_bitmap(target);
}

int main(int argc, char** argv) {
    set_log_level(LOG_ERR);
    if (process_args(argc, argv) != 0) {
        fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
        return 2;
    }
    set_log_level(log_level);

    if (!al_init() || !al_init_font_addon() || !al_init_primitives_addon()) {
        n_log(LOG_ERR, "allegro init failed");
        return 1;
    }
    ALLEGRO_FONT* font = al_create_builtin_font();
    if (!font) {
        n_log(LOG_ERR, "al_create_builtin_font failed");
        return 1;
    }
    N_GUI_CTX* gui = n_gui_new_ctx(font);
    if (!gui) {
        al_destroy_font(font);
        return 1;
    }
    int win = n_gui_add_window(gui, "W", 10.0f, 10.0f, 200.0f, 100.0f);
    n_gui_add_label(gui, win, "a label", 10.0f, 10.0f, 150.0f, 20.0f, N_GUI_ALIGN_LEFT);
    n_gui_open_window(gui, win);

    expect_true("a new context has no display size", gui->display_w == 0.0f && gui->display_h == 0.0f);

    draw_into(gui, 640, 400);
    expect_true("with none set, a draw takes the target's size",
                gui->display_w == 640.0f && gui->display_h == 400.0f);

    draw_into(gui, 1024, 300);
    expect_true("and follows it when the target changes, as a resize would",
                gui->display_w == 1024.0f && gui->display_h == 300.0f);

    n_gui_set_display_size(gui, 800.0f, 600.0f);
    draw_into(gui, 640, 400);
    expect_true("once a size is set, the target no longer overrides it",
                gui->display_w == 800.0f && gui->display_h == 600.0f);

    n_gui_destroy_ctx(&gui);
    al_destroy_font(font);
    if (failures) {
        fprintf(stderr, "ex_gui_display_fallback: %d failure(s)\n", failures);
        return 1;
    }
    printf("ex_gui_display_fallback: ok\n");
    return 0;
}