1 /* -*- mode: C; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
8 #define WINDOW_SIZE_JITTER 200
11 static GtkWidget *window;
12 static int window_width = WIDTH, window_height = HEIGHT;
15 static int frames_since_last_print = 0;
17 static double load_factor = 1.0;
18 static double cb_no_resize = FALSE;
20 static cairo_surface_t *source_surface;
23 ensure_resources(cairo_surface_t *target)
28 if (source_surface != NULL)
31 source_surface = cairo_surface_create_similar (target, CAIRO_CONTENT_COLOR_ALPHA, 2048, 2048);
32 cr = cairo_create(source_surface);
35 cairo_set_source_rgba(cr, 0, 0, 0, 0);
36 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
40 cairo_set_line_width(cr, 1.0);
42 for (j = 0; j < 16; j++)
43 for (i = 0; i < 16; i++)
45 cairo_set_source_rgba(cr,
46 ((i * 41) % 16) / 15.,
47 ((i * 31) % 16) / 15.,
48 ((i * 23) % 16) / 15.,
51 i * 128 + 64, j * 128 + 64,
52 64 - 0.5, 0, 2 * M_PI);
53 cairo_fill_preserve(cr);
54 cairo_set_source_rgba(cr,
55 ((i * 41) % 16) / 15.,
56 ((i * 31) % 16) / 15.,
57 ((i * 23) % 16) / 15.,
64 on_window_draw (GtkWidget *widget,
68 GRand *rand = g_rand_new_with_seed(0);
72 width = gtk_widget_get_allocated_width (widget);
73 height = gtk_widget_get_allocated_height (widget);
75 ensure_resources (cairo_get_target (cr));
77 cairo_set_source_rgb(cr, 1, 1, 1);
80 cairo_set_source_rgb(cr, 0, 0, 0);
81 cairo_set_line_width(cr, 1.0);
82 cairo_rectangle (cr, 0.5, 0.5, width - 1, height - 1);
85 for(i = 0; i < load_factor * 150; i++)
87 int source = g_rand_int_range(rand, 0, 255);
88 double phi = g_rand_double_range(rand, 0, 2 * M_PI) + angle;
89 double r = g_rand_double_range(rand, 0, width / 2 - 64);
92 int source_x = (source % 16) * 128;
93 int source_y = (source / 16) * 128;
95 x = round(width / 2 + r * cos(phi) - 64);
96 y = round(height / 2 - r * sin(phi) - 64);
98 cairo_set_source_surface(cr, source_surface,
99 x - source_x, y - source_y);
100 cairo_rectangle(cr, x, y, 128, 128);
108 on_frame (GtkTimeline *timeline,
113 static double last_print_time = 0;
115 current_time = g_get_monotonic_time () / 1000000.;
116 if (current_time >= last_print_time + 5)
118 if (frames_since_last_print != 0)
120 g_print ("%g\n", frames_since_last_print / (current_time - last_print_time));
121 frames_since_last_print = 0;
124 last_print_time = current_time;
126 frames_since_last_print++;
128 angle = 2 * M_PI * progress;
129 jitter = WINDOW_SIZE_JITTER * sin(angle);
133 window_width = WIDTH + jitter;
134 window_height = HEIGHT + jitter;
137 gtk_window_resize (GTK_WINDOW (window),
138 window_width, window_height);
140 gtk_widget_queue_draw (window);
143 static GOptionEntry options[] = {
144 { "factor", 'f', 0, G_OPTION_ARG_DOUBLE, &load_factor, "Load factor", "FACTOR" },
145 { "no-resize", 'n', 0, G_OPTION_ARG_NONE, &cb_no_resize, "No Resize", NULL },
150 main(int argc, char **argv)
152 GError *error = NULL;
154 GdkRectangle monitor_bounds;
155 GtkTimeline *timeline;
157 if (!gtk_init_with_args (&argc, &argv, "",
158 options, NULL, NULL))
160 g_printerr ("Option parsing failed: %s", error->message);
164 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
165 gtk_window_set_keep_above (GTK_WINDOW (window), TRUE);
166 gtk_window_set_gravity (GTK_WINDOW (window), GDK_GRAVITY_CENTER);
167 gtk_widget_set_app_paintable (window, TRUE);
169 g_signal_connect (window, "draw",
170 G_CALLBACK (on_window_draw), NULL);
171 g_signal_connect (window, "destroy",
172 G_CALLBACK (gtk_main_quit), NULL);
174 timeline = gtk_timeline_new (window, CYCLE_TIME * 1000);
175 gtk_timeline_set_loop (timeline, TRUE);
176 gtk_timeline_set_progress_type (timeline, GTK_TIMELINE_PROGRESS_LINEAR);
178 g_signal_connect (timeline, "frame",
179 G_CALLBACK (on_frame), NULL);
180 on_frame (timeline, 0.);
181 gtk_timeline_start (timeline);
183 screen = gtk_widget_get_screen (window);
184 gdk_screen_get_monitor_geometry (screen,
185 gdk_screen_get_primary_monitor (screen),
188 gtk_window_move (GTK_WINDOW (window),
189 monitor_bounds.x + (monitor_bounds.width - window_width) / 2,
190 monitor_bounds.y + (monitor_bounds.height - window_height) / 2);
192 gtk_widget_show (window);