1 /* -*- mode: c; c-basic-offset: 2 -*-
4 * gcc -Wall -g $(pkg-config --cflags --libs gtk+-2.0 cairo) \
5 * akamaru.c main.c -o akamaru
10 #include <cairo-xlib.h>
20 model_init_polygons (Model *model)
22 const int num_polygons = 5;
23 const double ground_level = 500;
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);
32 polygon_init_rectangle (&model->polygons[4], 300, 320, 400, 350);
34 model->num_polygons = num_polygons;
38 model_init_snake (Model *model)
40 const int num_objects = 20;
41 const int num_sticks = num_objects * 2 - 3;
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);
51 for (i = 0; i < num_objects; i++) {
52 object_init (&model->objects[i],
53 random() % 200 + 20, random() % 200 + 20, 1);
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;
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;
67 model->anchor_object = NULL;
71 model_init_rope (Model *model)
73 const int num_objects = 20;
74 const int num_sticks = num_objects - 1;
75 const int stick_length = 10;
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);
85 for (i = 0; i < num_objects; i++) {
86 object_init (&model->objects[i], 200, 40 + i * stick_length, 1);
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;
95 model->anchor_object = NULL;
99 model_init_curtain (Model *model)
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;
108 int i, j, index, stick_index;
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);
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;
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);
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;
139 model->offsets[0].objects[i] = &model->objects[i * num_rope_objects];
142 model->anchor_object = NULL;
146 model_init_grid (Model *model)
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;
159 memset (model, 0, sizeof *model);
160 model->objects = g_new (Object, num_objects);
161 model->num_objects = num_objects;
162 model->strings = g_new (String, num_strings);
163 model->num_strings = num_strings;
164 model->offsets = g_new (Offset, 1);
165 model->num_offsets = 1;
166 model_init_polygons (model);
168 model->offsets[0].num_objects = num_ropes;
169 model->offsets[0].objects = g_new (Object *, num_ropes);
170 model->offsets[0].dx = rope_offset;
171 model->offsets[0].dy = 0;
173 object = model->objects;
174 string = model->strings;
175 for (i = 0; i < num_ropes; i++) {
176 for (j = 0; j < num_rope_objects; j++) {
177 object_init (object, 200 + i * rope_offset, 40 + j * string_length, 1);
179 if (i + 1 < num_ropes)
180 string_init (string++,
181 object, object + num_rope_objects, string_length);
182 if (j + 1 < num_rope_objects)
183 string_init (string++, object, object + 1, string_length);
187 model->offsets[0].objects[i] = &model->objects[i * num_rope_objects];
190 model->anchor_object = NULL;
194 model_init_molecule (Model *model)
196 const int num_objects = 8;
197 const int num_springs = num_objects * 2;
198 const int spring_length = 50;
202 memset (model, 0, sizeof *model);
203 model->objects = g_new (Object, num_objects);
204 model->num_objects = num_objects;
205 model->springs = g_new (Spring, num_springs);
206 model->num_springs = num_springs;
209 for (i = 0; i < num_objects; i++)
210 object_init (&model->objects[i], 200 + i * 20, 200, 0);
212 spring = model->springs;
213 for (i = 0; i < num_objects; i++) {
214 spring_init (spring++, &model->objects[i],
215 &model->objects[(i + 1) % num_objects],
217 spring_init (spring++, &model->objects[i],
218 &model->objects[(i + 2) % num_objects],
225 model_init_wobbly (Model *model)
227 const int width = 8, height = 8;
228 const int num_objects = width * height;
229 const int num_offset_springs = (width - 1) * height + width * (height - 1);
230 const int distance = 30;
234 OffsetSpring *spring;
236 memset (model, 0, sizeof *model);
237 model->objects = g_new (Object, num_objects);
238 model->num_objects = num_objects;
239 model->offset_springs = g_new (OffsetSpring, num_offset_springs);
240 model->num_offset_springs = num_offset_springs;
243 model_init_polygons (model);
245 object = model->objects;
246 spring = model->offset_springs;
247 for (i = 0; i < width; i++) {
248 for (j = 0; j < height; j++) {
249 x = 200 + i * distance;
250 y = 40 + j * distance;
251 object_init (object, x, y, 0);
254 offset_spring_init (spring++, object, object + height, distance, 0);
257 offset_spring_init (spring++, object, object + 1, 0, distance);
265 model_init_dock (Model *model)
267 const int num_objects = 8;
268 const int num_spacers = (num_objects - 1) * (num_objects - 2) / 2;
269 const int num_springs = num_objects - 1;
270 const int distance = 30;
277 memset (model, 0, sizeof *model);
278 model->objects = g_new (Object, num_objects);
279 model->num_objects = num_objects;
280 model->springs = g_new (Spring, num_springs);
281 model->num_springs = num_springs;
282 model->spacers = g_new (Spacer, num_spacers);
283 model->num_spacers = num_spacers;
286 model_init_polygons (model);
287 model->polygons = g_new (Polygon, 1);
288 polygon_init_rectangle (&model->polygons[0], -400, 300, 1400, 350);
289 model->num_polygons = 1;
292 object = model->objects;
293 spring = model->springs;
294 spacer = model->spacers;
295 for (i = 0; i < num_objects; i++, object++) {
296 x = 200 + i * distance / 3;
298 object_init (object, x, y, 1);
301 spring_init (spring++,
305 for (j = 1; j < num_objects - i; j++) {
306 spacer_init (spacer++, object, object + j, distance);
311 typedef struct _Color Color;
313 double red, green, blue;
317 draw_sticks (cairo_t *cr,
323 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1);
325 cairo_set_line_width (cr, 2);
326 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
327 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
329 for (i = 0; i < model->num_sticks; i++) {
331 model->sticks[i].a->position.x,
332 model->sticks[i].a->position.y);
334 model->sticks[i].b->position.x,
335 model->sticks[i].b->position.y);
342 draw_strings (cairo_t *cr,
348 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1);
350 cairo_set_line_width (cr, 1);
351 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
352 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
354 for (i = 0; i < model->num_strings; i++) {
356 model->strings[i].a->position.x,
357 model->strings[i].a->position.y);
359 model->strings[i].b->position.x,
360 model->strings[i].b->position.y);
367 draw_offsets (cairo_t *cr,
373 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.5);
375 cairo_set_line_width (cr, 4);
376 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
377 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
379 for (i = 0; i < model->num_offsets; i++) {
380 for (j = 0; j < model->offsets[i].num_objects; j++) {
382 model->offsets[i].objects[j]->position.x,
383 model->offsets[i].objects[j]->position.y);
391 draw_springs (cairo_t *cr,
397 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
399 cairo_set_line_width (cr, 2);
400 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
401 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
403 for (i = 0; i < model->num_springs; i++) {
405 model->springs[i].a->position.x,
406 model->springs[i].a->position.y);
408 model->springs[i].b->position.x,
409 model->springs[i].b->position.y);
416 draw_offset_springs (cairo_t *cr,
422 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
424 cairo_set_line_width (cr, 2);
425 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
426 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
428 for (i = 0; i < model->num_offset_springs; i++) {
430 model->offset_springs[i].a->position.x,
431 model->offset_springs[i].a->position.y);
433 model->offset_springs[i].b->position.x,
434 model->offset_springs[i].b->position.y);
441 draw_polygons (cairo_t *cr, Model *model, Color *color)
446 for (i = 0; i < model->num_polygons; i++) {
447 p = &model->polygons[i];
448 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
451 for (j = 0; j < p->num_points; j++)
452 cairo_line_to (cr, p->points[j].x, p->points[j].y);
453 cairo_close_path (cr);
460 draw_objects (cairo_t *cr, Model *model, Color *color)
464 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
465 for (i = 0; i < model->num_objects; i++) {
466 cairo_arc (cr, model->objects[i].position.x,
467 model->objects[i].position.y,
473 static Color blue = { 0, 0, 1 };
474 static Color green = { 0, 1, 0 };
475 static Color red = { 1, 0, 0 };
476 static Color black = { 0, 0, 0 };
478 typedef struct _Closure Closure;
480 GtkWidget *drawing_area;
481 GtkWidget *fps_label;
485 struct timeval start;
489 draw_model (GtkWidget *widget, Model *model)
493 cr = gdk_cairo_create (widget->window);
495 cairo_set_source_rgb (cr, 1, 1, 1);
498 draw_polygons (cr, model, &blue);
499 draw_sticks (cr, model, &black);
500 draw_strings (cr, model, &green);
501 draw_springs (cr, model, &black);
502 draw_offsets (cr, model, &blue);
503 draw_offset_springs (cr, model, &blue);
504 draw_objects (cr, model, &red);
510 expose_event (GtkWidget *widget,
511 GdkEventExpose *event,
514 Closure *closure = data;
516 draw_model (widget, closure->model);
522 button_press_event (GtkWidget *widget,
523 GdkEventButton *event,
526 Closure *closure = data;
528 if (event->button != 1)
531 closure->model->anchor_position.x = event->x;
532 closure->model->anchor_position.y = event->y;
533 closure->model->anchor_object = model_find_nearest (closure->model,
540 button_release_event (GtkWidget *widget,
541 GdkEventButton *event,
544 Closure *closure = data;
546 if ((event->state & GDK_BUTTON1_MASK) == 0)
549 closure->model->anchor_object = NULL;
555 motion_notify_event (GtkWidget *widget,
556 GdkEventMotion *event,
559 Closure *closure = data;
561 GdkModifierType state;
563 gdk_window_get_pointer (event->window, &x, &y, &state);
565 closure->model->anchor_position.x = x + 0.5;
566 closure->model->anchor_position.y = y + 0.5;
571 typedef void (*ModelInitFunc) (Model *model);
574 model_changed (GtkComboBox *combo, gpointer user_data)
576 Closure *closure = user_data;
578 GtkTreeModel *tree_model;
582 tree_model = gtk_combo_box_get_model (combo);
583 if (!gtk_combo_box_get_active_iter (combo, &iter))
586 gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1);
588 model_fini (closure->model);
589 (*init) (closure->model);
592 static GtkTreeModel *
593 create_model_store (void)
599 { "Rope", model_init_rope },
600 { "Snake", model_init_snake },
601 { "Curtain", model_init_curtain },
602 { "Grid", model_init_grid },
603 { "Molecule", model_init_molecule },
604 { "Wobbly", model_init_wobbly },
605 { "Dock", model_init_dock }
612 store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
614 for (i = 0; i < G_N_ELEMENTS(models); i++) {
615 gtk_tree_store_append (store, &iter, NULL);
616 gtk_tree_store_set (store, &iter,
617 0, models[i].name, 1, models[i].init, -1);
620 return GTK_TREE_MODEL (store);
625 create_model_combo (Closure *closure)
628 GtkWidget *combo, *label;
630 GtkCellRenderer *renderer;
632 hbox = gtk_hbox_new (FALSE, 8);
634 label = gtk_label_new_with_mnemonic ("_Model:");
635 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
637 store = create_model_store ();
638 combo = gtk_combo_box_new_with_model (store);
639 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
640 g_object_unref (store);
642 renderer = gtk_cell_renderer_text_new ();
643 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
644 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
648 gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
649 gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0);
650 g_signal_connect (combo, "changed",
651 G_CALLBACK (model_changed), closure);
653 label = gtk_label_new ("Frames per second: 0");
654 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
656 closure->fps_label = label;
662 create_window (Closure *closure)
668 GtkWidget *model_combo;
670 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
671 gtk_window_set_title (GTK_WINDOW (window), "Akamaru");
673 g_signal_connect (window, "destroy",
674 G_CALLBACK (gtk_main_quit), &window);
676 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
678 vbox = gtk_vbox_new (FALSE, 8);
679 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
680 gtk_container_add (GTK_CONTAINER (window), vbox);
683 * Create the drawing area
686 frame = gtk_frame_new (NULL);
687 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
688 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
690 da = gtk_drawing_area_new ();
691 /* set a minimum size */
692 gtk_widget_set_size_request (da, 200, 200);
694 gtk_container_add (GTK_CONTAINER (frame), da);
696 /* Signals used to handle backing pixmap */
698 g_signal_connect (da, "expose_event",
699 G_CALLBACK (expose_event), closure);
703 g_signal_connect (da, "motion_notify_event",
704 G_CALLBACK (motion_notify_event), closure);
705 g_signal_connect (da, "button_press_event",
706 G_CALLBACK (button_press_event), closure);
707 g_signal_connect (da, "button_release_event",
708 G_CALLBACK (button_release_event), closure);
710 /* Ask to receive events the drawing area doesn't normally
713 gtk_widget_set_events (da, gtk_widget_get_events (da)
714 | GDK_LEAVE_NOTIFY_MASK
715 | GDK_BUTTON_PRESS_MASK
716 | GDK_BUTTON_RELEASE_MASK
717 | GDK_POINTER_MOTION_MASK
718 | GDK_POINTER_MOTION_HINT_MASK);
720 model_combo = create_model_combo (closure);
721 gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0);
723 closure->drawing_area = da;
727 timeout_callback (gpointer data)
729 Closure *closure = data;
731 model_step (closure->model, 0.2);
734 if (closure->i == 1) {
735 gtk_widget_queue_draw (closure->drawing_area);
737 closure->frame_count++;
740 if (closure->frame_count == 200) {
741 struct timeval end, elapsed;
745 closure->frame_count = 0;
746 gettimeofday (&end, NULL);
747 if (closure->start.tv_usec > end.tv_usec) {
748 end.tv_usec += 1000000;
752 elapsed.tv_usec = end.tv_usec - closure->start.tv_usec;
753 elapsed.tv_sec = end.tv_sec - closure->start.tv_sec;
755 total = elapsed.tv_sec + ((double) elapsed.tv_usec / 1e6);
759 closure->start = end;
760 snprintf (text, sizeof text, "Frames per second: %.2f", 200 / total);
761 gtk_label_set_text (GTK_LABEL (closure->fps_label), text);
768 main (int argc, char *argv[])
773 gtk_init (&argc, &argv);
774 model_init_rope (&model);
775 create_window (&closure);
777 gtk_widget_show_all (gtk_widget_get_toplevel (closure.drawing_area));
778 closure.model = &model;
779 closure.frame_count = 0;
780 gettimeofday (&closure.start, NULL);
781 g_timeout_add (40, timeout_callback, &closure);