]> Pileus Git - ~andy/gtk/blob - demos/pixbuf-demo/pixbuf-demo.c
64b3f0490adb18660d9352c5611fdb9de4b1fe32
[~andy/gtk] / demos / pixbuf-demo / 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 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.
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  * Lesser General Public License for more details.
16  *
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.
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 static int back_width, back_height;
53
54 /* Images */
55 static GdkPixbuf *images[N_IMAGES];
56
57 /* Widgets */
58 static 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         /* We pass NULL for the error return location, we don't care
69          * about the error message.
70          */
71         
72         background = gdk_pixbuf_new_from_file (BACKGROUND_NAME, NULL);
73         if (!background)
74                 return FALSE;
75
76         back_width = gdk_pixbuf_get_width (background);
77         back_height = gdk_pixbuf_get_height (background);
78
79         for (i = 0; i < N_IMAGES; i++) {
80                 images[i] = gdk_pixbuf_new_from_file (image_names[i], NULL);
81                 if (!images[i])
82                         return FALSE;
83         }
84
85         return TRUE;
86 }
87
88 /* Expose callback for the drawing area */
89 static gboolean
90 draw_cb (GtkWidget *widget, cairo_t *cr, gpointer data)
91 {
92         gdk_cairo_set_source_pixbuf (cr, frame, 0, 0);
93         cairo_paint (cr);
94
95         return TRUE;
96 }
97
98 #define CYCLE_LEN 60
99
100 static int frame_num;
101
102 /* Timeout handler to regenerate the frame */
103 static gint
104 timeout (gpointer data)
105 {
106         double f;
107         int i;
108         double xmid, ymid;
109         double radius;
110
111         gdk_pixbuf_copy_area (background, 0, 0, back_width, back_height,
112                               frame, 0, 0);
113
114         f = (double) (frame_num % CYCLE_LEN) / CYCLE_LEN;
115
116         xmid = back_width / 2.0;
117         ymid = back_height / 2.0;
118
119         radius = MIN (xmid, ymid) / 2.0;
120
121         for (i = 0; i < N_IMAGES; i++) {
122                 double ang;
123                 int xpos, ypos;
124                 int iw, ih;
125                 double r;
126                 GdkRectangle r1, r2, dest;
127                 double k;
128
129                 ang = 2.0 * G_PI * (double) i / N_IMAGES - f * 2.0 * G_PI;
130
131                 iw = gdk_pixbuf_get_width (images[i]);
132                 ih = gdk_pixbuf_get_height (images[i]);
133
134                 r = radius + (radius / 3.0) * sin (f * 2.0 * G_PI);
135
136                 xpos = floor (xmid + r * cos (ang) - iw / 2.0 + 0.5);
137                 ypos = floor (ymid + r * sin (ang) - ih / 2.0 + 0.5);
138
139                 k = (i & 1) ? sin (f * 2.0 * G_PI) : cos (f * 2.0 * G_PI);
140                 k = 2.0 * k * k;
141                 k = MAX (0.25, k);
142
143                 r1.x = xpos;
144                 r1.y = ypos;
145                 r1.width = iw * k;
146                 r1.height = ih * k;
147
148                 r2.x = 0;
149                 r2.y = 0;
150                 r2.width = back_width;
151                 r2.height = back_height;
152
153                 if (gdk_rectangle_intersect (&r1, &r2, &dest))
154                         gdk_pixbuf_composite (images[i],
155                                               frame,
156                                               dest.x, dest.y,
157                                               dest.width, dest.height,
158                                               xpos, ypos,
159                                               k, k,
160                                               GDK_INTERP_NEAREST,
161                                               ((i & 1)
162                                                ? MAX (127, fabs (255 * sin (f * 2.0 * G_PI)))
163                                                : MAX (127, fabs (255 * cos (f * 2.0 * G_PI)))));
164         }
165
166         gtk_widget_queue_draw (da);
167
168         frame_num++;
169         return TRUE;
170 }
171
172 static guint timeout_id;
173
174 /* Destroy handler for the window */
175 static void
176 destroy_cb (GObject *object, gpointer data)
177 {
178         g_source_remove (timeout_id);
179         timeout_id = 0;
180
181         gtk_main_quit ();
182 }
183
184 int
185 main (int argc, char **argv)
186 {
187         GtkWidget *window;
188
189         gtk_init (&argc, &argv);
190
191         if (!load_pixbufs ()) {
192                 g_message ("main(): Could not load all the pixbufs!");
193                 exit (EXIT_FAILURE);
194         }
195
196         frame = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, back_width, back_height);
197
198         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
199
200         gtk_widget_set_size_request (window, back_width, back_height);
201         gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
202
203         g_signal_connect (window, "destroy",
204                           G_CALLBACK (destroy_cb), NULL);
205
206         da = gtk_drawing_area_new ();
207
208         g_signal_connect (da, "draw",
209                           G_CALLBACK (draw_cb), NULL);
210
211         gtk_container_add (GTK_CONTAINER (window), da);
212
213         timeout_id = gdk_threads_add_timeout (FRAME_DELAY, timeout, NULL);
214
215         gtk_widget_show_all (window);
216         gtk_main ();
217
218         return 0;
219 }