1 /* -*- mode: c; c-basic-offset: 8 -*-
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_enclosing_rectangle (&model->polygons[0], 10, 10, 700, 500);
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];
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);
450 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.1);
451 cairo_fill_preserve (cr);
454 cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
464 draw_objects (cairo_t *cr, Model *model, Color *color)
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,
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 };
482 typedef struct _Closure Closure;
484 GtkWidget *drawing_area;
485 GtkWidget *fps_label;
489 struct timeval start;
493 draw_model (GtkWidget *widget, Model *model)
497 cr = gdk_cairo_create (widget->window);
499 cairo_set_source_rgb (cr, 1, 1, 1);
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);
514 expose_event (GtkWidget *widget,
515 GdkEventExpose *event,
518 Closure *closure = data;
520 draw_model (widget, closure->model);
526 button_press_event (GtkWidget *widget,
527 GdkEventButton *event,
530 Closure *closure = data;
532 if (event->button != 1)
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);
544 button_release_event (GtkWidget *widget,
545 GdkEventButton *event,
548 Closure *closure = data;
550 if ((event->state & GDK_BUTTON1_MASK) == 0)
553 closure->model->mouse_anchor.object = NULL;
559 motion_notify_event (GtkWidget *widget,
560 GdkEventMotion *event,
563 Closure *closure = data;
565 GdkModifierType state;
567 gdk_window_get_pointer (event->window, &x, &y, &state);
569 closure->model->mouse_anchor.x = x + 0.5;
570 closure->model->mouse_anchor.y = y + 0.5;
575 typedef void (*ModelInitFunc) (Model *model);
578 model_changed (GtkComboBox *combo, gpointer user_data)
580 Closure *closure = user_data;
582 GtkTreeModel *tree_model;
586 tree_model = gtk_combo_box_get_model (combo);
587 if (!gtk_combo_box_get_active_iter (combo, &iter))
590 gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1);
592 model_fini (closure->model);
593 (*init) (closure->model);
596 static GtkTreeModel *
597 create_model_store (void)
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 }
616 store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
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);
624 return GTK_TREE_MODEL (store);
629 create_model_combo (Closure *closure)
632 GtkWidget *combo, *label;
634 GtkCellRenderer *renderer;
636 hbox = gtk_hbox_new (FALSE, 8);
638 label = gtk_label_new_with_mnemonic ("_Model:");
639 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
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);
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,
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);
657 label = gtk_label_new ("Frames per second: 0");
658 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
660 closure->fps_label = label;
666 create_window (Closure *closure)
672 GtkWidget *model_combo;
674 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
675 gtk_window_set_title (GTK_WINDOW (window), "赤丸");
677 g_signal_connect (window, "destroy",
678 G_CALLBACK (gtk_main_quit), &window);
680 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
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);
687 * Create the drawing area
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);
694 da = gtk_drawing_area_new ();
695 /* set a minimum size */
696 gtk_widget_set_size_request (da, 200, 200);
698 gtk_container_add (GTK_CONTAINER (frame), da);
700 /* Signals used to handle backing pixmap */
702 g_signal_connect (da, "expose_event",
703 G_CALLBACK (expose_event), closure);
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);
714 /* Ask to receive events the drawing area doesn't normally
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);
724 model_combo = create_model_combo (closure);
725 gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0);
727 closure->drawing_area = da;
731 timeout_callback (gpointer data)
733 Closure *closure = data;
735 model_step (closure->model, 0.2);
738 if (closure->i == 1) {
739 gtk_widget_queue_draw (closure->drawing_area);
741 closure->frame_count++;
744 if (closure->frame_count == 200) {
745 struct timeval end, elapsed;
749 closure->frame_count = 0;
750 gettimeofday (&end, NULL);
751 if (closure->start.tv_usec > end.tv_usec) {
752 end.tv_usec += 1000000;
756 elapsed.tv_usec = end.tv_usec - closure->start.tv_usec;
757 elapsed.tv_sec = end.tv_sec - closure->start.tv_sec;
759 total = elapsed.tv_sec + ((double) elapsed.tv_usec / 1e6);
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);
772 main (int argc, char *argv[])
777 gtk_init (&argc, &argv);
778 model_init_rope (&model);
779 create_window (&closure);
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);