]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-simple-anim.c
82d908fc8488f2fe44a51b15a6505a3c817d1686
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf-simple-anim.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - Simple frame-based animations
3  *
4  * Copyright (C) Dom Lachowicz
5  *
6  * Authors: Dom Lachowicz <cinamod@hotmail.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *
23  * Based on code originally by:
24  *          Jonathan Blandford <jrb@redhat.com>
25  *          Havoc Pennington <hp@redhat.com>
26  */
27
28 #include <glib.h>
29
30 #include "gdk-pixbuf.h"
31 #include "gdk-pixbuf-io.h"
32 #include "gdk-pixbuf-simple-anim.h"
33 #include "gdk-pixbuf-alias.h"
34
35 struct _GdkPixbufSimpleAnimClass
36 {
37         GdkPixbufAnimationClass parent_class;
38 };
39
40 /* Private part of the GdkPixbufSimpleAnim structure */
41 struct _GdkPixbufSimpleAnim
42 {
43         GdkPixbufAnimation parent_instance;
44         
45         gint n_frames;
46         
47         gfloat rate;
48         gint total_time;
49         
50         GList *frames;
51         
52         gint width;
53         gint height;
54         
55         gboolean loop;
56 };
57
58 struct _GdkPixbufSimpleAnimIterClass
59 {
60         GdkPixbufAnimationIterClass parent_class;
61 };
62
63 struct _GdkPixbufSimpleAnimIter
64 {
65         GdkPixbufAnimationIter parent_instance;
66         
67         GdkPixbufSimpleAnim *simple_anim;
68         
69         GTimeVal start_time;
70         GTimeVal current_time;
71         
72         gint position;
73         
74         GList *current_frame;
75 };
76
77 typedef struct _GdkPixbufFrame GdkPixbufFrame;
78 struct _GdkPixbufFrame
79 {
80         GdkPixbuf *pixbuf;
81         gint delay_time;
82         gint elapsed;
83 };
84
85 static void gdk_pixbuf_simple_anim_finalize (GObject *object);
86
87 static gboolean   is_static_image  (GdkPixbufAnimation *animation);
88 static GdkPixbuf *get_static_image (GdkPixbufAnimation *animation);
89
90 static void       get_size         (GdkPixbufAnimation *anim,
91                                     gint               *width, 
92                                     gint               *height);
93 static GdkPixbufAnimationIter *get_iter (GdkPixbufAnimation *anim,
94                                          const GTimeVal     *start_time);
95
96
97 G_DEFINE_TYPE(GdkPixbufSimpleAnim, gdk_pixbuf_simple_anim, GDK_TYPE_PIXBUF_ANIMATION);
98
99 static void
100 gdk_pixbuf_simple_anim_init (GdkPixbufSimpleAnim *anim)
101 {
102 }
103
104 static void
105 gdk_pixbuf_simple_anim_class_init (GdkPixbufSimpleAnimClass *klass)
106 {
107         GObjectClass *object_class;
108         GdkPixbufAnimationClass *anim_class;
109
110         object_class = G_OBJECT_CLASS (klass);
111         anim_class = GDK_PIXBUF_ANIMATION_CLASS (klass);
112         
113         object_class->finalize = gdk_pixbuf_simple_anim_finalize;
114         
115         anim_class->is_static_image = is_static_image;
116         anim_class->get_static_image = get_static_image;
117         anim_class->get_size = get_size;
118         anim_class->get_iter = get_iter;
119 }
120
121 static void
122 gdk_pixbuf_simple_anim_finalize (GObject *object)
123 {
124         GdkPixbufSimpleAnim *anim;
125         GList *l;
126         GdkPixbufFrame *frame;
127         
128         anim = GDK_PIXBUF_SIMPLE_ANIM (object);        
129         
130         for (l = anim->frames; l; l = l->next) {
131                 frame = l->data;
132                 g_object_unref (frame->pixbuf);
133                 g_free (frame);
134         }
135         
136         g_list_free (anim->frames);
137         
138         G_OBJECT_CLASS (gdk_pixbuf_simple_anim_parent_class)->finalize (object);
139 }
140
141 static gboolean
142 is_static_image (GdkPixbufAnimation *animation)
143 {
144         GdkPixbufSimpleAnim *anim;
145         
146         anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
147
148         return (anim->frames != NULL && anim->frames->next == NULL);
149 }
150
151 static GdkPixbuf *
152 get_static_image (GdkPixbufAnimation *animation)
153 {
154         GdkPixbufSimpleAnim *anim;
155         
156         anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
157         
158         if (anim->frames == NULL)
159                 return NULL;
160         else
161                 return ((GdkPixbufFrame *)anim->frames->data)->pixbuf;
162 }
163
164 static void
165 get_size (GdkPixbufAnimation *animation,
166           gint               *width, 
167           gint               *height)
168 {
169         GdkPixbufSimpleAnim *anim;
170
171         anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
172         
173         if (width)
174                 *width = anim->width;
175         
176         if (height)
177                 *height = anim->height;
178 }
179
180 static void
181 iter_clear (GdkPixbufSimpleAnimIter *iter)
182 {
183         iter->current_frame = NULL;
184 }
185
186 static void
187 iter_restart (GdkPixbufSimpleAnimIter *iter)
188 {
189         iter_clear (iter);
190         
191         iter->current_frame = iter->simple_anim->frames;
192 }
193
194 static GdkPixbufAnimationIter *
195 get_iter (GdkPixbufAnimation *anim,
196           const GTimeVal    *start_time)
197 {
198         GdkPixbufSimpleAnimIter *iter;
199         
200         iter = g_object_new (GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, NULL);
201
202         iter->simple_anim = GDK_PIXBUF_SIMPLE_ANIM (anim);
203
204         g_object_ref (iter->simple_anim);
205         
206         iter_restart (iter);
207         
208         iter->start_time = *start_time;
209         iter->current_time = *start_time;
210         
211         return GDK_PIXBUF_ANIMATION_ITER (iter);
212 }
213
214 static void gdk_pixbuf_simple_anim_iter_finalize (GObject *object);
215
216 static gint       get_delay_time             (GdkPixbufAnimationIter *iter);
217 static GdkPixbuf *get_pixbuf                 (GdkPixbufAnimationIter *iter);
218 static gboolean   on_currently_loading_frame (GdkPixbufAnimationIter *iter);
219 static gboolean   advance                    (GdkPixbufAnimationIter *iter,
220                                               const GTimeVal         *current_time);
221
222 G_DEFINE_TYPE (GdkPixbufSimpleAnimIter, gdk_pixbuf_simple_anim_iter, GDK_TYPE_PIXBUF_ANIMATION_ITER);
223
224 static void
225 gdk_pixbuf_simple_anim_iter_init (GdkPixbufSimpleAnimIter *iter)
226 {
227 }
228
229 static void
230 gdk_pixbuf_simple_anim_iter_class_init (GdkPixbufSimpleAnimIterClass *klass)
231 {
232         GObjectClass *object_class;
233         GdkPixbufAnimationIterClass *anim_iter_class;
234
235         object_class = G_OBJECT_CLASS (klass);
236         anim_iter_class = GDK_PIXBUF_ANIMATION_ITER_CLASS (klass);
237         
238         object_class->finalize = gdk_pixbuf_simple_anim_iter_finalize;
239         
240         anim_iter_class->get_delay_time = get_delay_time;
241         anim_iter_class->get_pixbuf = get_pixbuf;
242         anim_iter_class->on_currently_loading_frame = on_currently_loading_frame;
243         anim_iter_class->advance = advance;
244 }
245
246 static void
247 gdk_pixbuf_simple_anim_iter_finalize (GObject *object)
248 {
249         GdkPixbufSimpleAnimIter *iter;
250         
251         iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (object);
252         iter_clear (iter);
253         
254         g_object_unref (iter->simple_anim);
255         
256         G_OBJECT_CLASS (gdk_pixbuf_simple_anim_iter_parent_class)->finalize (object);
257 }
258
259 static gboolean
260 advance (GdkPixbufAnimationIter *anim_iter,
261          const GTimeVal         *current_time)
262 {
263         GdkPixbufSimpleAnimIter *iter;
264         gint elapsed;
265         gint loop;
266         GList *tmp;
267         GList *old;
268         
269         iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
270         
271         iter->current_time = *current_time;
272         
273         /* We use milliseconds for all times */
274         elapsed = (((iter->current_time.tv_sec - iter->start_time.tv_sec) * G_USEC_PER_SEC +
275                     iter->current_time.tv_usec - iter->start_time.tv_usec)) / 1000;
276         
277         if (elapsed < 0) {
278                 /* Try to compensate; probably the system clock
279                  * was set backwards
280                  */
281                 iter->start_time = iter->current_time;
282                 elapsed = 0;
283         }
284         
285         g_assert (iter->simple_anim->total_time > 0);
286         
287         /* See how many times we've already played the full animation,
288          * and subtract time for that.
289          */
290         loop = elapsed / iter->simple_anim->total_time;
291         elapsed = elapsed % iter->simple_anim->total_time;
292         
293         iter->position = elapsed;
294         
295         /* Now move to the proper frame */
296         if (loop < 1)
297                 tmp = iter->simple_anim->frames;
298         else
299                 tmp = NULL;
300         
301         while (tmp != NULL) {
302                 GdkPixbufFrame *frame = tmp->data;
303                 
304                 if (iter->position >= frame->elapsed &&
305                     iter->position < (frame->elapsed + frame->delay_time))
306                         break;
307                 
308                 tmp = tmp->next;
309         }
310         
311         old = iter->current_frame;
312         
313         iter->current_frame = tmp;
314         
315         return iter->current_frame != old;
316 }
317
318 static gint
319 get_delay_time (GdkPixbufAnimationIter *anim_iter)
320 {
321         GdkPixbufFrame *frame;
322         GdkPixbufSimpleAnimIter *iter;
323
324         iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
325         
326         if (iter->current_frame) {
327                 frame = iter->current_frame->data;
328                 return frame->delay_time - (iter->position - frame->elapsed);
329         }
330         else {
331                 return -1;              /* show last frame forever */
332         }
333 }
334
335 static GdkPixbuf *
336 get_pixbuf (GdkPixbufAnimationIter *anim_iter)
337 {
338         GdkPixbufSimpleAnimIter *iter;
339         GdkPixbufFrame *frame;
340         
341         iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
342         
343         if (iter->current_frame)
344                 frame = iter->current_frame->data;
345         else if (g_list_length (iter->simple_anim->frames) > 0)
346                 frame = g_list_last (iter->simple_anim->frames)->data;
347         else
348                 frame = NULL;
349
350         if (frame == NULL)
351                 return NULL;
352         
353         return frame->pixbuf;
354 }
355
356 static gboolean
357 on_currently_loading_frame (GdkPixbufAnimationIter *anim_iter)
358 {
359   GdkPixbufSimpleAnimIter *iter;
360
361   iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
362
363   return iter->current_frame == NULL || iter->current_frame->next == NULL;
364 }
365
366 /**
367  * gdk_pixbuf_simple_anim_new:
368  * @width: the width of the animation
369  * @height: the height of the animation
370  * @rate: the speed of the animation, in frames per second
371  *
372  * Creates a new, empty animation.
373  *
374  * Returns: a newly allocated #GdkPixbufSimpleAnim 
375  *
376  * Since: 2.8
377  */
378 GdkPixbufSimpleAnim *
379 gdk_pixbuf_simple_anim_new (gint   width, 
380                             gint   height, 
381                             gfloat rate)
382 {
383   GdkPixbufSimpleAnim *anim;
384
385   anim = g_object_new (GDK_TYPE_PIXBUF_SIMPLE_ANIM, NULL);
386   anim->width = width;
387   anim->height = height;
388   anim->rate = rate;
389
390   return anim;
391 }
392
393 /**
394  * gdk_pixbuf_simple_anim_add_frame:
395  * @animation: a #GdkPixbufSimpleAnim
396  * @pixbuf: the pixbuf to add 
397  *
398  * Adds a new frame to @animation. The @pixbuf must
399  * have the dimensions specified when the animation 
400  * was constructed.
401  *
402  * Since: 2.8
403  */
404 void
405 gdk_pixbuf_simple_anim_add_frame (GdkPixbufSimpleAnim *animation,
406                                   GdkPixbuf           *pixbuf)
407 {
408   GdkPixbufFrame *frame;
409   int nframe = 0;
410   
411   g_return_if_fail (animation != NULL);
412   g_return_if_fail (pixbuf != NULL);
413   
414   nframe = g_list_length (animation->frames) + 1;
415   
416   frame = g_new0 (GdkPixbufFrame, 1);
417   frame->delay_time = (gint) (1000 / animation->rate);
418   frame->elapsed = (gint) (frame->delay_time * nframe);
419   animation->total_time += frame->delay_time;
420   frame->pixbuf = GDK_PIXBUF (g_object_ref (pixbuf));
421
422   animation->frames = g_list_append (animation->frames, frame);
423 }
424
425
426 #define __GDK_PIXBUF_SIMPLE_ANIM_C__
427 #include "gdk-pixbuf-aliasdef.c"