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;
69 model_init_rope (Model *model)
71 const int num_objects = 20;
72 const int num_sticks = num_objects - 1;
73 const int stick_length = 10;
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);
83 for (i = 0; i < num_objects; i++) {
84 object_init (&model->objects[i], 200, 40 + i * stick_length, 1);
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;
95 model_init_curtain (Model *model)
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;
104 int i, j, index, stick_index;
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);
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;
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);
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;
135 model->offsets[0].objects[i] = &model->objects[i * num_rope_objects];
140 model_init_grid (Model *model)
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;
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);
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;
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);
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);
181 model->offsets[0].objects[i] = &model->objects[i * num_rope_objects];
186 model_init_molecule (Model *model)
188 const int num_objects = 8;
189 const int num_springs = num_objects * 2;
190 const int spring_length = 50;
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;
201 for (i = 0; i < num_objects; i++)
202 object_init (&model->objects[i], 200 + i * 20, 200, 0);
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],
209 spring_init (spring++, &model->objects[i],
210 &model->objects[(i + 2) % num_objects],
217 model_init_wobbly (Model *model)
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;
226 OffsetSpring *spring;
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;
235 model_init_polygons (model);
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);
246 offset_spring_init (spring++, object, object + height, distance, 0);
249 offset_spring_init (spring++, object, object + 1, 0, distance);
257 model_init_dock (Model *model)
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;
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;
280 model->polygons = g_new (Polygon, 1);
281 model->num_polygons = 1;
282 polygon_init_rectangle (&model->polygons[0], -400, 300, 1400, 350);
284 model->anchors[0].x = 300;
285 model->anchors[0].y = 300;
286 model->anchors[0].object = &model->objects[0];
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;
294 object_init (object, x, y, 1);
297 spring_init (spring++,
301 for (j = 1; j < num_objects - i; j++) {
302 spacer_init (spacer++, object, object + j, distance);
307 typedef struct _Color Color;
309 double red, green, blue;
313 draw_sticks (cairo_t *cr,
319 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1);
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);
325 for (i = 0; i < model->num_sticks; i++) {
327 model->sticks[i].a->position.x,
328 model->sticks[i].a->position.y);
330 model->sticks[i].b->position.x,
331 model->sticks[i].b->position.y);
338 draw_strings (cairo_t *cr,
344 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1);
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);
350 for (i = 0; i < model->num_strings; i++) {
352 model->strings[i].a->position.x,
353 model->strings[i].a->position.y);
355 model->strings[i].b->position.x,
356 model->strings[i].b->position.y);
363 draw_offsets (cairo_t *cr,
369 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.5);
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);
375 for (i = 0; i < model->num_offsets; i++) {
376 for (j = 0; j < model->offsets[i].num_objects; j++) {
378 model->offsets[i].objects[j]->position.x,
379 model->offsets[i].objects[j]->position.y);
387 draw_springs (cairo_t *cr,
393 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
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);
399 for (i = 0; i < model->num_springs; i++) {
401 model->springs[i].a->position.x,
402 model->springs[i].a->position.y);
404 model->springs[i].b->position.x,
405 model->springs[i].b->position.y);
412 draw_offset_springs (cairo_t *cr,
418 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
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);
424 for (i = 0; i < model->num_offset_springs; i++) {
426 model->offset_springs[i].a->position.x,
427 model->offset_springs[i].a->position.y);
429 model->offset_springs[i].b->position.x,
430 model->offset_springs[i].b->position.y);
437 draw_polygons (cairo_t *cr, Model *model, Color *color)
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);
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);
456 draw_objects (cairo_t *cr, Model *model, Color *color)
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,
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 };
474 typedef struct _Closure Closure;
476 GtkWidget *drawing_area;
477 GtkWidget *fps_label;
481 struct timeval start;
485 draw_model (GtkWidget *widget, Model *model)
489 cr = gdk_cairo_create (widget->window);
491 cairo_set_source_rgb (cr, 1, 1, 1);
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);
506 expose_event (GtkWidget *widget,
507 GdkEventExpose *event,
510 Closure *closure = data;
512 draw_model (widget, closure->model);
518 button_press_event (GtkWidget *widget,
519 GdkEventButton *event,
522 Closure *closure = data;
524 if (event->button != 1)
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);
536 button_release_event (GtkWidget *widget,
537 GdkEventButton *event,
540 Closure *closure = data;
542 if ((event->state & GDK_BUTTON1_MASK) == 0)
545 closure->model->mouse_anchor.object = NULL;
551 motion_notify_event (GtkWidget *widget,
552 GdkEventMotion *event,
555 Closure *closure = data;
557 GdkModifierType state;
559 gdk_window_get_pointer (event->window, &x, &y, &state);
561 closure->model->mouse_anchor.x = x + 0.5;
562 closure->model->mouse_anchor.y = y + 0.5;
567 typedef void (*ModelInitFunc) (Model *model);
570 model_changed (GtkComboBox *combo, gpointer user_data)
572 Closure *closure = user_data;
574 GtkTreeModel *tree_model;
578 tree_model = gtk_combo_box_get_model (combo);
579 if (!gtk_combo_box_get_active_iter (combo, &iter))
582 gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1);
584 model_fini (closure->model);
585 (*init) (closure->model);
588 static GtkTreeModel *
589 create_model_store (void)
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 }
608 store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
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);
616 return GTK_TREE_MODEL (store);
621 create_model_combo (Closure *closure)
624 GtkWidget *combo, *label;
626 GtkCellRenderer *renderer;
628 hbox = gtk_hbox_new (FALSE, 8);
630 label = gtk_label_new_with_mnemonic ("_Model:");
631 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
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);
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,
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);
649 label = gtk_label_new ("Frames per second: 0");
650 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
652 closure->fps_label = label;
658 create_window (Closure *closure)
664 GtkWidget *model_combo;
666 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
667 gtk_window_set_title (GTK_WINDOW (window), "Akamaru");
669 g_signal_connect (window, "destroy",
670 G_CALLBACK (gtk_main_quit), &window);
672 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
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);
679 * Create the drawing area
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);
686 da = gtk_drawing_area_new ();
687 /* set a minimum size */
688 gtk_widget_set_size_request (da, 200, 200);
690 gtk_container_add (GTK_CONTAINER (frame), da);
692 /* Signals used to handle backing pixmap */
694 g_signal_connect (da, "expose_event",
695 G_CALLBACK (expose_event), closure);
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);
706 /* Ask to receive events the drawing area doesn't normally
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);
716 model_combo = create_model_combo (closure);
717 gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0);
719 closure->drawing_area = da;
723 timeout_callback (gpointer data)
725 Closure *closure = data;
727 model_step (closure->model, 0.2);
730 if (closure->i == 1) {
731 gtk_widget_queue_draw (closure->drawing_area);
733 closure->frame_count++;
736 if (closure->frame_count == 200) {
737 struct timeval end, elapsed;
741 closure->frame_count = 0;
742 gettimeofday (&end, NULL);
743 if (closure->start.tv_usec > end.tv_usec) {
744 end.tv_usec += 1000000;
748 elapsed.tv_usec = end.tv_usec - closure->start.tv_usec;
749 elapsed.tv_sec = end.tv_sec - closure->start.tv_sec;
751 total = elapsed.tv_sec + ((double) elapsed.tv_usec / 1e6);
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);
764 main (int argc, char *argv[])
769 gtk_init (&argc, &argv);
770 model_init_rope (&model);
771 create_window (&closure);
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);