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