1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - Simple frame-based animations
4 * Copyright (C) Dom Lachowicz
6 * Authors: Dom Lachowicz <cinamod@hotmail.com>
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.
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.
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.
23 * Based on code originally by:
24 * Jonathan Blandford <jrb@redhat.com>
25 * Havoc Pennington <hp@redhat.com>
30 #include "gdk-pixbuf.h"
31 #include "gdk-pixbuf-io.h"
32 #include "gdk-pixbuf-simple-anim.h"
33 #include "gdk-pixbuf-alias.h"
35 struct _GdkPixbufSimpleAnimClass
37 GdkPixbufAnimationClass parent_class;
40 /* Private part of the GdkPixbufSimpleAnim structure */
41 struct _GdkPixbufSimpleAnim
43 GdkPixbufAnimation parent_instance;
59 typedef struct _GdkPixbufSimpleAnimIter GdkPixbufSimpleAnimIter;
60 typedef struct _GdkPixbufSimpleAnimIterClass GdkPixbufSimpleAnimIterClass;
62 #define GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER (gdk_pixbuf_simple_anim_iter_get_type ())
63 #define GDK_PIXBUF_SIMPLE_ANIM_ITER(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, GdkPixbufSimpleAnimIter))
64 #define GDK_IS_PIXBUF_SIMPLE_ANIM_ITER(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER))
66 #define GDK_PIXBUF_SIMPLE_ANIM_ITER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, GdkPixbufSimpleAnimIterClass))
67 #define GDK_IS_PIXBUF_SIMPLE_ANIM_ITER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER))
68 #define GDK_PIXBUF_SIMPLE_ANIM_ITER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, GdkPixbufSimpleAnimIterClass))
70 GType gdk_pixbuf_simple_anim_iter_get_type (void) G_GNUC_CONST;
73 struct _GdkPixbufSimpleAnimIterClass
75 GdkPixbufAnimationIterClass parent_class;
78 struct _GdkPixbufSimpleAnimIter
80 GdkPixbufAnimationIter parent_instance;
82 GdkPixbufSimpleAnim *simple_anim;
85 GTimeVal current_time;
92 typedef struct _GdkPixbufFrame GdkPixbufFrame;
93 struct _GdkPixbufFrame
100 static void gdk_pixbuf_simple_anim_finalize (GObject *object);
102 static gboolean is_static_image (GdkPixbufAnimation *animation);
103 static GdkPixbuf *get_static_image (GdkPixbufAnimation *animation);
105 static void get_size (GdkPixbufAnimation *anim,
108 static GdkPixbufAnimationIter *get_iter (GdkPixbufAnimation *anim,
109 const GTimeVal *start_time);
112 G_DEFINE_TYPE (GdkPixbufSimpleAnim, gdk_pixbuf_simple_anim, GDK_TYPE_PIXBUF_ANIMATION)
115 gdk_pixbuf_simple_anim_init (GdkPixbufSimpleAnim *anim)
120 gdk_pixbuf_simple_anim_class_init (GdkPixbufSimpleAnimClass *klass)
122 GObjectClass *object_class;
123 GdkPixbufAnimationClass *anim_class;
125 object_class = G_OBJECT_CLASS (klass);
126 anim_class = GDK_PIXBUF_ANIMATION_CLASS (klass);
128 object_class->finalize = gdk_pixbuf_simple_anim_finalize;
130 anim_class->is_static_image = is_static_image;
131 anim_class->get_static_image = get_static_image;
132 anim_class->get_size = get_size;
133 anim_class->get_iter = get_iter;
137 gdk_pixbuf_simple_anim_finalize (GObject *object)
139 GdkPixbufSimpleAnim *anim;
141 GdkPixbufFrame *frame;
143 anim = GDK_PIXBUF_SIMPLE_ANIM (object);
145 for (l = anim->frames; l; l = l->next) {
147 g_object_unref (frame->pixbuf);
151 g_list_free (anim->frames);
153 G_OBJECT_CLASS (gdk_pixbuf_simple_anim_parent_class)->finalize (object);
157 is_static_image (GdkPixbufAnimation *animation)
159 GdkPixbufSimpleAnim *anim;
161 anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
163 return (anim->frames != NULL && anim->frames->next == NULL);
167 get_static_image (GdkPixbufAnimation *animation)
169 GdkPixbufSimpleAnim *anim;
171 anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
173 if (anim->frames == NULL)
176 return ((GdkPixbufFrame *)anim->frames->data)->pixbuf;
180 get_size (GdkPixbufAnimation *animation,
184 GdkPixbufSimpleAnim *anim;
186 anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
189 *width = anim->width;
192 *height = anim->height;
196 iter_clear (GdkPixbufSimpleAnimIter *iter)
198 iter->current_frame = NULL;
202 iter_restart (GdkPixbufSimpleAnimIter *iter)
206 iter->current_frame = iter->simple_anim->frames;
209 static GdkPixbufAnimationIter *
210 get_iter (GdkPixbufAnimation *anim,
211 const GTimeVal *start_time)
213 GdkPixbufSimpleAnimIter *iter;
215 iter = g_object_new (GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, NULL);
217 iter->simple_anim = GDK_PIXBUF_SIMPLE_ANIM (anim);
219 g_object_ref (iter->simple_anim);
223 iter->start_time = *start_time;
224 iter->current_time = *start_time;
226 return GDK_PIXBUF_ANIMATION_ITER (iter);
229 static void gdk_pixbuf_simple_anim_iter_finalize (GObject *object);
231 static gint get_delay_time (GdkPixbufAnimationIter *iter);
232 static GdkPixbuf *get_pixbuf (GdkPixbufAnimationIter *iter);
233 static gboolean on_currently_loading_frame (GdkPixbufAnimationIter *iter);
234 static gboolean advance (GdkPixbufAnimationIter *iter,
235 const GTimeVal *current_time);
237 G_DEFINE_TYPE (GdkPixbufSimpleAnimIter, gdk_pixbuf_simple_anim_iter, GDK_TYPE_PIXBUF_ANIMATION_ITER)
240 gdk_pixbuf_simple_anim_iter_init (GdkPixbufSimpleAnimIter *iter)
245 gdk_pixbuf_simple_anim_iter_class_init (GdkPixbufSimpleAnimIterClass *klass)
247 GObjectClass *object_class;
248 GdkPixbufAnimationIterClass *anim_iter_class;
250 object_class = G_OBJECT_CLASS (klass);
251 anim_iter_class = GDK_PIXBUF_ANIMATION_ITER_CLASS (klass);
253 object_class->finalize = gdk_pixbuf_simple_anim_iter_finalize;
255 anim_iter_class->get_delay_time = get_delay_time;
256 anim_iter_class->get_pixbuf = get_pixbuf;
257 anim_iter_class->on_currently_loading_frame = on_currently_loading_frame;
258 anim_iter_class->advance = advance;
262 gdk_pixbuf_simple_anim_iter_finalize (GObject *object)
264 GdkPixbufSimpleAnimIter *iter;
266 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (object);
269 g_object_unref (iter->simple_anim);
271 G_OBJECT_CLASS (gdk_pixbuf_simple_anim_iter_parent_class)->finalize (object);
275 advance (GdkPixbufAnimationIter *anim_iter,
276 const GTimeVal *current_time)
278 GdkPixbufSimpleAnimIter *iter;
284 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
286 iter->current_time = *current_time;
288 /* We use milliseconds for all times */
289 elapsed = (((iter->current_time.tv_sec - iter->start_time.tv_sec) * G_USEC_PER_SEC +
290 iter->current_time.tv_usec - iter->start_time.tv_usec)) / 1000;
293 /* Try to compensate; probably the system clock
296 iter->start_time = iter->current_time;
300 g_assert (iter->simple_anim->total_time > 0);
302 /* See how many times we've already played the full animation,
303 * and subtract time for that.
305 loop = elapsed / iter->simple_anim->total_time;
306 elapsed = elapsed % iter->simple_anim->total_time;
308 iter->position = elapsed;
310 /* Now move to the proper frame */
312 tmp = iter->simple_anim->frames;
316 while (tmp != NULL) {
317 GdkPixbufFrame *frame = tmp->data;
319 if (iter->position >= frame->elapsed &&
320 iter->position < (frame->elapsed + frame->delay_time))
326 old = iter->current_frame;
328 iter->current_frame = tmp;
330 return iter->current_frame != old;
334 get_delay_time (GdkPixbufAnimationIter *anim_iter)
336 GdkPixbufFrame *frame;
337 GdkPixbufSimpleAnimIter *iter;
339 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
341 if (iter->current_frame) {
342 frame = iter->current_frame->data;
343 return frame->delay_time - (iter->position - frame->elapsed);
346 return -1; /* show last frame forever */
351 get_pixbuf (GdkPixbufAnimationIter *anim_iter)
353 GdkPixbufSimpleAnimIter *iter;
354 GdkPixbufFrame *frame;
356 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
358 if (iter->current_frame)
359 frame = iter->current_frame->data;
360 else if (g_list_length (iter->simple_anim->frames) > 0)
361 frame = g_list_last (iter->simple_anim->frames)->data;
368 return frame->pixbuf;
372 on_currently_loading_frame (GdkPixbufAnimationIter *anim_iter)
374 GdkPixbufSimpleAnimIter *iter;
376 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
378 return iter->current_frame == NULL || iter->current_frame->next == NULL;
382 * gdk_pixbuf_simple_anim_new:
383 * @width: the width of the animation
384 * @height: the height of the animation
385 * @rate: the speed of the animation, in frames per second
387 * Creates a new, empty animation.
389 * Returns: a newly allocated #GdkPixbufSimpleAnim
393 GdkPixbufSimpleAnim *
394 gdk_pixbuf_simple_anim_new (gint width,
398 GdkPixbufSimpleAnim *anim;
400 anim = g_object_new (GDK_TYPE_PIXBUF_SIMPLE_ANIM, NULL);
402 anim->height = height;
409 * gdk_pixbuf_simple_anim_add_frame:
410 * @animation: a #GdkPixbufSimpleAnim
411 * @pixbuf: the pixbuf to add
413 * Adds a new frame to @animation. The @pixbuf must
414 * have the dimensions specified when the animation
420 gdk_pixbuf_simple_anim_add_frame (GdkPixbufSimpleAnim *animation,
423 GdkPixbufFrame *frame;
426 g_return_if_fail (animation != NULL);
427 g_return_if_fail (pixbuf != NULL);
429 nframe = g_list_length (animation->frames);
431 frame = g_new0 (GdkPixbufFrame, 1);
432 frame->delay_time = (gint) (1000 / animation->rate);
433 frame->elapsed = (gint) (frame->delay_time * nframe);
434 animation->total_time += frame->delay_time;
435 frame->pixbuf = GDK_PIXBUF (g_object_ref (pixbuf));
437 animation->frames = g_list_append (animation->frames, frame);
441 #define __GDK_PIXBUF_SIMPLE_ANIM_C__
442 #include "gdk-pixbuf-aliasdef.c"