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;
156 int i, j, index, string_index;
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);
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;
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);
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;
186 if (j + 1 < num_rope_objects) {
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;
195 model->offsets[0].objects[i] = &model->objects[i * num_rope_objects];
198 model->anchor_object = NULL;
202 model_init_molecule (Model *model)
204 const int num_objects = 8;
205 const int num_springs = num_objects * 2;
206 const int spring_length = 50;
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;
217 for (i = 0; i < num_objects; i++)
218 object_init (&model->objects[i], 200 + i * 20, 200, 0);
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],
225 spring_init (spring++, &model->objects[i],
226 &model->objects[(i + 2) % num_objects],
233 model_init_wobbly (Model *model)
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;
242 OffsetSpring *spring;
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;
251 model_init_polygons (model);
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);
262 offset_spring_init (spring++, object, object + height, distance, 0);
265 offset_spring_init (spring++, object, object + 1, 0, distance);
272 typedef struct _Color Color;
274 double red, green, blue;
278 draw_sticks (cairo_t *cr,
284 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1);
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);
290 for (i = 0; i < model->num_sticks; i++) {
292 model->sticks[i].a->position.x,
293 model->sticks[i].a->position.y);
295 model->sticks[i].b->position.x,
296 model->sticks[i].b->position.y);
303 draw_strings (cairo_t *cr,
309 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1);
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);
315 for (i = 0; i < model->num_strings; i++) {
317 model->strings[i].a->position.x,
318 model->strings[i].a->position.y);
320 model->strings[i].b->position.x,
321 model->strings[i].b->position.y);
328 draw_offsets (cairo_t *cr,
334 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.5);
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);
340 for (i = 0; i < model->num_offsets; i++) {
341 for (j = 0; j < model->offsets[i].num_objects; j++) {
343 model->offsets[i].objects[j]->position.x,
344 model->offsets[i].objects[j]->position.y);
352 draw_springs (cairo_t *cr,
358 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
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);
364 for (i = 0; i < model->num_springs; i++) {
366 model->springs[i].a->position.x,
367 model->springs[i].a->position.y);
369 model->springs[i].b->position.x,
370 model->springs[i].b->position.y);
377 draw_offset_springs (cairo_t *cr,
383 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
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);
389 for (i = 0; i < model->num_offset_springs; i++) {
391 model->offset_springs[i].a->position.x,
392 model->offset_springs[i].a->position.y);
394 model->offset_springs[i].b->position.x,
395 model->offset_springs[i].b->position.y);
402 draw_polygons (cairo_t *cr, Model *model, Color *color)
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);
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);
421 draw_objects (cairo_t *cr, Model *model, Color *color)
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,
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 };
439 typedef struct _Closure Closure;
441 GtkWidget *drawing_area;
442 GtkWidget *fps_label;
446 struct timeval start;
450 draw_model (GtkWidget *widget, Model *model)
454 cr = gdk_cairo_create (widget->window);
456 cairo_set_source_rgb (cr, 1, 1, 1);
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);
471 expose_event (GtkWidget *widget,
472 GdkEventExpose *event,
475 Closure *closure = data;
477 draw_model (widget, closure->model);
483 button_press_event (GtkWidget *widget,
484 GdkEventButton *event,
487 Closure *closure = data;
489 if (event->button != 1)
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,
501 button_release_event (GtkWidget *widget,
502 GdkEventButton *event,
505 Closure *closure = data;
507 if ((event->state & GDK_BUTTON1_MASK) == 0)
510 closure->model->anchor_object = NULL;
516 motion_notify_event (GtkWidget *widget,
517 GdkEventMotion *event,
520 Closure *closure = data;
522 GdkModifierType state;
524 gdk_window_get_pointer (event->window, &x, &y, &state);
526 closure->model->anchor_position.x = x + 0.5;
527 closure->model->anchor_position.y = y + 0.5;
532 typedef void (*ModelInitFunc) (Model *model);
535 model_changed (GtkComboBox *combo, gpointer user_data)
537 Closure *closure = user_data;
539 GtkTreeModel *tree_model;
543 tree_model = gtk_combo_box_get_model (combo);
544 if (!gtk_combo_box_get_active_iter (combo, &iter))
547 gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1);
549 model_fini (closure->model);
550 (*init) (closure->model);
553 static GtkTreeModel *
554 create_model_store (void)
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 }
572 store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
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);
580 return GTK_TREE_MODEL (store);
585 create_model_combo (Closure *closure)
588 GtkWidget *combo, *label;
590 GtkCellRenderer *renderer;
592 hbox = gtk_hbox_new (FALSE, 8);
594 label = gtk_label_new_with_mnemonic ("_Model:");
595 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
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);
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,
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);
613 label = gtk_label_new ("Frames per second: 0");
614 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
616 closure->fps_label = label;
622 create_window (Closure *closure)
628 GtkWidget *model_combo;
630 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
631 gtk_window_set_title (GTK_WINDOW (window), "Akamaru");
633 g_signal_connect (window, "destroy",
634 G_CALLBACK (gtk_main_quit), &window);
636 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
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);
643 * Create the drawing area
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);
650 da = gtk_drawing_area_new ();
651 /* set a minimum size */
652 gtk_widget_set_size_request (da, 200, 200);
654 gtk_container_add (GTK_CONTAINER (frame), da);
656 /* Signals used to handle backing pixmap */
658 g_signal_connect (da, "expose_event",
659 G_CALLBACK (expose_event), closure);
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);
670 /* Ask to receive events the drawing area doesn't normally
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);
680 model_combo = create_model_combo (closure);
681 gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0);
683 closure->drawing_area = da;
687 timeout_callback (gpointer data)
689 Closure *closure = data;
691 model_step (closure->model, 0.2);
694 if (closure->i == 1) {
695 gtk_widget_queue_draw (closure->drawing_area);
697 closure->frame_count++;
700 if (closure->frame_count == 200) {
701 struct timeval end, elapsed;
705 closure->frame_count = 0;
706 gettimeofday (&end, NULL);
707 if (closure->start.tv_usec > end.tv_usec) {
708 end.tv_usec += 1000000;
712 elapsed.tv_usec = end.tv_usec - closure->start.tv_usec;
713 elapsed.tv_sec = end.tv_sec - closure->start.tv_sec;
715 total = elapsed.tv_sec + ((double) elapsed.tv_usec / 1e6);
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);
728 main (int argc, char *argv[])
733 gtk_init (&argc, &argv);
734 model_init_rope (&model);
735 create_window (&closure);
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);