]> git.cworth.org Git - akamaru/blob - main.c
Indent to 8 columns rather than 2
[akamaru] / main.c
1 /*                                           -*- mode: c; c-basic-offset: 8 -*-
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_enclosing_rectangle (&model->polygons[0], 10, 10, 700, 500);
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     
445                 for (j = 0; j < p->num_points; j++)
446                         cairo_line_to (cr, p->points[j].x, p->points[j].y);
447                 cairo_close_path (cr);
448
449                 if (p->enclosing) {
450                         cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.1);
451                         cairo_fill_preserve (cr);
452                 }
453
454                 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
455                 if (p->enclosing)
456                         cairo_stroke (cr);
457                 else
458                         cairo_fill (cr);
459         }
460
461 }
462
463 static void
464 draw_objects (cairo_t *cr, Model *model, Color *color)
465 {
466         int i;
467
468         cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
469         for (i = 0; i < model->num_objects; i++) {
470                 cairo_arc (cr, model->objects[i].position.x,
471                            model->objects[i].position.y,
472                            3, 0, 2*M_PI);
473                 cairo_fill (cr);
474         }
475 }
476
477 static Color blue = { 0, 0, 1 };
478 static Color green = { 0, 1, 0 };
479 static Color red = { 1, 0, 0 };
480 static Color black = { 0, 0, 0 };
481
482 typedef struct _Closure Closure;
483 struct _Closure {
484         GtkWidget *drawing_area;
485         GtkWidget *fps_label;
486         Model *model;
487         int frame_count;
488         int i;
489         struct timeval start;
490 };
491
492 static void
493 draw_model (GtkWidget *widget, Model *model)
494 {
495         cairo_t *cr;
496
497         cr = gdk_cairo_create (widget->window);
498
499         cairo_set_source_rgb (cr, 1, 1, 1);
500         cairo_paint (cr);
501
502         draw_polygons (cr, model, &blue);
503         draw_sticks (cr, model, &black);
504         draw_strings (cr, model, &green);
505         draw_springs (cr, model, &black);
506         draw_offsets (cr, model, &blue);
507         draw_offset_springs (cr, model, &blue);
508         draw_objects (cr, model, &red);
509
510         cairo_destroy (cr);
511 }
512
513 static gboolean
514 expose_event (GtkWidget      *widget,
515               GdkEventExpose *event,
516               gpointer        data)
517 {
518         Closure *closure = data;
519
520         draw_model (widget, closure->model);
521
522         return TRUE;
523 }
524
525 static gboolean
526 button_press_event (GtkWidget      *widget,
527                     GdkEventButton *event,
528                     gpointer        data)
529 {
530         Closure *closure = data;
531
532         if (event->button != 1)
533                 return TRUE;
534
535         closure->model->mouse_anchor.x = event->x;
536         closure->model->mouse_anchor.y = event->y;
537         closure->model->mouse_anchor.object =
538                 model_find_nearest (closure->model, event->x, event->y);
539
540         return TRUE;
541 }
542
543 static gboolean
544 button_release_event (GtkWidget      *widget,
545                       GdkEventButton *event,
546                       gpointer        data)
547 {
548         Closure *closure = data;
549
550         if ((event->state & GDK_BUTTON1_MASK) == 0)
551                 return TRUE;
552
553         closure->model->mouse_anchor.object = NULL;
554
555         return TRUE;
556 }
557
558 static gboolean
559 motion_notify_event (GtkWidget      *widget,
560                      GdkEventMotion *event,
561                      gpointer        data)
562 {
563         Closure *closure = data;
564         int x, y;
565         GdkModifierType state;
566
567         gdk_window_get_pointer (event->window, &x, &y, &state);
568   
569         closure->model->mouse_anchor.x = x + 0.5;
570         closure->model->mouse_anchor.y = y + 0.5;
571
572         return TRUE;
573 }
574
575 typedef void (*ModelInitFunc) (Model *model);
576
577 static void
578 model_changed (GtkComboBox *combo, gpointer user_data)
579 {
580         Closure *closure = user_data;
581         GtkTreeIter iter;
582         GtkTreeModel *tree_model;
583         ModelInitFunc init;
584         char *name;
585
586         tree_model = gtk_combo_box_get_model (combo);
587         if (!gtk_combo_box_get_active_iter (combo, &iter))
588                 return;
589
590         gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1);
591
592         model_fini (closure->model);
593         (*init) (closure->model);
594 }
595
596 static GtkTreeModel *
597 create_model_store (void)
598 {
599         static struct {
600                 const char *name;
601                 ModelInitFunc init;
602         } models[] = {
603                 { "Rope", model_init_rope },
604                 { "Snake", model_init_snake },
605                 { "Curtain", model_init_curtain },
606                 { "Grid", model_init_grid },
607                 { "Molecule", model_init_molecule },
608                 { "Wobbly", model_init_wobbly },
609                 { "Dock", model_init_dock }
610         };
611
612         GtkTreeIter iter;
613         GtkTreeStore *store;
614         gint i;
615
616         store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
617
618         for (i = 0; i < G_N_ELEMENTS(models); i++) {
619                 gtk_tree_store_append (store, &iter, NULL);
620                 gtk_tree_store_set (store, &iter,
621                                     0, models[i].name, 1, models[i].init, -1);
622         }
623   
624         return GTK_TREE_MODEL (store);
625
626 }
627
628 static GtkWidget *
629 create_model_combo (Closure *closure)
630 {
631         GtkWidget *hbox;
632         GtkWidget *combo, *label;
633         GtkTreeModel *store;
634         GtkCellRenderer *renderer;
635
636         hbox = gtk_hbox_new (FALSE, 8);
637
638         label = gtk_label_new_with_mnemonic ("_Model:");
639         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
640
641         store = create_model_store ();
642         combo = gtk_combo_box_new_with_model (store);
643         gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
644         g_object_unref (store);
645
646         renderer = gtk_cell_renderer_text_new ();
647         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
648         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
649                                         "text", 0,
650                                         NULL);
651
652         gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
653         gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0);
654         g_signal_connect (combo, "changed",
655                           G_CALLBACK (model_changed), closure);
656
657         label = gtk_label_new ("Frames per second: 0");
658         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
659
660         closure->fps_label = label;
661
662         return hbox;
663 }
664
665 static void
666 create_window (Closure *closure)
667 {
668         GtkWidget *window;
669         GtkWidget *frame;
670         GtkWidget *vbox;
671         GtkWidget *da;
672         GtkWidget *model_combo;
673
674         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
675         gtk_window_set_title (GTK_WINDOW (window), "赤丸");
676
677         g_signal_connect (window, "destroy",
678                           G_CALLBACK (gtk_main_quit), &window);
679
680         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
681
682         vbox = gtk_vbox_new (FALSE, 8);
683         gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
684         gtk_container_add (GTK_CONTAINER (window), vbox);
685
686         /*
687          * Create the drawing area
688          */
689       
690         frame = gtk_frame_new (NULL);
691         gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
692         gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
693       
694         da = gtk_drawing_area_new ();
695         /* set a minimum size */
696         gtk_widget_set_size_request (da, 200, 200);
697
698         gtk_container_add (GTK_CONTAINER (frame), da);
699
700         /* Signals used to handle backing pixmap */
701       
702         g_signal_connect (da, "expose_event",
703                           G_CALLBACK (expose_event), closure);
704       
705         /* Event signals */
706       
707         g_signal_connect (da, "motion_notify_event",
708                           G_CALLBACK (motion_notify_event), closure);
709         g_signal_connect (da, "button_press_event",
710                           G_CALLBACK (button_press_event), closure);
711         g_signal_connect (da, "button_release_event",
712                           G_CALLBACK (button_release_event), closure);
713
714         /* Ask to receive events the drawing area doesn't normally
715          * subscribe to
716          */
717         gtk_widget_set_events (da, gtk_widget_get_events (da)
718                                | GDK_LEAVE_NOTIFY_MASK
719                                | GDK_BUTTON_PRESS_MASK
720                                | GDK_BUTTON_RELEASE_MASK
721                                | GDK_POINTER_MOTION_MASK
722                                | GDK_POINTER_MOTION_HINT_MASK);
723
724         model_combo = create_model_combo (closure);
725         gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0);
726
727         closure->drawing_area = da;
728 }
729
730 static gint
731 timeout_callback (gpointer data)
732 {
733         Closure *closure = data;
734
735         model_step (closure->model, 0.2);
736
737         closure->i++;
738         if (closure->i == 1) {
739                 gtk_widget_queue_draw (closure->drawing_area);
740                 closure->i = 0;
741                 closure->frame_count++;
742         }
743
744         if (closure->frame_count == 200) {
745                 struct timeval end, elapsed;
746                 double total;
747                 char text[50];
748
749                 closure->frame_count = 0;
750                 gettimeofday (&end, NULL);
751                 if (closure->start.tv_usec > end.tv_usec) {
752                         end.tv_usec += 1000000;
753                         end.tv_sec--;
754                 }
755
756                 elapsed.tv_usec = end.tv_usec - closure->start.tv_usec;
757                 elapsed.tv_sec = end.tv_sec - closure->start.tv_sec;
758
759                 total = elapsed.tv_sec + ((double) elapsed.tv_usec / 1e6);
760                 if (total < 0) {
761                         total = 0;
762                 }
763                 closure->start = end;
764                 snprintf (text, sizeof text, "Frames per second: %.2f", 200 / total);
765                 gtk_label_set_text (GTK_LABEL (closure->fps_label), text);
766         }
767
768         return TRUE;
769 }
770
771 int
772 main (int argc, char *argv[])
773 {
774         Closure closure;
775         Model model;
776
777         gtk_init (&argc, &argv);
778         model_init_rope (&model);
779         create_window (&closure);
780         closure.i = 0;
781         gtk_widget_show_all (gtk_widget_get_toplevel (closure.drawing_area));
782         closure.model = &model;
783         closure.frame_count = 0;
784         gettimeofday (&closure.start, NULL);
785         g_timeout_add (40, timeout_callback, &closure);
786         gtk_main ();
787
788         return 0;
789 }