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 expose_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
95 rowstride = gdk_pixbuf_get_rowstride (frame);
97 pixels = gdk_pixbuf_get_pixels (frame) + rowstride * event->area.y + event->area.x * 3;
99 gdk_draw_rgb_image_dithalign (widget->window,
100 widget->style->black_gc,
101 event->area.x, event->area.y,
102 event->area.width, event->area.height,
103 GDK_RGB_DITHER_NORMAL,
105 event->area.x, event->area.y);
112 static int frame_num;
114 /* Timeout handler to regenerate the frame */
116 timeout (gpointer data)
123 gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
126 f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;
128 xmid = back_width / 2.0;
129 ymid = back_height / 2.0;
131 radius = MIN (xmid, ymid) / 2.0;
133 for (i = 0; i < N_IMAGES; i++) {
138 GdkRectangle r1, r2, dest;
141 ang = 2.0 * G_PI * (double) i / N_IMAGES - f * 2.0 * G_PI;
143 iw = gdk_pixbuf_get_width (images[i]);
144 ih = gdk_pixbuf_get_height (images[i]);
146 r = radius + (radius / 3.0) * sin (f * 2.0 * G_PI);
148 xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
149 ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
151 k = (i & 1) ? sin (f * 2.0 * G_PI) : cos (f * 2.0 * G_PI);
162 r2.width = back_width;
163 r2.height = back_height;
165 if (gdk_rectangle_intersect (&r1, &r2, &dest))
166 gdk_pixbuf_composite (images[i],
169 dest.width, dest.height,
174 ? MAX (127, fabs (255 * sin (f * 2.0 * G_PI)))
175 : MAX (127, fabs (255 * cos (f * 2.0 * G_PI)))));
178 gtk_widget_queue_draw (da);
184 static guint timeout_id;
186 /* Destroy handler for the window */
188 destroy_cb (GtkObject *object, gpointer data)
190 g_source_remove (timeout_id);
196 extern void pixbuf_init();
199 main (int argc, char **argv)
204 gtk_init (&argc, &argv);
206 if (!load_pixbufs ()) {
207 g_message ("main(): Could not load all the pixbufs!");
211 frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
213 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
215 gtk_widget_set_size_request (window, back_width, back_height);
216 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
218 g_signal_connect (window, "destroy",
219 G_CALLBACK (destroy_cb), NULL);
221 da = gtk_drawing_area_new ();
223 g_signal_connect (da, "expose_event",
224 G_CALLBACK (expose_cb), NULL);
226 gtk_container_add (GTK_CONTAINER (window), da);
228 timeout_id = gdk_threads_add_timeout (FRAME_DELAY, timeout, NULL);
230 gtk_widget_show_all (window);