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

examples/ex_gui_global_scroll.c

Functions

Name
void wheel(N_GUI_CTX * gui, int dz)
turn the mouse wheel over the display
int process_args(int argc, char ** argv)
read -V
int main(int argc, char ** argv)
run the checks
void expect_true(const char * what, int ok)
count a failed check
void draw_once(N_GUI_CTX * gui)
draw one frame into a memory bitmap of the display's size

Attributes

Name
int log_level
log level from -V
int failures
failed checks

Defines

Name
ALLEGRO_UNSTABLE

Functions Documentation

function wheel

static void wheel(
    N_GUI_CTX * gui,
    int dz
)

turn the mouse wheel over the display

Parameters:

  • gui the GUI
  • dz notches

function process_args

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

read -V

Parameters:

  • argc argument count
  • argv arguments

Return: 0, or -1 on a bad argument

function main

int main(
    int argc,
    char ** argv
)

run the checks

Parameters:

  • argc argument count
  • argv arguments

Return: 0 on success

function expect_true

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

count a failed check

Parameters:

  • what the check
  • ok its result

function draw_once

static void draw_once(
    N_GUI_CTX * gui
)

draw one frame into a memory bitmap of the display's size

Parameters:

  • gui the GUI

Attributes Documentation

variable log_level

static int log_level = 3;

log level from -V

variable failures

static int failures = 0;

failed checks

Macros Documentation

define ALLEGRO_UNSTABLE

#define ALLEGRO_UNSTABLE 1

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#define ALLEGRO_UNSTABLE 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.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_once(N_GUI_CTX* gui) {
    int old = al_get_new_bitmap_flags();
    ALLEGRO_BITMAP* target = NULL;

    al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
    target = al_create_bitmap(400, 300);
    al_set_new_bitmap_flags(old);
    if (!target) 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);
}

static void wheel(N_GUI_CTX* gui, int dz) {
    ALLEGRO_EVENT ev;

    memset(&ev, 0, sizeof(ev));
    ev.type = ALLEGRO_EVENT_MOUSE_AXES;
    ev.mouse.x = 200;
    ev.mouse.y = 150;
    ev.mouse.dz = dz;
    n_gui_process_event(gui, ev);
}

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) return 1;
    N_GUI_CTX* gui = n_gui_new_ctx(font);
    if (!gui) {
        al_destroy_font(font);
        return 1;
    }
    n_gui_set_display_size(gui, 400.0f, 300.0f);
    /* Laid out edge to edge from fractions, as an application's columns are:
     * the right edge a rounding error past the display. */
    int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 400.000122f, 300.0f);
    n_gui_window_set_flags(gui, win, N_GUI_WIN_FRAMELESS);
    n_gui_open_window(gui, win);

    gui->global_scroll_x = 5.0f;
    gui->global_scroll_y = 5.0f;
    draw_once(gui);
    expect_true("a rounding error past the edge shows no scrollbar, so the view is not scrolled",
                gui->global_scroll_x == 0.0f && gui->global_scroll_y == 0.0f);
    wheel(gui, -3);
    draw_once(gui);
    expect_true("and the wheel does not scroll it", gui->global_scroll_y == 0.0f);

    n_gui_window_set_rect(gui, win, 0.0f, 0.0f, 460.0f, 300.0f);
    gui->global_scroll_x = 5.0f;
    draw_once(gui);
    expect_true("a window really past the edge keeps its horizontal scroll", gui->global_scroll_x == 5.0f);
    expect_true("and brings the vertical bar for the room the other takes", (wheel(gui, -3), draw_once(gui), gui->global_scroll_y > 0.0f));

    n_gui_window_set_rect(gui, win, 0.0f, 0.0f, 400.0f, 300.0f);
    draw_once(gui);
    expect_true("back inside, the view is not scrolled", gui->global_scroll_x == 0.0f && gui->global_scroll_y == 0.0f);

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