1 /* GdkPixbuf library - Scaling and compositing demo
3 * Copyright (C) 1999 The Free Software Foundation
5 * Authors: Federico Mena-Quintero <federico@gimp.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
28 #define FRAME_DELAY 50
30 #define BACKGROUND_NAME "background.jpg"
32 static const char *image_names[] = {
43 #define N_IMAGES (sizeof (image_names) / sizeof (image_names[0]))
46 static GdkPixbuf *frame;
48 /* Background image */
49 static GdkPixbuf *background;
50 static int back_width, back_height;
53 static GdkPixbuf *images[N_IMAGES];
60 /* Loads the images for the demo and returns whether the operation succeeded */
66 /* We pass NULL for the error return location, we don't care
67 * about the error message.
70 background = gdk_pixbuf_new_from_file (BACKGROUND_NAME, NULL);
74 back_width = gdk_pixbuf_get_width (background);
75 back_height = gdk_pixbuf_get_height (background);
77 for (i = 0; i < N_IMAGES; i++) {
78 images[i] = gdk_pixbuf_new_from_file (image_names[i], NULL);
86 /* Expose callback for the drawing area */
88 draw_cb (GtkWidget *widget, cairo_t *cr, gpointer data)
90 gdk_cairo_set_source_pixbuf (cr, frame, 0, 0);
100 /* Timeout handler to regenerate the frame */
102 timeout (gpointer data)
109 gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
112 f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;
114 xmid = back_width / 2.0;
115 ymid = back_height / 2.0;
117 radius = MIN (xmid, ymid) / 2.0;
119 for (i = 0; i < N_IMAGES; i++) {
124 GdkRectangle r1, r2, dest;
127 ang = 2.0 * G_PI * (double) i / N_IMAGES - f * 2.0 * G_PI;
129 iw = gdk_pixbuf_get_width (images[i]);
130 ih = gdk_pixbuf_get_height (images[i]);
132 r = radius + (radius / 3.0) * sin (f * 2.0 * G_PI);
134 xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
135 ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
137 k = (i & 1) ? sin (f * 2.0 * G_PI) : cos (f * 2.0 * G_PI);
148 r2.width = back_width;
149 r2.height = back_height;
151 if (gdk_rectangle_intersect (&r1, &r2, &dest))
152 gdk_pixbuf_composite (images[i],
155 dest.width, dest.height,
160 ? MAX (127, fabs (255 * sin (f * 2.0 * G_PI)))
161 : MAX (127, fabs (255 * cos (f * 2.0 * G_PI)))));
164 gtk_widget_queue_draw (da);
170 static guint timeout_id;
172 /* Destroy handler for the window */
174 destroy_cb (GObject *object, gpointer data)
176 g_source_remove (timeout_id);
183 main (int argc, char **argv)
187 gtk_init (&argc, &argv);
189 if (!load_pixbufs ()) {
190 g_message ("main(): Could not load all the pixbufs!");
194 frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
196 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
198 gtk_widget_set_size_request (window, back_width, back_height);
199 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
201 g_signal_connect (window, "destroy",
202 G_CALLBACK (destroy_cb), NULL);
204 da = gtk_drawing_area_new ();
206 g_signal_connect (da, "draw",
207 G_CALLBACK (draw_cb), NULL);
209 gtk_container_add (GTK_CONTAINER (window), da);
211 timeout_id = gdk_threads_add_timeout (FRAME_DELAY, timeout, NULL);
213 gtk_widget_show_all (window);