Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_round.c
Go to the documentation of this file.
1
22
23#define ALLEGRO_UNSTABLE 1
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
30#include "nilorea/n_common.h"
31#include "nilorea/n_log.h"
32#include "nilorea/n_gui.h"
33
34#include <allegro5/allegro_primitives.h>
35
36static int log_level = LOG_ERR;
37static int failures = 0;
38
40static void expect_true(const char* what, int ok) {
41 if (!ok) {
42 n_log(LOG_ERR, "FAIL: %s", what);
43 failures++;
44 } else {
45 n_log(LOG_INFO, "ok: %s", what);
46 }
47}
48
50static int process_args(int argc, char** argv) {
51 int opt = 0;
52
53 while ((opt = getopt(argc, argv, "V:")) != -1) {
54 if (opt == 'V') {
55 log_level = atoi(optarg);
56 } else {
57 return -1;
58 }
59 }
60 return 0;
61}
62
65static N_GUI_THEME flat(unsigned char r, unsigned char g, unsigned char b) {
66 ALLEGRO_COLOR c = al_map_rgb(r, g, b);
67 return n_gui_make_theme(c, c, c, c, c, c, al_map_rgb(0, 0, 0), al_map_rgb(0, 0, 0), al_map_rgb(0, 0, 0), 1.0f, 10.0f, 10.0f);
68}
69
71static int is_rgb(ALLEGRO_BITMAP* bmp, int x, int y, unsigned char r, unsigned char g, unsigned char b) {
72 unsigned char pr = 0, pg = 0, pb = 0;
73 al_unmap_rgb(al_get_pixel(bmp, x, y), &pr, &pg, &pb);
74 return pr == r && pg == g && pb == b;
75}
76
79static int corner_is_colour(ALLEGRO_BITMAP* bmp, int x0, int y0, int x1, int y1, unsigned char r, unsigned char g, unsigned char b) {
80 int minx = x1, miny = y1, x, y;
81 for (y = y0; y < y1; y++)
82 for (x = x0; x < x1; x++)
83 if (is_rgb(bmp, x, y, r, g, b)) {
84 if (x < minx) minx = x;
85 if (y < miny) miny = y;
86 }
87 if (minx == x1 || miny == y1) return -1;
88 /* one pixel in from the box's corner: an outline drawn on the shape's
89 * edge reaches half a pixel past a square fill, so the exact corner can
90 * be neither; one pixel in is inside a square shape and, at the radius
91 * of 10 used here (12.7 px from the arc's centre), outside a rounded one */
92 return is_rgb(bmp, minx + 1, miny + 1, r, g, b);
93}
94
97static void run(ALLEGRO_FONT* font, int mode, const char* name) {
98 char what[128];
99 int rounded = (mode == N_GUI_SHAPE_ROUNDED);
101 ALLEGRO_BITMAP* target = NULL;
102 int old = al_get_new_bitmap_flags();
103 int win, ta, cb, r;
104
105 if (!gui) {
106 expect_true("context", 0);
107 return;
108 }
109 if (mode != -1) n_gui_set_shape_mode(gui, mode);
110 n_gui_set_display_size(gui, 400.0f, 300.0f);
111 n_gui_set_default_theme(gui, flat(0, 160, 0)); /* the window: green */
112 win = n_gui_add_window(gui, "", 50.0f, 50.0f, 300.0f, 200.0f);
114 ta = n_gui_add_textarea(gui, win, 20.0f, 20.0f, 200.0f, 40.0f, 0, 32, NULL, NULL);
115 n_gui_set_widget_theme(gui, ta, flat(200, 0, 0)); /* red */
116 cb = n_gui_add_checkbox(gui, win, "", 20.0f, 100.0f, 200.0f, 40.0f, 0, NULL, NULL);
117 n_gui_set_widget_theme(gui, cb, flat(220, 220, 0)); /* yellow */
118
119 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
120 target = al_create_bitmap(400, 300);
121 al_set_new_bitmap_flags(old);
122 if (!target) {
123 expect_true("memory bitmap", 0);
125 return;
126 }
127 al_set_target_bitmap(target);
128 al_clear_to_color(al_map_rgb(0, 0, 160)); /* the screen: blue */
130
131 /* the window body: its own top-left corner */
132 r = corner_is_colour(target, 40, 40, 360, 260, 0, 160, 0);
133 snprintf(what, sizeof(what), "%s: window body %s", name, rounded ? "rounded" : "square");
134 expect_true(what, r == (rounded ? 0 : 1));
135 /* the text area, inside the window */
136 r = corner_is_colour(target, 60, 60, 300, 120, 200, 0, 0);
137 snprintf(what, sizeof(what), "%s: text area %s", name, rounded ? "rounded" : "square");
138 expect_true(what, r == (rounded ? 0 : 1));
139 /* the checkbox's box */
140 r = corner_is_colour(target, 60, 140, 300, 200, 220, 220, 0);
141 snprintf(what, sizeof(what), "%s: checkbox %s", name, rounded ? "rounded" : "square");
142 expect_true(what, r == (rounded ? 0 : 1));
143
144 al_set_target_bitmap(NULL);
145 al_destroy_bitmap(target);
147}
148
149int main(int argc, char** argv) {
151 if (process_args(argc, argv) != 0) {
152 fprintf(stderr, "usage: %s [-V LOG_LEVEL]\n", argv[0]);
153 return 1;
154 }
156
157 if (!al_init() || !al_init_font_addon() || !al_init_primitives_addon()) {
158 n_log(LOG_ERR, "allegro init failed");
159 return 1;
160 }
161 ALLEGRO_FONT* font = al_create_builtin_font();
162 if (!font) return 1;
163
164 run(font, -1, "default mode");
165 run(font, N_GUI_SHAPE_RECT, "RECT mode");
166 run(font, N_GUI_SHAPE_ROUNDED, "ROUNDED mode");
167
168 al_destroy_font(font);
169 if (failures) {
170 fprintf(stderr, "ex_gui_round: %d failure(s)\n", failures);
171 return 1;
172 }
173 printf("ex_gui_round: all checks passed\n");
174 return 0;
175}
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 float flat(const float *xs, const float *ys, int count, float x, void *user_data)
a caller's own curve: flat
static N_GUI_CTX * gui
static int mode
static int is_rgb(ALLEGRO_BITMAP *bmp, int x, int y, unsigned char r, unsigned char g, unsigned char b)
is pixel (x, y) exactly this colour?
static int corner_is_colour(ALLEGRO_BITMAP *bmp, int x0, int y0, int x1, int y1, unsigned char r, unsigned char g, unsigned char b)
whether the top-left corner of a colour's bounding box (searched in a region) is that colour: 1 squar...
int run
Definition ex_kafka.c:56
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_set_widget_theme(N_GUI_CTX *ctx, int widget_id, N_GUI_THEME theme)
Override the theme of a specific widget.
Definition n_gui.c:3795
#define N_GUI_WIN_FIXED_POSITION
disable window dragging (default:enable)
Definition n_gui.h:274
#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_set_shape_mode(N_GUI_CTX *ctx, int shape_mode)
Set the global widget shape (round or square GUI).
Definition n_gui.c:12361
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_set_default_theme(N_GUI_CTX *ctx, N_GUI_THEME theme)
Set the default theme on a context and sync scrollbar style colors.
Definition n_gui.c:919
#define N_GUI_SHAPE_ROUNDED
rounded rectangle shape
Definition n_gui.h:172
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
int n_gui_add_textarea(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int multiline, size_t char_limit, void(*on_change)(int, const char *, void *), void *user_data)
Add a text area widget.
Definition n_gui.c:3162
int n_gui_add_checkbox(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int initial_checked, void(*on_toggle)(int, int, void *), void *user_data)
Add a checkbox widget.
Definition n_gui.c:3207
#define N_GUI_SHAPE_RECT
rectangle shape (default)
Definition n_gui.h:170
N_GUI_THEME n_gui_make_theme(ALLEGRO_COLOR bg, ALLEGRO_COLOR bg_hover, ALLEGRO_COLOR bg_active, ALLEGRO_COLOR border, ALLEGRO_COLOR border_hover, ALLEGRO_COLOR border_active, ALLEGRO_COLOR text, ALLEGRO_COLOR text_hover, ALLEGRO_COLOR text_active, float border_thickness, float corner_rx, float corner_ry)
Create a custom theme with explicit colours.
Definition n_gui.c:825
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
Color theme for a widget.
Definition n_gui.h:384
#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.