Headless: the builtin font and memory bitmaps. Builds a picker, opens it on a colour and checks the sliders show it without calling back; clicks the middle of the green slider and checks the callback gets the new green and the old rest; closes it and checks the callback is forgotten. Then draws swatches and reads their pixels, and writes colours as hex.
#define ALLEGRO_UNSTABLE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <allegro5/allegro_primitives.h>
static float changed[4] = {0.0f, 0.0f, 0.0f, 0.0f};
if (!ok) {
} else {
}
}
int opt = 0;
while ((opt = getopt(argc, argv, "V:")) != -1) {
if (opt == 'V') {
} else {
return -1;
}
}
return 0;
}
static void on_change(
const float rgba[4],
void* user_data) {
int c = 0;
for (c = 0; c < 4; c++)
changed[c] = rgba[c];
if (user_data) (*(int*)user_data)++;
}
ALLEGRO_EVENT ev;
if (!win || !wgt) return;
memset(&ev, 0, sizeof(ev));
ev.mouse.x = (int)(win->
x + wgt->
x + wgt->
w / 2.0f);
ev.mouse.y = (int)(win->
y + win->
titlebar_h + wgt->
y + wgt->
h / 2.0f);
ev.mouse.button = 1;
ev.type = ALLEGRO_EVENT_MOUSE_AXES;
ev.type = ALLEGRO_EVENT_MOUSE_BUTTON_DOWN;
ev.type = ALLEGRO_EVENT_MOUSE_BUTTON_UP;
}
ALLEGRO_BITMAP* target = NULL;
ALLEGRO_COLOR px;
int old = al_get_new_bitmap_flags();
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
target = al_create_bitmap(40, 40);
al_set_new_bitmap_flags(old);
al_set_target_bitmap(target);
al_clear_to_color(al_map_rgb(0, 0, 0));
px = al_get_pixel(target, 21, 21);
al_set_target_bitmap(NULL);
al_destroy_bitmap(target);
return px;
}
int main(
int argc,
char** argv) {
const float start[4] = {1.0f, 0.0f, 0.0f, 128.0f / 255.0f};
float now[4] = {0.0f, 0.0f, 0.0f, 0.0f};
char hex[16] = "";
int counted = 0;
unsigned char r = 0, g = 0, b = 0;
fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
return 2;
}
if (!al_init() || !al_init_font_addon() || !al_init_primitives_addon()) {
return 1;
}
ALLEGRO_FONT*
font = al_create_builtin_font();
return 1;
}
if (picker) {
w && (
int)(w->
x + w->
w / 2.0f + 0.5f) == 320);
expect_true(
"free sets the handle to NULL", picker == NULL);
}
{
const float blue[4] = {0.0f, 0.0f, 1.0f, 1.0f};
const float clear[4] = {1.0f, 0.0f, 0.0f, 0.0f};
expect_true(
"an opaque swatch is its colour", r == 0 && g == 0 && b == 255);
expect_true(
"a see-through swatch shows the checkerboard grey", r == g && g == b && r > 100);
}
expect_true(
"hex writes #RRGGBBAA", strcmp(hex,
"#FF000080") == 0);
{
const float wild[4] = {-1.0f, 2.0f, 0.5f, 1.0f};
expect_true(
"hex clamps and rounds", strcmp(hex,
"#00FF80FF") == 0);
}
fprintf(stderr,
"ex_gui_color_picker: %d failure(s)\n",
failures);
return 1;
}
printf("ex_gui_color_picker: ok\n");
return 0;
}
void process_args(int argc, char **argv)
static void expect_true(const char *label, int cond)
static ALLEGRO_COLOR swatch_centre(const float rgba[4])
draw a swatch into a memory bitmap and read the pixel at its centre
static int changes
how many times the picker called back, and with what
static void click_middle(N_GUI_CTX *gui, int window_id, int widget_id)
click the middle of a widget, in display coordinates
static void on_change(const float rgba[4], void *user_data)
the picker's callback
FONT * font
the default 8x8 font, valid after allegro_init()
void clear(BITMAP *bmp)
clear to black
float x
position x on screen
float titlebar_h
title bar height
float x
position x relative to parent window
float y
position y relative to parent window
float y
position y on screen
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.
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
double n_gui_slider_get_value(N_GUI_CTX *ctx, int widget_id)
get the current value of a slider widget
N_GUI_WIDGET * n_gui_get_widget(N_GUI_CTX *ctx, int widget_id)
Get a widget pointer by id.
N_GUI_WINDOW * n_gui_get_window(N_GUI_CTX *ctx, int window_id)
Get a window pointer by id.
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
The top-level GUI context that holds all windows.
A pseudo window that contains widgets.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_INFO
informational
int n_gui_color_picker_get(const N_GUI_COLOR_PICKER *picker, float rgba[4])
The colour the picker holds now.
void n_gui_color_picker_open(N_GUI_COLOR_PICKER *picker, const char *what, const float rgba[4], N_GUI_COLOR_FN on_change, void *user_data)
Open the picker on a colour, centred on the display.
void n_gui_color_to_hex(const float rgba[4], char *out, size_t len)
Write a colour as #RRGGBBAA, each channel clamped to 0..1 and rounded to the nearest of 255 steps.
int n_gui_color_picker_window(const N_GUI_COLOR_PICKER *picker)
The picker's window id, for a caller that walks its windows or sets their flags.
int n_gui_color_picker_slider(const N_GUI_COLOR_PICKER *picker, int channel)
The slider for one channel, for a caller or a test that drives it.
N_GUI_COLOR_PICKER * n_gui_color_picker_new(N_GUI_CTX *ctx, const char *title, float scale)
Build a colour picker window in ctx, closed.
void n_gui_color_picker_close(N_GUI_COLOR_PICKER *picker)
Close the picker.
void n_gui_draw_color_swatch(float x, float y, float w, float h, const float rgba[4])
Draw a colour as a swatch, over a checkerboard so its alpha shows, with a thin dark border.
void n_gui_color_picker_free(N_GUI_COLOR_PICKER **picker)
Close the picker's window and free the handle.
int n_gui_color_picker_is_open(const N_GUI_COLOR_PICKER *picker)
Whether the picker is open.
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
a colour picker: the ids of its widgets and the colour being edited
A colour picker dialog for N_GUI, and a swatch to show a colour.