X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=akamaru.c;h=14d06015b67c1d257d35bbee4aa44ef9c578529d;hb=d3969b617a9f27962f5bd8b1113763bf7134fc47;hp=8fa2ec00f6dc4b22e494a8bd24c255196c5e18fa;hpb=407da16255fcc13f0510f7149bafe4484c4491a9;p=akamaru diff --git a/akamaru.c b/akamaru.c index 8fa2ec0..14d0601 100644 --- a/akamaru.c +++ b/akamaru.c @@ -20,6 +20,7 @@ #include #include #include +#include #include const double ground_friction = 0.1, ground_level = 400; @@ -35,6 +36,7 @@ struct _xy_pair { typedef struct _Object Object; typedef struct _Stick Stick; +typedef struct _String String; typedef struct _Offset Offset; typedef struct _Model Model; @@ -54,6 +56,11 @@ struct _Stick { int length; }; +struct _String { + Object *a, *b; + int length; +}; + struct _Offset { Object *a, *b; int dx, dy; @@ -64,6 +71,8 @@ struct _Model { Object *objects; int num_sticks; Stick *sticks; + int num_strings; + String *strings; int num_offsets; Offset *offsets; double k; @@ -86,6 +95,9 @@ model_init_snake (Model *model) model->num_objects = num_objects; model->sticks = g_new (Stick, num_sticks); model->num_sticks = num_sticks; + model->strings = NULL; + model->num_strings = 0; + model->offsets = NULL; model->num_offsets = 0; for (i = 0; i < num_objects; i++) { @@ -121,6 +133,8 @@ model_init_rope (Model *model) model->num_objects = num_objects; model->sticks = g_new (Stick, num_sticks); model->num_sticks = num_sticks; + model->strings = NULL; + model->num_strings = 0; model->offsets = NULL; model->num_offsets = 0; @@ -156,6 +170,8 @@ model_init_curtain (Model *model) model->num_objects = num_objects; model->sticks = g_new (Stick, num_sticks); model->num_sticks = num_sticks; + model->strings = NULL; + model->num_strings = 0; model->offsets = g_new (Offset, num_ropes - 1); model->num_offsets = num_ropes - 1; @@ -191,27 +207,29 @@ model_init_curtain (Model *model) static void model_init_grid (Model *model) { - const int num_ropes = 15; - const int num_rope_objects = 15; + const int num_ropes = 10; + const int num_rope_objects = 10; const int num_objects = num_ropes * num_rope_objects; - const int num_sticks = num_ropes * (num_rope_objects - 1) + + const int num_strings = num_ropes * (num_rope_objects - 1) + (num_ropes - 1) * num_rope_objects; - const int stick_length = 10; + const int string_length = 10; const int rope_offset = 10; double x, y; - int i, j, index, stick_index; + int i, j, index, string_index; model->objects = g_new (Object, num_objects); model->num_objects = num_objects; - model->sticks = g_new (Stick, num_sticks); - model->num_sticks = num_sticks; + model->sticks = NULL; + model->num_sticks = 0; + model->strings = g_new (String, num_strings); + model->num_strings = num_strings; model->offsets = g_new (Offset, num_ropes - 1); model->num_offsets = num_ropes - 1; for (i = 0; i < num_ropes; i++) { for (j = 0; j < num_rope_objects; j++) { x = 200 + i * rope_offset; - y = 40 + j * stick_length; + y = 40 + j * string_length; index = i * num_rope_objects + j; model->objects[index].position.x = x; model->objects[index].position.y = y; @@ -219,18 +237,18 @@ model_init_grid (Model *model) model->objects[index].previous_position.y = y; if (i + 1 < num_ropes) { - stick_index = i * num_rope_objects + j; - model->sticks[stick_index].a = &model->objects[index]; - model->sticks[stick_index].b = &model->objects[index + num_rope_objects]; - model->sticks[stick_index].length = stick_length; + string_index = i * num_rope_objects + j; + model->strings[string_index].a = &model->objects[index]; + model->strings[string_index].b = &model->objects[index + num_rope_objects]; + model->strings[string_index].length = string_length; } if (j + 1 < num_rope_objects) { - stick_index = + string_index = (num_ropes - 1) * num_rope_objects + i * (num_rope_objects - 1) + j; - model->sticks[stick_index].a = &model->objects[index]; - model->sticks[stick_index].b = &model->objects[index + 1]; - model->sticks[stick_index].length = stick_length; + model->strings[string_index].a = &model->objects[index]; + model->strings[string_index].b = &model->objects[index + 1]; + model->strings[string_index].length = string_length; } } @@ -254,6 +272,9 @@ model_fini (Model *model) g_free (model->sticks); model->sticks = NULL; model->sticks = 0; + g_free (model->strings); + model->strings = NULL; + model->strings = 0; g_free (model->offsets); model->offsets = NULL; model->num_offsets = 0; @@ -266,7 +287,7 @@ model_accumulate_forces (Model *model) for (i = 0; i < model->num_objects; i++) { model->objects[i].force.x = 0; - model->objects[i].force.y = 2; + model->objects[i].force.y = 3; } } @@ -292,6 +313,29 @@ model_integrate (Model *model, double step) } } +/* The square root in the distance computation for the string and + * stick constraints can be aproximated using Newton: + * + * distance = + * (model->sticks[i].length + + * (dx * dx + dy * dy) / model->sticks[i].length) / 2; + * + * This works really well, since the constraints aren't typically + * violated much. Thus, the distance is really close to the stick + * length, which then makes a good initial guess. However, the + * approximation seems to be slower that just calling sqrt()... + */ + +static inline double +estimate_distance (double dx, double dy, double r) +{ +#ifdef APPROXIMATE_SQUARE_ROOTS + return (r + (dx * dx + dy * dy) / r) / 2; +#else + return sqrt (dx * dx + dy * dy); +#endif +} + static void model_constrain (Model *model, double step) { @@ -350,36 +394,35 @@ model_constrain (Model *model, double step) model->offsets[i].b->position.y = y + model->offsets[i].dy / 2; } -#if 1 + /* String constraints. */ + for (i = 0; i < model->num_strings; i++) { + x = model->strings[i].a->position.x; + y = model->strings[i].a->position.y; + dx = model->strings[i].b->position.x - x; + dy = model->strings[i].b->position.y - y; + distance = estimate_distance (dx, dy, model->strings[i].length); + if (distance < model->strings[i].length) + continue; + fraction = (distance - model->strings[i].length) / distance / 2; + model->strings[i].a->position.x = x + dx * fraction; + model->strings[i].a->position.y = y + dy * fraction; + model->strings[i].b->position.x = x + dx * (1 - fraction); + model->strings[i].b->position.y = y + dy * (1 - fraction); + } + /* Stick constraints. */ for (i = 0; i < model->num_sticks; i++) { x = model->sticks[i].a->position.x; y = model->sticks[i].a->position.y; dx = model->sticks[i].b->position.x - x; dy = model->sticks[i].b->position.y - y; - distance = sqrt (dx * dx + dy * dy); + distance = estimate_distance (dx, dy, model->sticks[i].length); fraction = (distance - model->sticks[i].length) / distance / 2; model->sticks[i].a->position.x = x + dx * fraction; model->sticks[i].a->position.y = y + dy * fraction; model->sticks[i].b->position.x = x + dx * (1 - fraction); model->sticks[i].b->position.y = y + dy * (1 - fraction); } -#else - /* Stick constraints, without square roots. */ - squared = stick_length * stick_length; - for (i = 0; i < model->num_objects - 1; i++) { - j = i + 1; - x = model->objects[i].position.x; - y = model->objects[i].position.y; - dx = model->objects[j].position.x - x; - dy = model->objects[j].position.y - y; - fraction = squared / (dx * dx + dy * dy + squared) - 0.5; - model->objects[i].position.x = x + dx * fraction; - model->objects[i].position.y = y + dy * fraction; - model->objects[j].position.x = x + dx * (1 - fraction); - model->objects[j].position.y = y + dy * (1 - fraction); - } -#endif } static void @@ -390,7 +433,7 @@ model_step (Model *model, double delta_t) model_accumulate_forces (model); model_integrate (model, delta_t); - for (i = 0; i < 15; i++) + for (i = 0; i < 100; i++) model_constrain (model, delta_t); model->theta += delta_t; @@ -455,6 +498,31 @@ draw_sticks (cairo_t *cr, cairo_stroke (cr); } +static void +draw_strings (cairo_t *cr, + Model *model, + Color *color) +{ + int i; + + cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1); + cairo_new_path (cr); + cairo_set_line_width (cr, 1); + cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); + cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); + + for (i = 0; i < model->num_strings; i++) { + cairo_move_to (cr, + model->strings[i].a->position.x, + model->strings[i].a->position.y); + cairo_line_to (cr, + model->strings[i].b->position.x, + model->strings[i].b->position.y); + } + + cairo_stroke (cr); +} + static void draw_offsets (cairo_t *cr, Model *model, @@ -512,16 +580,27 @@ draw_objects (cairo_t *cr, Model *model, Color *color) } static Color blue = { 0, 0, 1 }; +static Color green = { 0, 1, 0 }; static Color red = { 1, 0, 0 }; static Color black = { 0, 0, 0 }; static Color white = { 1, 1, 1 }; +typedef struct _Closure Closure; +struct _Closure { + GtkWidget *drawing_area; + GtkWidget *fps_label; + Model *model; + int frame_count; + int i; + struct timeval start; +}; + static gboolean expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer data) { - Model *model = data; + Closure *closure = data; cairo_t *cr; cr = gdk_cairo_create (widget->window); @@ -529,10 +608,11 @@ expose_event (GtkWidget *widget, cairo_set_source_rgb (cr, 1, 1, 1); cairo_paint (cr); - draw_constraints (cr, model, &red); - draw_sticks (cr, model, &black); - draw_offsets (cr, model, &blue); - draw_objects (cr, model, &white); + draw_constraints (cr, closure->model, &red); + draw_sticks (cr, closure->model, &black); + draw_strings (cr, closure->model, &green); + draw_offsets (cr, closure->model, &blue); + draw_objects (cr, closure->model, &white); cairo_destroy (cr); @@ -544,14 +624,15 @@ button_press_event (GtkWidget *widget, GdkEventButton *event, gpointer data) { - Model *model = data; + Closure *closure = data; if (event->button != 1) return TRUE; - model->anchor_position.x = event->x; - model->anchor_position.y = event->y; - model->anchor_object = model_find_nearest (model, event->x, event->y); + closure->model->anchor_position.x = event->x; + closure->model->anchor_position.y = event->y; + closure->model->anchor_object = model_find_nearest (closure->model, + event->x, event->y); return TRUE; } @@ -561,12 +642,12 @@ button_release_event (GtkWidget *widget, GdkEventButton *event, gpointer data) { - Model *model = data; + Closure *closure = data; if ((event->state & GDK_BUTTON1_MASK) == 0) return TRUE; - model->anchor_object = NULL; + closure->model->anchor_object = NULL; return TRUE; } @@ -576,14 +657,14 @@ motion_notify_event (GtkWidget *widget, GdkEventMotion *event, gpointer data) { - Model *model = data; + Closure *closure = data; int x, y; GdkModifierType state; gdk_window_get_pointer (event->window, &x, &y, &state); - model->anchor_position.x = x + 0.5; - model->anchor_position.y = y + 0.5; + closure->model->anchor_position.x = x + 0.5; + closure->model->anchor_position.y = y + 0.5; return TRUE; } @@ -593,7 +674,7 @@ typedef void (*ModelInitFunc) (Model *model); static void model_changed (GtkComboBox *combo, gpointer user_data) { - Model *model = user_data; + Closure *closure = user_data; GtkTreeIter iter; GtkTreeModel *tree_model; ModelInitFunc init; @@ -605,8 +686,8 @@ model_changed (GtkComboBox *combo, gpointer user_data) gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1); - model_fini (model); - (*init) (model); + model_fini (closure->model); + (*init) (closure->model); } static GtkTreeModel * @@ -639,7 +720,7 @@ create_model_store (void) } static GtkWidget * -create_model_combo (Model *model) +create_model_combo (Closure *closure) { GtkWidget *hbox; GtkWidget *combo, *label; @@ -665,13 +746,18 @@ create_model_combo (Model *model) gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo); gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0); g_signal_connect (combo, "changed", - G_CALLBACK (model_changed), model); + G_CALLBACK (model_changed), closure); + + label = gtk_label_new ("Frames per second: 0"); + gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); + + closure->fps_label = label; return hbox; } -static GtkWidget * -create_window (Model *model) +static void +create_window (Closure *closure) { GtkWidget *window; GtkWidget *frame; @@ -680,7 +766,7 @@ create_window (Model *model) GtkWidget *model_combo; window = gtk_window_new (GTK_WINDOW_TOPLEVEL); - gtk_window_set_title (GTK_WINDOW (window), "Drawing Area"); + gtk_window_set_title (GTK_WINDOW (window), "Akamaru"); g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), &window); @@ -708,16 +794,16 @@ create_window (Model *model) /* Signals used to handle backing pixmap */ g_signal_connect (da, "expose_event", - G_CALLBACK (expose_event), model); + G_CALLBACK (expose_event), closure); /* Event signals */ g_signal_connect (da, "motion_notify_event", - G_CALLBACK (motion_notify_event), model); + G_CALLBACK (motion_notify_event), closure); g_signal_connect (da, "button_press_event", - G_CALLBACK (button_press_event), model); + G_CALLBACK (button_press_event), closure); g_signal_connect (da, "button_release_event", - G_CALLBACK (button_release_event), model); + G_CALLBACK (button_release_event), closure); /* Ask to receive events the drawing area doesn't normally * subscribe to @@ -729,32 +815,49 @@ create_window (Model *model) | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK); - model_combo = create_model_combo (model); + model_combo = create_model_combo (closure); gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0); - return da; + closure->drawing_area = da; } -typedef struct _Closure Closure; -struct _Closure { - GtkWidget *drawing_area; - Model *model; - int i; -}; - static gint timeout_callback (gpointer data) { Closure *closure = data; int i; - for (i = 0; i < 3; i++) - model_step (closure->model, 0.5); + model_step (closure->model, 1); closure->i++; if (closure->i == 1) { gtk_widget_queue_draw (closure->drawing_area); closure->i = 0; + closure->frame_count++; + } + + if (closure->frame_count == 200) { + struct timeval end, elapsed; + double total; + char text[50]; + + closure->frame_count = 0; + gettimeofday (&end, NULL); + if (closure->start.tv_usec > end.tv_usec) { + end.tv_usec += 1000000; + end.tv_sec--; + } + + elapsed.tv_usec = end.tv_usec - closure->start.tv_usec; + elapsed.tv_sec = end.tv_sec - closure->start.tv_sec; + + total = elapsed.tv_sec + ((double) elapsed.tv_usec / 1e6); + if (total < 0) { + total = 0; + } + closure->start = end; + snprintf (text, sizeof text, "Frames per second: %.2f", 200 / total); + gtk_label_set_text (GTK_LABEL (closure->fps_label), text); } return TRUE; @@ -768,11 +871,13 @@ main (int argc, char *argv[]) gtk_init (&argc, &argv); model_init_rope (&model); - closure.drawing_area = create_window (&model); + create_window (&closure); closure.i = 0; gtk_widget_show_all (gtk_widget_get_toplevel (closure.drawing_area)); closure.model = &model; - g_timeout_add (100, timeout_callback, &closure); + closure.frame_count = 0; + gettimeofday (&closure.start, NULL); + g_timeout_add (40, timeout_callback, &closure); gtk_main (); return 0;