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 Library 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 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library 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 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 background = gdk_pixbuf_new_from_file (BACKGROUND_NAME);
72 back_width = gdk_pixbuf_get_width (background);
73 back_height = gdk_pixbuf_get_height (background);
75 for (i = 0; i < N_IMAGES; i++) {
76 images[i] = gdk_pixbuf_new_from_file (image_names[i]);
84 /* Expose callback for the drawing area */
86 expose_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
91 rowstride = gdk_pixbuf_get_rowstride (frame);
93 pixels = gdk_pixbuf_get_pixels (frame) + rowstride * event->area.y + event->area.x * 3;
95 gdk_draw_rgb_image_dithalign (widget->window,
96 widget->style->black_gc,
97 event->area.x, event->area.y,
98 event->area.width, event->area.height,
99 GDK_RGB_DITHER_NORMAL,
101 event->area.x, event->area.y);
108 static int frame_num;
110 /* Timeout handler to regenerate the frame */
112 timeout (gpointer data)
119 gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
122 f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;
124 xmid = back_width / 2.0;
125 ymid = back_height / 2.0;
127 radius = MIN (xmid, ymid) / 2.0;
129 for (i = 0; i < N_IMAGES; i++) {
134 GdkRectangle r1, r2, dest;
137 ang = 2.0 * M_PI * (double) i / N_IMAGES - f * 2.0 * M_PI;
139 iw = gdk_pixbuf_get_width (images[i]);
140 ih = gdk_pixbuf_get_height (images[i]);
142 r = radius + (radius / 3.0) * sin (f * 2.0 * M_PI);
144 xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
145 ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
147 k = (i & 1) ? sin (f * 2.0 * M_PI) : cos (f * 2.0 * M_PI);
158 r2.width = back_width;
159 r2.height = back_height;
161 if (gdk_rectangle_intersect (&r1, &r2, &dest))
162 gdk_pixbuf_composite (images[i],
165 dest.width, dest.height,
170 ? MAX (127, fabs (255 * sin (f * 2.0 * M_PI)))
171 : MAX (127, fabs (255 * cos (f * 2.0 * M_PI)))));
174 gtk_widget_draw (da, NULL);
180 static guint timeout_id;
182 /* Destroy handler for the window */
184 destroy_cb (GtkObject *object, gpointer data)
186 gtk_timeout_remove (timeout_id);
192 extern void pixbuf_init();
195 main (int argc, char **argv)
200 gtk_init (&argc, &argv);
203 if (!load_pixbufs ()) {
204 g_message ("main(): Could not load all the pixbufs!");
208 frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
210 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
211 gtk_widget_set_usize (window, back_width, back_height);
212 gtk_window_set_policy (GTK_WINDOW (window), FALSE, FALSE, FALSE);
214 gtk_signal_connect (GTK_OBJECT (window), "destroy",
215 GTK_SIGNAL_FUNC (destroy_cb), NULL);
217 da = gtk_drawing_area_new ();
219 gtk_signal_connect (GTK_OBJECT (da), "expose_event",
220 GTK_SIGNAL_FUNC (expose_cb), NULL);
222 gtk_container_add (GTK_CONTAINER (window), da);
224 timeout_id = gtk_timeout_add (FRAME_DELAY, timeout, NULL);
226 gtk_widget_show_all (window);