]> Pileus Git - ~andy/gtk/blob - demos/pixbuf-demo.c
Work with srcdir != builddir.
[~andy/gtk] / demos / pixbuf-demo.c
1 /* GdkPixbuf library - Scaling and compositing demo
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
5  * Authors: Federico Mena-Quintero <federico@gimp.org>
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include <config.h>
24 #include <stdlib.h>
25 #include <gtk/gtk.h>
26 #include <math.h>
27
28 \f
29
30 #define FRAME_DELAY 50
31
32 #define BACKGROUND_NAME "background.jpg"
33
34 static const char *image_names[] = {
35         "apple-red.png",
36         "gnome-applets.png",
37         "gnome-calendar.png",
38         "gnome-foot.png",
39         "gnome-gmush.png",
40         "gnome-gimp.png",
41         "gnome-gsame.png",
42         "gnu-keys.png"
43 };
44
45 #define N_IMAGES (sizeof (image_names) / sizeof (image_names[0]))
46
47 /* Current frame */
48 static GdkPixbuf *frame;
49
50 /* Background image */
51 static GdkPixbuf *background;
52 int back_width, back_height;
53
54 /* Images */
55 static GdkPixbuf *images[N_IMAGES];
56
57 /* Widgets */
58 GtkWidget *da;
59
60 \f
61
62 /* Loads the images for the demo and returns whether the operation succeeded */
63 static gboolean
64 load_pixbufs (void)
65 {
66         int i;
67
68         background = gdk_pixbuf_new_from_file (BACKGROUND_NAME);
69         if (!background)
70                 return FALSE;
71
72         back_width = gdk_pixbuf_get_width (background);
73         back_height = gdk_pixbuf_get_height (background);
74
75         for (i = 0; i < N_IMAGES; i++) {
76                 images[i] = gdk_pixbuf_new_from_file (image_names[i]);
77                 if (!images[i])
78                         return FALSE;
79         }
80
81         return TRUE;
82 }
83
84 /* Expose callback for the drawing area */
85 static gint
86 expose_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
87 {
88         guchar *pixels;
89         int rowstride;
90
91         rowstride = gdk_pixbuf_get_rowstride (frame);
92
93         pixels = gdk_pixbuf_get_pixels (frame) + rowstride * event->area.y + event->area.x * 3;
94                   
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,
100                                       pixels, rowstride,
101                                       event->area.x, event->area.y);
102
103         return TRUE;
104 }
105
106 #define CYCLE_LEN 60
107
108 static int frame_num;
109
110 /* Timeout handler to regenerate the frame */
111 static gint
112 timeout (gpointer data)
113 {
114         double f;
115         int i;
116         double xmid, ymid;
117         double radius;
118
119         gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
120                               frame, 0, 0);
121
122         f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;
123
124         xmid = back_width / 2.0;
125         ymid = back_height / 2.0;
126
127         radius = MIN (xmid, ymid) / 2.0;
128
129         for (i = 0; i < N_IMAGES; i++) {
130                 double ang;
131                 int xpos, ypos;
132                 int iw, ih;
133                 double r;
134                 GdkRectangle r1, r2, dest;
135                 double k;
136
137                 ang = 2.0 * M_PI * (double) i / N_IMAGES - f * 2.0 * M_PI;
138
139                 iw = gdk_pixbuf_get_width (images[i]);
140                 ih = gdk_pixbuf_get_height (images[i]);
141
142                 r = radius + (radius / 3.0) * sin (f * 2.0 * M_PI);
143
144                 xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
145                 ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
146
147                 k = (i & 1) ? sin (f * 2.0 * M_PI) : cos (f * 2.0 * M_PI);
148                 k = 2.0 * k * k;
149                 k = MAX (0.25, k);
150
151                 r1.x = xpos;
152                 r1.y = ypos;
153                 r1.width = iw * k;
154                 r1.height = ih * k;
155
156                 r2.x = 0;
157                 r2.y = 0;
158                 r2.width = back_width;
159                 r2.height = back_height;
160
161                 if (gdk_rectangle_intersect (&r1, &r2, &dest))
162                         gdk_pixbuf_composite (images[i],
163                                               frame,
164                                               dest.x, dest.y,
165                                               dest.width, dest.height,
166                                               xpos, ypos,
167                                               k, k,
168                                               GDK_INTERP_NEAREST,
169                                               ((i & 1)
170                                                ? MAX (127, fabs (255 * sin (f * 2.0 * M_PI)))
171                                                : MAX (127, fabs (255 * cos (f * 2.0 * M_PI)))));
172         }
173
174         gtk_widget_draw (da, NULL);
175
176         frame_num++;
177         return TRUE;
178 }
179
180 static guint timeout_id;
181
182 /* Destroy handler for the window */
183 static void
184 destroy_cb (GtkObject *object, gpointer data)
185 {
186         gtk_timeout_remove (timeout_id);
187         timeout_id = 0;
188
189         gtk_main_quit ();
190 }
191
192 extern void pixbuf_init();
193
194 int
195 main (int argc, char **argv)
196 {
197         GtkWidget *window;
198
199         pixbuf_init ();
200         gtk_init (&argc, &argv);
201         gdk_rgb_init ();
202
203         if (!load_pixbufs ()) {
204                 g_message ("main(): Could not load all the pixbufs!");
205                 exit (EXIT_FAILURE);
206         }
207
208         frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
209
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);
213
214         gtk_signal_connect (GTK_OBJECT (window), "destroy",
215                             GTK_SIGNAL_FUNC (destroy_cb), NULL);
216
217         da = gtk_drawing_area_new ();
218
219         gtk_signal_connect (GTK_OBJECT (da), "expose_event",
220                             GTK_SIGNAL_FUNC (expose_cb), NULL);
221
222         gtk_container_add (GTK_CONTAINER (window), da);
223
224         timeout_id = gtk_timeout_add (FRAME_DELAY, timeout, NULL);
225
226         gtk_widget_show_all (window);
227         gtk_main ();
228
229         return 0;
230 }