]> git.cworth.org Git - akamaru/commitdiff
Split gtk+ demo code out into main.c. Core model code is now in akamaru.c.
authorKristian Høgsberg <krh@redhat.com>
Tue, 23 May 2006 19:48:39 +0000 (15:48 -0400)
committerKristian Høgsberg <krh@dinky.bitplanet.net>
Tue, 23 May 2006 19:48:39 +0000 (15:48 -0400)
Makefile
akamaru.c
akamaru.h [new file with mode: 0644]
main.c [new file with mode: 0644]

index f16a8313225860e8d44c399d192f0d6adca02999..c98106c323de14eabc4cb3b82e81c5ea98a7c7cb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ LDLIBS = $(shell pkg-config --libs gtk+-2.0 cairo)
 LDFLAGS = -g
 
 target = akamaru
-objs = akamaru.o
+objs = akamaru.o main.o
 
 $(target) : $(objs)
 
index 2ee99ad17fe0b4f4e5da6061b10605a6b5cb239a..8dc477cc0f2ee4b7c10edb92f2828ef2102a2333 100644 (file)
--- a/akamaru.c
+++ b/akamaru.c
@@ -1,9 +1,4 @@
 /*                                           -*- mode: c; c-basic-offset: 2 -*-
- * To compile:
- *
- *     gcc -Wall -g $(pkg-config --cflags --libs gtk+-2.0 cairo) \
- *             akamaru.c -o akamaru
- *
  * See:
  *
  *     http://en.wikipedia.org/wiki/Verlet_integration
 #include <sys/time.h>
 #include <math.h>
 
-const double ground_level = 500;
-const double elasticity = 0.8;
-
-typedef struct _xy_pair Point;
-typedef struct _xy_pair Vector;
-struct _xy_pair {
-  double x, y;
-};
-
-typedef struct _Object Object;
-typedef struct _Stick Stick;
-typedef struct _String String;
-typedef struct _Spring Spring;
-typedef struct _OffsetSpring OffsetSpring;
-typedef struct _Polygon Polygon;
-typedef struct _Offset Offset;
-typedef struct _Model Model;
-
-struct _Object {
-  Vector force;
-
-  Point position;
-  Point previous_position;
-  Vector velocity;
-
-  double mass;
-  double theta;
-};
-
-struct _Stick {
-  Object *a, *b;
-  int length;
-};
+#include "akamaru.h"
 
-struct _String {
-  Object *a, *b;
-  int length;
-};
+const double elasticity = 0.7;
 
-struct _Offset {
-  int num_objects;
-  Object **objects;
-  int dx, dy;
-};
-
-struct _Spring {
-  Object *a, *b;
-  int length;
-};
-
-struct _OffsetSpring {
-  Object *a, *b;
-  int dx, dy;
-};
-
-struct _Polygon {
-  int num_points;
-  Point *points;
-  Vector *normals;
-  int edge;
-};
-
-struct _Model {
-  int num_objects;
-  Object *objects;
-  int num_sticks;
-  Stick *sticks;
-  int num_strings;
-  String *strings;
-  int num_offsets;
-  Offset *offsets;
-  int num_springs;
-  Spring *springs;
-  int num_offset_springs;
-  OffsetSpring *offset_springs;
-  int num_polygons;
-  Polygon *polygons;
-  double k;
-  double friction;
-
-  Object *anchor_object;
-  Vector anchor_position;
-
-  double theta;
-};
-
-static void
+void
 polygon_init (Polygon *p, int num_points, ...)
 {
   double dx, dy, length;
@@ -145,7 +58,7 @@ polygon_init (Polygon *p, int num_points, ...)
   }
 }
 
-static void
+void
 polygon_init_diamond (Polygon *polygon, double x, double y)
 {
   return polygon_init (polygon, 5, 
@@ -156,303 +69,29 @@ polygon_init_diamond (Polygon *polygon, double x, double y)
                       x + 50, y - 20);
 }
 
-static void
+void
 polygon_init_rectangle (Polygon *polygon, double x0, double y0,
                        double x1, double y1)
 {
   return polygon_init (polygon, 4, x0, y0, x0, y1, x1, y1, x1, y0);
 }
 
-static void
-model_init_polygons (Model *model)
-{
-  const int num_polygons = 5;
-
-  model->polygons = g_new (Polygon, num_polygons);
-  polygon_init_diamond (&model->polygons[0], 250, 300);
-  polygon_init_diamond (&model->polygons[1], 400, 150);
-  polygon_init_rectangle (&model->polygons[2], -100, 200, 200, 250);
-  polygon_init_rectangle (&model->polygons[3], -200, ground_level,
-                         1200, ground_level + 400);
-
-  polygon_init_rectangle (&model->polygons[4], 300, 320, 400, 350);
-
-  model->num_polygons = num_polygons;
-}
-
-static void
-model_init_snake (Model *model)
-{
-  const int num_objects = 20;
-  const int num_sticks = num_objects * 2 - 3;
-  int i;
-
-  memset (model, 0, sizeof *model);
-  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_init_polygons (model);
-
-  for (i = 0; i < num_objects; i++) {
-    model->objects[i].position.x = random() % 200 + 20;
-    model->objects[i].position.y = random() % 200 + 20;
-    model->objects[i].previous_position.x = random() % 200 + 20;
-    model->objects[i].previous_position.y = random() % 200 + 20;
-    model->objects[i].mass = 1;
-
-    if (i + 1 < num_objects) {
-      model->sticks[i * 2].a = &model->objects[i];
-      model->sticks[i * 2].b = &model->objects[i + 1];
-      model->sticks[i * 2].length = random() % 20 + 20;
-    }
-    if (i + 2 < num_objects) {
-      model->sticks[i * 2 + 1].a = &model->objects[i];
-      model->sticks[i * 2 + 1].b = &model->objects[i + 2];
-      model->sticks[i * 2 + 1].length = random() % 20 + 20;
-    }
-  }
-
-  model->anchor_object = NULL;
-}
-
-static void
-model_init_rope (Model *model)
-{
-  const int num_objects = 20;
-  const int num_sticks = num_objects - 1;
-  const int stick_length = 10;
-  int i;
-
-  memset (model, 0, sizeof *model);
-  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_init_polygons (model);
-
-  for (i = 0; i < num_objects; i++) {
-    model->objects[i].position.x = 200;
-    model->objects[i].position.y = 40 + i * stick_length;
-    model->objects[i].previous_position.x = 200;
-    model->objects[i].previous_position.y = 40 + i * stick_length;
-    model->objects[i].mass = 1;
-
-    if (i + 1 < num_objects) {
-      model->sticks[i].a = &model->objects[i];
-      model->sticks[i].b = &model->objects[i + 1];
-      model->sticks[i].length = stick_length;
-    }
-  }
-
-  model->anchor_object = NULL;
-}
-
-static void
-model_init_curtain (Model *model)
-{
-  const int num_ropes = 5;
-  const int num_rope_objects = 15;
-  const int num_objects = num_ropes * num_rope_objects;
-  const int num_sticks = num_ropes * (num_rope_objects - 1);
-  const int stick_length = 10;
-  const int rope_offset = 30;
-  double x, y;
-  int i, j, index, stick_index;
-
-  memset (model, 0, sizeof *model);
-  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->offsets = g_new (Offset, 1);
-  model->num_offsets = 1;
-  model_init_polygons (model);
-
-  model->offsets[0].num_objects = num_ropes;
-  model->offsets[0].objects = g_new (Object *, num_ropes);
-  model->offsets[0].dx = rope_offset;
-  model->offsets[0].dy = 0;
-
-  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;
-      index = i * num_rope_objects + j;
-      model->objects[index].position.x = x;
-      model->objects[index].position.y = y;
-      model->objects[index].previous_position.x = x;
-      model->objects[index].previous_position.y = y;
-      model->objects[i].mass = 1;
-
-      if (j + 1 < num_rope_objects) {
-       stick_index = 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->offsets[0].objects[i] = &model->objects[i * num_rope_objects];
-  }
-
-  model->anchor_object = NULL;
-}
-
-static void
-model_init_grid (Model *model)
-{
-  const int num_ropes = 4;
-  const int num_rope_objects = 4;
-  const int num_objects = num_ropes * num_rope_objects;
-  const int num_strings = num_ropes * (num_rope_objects - 1) +
-    (num_ropes - 1) * num_rope_objects;
-  const int string_length = 20;
-  const int rope_offset = 20;
-  double x, y;
-  int i, j, index, string_index;
-
-  memset (model, 0, sizeof *model);
-  model->objects = g_new (Object, num_objects);
-  model->num_objects = num_objects;
-  model->strings = g_new (String, num_strings);
-  model->num_strings = num_strings;
-  model->offsets = g_new (Offset, 1);
-  model->num_offsets = 1;
-  model_init_polygons (model);
-
-  model->offsets[0].num_objects = num_ropes;
-  model->offsets[0].objects = g_new (Object *, num_ropes);
-  model->offsets[0].dx = rope_offset;
-  model->offsets[0].dy = 0;
-
-  for (i = 0; i < num_ropes; i++) {
-    for (j = 0; j < num_rope_objects; j++) {
-      x = 200 + i * rope_offset;
-      y = 40 + j * string_length;
-      index = i * num_rope_objects + j;
-      model->objects[index].position.x = x;
-      model->objects[index].position.y = y;
-      model->objects[index].previous_position.x = x;
-      model->objects[index].previous_position.y = y;
-      model->objects[index].mass = 1;
-
-      if (i + 1 < num_ropes) {
-       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) {
-       string_index =
-         (num_ropes - 1) * num_rope_objects + i * (num_rope_objects - 1) + j;
-       model->strings[string_index].a = &model->objects[index];
-       model->strings[string_index].b = &model->objects[index + 1];
-       model->strings[string_index].length = string_length;
-      }
-    }
-
-    model->offsets[0].objects[i] = &model->objects[i * num_rope_objects];
-  }
-
-  model->anchor_object = NULL;
-}
-
-static void
-model_init_molecule (Model *model)
+void
+model_fini (Model *model)
 {
-  const int num_objects = 8;
-  const int num_springs = num_objects * 2;
-  const int spring_length = 50;
   int i;
 
-  memset (model, 0, sizeof *model);
-  model->objects = g_new (Object, num_objects);
-  model->num_objects = num_objects;
-  model->springs = g_new (Spring, num_springs);
-  model->num_springs = num_springs;
-  model->k = 0.2;
-
-  for (i = 0; i < num_objects; i++) {
-    model->objects[i].position.x = 200 + i * 20;
-    model->objects[i].position.y = 200;
-    model->objects[i].previous_position.x = 200 + i * 20;
-    model->objects[i].previous_position.y = 200;
-    model->objects[i].mass = 0;
-  }
-
-  for (i = 0; i < num_objects; i++) {
-    model->springs[i * 2].a = &model->objects[i];
-    model->springs[i * 2].b = &model->objects[(i + 1) % num_objects];
-    model->springs[i * 2].length = spring_length;
-    model->springs[i * 2 + 1].a = &model->objects[i];
-    model->springs[i * 2 + 1].b = &model->objects[(i + 2) % num_objects];
-    model->springs[i * 2 + 1].length = spring_length;
-  }
-}
-
-
-static void
-model_init_wobbly (Model *model)
-{
-  const int width = 6, height = 6;
-  const int num_objects = width * height;
-  const int num_offset_springs = (width - 1) * height + width * (height - 1);
-  const int distance = 30;
-  double x, y;
-  int i, j, object_index, spring_index;
-
-  memset (model, 0, sizeof *model);
-  model->objects = g_new (Object, num_objects);
-  model->num_objects = num_objects;
-  model->offset_springs = g_new (OffsetSpring, num_offset_springs);
-  model->num_offset_springs = num_offset_springs;
-  model->k = 0.6;
-
-  model_init_polygons (model);
-
-  object_index = 0;
-  spring_index = 0;
-  for (i = 0; i < width; i++) {
-    for (j = 0; j < height; j++) {
-      x = 200 + i * distance;
-      y = 40 + j * distance;
-      model->objects[object_index].position.x = x;
-      model->objects[object_index].position.y = y;
-      model->objects[object_index].previous_position.x = x;
-      model->objects[object_index].previous_position.y = y;
-      model->objects[object_index].mass = 0.3;
-
-      if (i + 1 < width) {
-       model->offset_springs[spring_index].a = &model->objects[object_index];
-       model->offset_springs[spring_index].b = &model->objects[object_index + height];
-       model->offset_springs[spring_index].dx = distance;
-       model->offset_springs[spring_index].dy = 0;
-       spring_index++;
-      }
-      
-      if (j + 1 < height) {
-       model->offset_springs[spring_index].a = &model->objects[object_index];
-       model->offset_springs[spring_index].b = &model->objects[object_index + 1];
-       model->offset_springs[spring_index].dx = 0;
-       model->offset_springs[spring_index].dy = distance;
-       spring_index++;
-      }
-
-      object_index++;
-    }
-  }
-}
-
-
-static void
-model_fini (Model *model)
-{
   g_free (model->objects);
   g_free (model->sticks);
   g_free (model->strings);
-  g_free (model->offsets);
+  for (i = 0; i < model->num_offsets; i++)
+    g_free (model->offsets[i].objects);
+  g_free (model->springs);
+  g_free (model->offset_springs);
+  for (i = 0; i < model->num_polygons; i++)
+    g_free (model->polygons[i].points);
+  g_free (model->polygons);
+
   memset (model, 0, sizeof *model);
 }
 
@@ -684,7 +323,7 @@ model_constrain (Model *model)
     model_constrain_polygon (model, &model->polygons[i]);
 }
 
-static void
+void
 model_step (Model *model, double delta_t)
 {
   int i;
@@ -709,7 +348,7 @@ object_distance (Object *object, double x, double y)
   return sqrt (dx*dx + dy*dy);
 }
 
-static Object *
+Object *
 model_find_nearest (Model *model, double x, double y)
 {
   Object *object;
@@ -726,478 +365,3 @@ model_find_nearest (Model *model, double x, double y)
 
   return object;
 }
-
-typedef struct _Color Color;
-struct _Color {
-  double red, green, blue;
-};
-
-static void
-draw_sticks (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, 2);
-  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
-  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
-
-  for (i = 0; i < model->num_sticks; i++) {
-    cairo_move_to (cr,
-                  model->sticks[i].a->position.x,
-                  model->sticks[i].a->position.y);
-    cairo_line_to (cr,
-                  model->sticks[i].b->position.x,
-                  model->sticks[i].b->position.y);
-  }
-
-  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,
-             Color   *color)
-{
-  int i, j;
-
-  cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.5);
-  cairo_new_path (cr);
-  cairo_set_line_width (cr, 4);
-  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
-  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
-
-  for (i = 0; i < model->num_offsets; i++) {
-    for (j = 0; j < model->offsets[i].num_objects; j++) {
-      cairo_line_to (cr,
-                    model->offsets[i].objects[j]->position.x,
-                    model->offsets[i].objects[j]->position.y);
-    }
-    cairo_stroke (cr);
-  }
-
-}
-
-static void
-draw_springs (cairo_t *cr,
-             Model   *model,
-             Color   *color)
-{
-  int i;
-
-  cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
-  cairo_new_path (cr);
-  cairo_set_line_width (cr, 2);
-  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
-  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
-
-  for (i = 0; i < model->num_springs; i++) {
-    cairo_move_to (cr,
-                  model->springs[i].a->position.x,
-                  model->springs[i].a->position.y);
-    cairo_line_to (cr,
-                  model->springs[i].b->position.x,
-                  model->springs[i].b->position.y);
-  }
-
-  cairo_stroke (cr);
-}
-
-static void
-draw_offset_springs (cairo_t *cr,
-                    Model   *model,
-                    Color   *color)
-{
-  int i;
-
-  cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
-  cairo_new_path (cr);
-  cairo_set_line_width (cr, 2);
-  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
-  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
-
-  for (i = 0; i < model->num_offset_springs; i++) {
-    cairo_move_to (cr,
-                  model->offset_springs[i].a->position.x,
-                  model->offset_springs[i].a->position.y);
-    cairo_line_to (cr,
-                  model->offset_springs[i].b->position.x,
-                  model->offset_springs[i].b->position.y);
-  }
-
-  cairo_stroke (cr);
-}
-
-static void
-draw_polygons (cairo_t *cr, Model *model, Color *color)
-{
-  Polygon *p;
-  int i, j;
-
-  for (i = 0; i < model->num_polygons; i++) {
-    p = &model->polygons[i];
-    cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
-
-    
-    for (j = 0; j < p->num_points; j++)
-      cairo_line_to (cr, p->points[j].x, p->points[j].y);
-    cairo_close_path (cr);
-  }
-  cairo_fill (cr);
-
-}
-
-static void
-draw_objects (cairo_t *cr, Model *model, Color *color)
-{
-  int i;
-
-  cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
-  for (i = 0; i < model->num_objects; i++) {
-    cairo_arc (cr, model->objects[i].position.x,
-              model->objects[i].position.y,
-              3, 0, 2*M_PI);
-    cairo_fill (cr);
-  }
-}
-
-static Color blue = { 0, 0, 1 };
-static Color green = { 0, 1, 0 };
-static Color red = { 1, 0, 0 };
-static Color black = { 0, 0, 0 };
-
-typedef struct _Closure Closure;
-struct _Closure {
-  GtkWidget *drawing_area;
-  GtkWidget *fps_label;
-  Model *model;
-  int frame_count;
-  int i;
-  struct timeval start;
-};
-
-static void
-draw_model (GtkWidget *widget, Model *model)
-{
-  cairo_t *cr;
-
-  cr = gdk_cairo_create (widget->window);
-
-  cairo_set_source_rgb (cr, 1, 1, 1);
-  cairo_paint (cr);
-
-  draw_polygons (cr, model, &blue);
-  draw_sticks (cr, model, &black);
-  draw_strings (cr, model, &green);
-  draw_springs (cr, model, &black);
-  draw_offsets (cr, model, &blue);
-  draw_offset_springs (cr, model, &blue);
-  draw_objects (cr, model, &red);
-
-  cairo_destroy (cr);
-}
-
-static gboolean
-expose_event (GtkWidget      *widget,
-             GdkEventExpose *event,
-             gpointer        data)
-{
-  Closure *closure = data;
-
-  draw_model (widget, closure->model);
-
-  return TRUE;
-}
-
-static gboolean
-button_press_event (GtkWidget     *widget,
-                   GdkEventButton *event,
-                   gpointer        data)
-{
-  Closure *closure = data;
-
-  if (event->button != 1)
-    return TRUE;
-
-  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;
-}
-
-static gboolean
-button_release_event (GtkWidget             *widget,
-                     GdkEventButton *event,
-                     gpointer        data)
-{
-  Closure *closure = data;
-
-  if ((event->state & GDK_BUTTON1_MASK) == 0)
-    return TRUE;
-
-  closure->model->anchor_object = NULL;
-
-  return TRUE;
-}
-
-static gboolean
-motion_notify_event (GtkWidget     *widget,
-                    GdkEventMotion *event,
-                    gpointer        data)
-{
-  Closure *closure = data;
-  int x, y;
-  GdkModifierType state;
-
-  gdk_window_get_pointer (event->window, &x, &y, &state);
-  
-  closure->model->anchor_position.x = x + 0.5;
-  closure->model->anchor_position.y = y + 0.5;
-
-  return TRUE;
-}
-
-typedef void (*ModelInitFunc) (Model *model);
-
-static void
-model_changed (GtkComboBox *combo, gpointer user_data)
-{
-  Closure *closure = user_data;
-  GtkTreeIter iter;
-  GtkTreeModel *tree_model;
-  ModelInitFunc init;
-  char *name;
-
-  tree_model = gtk_combo_box_get_model (combo);
-  if (!gtk_combo_box_get_active_iter (combo, &iter))
-    return;
-
-  gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1);
-
-  model_fini (closure->model);
-  (*init) (closure->model);
-}
-
-static GtkTreeModel *
-create_model_store (void)
-{
-  static struct {
-    const char *name;
-    ModelInitFunc init;
-  } models[] = {
-    { "Rope", model_init_rope },
-    { "Snake", model_init_snake },
-    { "Curtain", model_init_curtain },
-    { "Grid", model_init_grid },
-    { "Molecule", model_init_molecule },
-    { "Wobbly", model_init_wobbly }
-  };
-
-  GtkTreeIter iter;
-  GtkTreeStore *store;
-  gint i;
-
-  store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
-
-  for (i = 0; i < G_N_ELEMENTS(models); i++) {
-    gtk_tree_store_append (store, &iter, NULL);
-    gtk_tree_store_set (store, &iter,
-                       0, models[i].name, 1, models[i].init, -1);
- }
-  
-  return GTK_TREE_MODEL (store);
-
-}
-
-static GtkWidget *
-create_model_combo (Closure *closure)
-{
-  GtkWidget *hbox;
-  GtkWidget *combo, *label;
-  GtkTreeModel *store;
-  GtkCellRenderer *renderer;
-
-  hbox = gtk_hbox_new (FALSE, 8);
-
-  label = gtk_label_new_with_mnemonic ("_Model:");
-  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
-
-  store = create_model_store ();
-  combo = gtk_combo_box_new_with_model (store);
-  gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
-  g_object_unref (store);
-
-  renderer = gtk_cell_renderer_text_new ();
-  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
-  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
-                                 "text", 0,
-                                 NULL);
-
-  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), 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 void
-create_window (Closure *closure)
-{
-  GtkWidget *window;
-  GtkWidget *frame;
-  GtkWidget *vbox;
-  GtkWidget *da;
-  GtkWidget *model_combo;
-
-  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-  gtk_window_set_title (GTK_WINDOW (window), "Akamaru");
-
-  g_signal_connect (window, "destroy",
-                   G_CALLBACK (gtk_main_quit), &window);
-
-  gtk_container_set_border_width (GTK_CONTAINER (window), 8);
-
-  vbox = gtk_vbox_new (FALSE, 8);
-  gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
-  gtk_container_add (GTK_CONTAINER (window), vbox);
-
-  /*
-   * Create the drawing area
-   */
-      
-  frame = gtk_frame_new (NULL);
-  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
-  gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
-      
-  da = gtk_drawing_area_new ();
-  /* set a minimum size */
-  gtk_widget_set_size_request (da, 200, 200);
-
-  gtk_container_add (GTK_CONTAINER (frame), da);
-
-  /* Signals used to handle backing pixmap */
-      
-  g_signal_connect (da, "expose_event",
-                   G_CALLBACK (expose_event), closure);
-      
-  /* Event signals */
-      
-  g_signal_connect (da, "motion_notify_event",
-                   G_CALLBACK (motion_notify_event), closure);
-  g_signal_connect (da, "button_press_event",
-                   G_CALLBACK (button_press_event), closure);
-  g_signal_connect (da, "button_release_event",
-                   G_CALLBACK (button_release_event), closure);
-
-  /* Ask to receive events the drawing area doesn't normally
-   * subscribe to
-   */
-  gtk_widget_set_events (da, gtk_widget_get_events (da)
-                        | GDK_LEAVE_NOTIFY_MASK
-                        | GDK_BUTTON_PRESS_MASK
-                        | GDK_BUTTON_RELEASE_MASK
-                        | GDK_POINTER_MOTION_MASK
-                        | GDK_POINTER_MOTION_HINT_MASK);
-
-  model_combo = create_model_combo (closure);
-  gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0);
-
-  closure->drawing_area = da;
-}
-
-static gint
-timeout_callback (gpointer data)
-{
-  Closure *closure = data;
-
-  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;
-}
-
-int
-main (int argc, char *argv[])
-{
-  Closure closure;
-  Model model;
-
-  gtk_init (&argc, &argv);
-  model_init_rope (&model);
-  create_window (&closure);
-  closure.i = 0;
-  gtk_widget_show_all (gtk_widget_get_toplevel (closure.drawing_area));
-  closure.model = &model;
-  closure.frame_count = 0;
-  gettimeofday (&closure.start, NULL);
-  g_timeout_add (40, timeout_callback, &closure);
-  gtk_main ();
-
-  return 0;
-}
diff --git a/akamaru.h b/akamaru.h
new file mode 100644 (file)
index 0000000..bfec60f
--- /dev/null
+++ b/akamaru.h
@@ -0,0 +1,98 @@
+#ifndef __AKAMARU_H__
+#define __AKAMARU_H__
+
+typedef struct _xy_pair Point;
+typedef struct _xy_pair Vector;
+struct _xy_pair {
+  double x, y;
+};
+
+typedef struct _Object Object;
+typedef struct _Stick Stick;
+typedef struct _String String;
+typedef struct _Spring Spring;
+typedef struct _OffsetSpring OffsetSpring;
+typedef struct _Polygon Polygon;
+typedef struct _Offset Offset;
+typedef struct _Model Model;
+
+struct _Object {
+  Vector force;
+
+  Point position;
+  Point previous_position;
+  Vector velocity;
+
+  double mass;
+  double theta;
+};
+
+struct _Stick {
+  Object *a, *b;
+  int length;
+};
+
+struct _String {
+  Object *a, *b;
+  int length;
+};
+
+struct _Offset {
+  int num_objects;
+  Object **objects;
+  int dx, dy;
+};
+
+struct _Spring {
+  Object *a, *b;
+  int length;
+};
+
+struct _OffsetSpring {
+  Object *a, *b;
+  int dx, dy;
+};
+
+struct _Polygon {
+  int num_points;
+  Point *points;
+  Vector *normals;
+  int edge;
+};
+
+struct _Model {
+  int num_objects;
+  Object *objects;
+  int num_sticks;
+  Stick *sticks;
+  int num_strings;
+  String *strings;
+  int num_offsets;
+  Offset *offsets;
+  int num_springs;
+  Spring *springs;
+  int num_offset_springs;
+  OffsetSpring *offset_springs;
+  int num_polygons;
+  Polygon *polygons;
+  double k;
+  double friction;
+
+  Object *anchor_object;
+  Vector anchor_position;
+
+  double theta;
+};
+
+void polygon_init (Polygon *p, int num_points, ...);
+void polygon_init_diamond (Polygon *polygon, double x, double y);
+void polygon_init_rectangle (Polygon *polygon, double x0, double y0,
+                            double x1, double y1);
+
+void model_fini (Model *model);
+
+void model_step (Model *model, double delta_t);
+
+Object *model_find_nearest (Model *model, double x, double y);
+
+#endif
diff --git a/main.c b/main.c
new file mode 100644 (file)
index 0000000..0aec615
--- /dev/null
+++ b/main.c
@@ -0,0 +1,775 @@
+/*                                           -*- mode: c; c-basic-offset: 2 -*-
+ * To compile:
+ *
+ *     gcc -Wall -g $(pkg-config --cflags --libs gtk+-2.0 cairo) \
+ *             akamaru.c main.c -o akamaru
+ */
+
+#include <gtk/gtk.h>
+#include <cairo.h>
+#include <cairo-xlib.h>
+#include <gdk/gdkx.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+#include <math.h>
+
+#include "akamaru.h"
+
+static void
+model_init_polygons (Model *model)
+{
+  const int num_polygons = 5;
+  const double ground_level = 500;
+
+  model->polygons = g_new (Polygon, num_polygons);
+  polygon_init_diamond (&model->polygons[0], 250, 300);
+  polygon_init_diamond (&model->polygons[1], 400, 150);
+  polygon_init_rectangle (&model->polygons[2], -100, 200, 200, 250);
+  polygon_init_rectangle (&model->polygons[3], -200, ground_level,
+                         1200, ground_level + 400);
+
+  polygon_init_rectangle (&model->polygons[4], 300, 320, 400, 350);
+
+  model->num_polygons = num_polygons;
+}
+
+static void
+model_init_snake (Model *model)
+{
+  const int num_objects = 20;
+  const int num_sticks = num_objects * 2 - 3;
+  int i;
+
+  memset (model, 0, sizeof *model);
+  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_init_polygons (model);
+
+  for (i = 0; i < num_objects; i++) {
+    model->objects[i].position.x = random() % 200 + 20;
+    model->objects[i].position.y = random() % 200 + 20;
+    model->objects[i].previous_position.x = random() % 200 + 20;
+    model->objects[i].previous_position.y = random() % 200 + 20;
+    model->objects[i].mass = 1;
+
+    if (i + 1 < num_objects) {
+      model->sticks[i * 2].a = &model->objects[i];
+      model->sticks[i * 2].b = &model->objects[i + 1];
+      model->sticks[i * 2].length = random() % 20 + 20;
+    }
+    if (i + 2 < num_objects) {
+      model->sticks[i * 2 + 1].a = &model->objects[i];
+      model->sticks[i * 2 + 1].b = &model->objects[i + 2];
+      model->sticks[i * 2 + 1].length = random() % 20 + 20;
+    }
+  }
+
+  model->anchor_object = NULL;
+}
+
+static void
+model_init_rope (Model *model)
+{
+  const int num_objects = 20;
+  const int num_sticks = num_objects - 1;
+  const int stick_length = 10;
+  int i;
+
+  memset (model, 0, sizeof *model);
+  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_init_polygons (model);
+
+  for (i = 0; i < num_objects; i++) {
+    model->objects[i].position.x = 200;
+    model->objects[i].position.y = 40 + i * stick_length;
+    model->objects[i].previous_position.x = 200;
+    model->objects[i].previous_position.y = 40 + i * stick_length;
+    model->objects[i].mass = 1;
+
+    if (i + 1 < num_objects) {
+      model->sticks[i].a = &model->objects[i];
+      model->sticks[i].b = &model->objects[i + 1];
+      model->sticks[i].length = stick_length;
+    }
+  }
+
+  model->anchor_object = NULL;
+}
+
+static void
+model_init_curtain (Model *model)
+{
+  const int num_ropes = 5;
+  const int num_rope_objects = 15;
+  const int num_objects = num_ropes * num_rope_objects;
+  const int num_sticks = num_ropes * (num_rope_objects - 1);
+  const int stick_length = 10;
+  const int rope_offset = 30;
+  double x, y;
+  int i, j, index, stick_index;
+
+  memset (model, 0, sizeof *model);
+  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->offsets = g_new (Offset, 1);
+  model->num_offsets = 1;
+  model_init_polygons (model);
+
+  model->offsets[0].num_objects = num_ropes;
+  model->offsets[0].objects = g_new (Object *, num_ropes);
+  model->offsets[0].dx = rope_offset;
+  model->offsets[0].dy = 0;
+
+  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;
+      index = i * num_rope_objects + j;
+      model->objects[index].position.x = x;
+      model->objects[index].position.y = y;
+      model->objects[index].previous_position.x = x;
+      model->objects[index].previous_position.y = y;
+      model->objects[i].mass = 1;
+
+      if (j + 1 < num_rope_objects) {
+       stick_index = 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->offsets[0].objects[i] = &model->objects[i * num_rope_objects];
+  }
+
+  model->anchor_object = NULL;
+}
+
+static void
+model_init_grid (Model *model)
+{
+  const int num_ropes = 4;
+  const int num_rope_objects = 4;
+  const int num_objects = num_ropes * num_rope_objects;
+  const int num_strings = num_ropes * (num_rope_objects - 1) +
+    (num_ropes - 1) * num_rope_objects;
+  const int string_length = 20;
+  const int rope_offset = 20;
+  double x, y;
+  int i, j, index, string_index;
+
+  memset (model, 0, sizeof *model);
+  model->objects = g_new (Object, num_objects);
+  model->num_objects = num_objects;
+  model->strings = g_new (String, num_strings);
+  model->num_strings = num_strings;
+  model->offsets = g_new (Offset, 1);
+  model->num_offsets = 1;
+  model_init_polygons (model);
+
+  model->offsets[0].num_objects = num_ropes;
+  model->offsets[0].objects = g_new (Object *, num_ropes);
+  model->offsets[0].dx = rope_offset;
+  model->offsets[0].dy = 0;
+
+  for (i = 0; i < num_ropes; i++) {
+    for (j = 0; j < num_rope_objects; j++) {
+      x = 200 + i * rope_offset;
+      y = 40 + j * string_length;
+      index = i * num_rope_objects + j;
+      model->objects[index].position.x = x;
+      model->objects[index].position.y = y;
+      model->objects[index].previous_position.x = x;
+      model->objects[index].previous_position.y = y;
+      model->objects[index].mass = 1;
+
+      if (i + 1 < num_ropes) {
+       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) {
+       string_index =
+         (num_ropes - 1) * num_rope_objects + i * (num_rope_objects - 1) + j;
+       model->strings[string_index].a = &model->objects[index];
+       model->strings[string_index].b = &model->objects[index + 1];
+       model->strings[string_index].length = string_length;
+      }
+    }
+
+    model->offsets[0].objects[i] = &model->objects[i * num_rope_objects];
+  }
+
+  model->anchor_object = NULL;
+}
+
+static void
+model_init_molecule (Model *model)
+{
+  const int num_objects = 8;
+  const int num_springs = num_objects * 2;
+  const int spring_length = 50;
+  int i;
+
+  memset (model, 0, sizeof *model);
+  model->objects = g_new (Object, num_objects);
+  model->num_objects = num_objects;
+  model->springs = g_new (Spring, num_springs);
+  model->num_springs = num_springs;
+  model->k = 0.2;
+
+  for (i = 0; i < num_objects; i++) {
+    model->objects[i].position.x = 200 + i * 20;
+    model->objects[i].position.y = 200;
+    model->objects[i].previous_position.x = 200 + i * 20;
+    model->objects[i].previous_position.y = 200;
+    model->objects[i].mass = 0;
+  }
+
+  for (i = 0; i < num_objects; i++) {
+    model->springs[i * 2].a = &model->objects[i];
+    model->springs[i * 2].b = &model->objects[(i + 1) % num_objects];
+    model->springs[i * 2].length = spring_length;
+    model->springs[i * 2 + 1].a = &model->objects[i];
+    model->springs[i * 2 + 1].b = &model->objects[(i + 2) % num_objects];
+    model->springs[i * 2 + 1].length = spring_length;
+  }
+}
+
+
+static void
+model_init_wobbly (Model *model)
+{
+  const int width = 6, height = 6;
+  const int num_objects = width * height;
+  const int num_offset_springs = (width - 1) * height + width * (height - 1);
+  const int distance = 10;
+  double x, y;
+  int i, j, object_index, spring_index;
+
+  memset (model, 0, sizeof *model);
+  model->objects = g_new (Object, num_objects);
+  model->num_objects = num_objects;
+  model->offset_springs = g_new (OffsetSpring, num_offset_springs);
+  model->num_offset_springs = num_offset_springs;
+  model->k = 1;
+
+  model_init_polygons (model);
+
+  object_index = 0;
+  spring_index = 0;
+  for (i = 0; i < width; i++) {
+    for (j = 0; j < height; j++) {
+      x = 200 + i * distance;
+      y = 40 + j * distance;
+      model->objects[object_index].position.x = x;
+      model->objects[object_index].position.y = y;
+      model->objects[object_index].previous_position.x = x;
+      model->objects[object_index].previous_position.y = y;
+      model->objects[object_index].mass = 0.3;
+
+      if (i + 1 < width) {
+       model->offset_springs[spring_index].a = &model->objects[object_index];
+       model->offset_springs[spring_index].b = &model->objects[object_index + height];
+       model->offset_springs[spring_index].dx = distance;
+       model->offset_springs[spring_index].dy = 0;
+       spring_index++;
+      }
+      
+      if (j + 1 < height) {
+       model->offset_springs[spring_index].a = &model->objects[object_index];
+       model->offset_springs[spring_index].b = &model->objects[object_index + 1];
+       model->offset_springs[spring_index].dx = 0;
+       model->offset_springs[spring_index].dy = distance;
+       spring_index++;
+      }
+
+      object_index++;
+    }
+  }
+}
+
+typedef struct _Color Color;
+struct _Color {
+  double red, green, blue;
+};
+
+static void
+draw_sticks (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, 2);
+  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
+  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
+
+  for (i = 0; i < model->num_sticks; i++) {
+    cairo_move_to (cr,
+                  model->sticks[i].a->position.x,
+                  model->sticks[i].a->position.y);
+    cairo_line_to (cr,
+                  model->sticks[i].b->position.x,
+                  model->sticks[i].b->position.y);
+  }
+
+  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,
+             Color   *color)
+{
+  int i, j;
+
+  cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.5);
+  cairo_new_path (cr);
+  cairo_set_line_width (cr, 4);
+  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
+  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
+
+  for (i = 0; i < model->num_offsets; i++) {
+    for (j = 0; j < model->offsets[i].num_objects; j++) {
+      cairo_line_to (cr,
+                    model->offsets[i].objects[j]->position.x,
+                    model->offsets[i].objects[j]->position.y);
+    }
+    cairo_stroke (cr);
+  }
+
+}
+
+static void
+draw_springs (cairo_t *cr,
+             Model   *model,
+             Color   *color)
+{
+  int i;
+
+  cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
+  cairo_new_path (cr);
+  cairo_set_line_width (cr, 2);
+  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
+  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
+
+  for (i = 0; i < model->num_springs; i++) {
+    cairo_move_to (cr,
+                  model->springs[i].a->position.x,
+                  model->springs[i].a->position.y);
+    cairo_line_to (cr,
+                  model->springs[i].b->position.x,
+                  model->springs[i].b->position.y);
+  }
+
+  cairo_stroke (cr);
+}
+
+static void
+draw_offset_springs (cairo_t *cr,
+                    Model   *model,
+                    Color   *color)
+{
+  int i;
+
+  cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
+  cairo_new_path (cr);
+  cairo_set_line_width (cr, 2);
+  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
+  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
+
+  for (i = 0; i < model->num_offset_springs; i++) {
+    cairo_move_to (cr,
+                  model->offset_springs[i].a->position.x,
+                  model->offset_springs[i].a->position.y);
+    cairo_line_to (cr,
+                  model->offset_springs[i].b->position.x,
+                  model->offset_springs[i].b->position.y);
+  }
+
+  cairo_stroke (cr);
+}
+
+static void
+draw_polygons (cairo_t *cr, Model *model, Color *color)
+{
+  Polygon *p;
+  int i, j;
+
+  for (i = 0; i < model->num_polygons; i++) {
+    p = &model->polygons[i];
+    cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
+
+    
+    for (j = 0; j < p->num_points; j++)
+      cairo_line_to (cr, p->points[j].x, p->points[j].y);
+    cairo_close_path (cr);
+  }
+  cairo_fill (cr);
+
+}
+
+static void
+draw_objects (cairo_t *cr, Model *model, Color *color)
+{
+  int i;
+
+  cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
+  for (i = 0; i < model->num_objects; i++) {
+    cairo_arc (cr, model->objects[i].position.x,
+              model->objects[i].position.y,
+              3, 0, 2*M_PI);
+    cairo_fill (cr);
+  }
+}
+
+static Color blue = { 0, 0, 1 };
+static Color green = { 0, 1, 0 };
+static Color red = { 1, 0, 0 };
+static Color black = { 0, 0, 0 };
+
+typedef struct _Closure Closure;
+struct _Closure {
+  GtkWidget *drawing_area;
+  GtkWidget *fps_label;
+  Model *model;
+  int frame_count;
+  int i;
+  struct timeval start;
+};
+
+static void
+draw_model (GtkWidget *widget, Model *model)
+{
+  cairo_t *cr;
+
+  cr = gdk_cairo_create (widget->window);
+
+  cairo_set_source_rgb (cr, 1, 1, 1);
+  cairo_paint (cr);
+
+  draw_polygons (cr, model, &blue);
+  draw_sticks (cr, model, &black);
+  draw_strings (cr, model, &green);
+  draw_springs (cr, model, &black);
+  draw_offsets (cr, model, &blue);
+  draw_offset_springs (cr, model, &blue);
+  draw_objects (cr, model, &red);
+
+  cairo_destroy (cr);
+}
+
+static gboolean
+expose_event (GtkWidget      *widget,
+             GdkEventExpose *event,
+             gpointer        data)
+{
+  Closure *closure = data;
+
+  draw_model (widget, closure->model);
+
+  return TRUE;
+}
+
+static gboolean
+button_press_event (GtkWidget     *widget,
+                   GdkEventButton *event,
+                   gpointer        data)
+{
+  Closure *closure = data;
+
+  if (event->button != 1)
+    return TRUE;
+
+  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;
+}
+
+static gboolean
+button_release_event (GtkWidget             *widget,
+                     GdkEventButton *event,
+                     gpointer        data)
+{
+  Closure *closure = data;
+
+  if ((event->state & GDK_BUTTON1_MASK) == 0)
+    return TRUE;
+
+  closure->model->anchor_object = NULL;
+
+  return TRUE;
+}
+
+static gboolean
+motion_notify_event (GtkWidget     *widget,
+                    GdkEventMotion *event,
+                    gpointer        data)
+{
+  Closure *closure = data;
+  int x, y;
+  GdkModifierType state;
+
+  gdk_window_get_pointer (event->window, &x, &y, &state);
+  
+  closure->model->anchor_position.x = x + 0.5;
+  closure->model->anchor_position.y = y + 0.5;
+
+  return TRUE;
+}
+
+typedef void (*ModelInitFunc) (Model *model);
+
+static void
+model_changed (GtkComboBox *combo, gpointer user_data)
+{
+  Closure *closure = user_data;
+  GtkTreeIter iter;
+  GtkTreeModel *tree_model;
+  ModelInitFunc init;
+  char *name;
+
+  tree_model = gtk_combo_box_get_model (combo);
+  if (!gtk_combo_box_get_active_iter (combo, &iter))
+    return;
+
+  gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1);
+
+  model_fini (closure->model);
+  (*init) (closure->model);
+}
+
+static GtkTreeModel *
+create_model_store (void)
+{
+  static struct {
+    const char *name;
+    ModelInitFunc init;
+  } models[] = {
+    { "Rope", model_init_rope },
+    { "Snake", model_init_snake },
+    { "Curtain", model_init_curtain },
+    { "Grid", model_init_grid },
+    { "Molecule", model_init_molecule },
+    { "Wobbly", model_init_wobbly }
+  };
+
+  GtkTreeIter iter;
+  GtkTreeStore *store;
+  gint i;
+
+  store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
+
+  for (i = 0; i < G_N_ELEMENTS(models); i++) {
+    gtk_tree_store_append (store, &iter, NULL);
+    gtk_tree_store_set (store, &iter,
+                       0, models[i].name, 1, models[i].init, -1);
+ }
+  
+  return GTK_TREE_MODEL (store);
+
+}
+
+static GtkWidget *
+create_model_combo (Closure *closure)
+{
+  GtkWidget *hbox;
+  GtkWidget *combo, *label;
+  GtkTreeModel *store;
+  GtkCellRenderer *renderer;
+
+  hbox = gtk_hbox_new (FALSE, 8);
+
+  label = gtk_label_new_with_mnemonic ("_Model:");
+  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
+
+  store = create_model_store ();
+  combo = gtk_combo_box_new_with_model (store);
+  gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
+  g_object_unref (store);
+
+  renderer = gtk_cell_renderer_text_new ();
+  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
+  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
+                                 "text", 0,
+                                 NULL);
+
+  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), 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 void
+create_window (Closure *closure)
+{
+  GtkWidget *window;
+  GtkWidget *frame;
+  GtkWidget *vbox;
+  GtkWidget *da;
+  GtkWidget *model_combo;
+
+  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+  gtk_window_set_title (GTK_WINDOW (window), "Akamaru");
+
+  g_signal_connect (window, "destroy",
+                   G_CALLBACK (gtk_main_quit), &window);
+
+  gtk_container_set_border_width (GTK_CONTAINER (window), 8);
+
+  vbox = gtk_vbox_new (FALSE, 8);
+  gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
+  gtk_container_add (GTK_CONTAINER (window), vbox);
+
+  /*
+   * Create the drawing area
+   */
+      
+  frame = gtk_frame_new (NULL);
+  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
+  gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
+      
+  da = gtk_drawing_area_new ();
+  /* set a minimum size */
+  gtk_widget_set_size_request (da, 200, 200);
+
+  gtk_container_add (GTK_CONTAINER (frame), da);
+
+  /* Signals used to handle backing pixmap */
+      
+  g_signal_connect (da, "expose_event",
+                   G_CALLBACK (expose_event), closure);
+      
+  /* Event signals */
+      
+  g_signal_connect (da, "motion_notify_event",
+                   G_CALLBACK (motion_notify_event), closure);
+  g_signal_connect (da, "button_press_event",
+                   G_CALLBACK (button_press_event), closure);
+  g_signal_connect (da, "button_release_event",
+                   G_CALLBACK (button_release_event), closure);
+
+  /* Ask to receive events the drawing area doesn't normally
+   * subscribe to
+   */
+  gtk_widget_set_events (da, gtk_widget_get_events (da)
+                        | GDK_LEAVE_NOTIFY_MASK
+                        | GDK_BUTTON_PRESS_MASK
+                        | GDK_BUTTON_RELEASE_MASK
+                        | GDK_POINTER_MOTION_MASK
+                        | GDK_POINTER_MOTION_HINT_MASK);
+
+  model_combo = create_model_combo (closure);
+  gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0);
+
+  closure->drawing_area = da;
+}
+
+static gint
+timeout_callback (gpointer data)
+{
+  Closure *closure = data;
+
+  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;
+}
+
+int
+main (int argc, char *argv[])
+{
+  Closure closure;
+  Model model;
+
+  gtk_init (&argc, &argv);
+  model_init_rope (&model);
+  create_window (&closure);
+  closure.i = 0;
+  gtk_widget_show_all (gtk_widget_get_toplevel (closure.drawing_area));
+  closure.model = &model;
+  closure.frame_count = 0;
+  gettimeofday (&closure.start, NULL);
+  g_timeout_add (40, timeout_callback, &closure);
+  gtk_main ();
+
+  return 0;
+}