From 370ac137ff0ce9dfc2be54540911fc772ffabdce Mon Sep 17 00:00:00 2001 From: Antonio Carzaniga Date: Mon, 29 Jul 2019 15:48:15 +0200 Subject: [PATCH 1/4] added clear (alpha) parameter to draw animation trails. --- balls.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/balls.c b/balls.c index 9a71616..9f1967c 100644 --- a/balls.c +++ b/balls.c @@ -39,6 +39,8 @@ unsigned int n_balls = 50; static double g_y = 20; static double g_x = 0; +static double clear_alpha = 1.0; + void random_velocity(struct ball * p) { double r2; do { @@ -154,7 +156,7 @@ static void draw_balls_onto_window () { cr = gdk_cairo_create(window->window); /* clear pixmap */ - cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0); + cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, clear_alpha); cairo_paint(cr); draw_gravity_vector(); @@ -239,7 +241,8 @@ void print_usage (const char * progname) { "\tfy=\n" "\tradius=-\n" "\tv=-\n" - "\tdelta=\n", + "\tdelta=\n" + "\tclear=\n" progname); } @@ -283,6 +286,8 @@ int main (int argc, const char *argv[]) { continue; if (sscanf(argv[i], "delta=%lf", &delta) == 1) continue; + if (sscanf(argv[i], "clear=%lf", &clear_alpha) == 1) + continue; print_usage(argv[0]); return 1; } From 5c9d152eaf856d296c99b5ee50cc745eef2f1477 Mon Sep 17 00:00:00 2001 From: Antonio Carzaniga Date: Mon, 29 Jul 2019 15:49:42 +0200 Subject: [PATCH 2/4] disappearing gravity vector --- balls.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/balls.c b/balls.c index 9f1967c..4e8b3d9 100644 --- a/balls.c +++ b/balls.c @@ -140,15 +140,22 @@ static void update_state () { static GtkWidget * window; static cairo_t * cr = 0; +static int gravity_vector_countdown = 0; +static int gravity_vector_init = 300; + static void draw_gravity_vector() { - cairo_new_path(cr); - cairo_move_to(cr, width/2, height/2); - cairo_line_to(cr, width/2 + g_x, height/2 + g_y); - cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); - cairo_set_line_width(cr, 1.0); - cairo_stroke(cr); - cairo_arc(cr, width/2 + g_x, height/2 + g_y, 3, 0, 2*M_PI); - cairo_fill(cr); + if (gravity_vector_countdown != 0) { + cairo_new_path(cr); + cairo_move_to(cr, width/2, height/2); + cairo_line_to(cr, width/2 + g_x, height/2 + g_y); + cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); + cairo_set_line_width(cr, 1.0); + cairo_stroke(cr); + cairo_arc(cr, width/2 + g_x, height/2 + g_y, 3, 0, 2*M_PI); + cairo_fill(cr); + if (gravity_vector_countdown > 0) + --gravity_vector_countdown; + } } static void draw_balls_onto_window () { @@ -194,20 +201,23 @@ static gint keyboard_input (GtkWidget *widget, GdkEventKey *event) { switch(event->keyval) { case GDK_KEY_Up: g_y -= 10; + gravity_vector_countdown = gravity_vector_init; break; case GDK_KEY_Down: g_y += 10; + gravity_vector_countdown = gravity_vector_init; break; case GDK_KEY_Left: g_x -= 10; + gravity_vector_countdown = gravity_vector_init; break; case GDK_KEY_Right: g_x += 10; + gravity_vector_countdown = gravity_vector_init; break; - case GDK_KEY_F: - case GDK_KEY_f: - g_x = rand() % 201 - 100; - g_y = rand() % 201 - 100; + case GDK_KEY_G: + case GDK_KEY_g: + gravity_vector_countdown = gravity_vector_init; break; case GDK_KEY_Q: case GDK_KEY_q: From d3411b9ab0c5180c62839b9af61516750377baa6 Mon Sep 17 00:00:00 2001 From: Antonio Carzaniga Date: Mon, 29 Jul 2019 15:50:53 +0200 Subject: [PATCH 3/4] fixed minor editing bug. --- balls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/balls.c b/balls.c index 4e8b3d9..2e3444f 100644 --- a/balls.c +++ b/balls.c @@ -252,7 +252,7 @@ void print_usage (const char * progname) { "\tradius=-\n" "\tv=-\n" "\tdelta=\n" - "\tclear=\n" + "\tclear=\n", progname); } From 24bbae26bf9398181327418f697f98237aeaada2 Mon Sep 17 00:00:00 2001 From: Antonio Carzaniga Date: Mon, 29 Jul 2019 18:36:32 +0200 Subject: [PATCH 4/4] default build target is compile (no run). --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index 7c27449..7de7005 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,9 @@ LIBS=$(GTK_LIBS) PROGS=balls +.PHONY: default +default: all + .PHONY: run run: balls ./balls