Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_global_scroll.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28
29#define ALLEGRO_UNSTABLE 1
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35
36#include "nilorea/n_common.h"
37#include "nilorea/n_log.h"
38#include "nilorea/n_gui.h"
39
41static int log_level = LOG_ERR;
43static int failures = 0;
44
48static void expect_true(const char* what, int ok) {
49 if (!ok) {
50 n_log(LOG_ERR, "FAIL: %s", what);
51 failures++;
52 } else {
53 n_log(LOG_INFO, "ok: %s", what);
54 }
55}
56
61static int process_args(int argc, char** argv) {
62 int opt = 0;
63
64 while ((opt = getopt(argc, argv, "V:")) != -1) {
65 if (opt == 'V') {
66 log_level = atoi(optarg);
67 } else {
68 return -1;
69 }
70 }
71 return 0;
72}
73
76static void draw_once(N_GUI_CTX* gui) {
77 int old = al_get_new_bitmap_flags();
78 ALLEGRO_BITMAP* target = NULL;
79
80 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
81 target = al_create_bitmap(400, 300);
82 al_set_new_bitmap_flags(old);
83 if (!target) return;
84 al_set_target_bitmap(target);
85 al_clear_to_color(al_map_rgb(0, 0, 0));
87 al_set_target_bitmap(NULL);
88 al_destroy_bitmap(target);
89}
90
94static void wheel(N_GUI_CTX* gui, int dz) {
95 ALLEGRO_EVENT ev;
96
97 memset(&ev, 0, sizeof(ev));
98 ev.type = ALLEGRO_EVENT_MOUSE_AXES;
99 ev.mouse.x = 200;
100 ev.mouse.y = 150;
101 ev.mouse.dz = dz;
103}
104
109int main(int argc, char** argv) {
111 if (process_args(argc, argv) != 0) {
112 fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
113 return 2;
114 }
116 if (!al_init() || !al_init_font_addon() || !al_init_primitives_addon()) {
117 n_log(LOG_ERR, "allegro init failed");
118 return 1;
119 }
120 ALLEGRO_FONT* font = al_create_builtin_font();
121 if (!font) return 1;
123 if (!gui) {
124 al_destroy_font(font);
125 return 1;
126 }
127 n_gui_set_display_size(gui, 400.0f, 300.0f);
128 /* Laid out edge to edge from fractions, as an application's columns are:
129 * the right edge a rounding error past the display. */
130 int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 400.000122f, 300.0f);
133
134 gui->global_scroll_x = 5.0f;
135 gui->global_scroll_y = 5.0f;
136 draw_once(gui);
137 expect_true("a rounding error past the edge shows no scrollbar, so the view is not scrolled",
138 gui->global_scroll_x == 0.0f && gui->global_scroll_y == 0.0f);
139 wheel(gui, -3);
140 draw_once(gui);
141 expect_true("and the wheel does not scroll it", gui->global_scroll_y == 0.0f);
142
143 n_gui_window_set_rect(gui, win, 0.0f, 0.0f, 460.0f, 300.0f);
144 gui->global_scroll_x = 5.0f;
145 draw_once(gui);
146 expect_true("a window really past the edge keeps its horizontal scroll", gui->global_scroll_x == 5.0f);
147 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));
148
149 n_gui_window_set_rect(gui, win, 0.0f, 0.0f, 400.0f, 300.0f);
150 draw_once(gui);
151 expect_true("back inside, the view is not scrolled", gui->global_scroll_x == 0.0f && gui->global_scroll_y == 0.0f);
152
154 al_destroy_font(font);
155 if (failures) {
156 fprintf(stderr, "ex_gui_global_scroll: %d failure(s)\n", failures);
157 return 1;
158 }
159 printf("ex_gui_global_scroll: ok\n");
160 return 0;
161}
void process_args(int argc, char **argv)
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void expect_true(const char *label, int cond)
Definition ex_atlas.c:53
static int log_level
static N_GUI_CTX * gui
static void wheel(N_GUI_CTX *gui, int dz)
turn the mouse wheel over the display
static void draw_once(N_GUI_CTX *gui)
draw one frame into a memory bitmap of the display's size
FONT * font
the default 8x8 font, valid after allegro_init()
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
set the display (viewport) size so global scrollbars can be computed.
Definition n_gui.c:12348
void n_gui_window_set_rect(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h)
Move and resize a window at runtime, reflowing its widgets.
Definition n_gui.c:12196
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
Definition n_gui.c:13611
#define N_GUI_WIN_FRAMELESS
frameless window: no title bar drawn, drag via window body unless N_GUI_WIN_FIXED_POSITION is also se...
Definition n_gui.h:276
void n_gui_draw(N_GUI_CTX *ctx)
draw all visible GUI windows and widgets
Definition n_gui.c:11735
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
Definition n_gui.c:1277
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
Definition n_gui.c:1103
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
Definition n_gui.c:1436
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
Definition n_gui.c:2515
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_INFO
informational
Definition n_log.h:106
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Generic log system.