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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
30 #define FRAME_DELAY 50
32 #define BACKGROUND_NAME "background.jpg"
34 static const char *image_names[] = {
45 #define N_IMAGES (sizeof (image_names) / sizeof (image_names[0]))
48 static GdkPixbuf *frame;
50 /* Background image */
51 static GdkPixbuf *background;
52 static int back_width, back_height;
55 static GdkPixbuf *images[N_IMAGES];
62 /* Loads the images for the demo and returns whether the operation succeeded */
68 /* We pass NULL for the error return location, we don't care
69 * about the error message.
72 background = gdk_pixbuf_new_from_file (BACKGROUND_NAME, NULL);
76 back_width = gdk_pixbuf_get_width (background);
77 back_height = gdk_pixbuf_get_height (background);
79 for (i = 0; i < N_IMAGES; i++) {
80 images[i] = gdk_pixbuf_new_from_file (image_names[i], NULL);
88 /* Expose callback for the drawing area */
90 draw_cb (GtkWidget *widget, cairo_t *cr, gpointer data)
92 gdk_cairo_set_source_pixbuf (cr, frame, 0, 0);
100 static int frame_num;
102 /* Timeout handler to regenerate the frame */
104 timeout (gpointer data)
111 gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
114 f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;
116 xmid = back_width / 2.0;
117 ymid = back_height / 2.0;
119 radius = MIN (xmid, ymid) / 2.0;
121 for (i = 0; i < N_IMAGES; i++) {
126 GdkRectangle r1, r2, dest;
129 ang = 2.0 * G_PI * (double) i / N_IMAGES - f * 2.0 * G_PI;
131 iw = gdk_pixbuf_get_width (images[i]);
132 ih = gdk_pixbuf_get_height (images[i]);
134 r = radius + (radius / 3.0) * sin (f * 2.0 * G_PI);
136 xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
137 ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
139 k = (i & 1) ? sin (f * 2.0 * G_PI) : cos (f * 2.0 * G_PI);
150 r2.width = back_width;
151 r2.height = back_height;
153 if (gdk_rectangle_intersect (&r1, &r2, &dest))
154 gdk_pixbuf_composite (images[i],
157 dest.width, dest.height,
162 ? MAX (127, fabs (255 * sin (f * 2.0 * G_PI)))
163 : MAX (127, fabs (255 * cos (f * 2.0 * G_PI)))));
166 gtk_widget_queue_draw (da);
172 static guint timeout_id;
174 /* Destroy handler for the window */
176 destroy_cb (GObject *object, gpointer data)
178 g_source_remove (timeout_id);
185 main (int argc, char **argv)
189 gtk_init (&argc, &argv);
191 if (!load_pixbufs ()) {
192 g_message ("main(): Could not load all the pixbufs!");
196 frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
198 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
200 gtk_widget_set_size_request (window, back_width, back_height);
201 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
203 g_signal_connect (window, "destroy",
204 G_CALLBACK (destroy_cb), NULL);
206 da = gtk_drawing_area_new ();
208 g_signal_connect (da, "draw",
209 G_CALLBACK (draw_cb), NULL);
211 gtk_container_add (GTK_CONTAINER (window), da);
213 timeout_id = gdk_threads_add_timeout (FRAME_DELAY, timeout, NULL);
215 gtk_widget_show_all (window);