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