3 * A GdkPixbuf represents an image, normally in RGB or RGBA format.
4 * Pixbufs are normally used to load files from disk and perform
7 * This demo is not all that educational, but looks cool. It was written
8 * by Extreme Pixbuf Hacker Federico Mena Quintero. It also shows
9 * off how to use GtkDrawingArea to do a simple animation.
11 * Look at the Image demo for additional pixbuf usage examples.
20 #include "demo-common.h"
22 #define FRAME_DELAY 50
24 #define BACKGROUND_NAME "background.jpg"
26 static const char *image_names[] = {
37 #define N_IMAGES G_N_ELEMENTS (image_names)
40 static GtkWidget *window = NULL;
43 static GdkPixbuf *frame;
45 /* Background image */
46 static GdkPixbuf *background;
47 static gint back_width, back_height;
50 static GdkPixbuf *images[N_IMAGES];
55 /* Loads the images for the demo and returns whether the operation succeeded */
57 load_pixbufs (GError **error)
63 return TRUE; /* already loaded earlier */
65 /* demo_find_file() looks in the the current directory first,
66 * so you can run gtk-demo without installing GTK, then looks
67 * in the location where the file is installed.
69 filename = demo_find_file (BACKGROUND_NAME, error);
71 return FALSE; /* note that "error" was filled in and returned */
73 background = gdk_pixbuf_new_from_file (filename, error);
77 return FALSE; /* Note that "error" was filled with a GError */
79 back_width = gdk_pixbuf_get_width (background);
80 back_height = gdk_pixbuf_get_height (background);
82 for (i = 0; i < N_IMAGES; i++)
84 filename = demo_find_file (image_names[i], error);
86 return FALSE; /* Note that "error" was filled with a GError */
88 images[i] = gdk_pixbuf_new_from_file (filename, error);
92 return FALSE; /* Note that "error" was filled with a GError */
98 /* Expose callback for the drawing area */
100 expose_cb (GtkWidget *widget,
101 GdkEventExpose *event,
107 rowstride = gdk_pixbuf_get_rowstride (frame);
109 pixels = gdk_pixbuf_get_pixels (frame) + rowstride * event->area.y + event->area.x * 3;
111 gdk_draw_rgb_image_dithalign (widget->window,
112 widget->style->black_gc,
113 event->area.x, event->area.y,
114 event->area.width, event->area.height,
115 GDK_RGB_DITHER_NORMAL,
117 event->area.x, event->area.y);
124 static int frame_num;
126 /* Timeout handler to regenerate the frame */
128 timeout (gpointer data)
135 gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
138 f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;
140 xmid = back_width / 2.0;
141 ymid = back_height / 2.0;
143 radius = MIN (xmid, ymid) / 2.0;
145 for (i = 0; i < N_IMAGES; i++)
151 GdkRectangle r1, r2, dest;
154 ang = 2.0 * G_PI * (double) i / N_IMAGES - f * 2.0 * G_PI;
156 iw = gdk_pixbuf_get_width (images[i]);
157 ih = gdk_pixbuf_get_height (images[i]);
159 r = radius + (radius / 3.0) * sin (f * 2.0 * G_PI);
161 xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
162 ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
164 k = (i & 1) ? sin (f * 2.0 * G_PI) : cos (f * 2.0 * G_PI);
175 r2.width = back_width;
176 r2.height = back_height;
178 if (gdk_rectangle_intersect (&r1, &r2, &dest))
179 gdk_pixbuf_composite (images[i],
182 dest.width, dest.height,
187 ? MAX (127, fabs (255 * sin (f * 2.0 * G_PI)))
188 : MAX (127, fabs (255 * cos (f * 2.0 * G_PI)))));
191 gtk_widget_queue_draw (da);
197 static guint timeout_id;
200 cleanup_callback (GtkObject *object,
203 g_source_remove (timeout_id);
215 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
216 gtk_window_set_title (GTK_WINDOW (window), "Pixbufs");
217 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
219 g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window);
220 g_signal_connect (window, "destroy", G_CALLBACK (cleanup_callback), NULL);
224 if (!load_pixbufs (&error))
228 dialog = gtk_message_dialog_new (GTK_WINDOW (window),
229 GTK_DIALOG_DESTROY_WITH_PARENT,
232 "Failed to load an image: %s",
235 g_error_free (error);
237 g_signal_connect (dialog, "response",
238 G_CALLBACK (gtk_widget_destroy), NULL);
240 gtk_widget_show (dialog);
244 gtk_widget_set_size_request (window, back_width, back_height);
246 frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
248 da = gtk_drawing_area_new ();
250 g_signal_connect (da, "expose_event",
251 G_CALLBACK (expose_cb), NULL);
253 gtk_container_add (GTK_CONTAINER (window), da);
255 timeout_id = g_timeout_add (FRAME_DELAY, timeout, NULL);
259 if (!GTK_WIDGET_VISIBLE (window))
261 gtk_widget_show_all (window);
265 gtk_widget_destroy (window);