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.
26 #include <gdk-pixbuf/gdk-pixbuf.h>
31 #define FRAME_DELAY 50
33 #define BACKGROUND_NAME "background.jpg"
35 static const char *image_names[] = {
46 #define N_IMAGES (sizeof (image_names) / sizeof (image_names[0]))
49 static GdkPixbuf *frame;
51 /* Background image */
52 static GdkPixbuf *background;
53 int back_width, back_height;
56 static GdkPixbuf *images[N_IMAGES];
63 /* Loads the images for the demo and returns whether the operation succeeded */
69 background = gdk_pixbuf_new_from_file (BACKGROUND_NAME);
73 back_width = gdk_pixbuf_get_width (background);
74 back_height = gdk_pixbuf_get_height (background);
76 for (i = 0; i < N_IMAGES; i++) {
77 images[i] = gdk_pixbuf_new_from_file (image_names[i]);
85 /* Expose callback for the drawing area */
87 expose_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
92 rowstride = gdk_pixbuf_get_rowstride (frame);
94 pixels = gdk_pixbuf_get_pixels (frame) + rowstride * event->area.y + event->area.x * 3;
96 gdk_draw_rgb_image_dithalign (widget->window,
97 widget->style->black_gc,
98 event->area.x, event->area.y,
99 event->area.width, event->area.height,
100 GDK_RGB_DITHER_NORMAL,
102 event->area.x, event->area.y);
109 static int frame_num;
111 /* Timeout handler to regenerate the frame */
113 timeout (gpointer data)
120 gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
123 f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;
125 xmid = back_width / 2.0;
126 ymid = back_height / 2.0;
128 radius = MIN (xmid, ymid) / 2.0;
130 for (i = 0; i < N_IMAGES; i++) {
135 GdkRectangle r1, r2, dest;
138 ang = 2.0 * M_PI * (double) i / N_IMAGES - f * 2.0 * M_PI;
140 iw = gdk_pixbuf_get_width (images[i]);
141 ih = gdk_pixbuf_get_height (images[i]);
143 r = radius + (radius / 3.0) * sin (f * 2.0 * M_PI);
145 xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
146 ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
148 k = (i & 1) ? sin (f * 2.0 * M_PI) : cos (f * 2.0 * M_PI);
159 r2.width = back_width;
160 r2.height = back_height;
162 if (gdk_rectangle_intersect (&r1, &r2, &dest))
163 gdk_pixbuf_composite (images[i],
166 dest.width, dest.height,
171 ? MAX (127, fabs (255 * sin (f * 2.0 * M_PI)))
172 : MAX (127, fabs (255 * cos (f * 2.0 * M_PI)))));
175 gtk_widget_draw (da, NULL);
181 static guint timeout_id;
183 /* Destroy handler for the window */
185 destroy_cb (GtkObject *object, gpointer data)
187 gtk_timeout_remove (timeout_id);
194 main (int argc, char **argv)
198 gtk_init (&argc, &argv);
201 if (!load_pixbufs ()) {
202 g_message ("main(): Could not load all the pixbufs!");
206 frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
208 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
209 gtk_widget_set_usize (window, back_width, back_height);
210 gtk_window_set_policy (GTK_WINDOW (window), FALSE, FALSE, FALSE);
212 gtk_signal_connect (GTK_OBJECT (window), "destroy",
213 GTK_SIGNAL_FUNC (destroy_cb), NULL);
215 da = gtk_drawing_area_new ();
217 gtk_signal_connect (GTK_OBJECT (da), "expose_event",
218 GTK_SIGNAL_FUNC (expose_cb), NULL);
220 gtk_container_add (GTK_CONTAINER (window), da);
222 timeout_id = gtk_timeout_add (FRAME_DELAY, timeout, NULL);
224 gtk_widget_show_all (window);