]> git.cworth.org Git - akamaru/blob - main.c
Add anchor constraint and use it to lock center point in dock model.
[akamaru] / main.c
1 /*                                           -*- mode: c; c-basic-offset: 2 -*-
2  * To compile:
3  *
4  *     gcc -Wall -g $(pkg-config --cflags --libs gtk+-2.0 cairo) \
5  *             akamaru.c main.c -o akamaru
6  */
7
8 #include <gtk/gtk.h>
9 #include <cairo.h>
10 #include <cairo-xlib.h>
11 #include <gdk/gdkx.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/time.h>
15 #include <math.h>
16
17 #include "akamaru.h"
18
19 static void
20 model_init_polygons (Model *model)
21 {
22   const int num_polygons = 5;
23   const double ground_level = 500;
24
25   model->polygons = g_new (Polygon, num_polygons);
26   polygon_init_diamond (&model->polygons[0], 250, 300);
27   polygon_init_diamond (&model->polygons[1], 400, 150);
28   polygon_init_rectangle (&model->polygons[2], -100, 200, 200, 250);
29   polygon_init_rectangle (&model->polygons[3], -200, ground_level,
30                           1200, ground_level + 400);
31
32   polygon_init_rectangle (&model->polygons[4], 300, 320, 400, 350);
33
34   model->num_polygons = num_polygons;
35 }
36
37 static void
38 model_init_snake (Model *model)
39 {
40   const int num_objects = 20;
41   const int num_sticks = num_objects * 2 - 3;
42   int i;
43
44   memset (model, 0, sizeof *model);
45   model->objects = g_new (Object, num_objects);
46   model->num_objects = num_objects;
47   model->sticks = g_new (Stick, num_sticks);
48   model->num_sticks = num_sticks;
49   model_init_polygons (model);
50
51   for (i = 0; i < num_objects; i++) {
52     object_init (&model->objects[i],
53                  random() % 200 + 20, random() % 200 + 20, 1);
54
55     if (i + 1 < num_objects) {
56       model->sticks[i * 2].a = &model->objects[i];
57       model->sticks[i * 2].b = &model->objects[i + 1];
58       model->sticks[i * 2].length = random() % 20 + 20;
59     }
60     if (i + 2 < num_objects) {
61       model->sticks[i * 2 + 1].a = &model->objects[i];
62       model->sticks[i * 2 + 1].b = &model->objects[i + 2];
63       model->sticks[i * 2 + 1].length = random() % 20 + 20;
64     }
65   }
66 }
67
68 static void
69 model_init_rope (Model *model)
70 {
71   const int num_objects = 20;
72   const int num_sticks = num_objects - 1;
73   const int stick_length = 10;
74   int i;
75
76   memset (model, 0, sizeof *model);
77   model->objects = g_new (Object, num_objects);
78   model->num_objects = num_objects;
79   model->sticks = g_new (Stick, num_sticks);
80   model->num_sticks = num_sticks;
81   model_init_polygons (model);
82
83   for (i = 0; i < num_objects; i++) {
84     object_init (&model->objects[i], 200, 40 + i * stick_length, 1);
85
86     if (i + 1 < num_objects) {
87       model->sticks[i].a = &model->objects[i];
88       model->sticks[i].b = &model->objects[i + 1];
89       model->sticks[i].length = stick_length;
90     }
91   }
92 }
93
94 static void
95 model_init_curtain (Model *model)
96 {
97   const int num_ropes = 5;
98   const int num_rope_objects = 15;
99   const int num_objects = num_ropes * num_rope_objects;
100   const int num_sticks = num_ropes * (num_rope_objects - 1);
101   const int stick_length = 10;
102   const int rope_offset = 30;
103   double x, y;
104   int i, j, index, stick_index;
105
106   memset (model, 0, sizeof *model);
107   model->objects = g_new (Object, num_objects);
108   model->num_objects = num_objects;
109   model->sticks = g_new (Stick, num_sticks);
110   model->num_sticks = num_sticks;
111   model->offsets = g_new (Offset, 1);
112   model->num_offsets = 1;
113   model_init_polygons (model);
114
115   model->offsets[0].num_objects = num_ropes;
116   model->offsets[0].objects = g_new (Object *, num_ropes);
117   model->offsets[0].dx = rope_offset;
118   model->offsets[0].dy = 0;
119
120   for (i = 0; i < num_ropes; i++) {
121     for (j = 0; j < num_rope_objects; j++) {
122       x = 200 + i * rope_offset;
123       y = 40 + j * stick_length;
124       index = i * num_rope_objects + j;
125       object_init (&model->objects[index], x, y, 1);
126
127       if (j + 1 < num_rope_objects) {
128         stick_index = i * (num_rope_objects - 1) + j;
129         model->sticks[stick_index].a = &model->objects[index];
130         model->sticks[stick_index].b = &model->objects[index + 1];
131         model->sticks[stick_index].length = stick_length;
132       }
133     }
134
135     model->offsets[0].objects[i] = &model->objects[i * num_rope_objects];
136   }
137 }
138
139 static void
140 model_init_grid (Model *model)
141 {
142   const int num_ropes = 8;
143   const int num_rope_objects = 8;
144   const int num_objects = num_ropes * num_rope_objects;
145   const int num_strings = num_ropes * (num_rope_objects - 1) +
146     (num_ropes - 1) * num_rope_objects;
147   const int string_length = 20;
148   const int rope_offset = 20;
149   Object *object;
150   String *string;
151   int i, j;
152
153   memset (model, 0, sizeof *model);
154   model->objects = g_new (Object, num_objects);
155   model->num_objects = num_objects;
156   model->strings = g_new (String, num_strings);
157   model->num_strings = num_strings;
158   model->offsets = g_new (Offset, 1);
159   model->num_offsets = 1;
160   model_init_polygons (model);
161
162   model->offsets[0].num_objects = num_ropes;
163   model->offsets[0].objects = g_new (Object *, num_ropes);
164   model->offsets[0].dx = rope_offset;
165   model->offsets[0].dy = 0;
166
167   object = model->objects;
168   string = model->strings;
169   for (i = 0; i < num_ropes; i++) {
170     for (j = 0; j < num_rope_objects; j++) {
171       object_init (object, 200 + i * rope_offset, 40 + j * string_length, 1);
172
173       if (i + 1 < num_ropes)
174         string_init (string++,
175                      object, object + num_rope_objects, string_length);
176       if (j + 1 < num_rope_objects)
177         string_init (string++, object, object + 1, string_length);
178       object++;
179     }
180
181     model->offsets[0].objects[i] = &model->objects[i * num_rope_objects];
182   }
183 }
184
185 static void
186 model_init_molecule (Model *model)
187 {
188   const int num_objects = 8;
189   const int num_springs = num_objects * 2;
190   const int spring_length = 50;
191   int i;
192   Spring *spring;
193
194   memset (model, 0, sizeof *model);
195   model->objects = g_new (Object, num_objects);
196   model->num_objects = num_objects;
197   model->springs = g_new (Spring, num_springs);
198   model->num_springs = num_springs;
199   model->k = 2;
200
201   for (i = 0; i < num_objects; i++)
202     object_init (&model->objects[i], 200 + i * 20, 200, 0);
203
204   spring = model->springs;
205   for (i = 0; i < num_objects; i++) {
206     spring_init (spring++, &model->objects[i],
207                  &model->objects[(i + 1) % num_objects],
208                  spring_length);
209     spring_init (spring++, &model->objects[i],
210                  &model->objects[(i + 2) % num_objects],
211                  spring_length);
212   }
213 }
214
215
216 static void
217 model_init_wobbly (Model *model)
218 {
219   const int width = 8, height = 8;
220   const int num_objects = width * height;
221   const int num_offset_springs = (width - 1) * height + width * (height - 1);
222   const int distance = 30;
223   double x, y;
224   int i, j;
225   Object *object;
226   OffsetSpring *spring;
227
228   memset (model, 0, sizeof *model);
229   model->objects = g_new (Object, num_objects);
230   model->num_objects = num_objects;
231   model->offset_springs = g_new (OffsetSpring, num_offset_springs);
232   model->num_offset_springs = num_offset_springs;
233   model->k = 4.5;
234
235   model_init_polygons (model);
236
237   object = model->objects;
238   spring = model->offset_springs;
239   for (i = 0; i < width; i++) {
240     for (j = 0; j < height; j++) {
241       x = 200 + i * distance;
242       y = 40 + j * distance;
243       object_init (object, x, y, 0);
244
245       if (i + 1 < width)
246         offset_spring_init (spring++, object, object + height, distance, 0);
247       
248       if (j + 1 < height)
249         offset_spring_init (spring++, object, object + 1, 0, distance);
250
251       object++;
252     }
253   }
254 }
255
256 static void
257 model_init_dock (Model *model)
258 {
259   const int num_objects = 8;
260   const int num_spacers = (num_objects - 1) * (num_objects - 2) / 2;
261   const int num_springs = num_objects - 1;
262   const int distance = 30;
263   double x, y;
264   int i, j;
265   Object *object;
266   Spring *spring;
267   Spacer *spacer;
268
269   memset (model, 0, sizeof *model);
270   model->objects = g_new (Object, num_objects);
271   model->num_objects = num_objects;
272   model->springs = g_new (Spring, num_springs);
273   model->num_springs = num_springs;
274   model->spacers = g_new (Spacer, num_spacers);
275   model->num_spacers = num_spacers;
276   model->anchors = g_new (Anchor, 1);
277   model->num_anchors = 1;
278   model->k = 0.1;
279
280   model->polygons = g_new (Polygon, 1);
281   model->num_polygons = 1;
282   polygon_init_rectangle (&model->polygons[0], -400, 300, 1400, 350);
283
284   model->anchors[0].x = 300;
285   model->anchors[0].y = 300;
286   model->anchors[0].object = &model->objects[0];
287
288   object = model->objects;
289   spring = model->springs;
290   spacer = model->spacers;
291   for (i = 0; i < num_objects; i++, object++) {
292     x = 200 + i * distance / 3;
293     y = 40;
294     object_init (object, x, y, 1);
295     if (i == 0)
296       continue;
297     spring_init (spring++,
298                  &model->objects[0],
299                  &model->objects[i],
300                  distance);
301     for (j = 1; j < num_objects - i; j++) {
302       spacer_init (spacer++, object, object + j, distance);
303     }
304   }
305 }
306
307 typedef struct _Color Color;
308 struct _Color {
309   double red, green, blue;
310 };
311
312 static void
313 draw_sticks (cairo_t *cr,
314              Model   *model,
315              Color   *color)
316 {
317   int i;
318
319   cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1);
320   cairo_new_path (cr);
321   cairo_set_line_width (cr, 2);
322   cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
323   cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
324
325   for (i = 0; i < model->num_sticks; i++) {
326     cairo_move_to (cr,
327                    model->sticks[i].a->position.x,
328                    model->sticks[i].a->position.y);
329     cairo_line_to (cr,
330                    model->sticks[i].b->position.x,
331                    model->sticks[i].b->position.y);
332   }
333
334   cairo_stroke (cr);
335 }
336
337 static void
338 draw_strings (cairo_t *cr,
339               Model   *model,
340               Color   *color)
341 {
342   int i;
343
344   cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1);
345   cairo_new_path (cr);
346   cairo_set_line_width (cr, 1);
347   cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
348   cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
349
350   for (i = 0; i < model->num_strings; i++) {
351     cairo_move_to (cr,
352                    model->strings[i].a->position.x,
353                    model->strings[i].a->position.y);
354     cairo_line_to (cr,
355                    model->strings[i].b->position.x,
356                    model->strings[i].b->position.y);
357   }
358
359   cairo_stroke (cr);
360 }
361
362 static void
363 draw_offsets (cairo_t *cr,
364               Model   *model,
365               Color   *color)
366 {
367   int i, j;
368
369   cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.5);
370   cairo_new_path (cr);
371   cairo_set_line_width (cr, 4);
372   cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
373   cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
374
375   for (i = 0; i < model->num_offsets; i++) {
376     for (j = 0; j < model->offsets[i].num_objects; j++) {
377       cairo_line_to (cr,
378                      model->offsets[i].objects[j]->position.x,
379                      model->offsets[i].objects[j]->position.y);
380     }
381     cairo_stroke (cr);
382   }
383
384 }
385
386 static void
387 draw_springs (cairo_t *cr,
388               Model   *model,
389               Color   *color)
390 {
391   int i;
392
393   cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
394   cairo_new_path (cr);
395   cairo_set_line_width (cr, 2);
396   cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
397   cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
398
399   for (i = 0; i < model->num_springs; i++) {
400     cairo_move_to (cr,
401                    model->springs[i].a->position.x,
402                    model->springs[i].a->position.y);
403     cairo_line_to (cr,
404                    model->springs[i].b->position.x,
405                    model->springs[i].b->position.y);
406   }
407
408   cairo_stroke (cr);
409 }
410
411 static void
412 draw_offset_springs (cairo_t *cr,
413                      Model   *model,
414                      Color   *color)
415 {
416   int i;
417
418   cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
419   cairo_new_path (cr);
420   cairo_set_line_width (cr, 2);
421   cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
422   cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
423
424   for (i = 0; i < model->num_offset_springs; i++) {
425     cairo_move_to (cr,
426                    model->offset_springs[i].a->position.x,
427                    model->offset_springs[i].a->position.y);
428     cairo_line_to (cr,
429                    model->offset_springs[i].b->position.x,
430                    model->offset_springs[i].b->position.y);
431   }
432
433   cairo_stroke (cr);
434 }
435
436 static void
437 draw_polygons (cairo_t *cr, Model *model, Color *color)
438 {
439   Polygon *p;
440   int i, j;
441
442   for (i = 0; i < model->num_polygons; i++) {
443     p = &model->polygons[i];
444     cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
445
446     
447     for (j = 0; j < p->num_points; j++)
448       cairo_line_to (cr, p->points[j].x, p->points[j].y);
449     cairo_close_path (cr);
450   }
451   cairo_fill (cr);
452
453 }
454
455 static void
456 draw_objects (cairo_t *cr, Model *model, Color *color)
457 {
458   int i;
459
460   cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
461   for (i = 0; i < model->num_objects; i++) {
462     cairo_arc (cr, model->objects[i].position.x,
463                model->objects[i].position.y,
464                3, 0, 2*M_PI);
465     cairo_fill (cr);
466   }
467 }
468
469 static Color blue = { 0, 0, 1 };
470 static Color green = { 0, 1, 0 };
471 static Color red = { 1, 0, 0 };
472 static Color black = { 0, 0, 0 };
473
474 typedef struct _Closure Closure;
475 struct _Closure {
476   GtkWidget *drawing_area;
477   GtkWidget *fps_label;
478   Model *model;
479   int frame_count;
480   int i;
481   struct timeval start;
482 };
483
484 static void
485 draw_model (GtkWidget *widget, Model *model)
486 {
487   cairo_t *cr;
488
489   cr = gdk_cairo_create (widget->window);
490
491   cairo_set_source_rgb (cr, 1, 1, 1);
492   cairo_paint (cr);
493
494   draw_polygons (cr, model, &blue);
495   draw_sticks (cr, model, &black);
496   draw_strings (cr, model, &green);
497   draw_springs (cr, model, &black);
498   draw_offsets (cr, model, &blue);
499   draw_offset_springs (cr, model, &blue);
500   draw_objects (cr, model, &red);
501
502   cairo_destroy (cr);
503 }
504
505 static gboolean
506 expose_event (GtkWidget      *widget,
507               GdkEventExpose *event,
508               gpointer        data)
509 {
510   Closure *closure = data;
511
512   draw_model (widget, closure->model);
513
514   return TRUE;
515 }
516
517 static gboolean
518 button_press_event (GtkWidget      *widget,
519                     GdkEventButton *event,
520                     gpointer        data)
521 {
522   Closure *closure = data;
523
524   if (event->button != 1)
525     return TRUE;
526
527   closure->model->mouse_anchor.x = event->x;
528   closure->model->mouse_anchor.y = event->y;
529   closure->model->mouse_anchor.object =
530     model_find_nearest (closure->model, event->x, event->y);
531
532   return TRUE;
533 }
534
535 static gboolean
536 button_release_event (GtkWidget      *widget,
537                       GdkEventButton *event,
538                       gpointer        data)
539 {
540   Closure *closure = data;
541
542   if ((event->state & GDK_BUTTON1_MASK) == 0)
543     return TRUE;
544
545   closure->model->mouse_anchor.object = NULL;
546
547   return TRUE;
548 }
549
550 static gboolean
551 motion_notify_event (GtkWidget      *widget,
552                      GdkEventMotion *event,
553                      gpointer        data)
554 {
555   Closure *closure = data;
556   int x, y;
557   GdkModifierType state;
558
559   gdk_window_get_pointer (event->window, &x, &y, &state);
560   
561   closure->model->mouse_anchor.x = x + 0.5;
562   closure->model->mouse_anchor.y = y + 0.5;
563
564   return TRUE;
565 }
566
567 typedef void (*ModelInitFunc) (Model *model);
568
569 static void
570 model_changed (GtkComboBox *combo, gpointer user_data)
571 {
572   Closure *closure = user_data;
573   GtkTreeIter iter;
574   GtkTreeModel *tree_model;
575   ModelInitFunc init;
576   char *name;
577
578   tree_model = gtk_combo_box_get_model (combo);
579   if (!gtk_combo_box_get_active_iter (combo, &iter))
580     return;
581
582   gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1);
583
584   model_fini (closure->model);
585   (*init) (closure->model);
586 }
587
588 static GtkTreeModel *
589 create_model_store (void)
590 {
591   static struct {
592     const char *name;
593     ModelInitFunc init;
594   } models[] = {
595     { "Rope", model_init_rope },
596     { "Snake", model_init_snake },
597     { "Curtain", model_init_curtain },
598     { "Grid", model_init_grid },
599     { "Molecule", model_init_molecule },
600     { "Wobbly", model_init_wobbly },
601     { "Dock", model_init_dock }
602   };
603
604   GtkTreeIter iter;
605   GtkTreeStore *store;
606   gint i;
607
608   store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
609
610   for (i = 0; i < G_N_ELEMENTS(models); i++) {
611     gtk_tree_store_append (store, &iter, NULL);
612     gtk_tree_store_set (store, &iter,
613                         0, models[i].name, 1, models[i].init, -1);
614  }
615   
616   return GTK_TREE_MODEL (store);
617
618 }
619
620 static GtkWidget *
621 create_model_combo (Closure *closure)
622 {
623   GtkWidget *hbox;
624   GtkWidget *combo, *label;
625   GtkTreeModel *store;
626   GtkCellRenderer *renderer;
627
628   hbox = gtk_hbox_new (FALSE, 8);
629
630   label = gtk_label_new_with_mnemonic ("_Model:");
631   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
632
633   store = create_model_store ();
634   combo = gtk_combo_box_new_with_model (store);
635   gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
636   g_object_unref (store);
637
638   renderer = gtk_cell_renderer_text_new ();
639   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
640   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
641                                   "text", 0,
642                                   NULL);
643
644   gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
645   gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0);
646   g_signal_connect (combo, "changed",
647                     G_CALLBACK (model_changed), closure);
648
649   label = gtk_label_new ("Frames per second: 0");
650   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
651
652   closure->fps_label = label;
653
654   return hbox;
655 }
656
657 static void
658 create_window (Closure *closure)
659 {
660   GtkWidget *window;
661   GtkWidget *frame;
662   GtkWidget *vbox;
663   GtkWidget *da;
664   GtkWidget *model_combo;
665
666   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
667   gtk_window_set_title (GTK_WINDOW (window), "Akamaru");
668
669   g_signal_connect (window, "destroy",
670                     G_CALLBACK (gtk_main_quit), &window);
671
672   gtk_container_set_border_width (GTK_CONTAINER (window), 8);
673
674   vbox = gtk_vbox_new (FALSE, 8);
675   gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
676   gtk_container_add (GTK_CONTAINER (window), vbox);
677
678   /*
679    * Create the drawing area
680    */
681       
682   frame = gtk_frame_new (NULL);
683   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
684   gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
685       
686   da = gtk_drawing_area_new ();
687   /* set a minimum size */
688   gtk_widget_set_size_request (da, 200, 200);
689
690   gtk_container_add (GTK_CONTAINER (frame), da);
691
692   /* Signals used to handle backing pixmap */
693       
694   g_signal_connect (da, "expose_event",
695                     G_CALLBACK (expose_event), closure);
696       
697   /* Event signals */
698       
699   g_signal_connect (da, "motion_notify_event",
700                     G_CALLBACK (motion_notify_event), closure);
701   g_signal_connect (da, "button_press_event",
702                     G_CALLBACK (button_press_event), closure);
703   g_signal_connect (da, "button_release_event",
704                     G_CALLBACK (button_release_event), closure);
705
706   /* Ask to receive events the drawing area doesn't normally
707    * subscribe to
708    */
709   gtk_widget_set_events (da, gtk_widget_get_events (da)
710                          | GDK_LEAVE_NOTIFY_MASK
711                          | GDK_BUTTON_PRESS_MASK
712                          | GDK_BUTTON_RELEASE_MASK
713                          | GDK_POINTER_MOTION_MASK
714                          | GDK_POINTER_MOTION_HINT_MASK);
715
716   model_combo = create_model_combo (closure);
717   gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0);
718
719   closure->drawing_area = da;
720 }
721
722 static gint
723 timeout_callback (gpointer data)
724 {
725   Closure *closure = data;
726
727   model_step (closure->model, 0.2);
728
729   closure->i++;
730   if (closure->i == 1) {
731     gtk_widget_queue_draw (closure->drawing_area);
732     closure->i = 0;
733     closure->frame_count++;
734   }
735
736   if (closure->frame_count == 200) {
737     struct timeval end, elapsed;
738     double total;
739     char text[50];
740
741     closure->frame_count = 0;
742     gettimeofday (&end, NULL);
743     if (closure->start.tv_usec > end.tv_usec) {
744       end.tv_usec += 1000000;
745       end.tv_sec--;
746     }
747
748     elapsed.tv_usec = end.tv_usec - closure->start.tv_usec;
749     elapsed.tv_sec = end.tv_sec - closure->start.tv_sec;
750
751     total = elapsed.tv_sec + ((double) elapsed.tv_usec / 1e6);
752     if (total < 0) {
753       total = 0;
754     }
755     closure->start = end;
756     snprintf (text, sizeof text, "Frames per second: %.2f", 200 / total);
757     gtk_label_set_text (GTK_LABEL (closure->fps_label), text);
758   }
759
760   return TRUE;
761 }
762
763 int
764 main (int argc, char *argv[])
765 {
766   Closure closure;
767   Model model;
768
769   gtk_init (&argc, &argv);
770   model_init_rope (&model);
771   create_window (&closure);
772   closure.i = 0;
773   gtk_widget_show_all (gtk_widget_get_toplevel (closure.drawing_area));
774   closure.model = &model;
775   closure.frame_count = 0;
776   gettimeofday (&closure.start, NULL);
777   g_timeout_add (40, timeout_callback, &closure);
778   gtk_main ();
779
780   return 0;
781 }