Headless: a press adds a point, a drag moves it within its neighbours, the ends keep their x, a right press removes an inner point and not an end, the maximum is kept, points set from code are sorted and refused during a drag, the caller's curve is what the editor evaluates, and the built-in spline does not overshoot. Drawn once into a memory bitmap, background included.
#define ALLEGRO_UNSTABLE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <allegro5/allegro_primitives.h>
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(
int widget_id,
int dragging,
void* user_data) {
(void)widget_id;
(void)user_data;
}
static float flat(
const float* xs,
const float* ys,
int count,
float x,
void* user_data) {
(void)xs;
(void)ys;
(void)count;
(void)x;
(void)user_data;
return 0.5f;
}
static void background(
float x,
float y,
float w,
float h,
void* user_data) {
(void)user_data;
al_draw_filled_rectangle(x, y, x + w, y + h, al_map_rgba(40, 40, 40, 128));
}
ALLEGRO_EVENT ev;
memset(&ev, 0, sizeof(ev));
ev.type = (ALLEGRO_EVENT_TYPE)
type;
ev.mouse.x = x;
ev.mouse.y = y;
ev.mouse.dx =
type == ALLEGRO_EVENT_MOUSE_AXES ? 1 : 0;
ev.mouse.button = (unsigned int)button;
}
#define AT_X(cx) ((int)(50.0f + (cx) * 200.0f + 0.5f))
#define AT_Y(cy) ((int)(50.0f + (1.0f - (cy)) * 100.0f + 0.5f))
}
}
static int near(
float a,
float b) {
return a - b < 0.011f && b - a < 0.011f;
}
int main(
int argc,
char** argv) {
float xs[8], ys[8];
int n = 0, i = 0, monotone = 1, bounded = 1;
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;
}
expect_true(
"it starts as the identity", n == 2 && xs[0] == 0.0f && ys[0] == 0.0f && xs[1] == 1.0f && ys[1] == 1.0f);
drag(
gui, 0.5f, 0.75f, 0.3f, 0.9f);
expect_true(
"a press off the points adds one", n == 3);
expect_true(
"an inner point stops short of its neighbour", xs[1] < xs[2] && xs[1] > 0.99f);
expect_true(
"an end moves up and down only", xs[0] == 0.0f &&
near(ys[0], 0.5f));
{
const float sx[4] = {1.0f, 0.25f, 0.0f, 0.75f};
const float sy[4] = {1.0f, 0.1f, 0.0f, 0.9f};
expect_true(
"come back sorted", n == 4 && xs[0] == 0.0f && xs[1] == 0.25f && ys[1] == 0.1f && xs[3] == 1.0f);
}
{
const float px[3] = {0.0f, 0.5f, 1.0f};
const float py[3] = {0.0f, 1.0f, 1.0f};
float prev = 0.0f;
for (i = 0; i <= 200; i++) {
if (v < prev - 1e-6f) monotone = 0;
if (v > 1.0f || v < 0.0f) bounded = 0;
prev = v;
}
expect_true(
"the spline through rising points never falls", monotone);
}
{
}
{
ALLEGRO_BITMAP* target = NULL;
int old = al_get_new_bitmap_flags();
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
target = al_create_bitmap(400, 300);
al_set_new_bitmap_flags(old);
al_set_target_bitmap(target);
al_clear_to_color(al_map_rgb(0, 0, 0));
al_set_target_bitmap(NULL);
al_destroy_bitmap(target);
}
fprintf(stderr,
"ex_gui_curve: %d failure(s)\n",
failures);
return 1;
}
printf("ex_gui_curve: ok\n");
return 0;
}
void process_args(int argc, char **argv)
static void expect_true(const char *label, int cond)
static int changes
how many times the picker called back, and with what
static void on_change(const float rgba[4], void *user_data)
the picker's callback
static void drag(N_GUI_CTX *gui, float fu, float fv, float tu, float tv)
press at one point, drag to another, release
static void feed(N_GUI_CTX *gui, int type, int x, int y, int button)
feed one mouse event at display coordinates
static int near(float a, float b)
is a near b
#define AT_X(u)
the wheel sits at 50,50 and is 200 by 200, its disc of radius 98 centred at 150,150: display position...
#define AT_Y(v)
as AT_X, for v, which goes up
static void background(float x, float y, float w, float h, void *user_data)
a background
static int background_drawn
did the background draw
static void right(N_GUI_CTX *gui, float cx, float cy)
right press
static float flat(const float *xs, const float *ys, int count, float x, void *user_data)
a caller's own curve: flat
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
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.
#define N_GUI_ALIGN_LEFT
left aligned text
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
int n_gui_add_label(N_GUI_CTX *ctx, int window_id, const char *text, float x, float y, float w, float h, int align)
Add a static text label.
#define N_GUI_WIN_FRAMELESS
frameless window: no title bar drawn, drag via window body unless N_GUI_WIN_FIXED_POSITION is also se...
void n_gui_draw(N_GUI_CTX *ctx)
draw all visible GUI windows and widgets
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.
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.
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
The top-level GUI context that holds all windows.
#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_curve_new(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int max_points, N_GUI_CURVE_CHANGE on_change, void *user_data)
Add a curve editor to a window.
int n_gui_curve_get_points(N_GUI_CTX *ctx, int widget_id, float *xs, float *ys, int max)
Read the points.
float n_gui_curve_eval(N_GUI_CTX *ctx, int widget_id, float x)
The editor's curve at x, as it is drawn.
void n_gui_curve_set_point_radius(N_GUI_CTX *ctx, int widget_id, float radius)
Size of the drawn points, for a display scale.
void n_gui_curve_set_eval(N_GUI_CTX *ctx, int widget_id, N_GUI_CURVE_EVAL eval, void *user_data)
Draw the caller's own curve through the points.
void n_gui_curve_set_background(N_GUI_CTX *ctx, int widget_id, N_GUI_CURVE_BACKGROUND draw, void *user_data)
Draw something behind the grid and the curve.
float n_gui_curve_eval_points(const float *xs, const float *ys, int count, float x)
A monotone cubic Hermite spline through points.
int n_gui_curve_set_points(N_GUI_CTX *ctx, int widget_id, const float *xs, const float *ys, int count)
Replace the points.
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
A curve editor for N_GUI: control points on a curve over [0,1].