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;
58 struct _GdkPixbufSimpleAnimIterClass
60 GdkPixbufAnimationIterClass parent_class;
63 struct _GdkPixbufSimpleAnimIter
65 GdkPixbufAnimationIter parent_instance;
67 GdkPixbufSimpleAnim *simple_anim;
70 GTimeVal current_time;
77 typedef struct _GdkPixbufFrame GdkPixbufFrame;
78 struct _GdkPixbufFrame
85 static void gdk_pixbuf_simple_anim_finalize (GObject *object);
87 static gboolean is_static_image (GdkPixbufAnimation *animation);
88 static GdkPixbuf *get_static_image (GdkPixbufAnimation *animation);
90 static void get_size (GdkPixbufAnimation *anim,
93 static GdkPixbufAnimationIter *get_iter (GdkPixbufAnimation *anim,
94 const GTimeVal *start_time);
97 G_DEFINE_TYPE(GdkPixbufSimpleAnim, gdk_pixbuf_simple_anim, GDK_TYPE_PIXBUF_ANIMATION);
100 gdk_pixbuf_simple_anim_init (GdkPixbufSimpleAnim *anim)
105 gdk_pixbuf_simple_anim_class_init (GdkPixbufSimpleAnimClass *klass)
107 GObjectClass *object_class;
108 GdkPixbufAnimationClass *anim_class;
110 object_class = G_OBJECT_CLASS (klass);
111 anim_class = GDK_PIXBUF_ANIMATION_CLASS (klass);
113 object_class->finalize = gdk_pixbuf_simple_anim_finalize;
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;
122 gdk_pixbuf_simple_anim_finalize (GObject *object)
124 GdkPixbufSimpleAnim *anim;
126 GdkPixbufFrame *frame;
128 anim = GDK_PIXBUF_SIMPLE_ANIM (object);
130 for (l = anim->frames; l; l = l->next) {
132 g_object_unref (frame->pixbuf);
136 g_list_free (anim->frames);
138 G_OBJECT_CLASS (gdk_pixbuf_simple_anim_parent_class)->finalize (object);
142 is_static_image (GdkPixbufAnimation *animation)
144 GdkPixbufSimpleAnim *anim;
146 anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
148 return (anim->frames != NULL && anim->frames->next == NULL);
152 get_static_image (GdkPixbufAnimation *animation)
154 GdkPixbufSimpleAnim *anim;
156 anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
158 if (anim->frames == NULL)
161 return ((GdkPixbufFrame *)anim->frames->data)->pixbuf;
165 get_size (GdkPixbufAnimation *animation,
169 GdkPixbufSimpleAnim *anim;
171 anim = GDK_PIXBUF_SIMPLE_ANIM (animation);
174 *width = anim->width;
177 *height = anim->height;
181 iter_clear (GdkPixbufSimpleAnimIter *iter)
183 iter->current_frame = NULL;
187 iter_restart (GdkPixbufSimpleAnimIter *iter)
191 iter->current_frame = iter->simple_anim->frames;
194 static GdkPixbufAnimationIter *
195 get_iter (GdkPixbufAnimation *anim,
196 const GTimeVal *start_time)
198 GdkPixbufSimpleAnimIter *iter;
200 iter = g_object_new (GDK_TYPE_PIXBUF_SIMPLE_ANIM_ITER, NULL);
202 iter->simple_anim = GDK_PIXBUF_SIMPLE_ANIM (anim);
204 g_object_ref (iter->simple_anim);
208 iter->start_time = *start_time;
209 iter->current_time = *start_time;
211 return GDK_PIXBUF_ANIMATION_ITER (iter);
214 static void gdk_pixbuf_simple_anim_iter_finalize (GObject *object);
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);
222 G_DEFINE_TYPE (GdkPixbufSimpleAnimIter, gdk_pixbuf_simple_anim_iter, GDK_TYPE_PIXBUF_ANIMATION_ITER);
225 gdk_pixbuf_simple_anim_iter_init (GdkPixbufSimpleAnimIter *iter)
230 gdk_pixbuf_simple_anim_iter_class_init (GdkPixbufSimpleAnimIterClass *klass)
232 GObjectClass *object_class;
233 GdkPixbufAnimationIterClass *anim_iter_class;
235 object_class = G_OBJECT_CLASS (klass);
236 anim_iter_class = GDK_PIXBUF_ANIMATION_ITER_CLASS (klass);
238 object_class->finalize = gdk_pixbuf_simple_anim_iter_finalize;
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;
247 gdk_pixbuf_simple_anim_iter_finalize (GObject *object)
249 GdkPixbufSimpleAnimIter *iter;
251 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (object);
254 g_object_unref (iter->simple_anim);
256 G_OBJECT_CLASS (gdk_pixbuf_simple_anim_iter_parent_class)->finalize (object);
260 advance (GdkPixbufAnimationIter *anim_iter,
261 const GTimeVal *current_time)
263 GdkPixbufSimpleAnimIter *iter;
269 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
271 iter->current_time = *current_time;
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;
278 /* Try to compensate; probably the system clock
281 iter->start_time = iter->current_time;
285 g_assert (iter->simple_anim->total_time > 0);
287 /* See how many times we've already played the full animation,
288 * and subtract time for that.
290 loop = elapsed / iter->simple_anim->total_time;
291 elapsed = elapsed % iter->simple_anim->total_time;
293 iter->position = elapsed;
295 /* Now move to the proper frame */
297 tmp = iter->simple_anim->frames;
301 while (tmp != NULL) {
302 GdkPixbufFrame *frame = tmp->data;
304 if (iter->position >= frame->elapsed &&
305 iter->position < (frame->elapsed + frame->delay_time))
311 old = iter->current_frame;
313 iter->current_frame = tmp;
315 return iter->current_frame != old;
319 get_delay_time (GdkPixbufAnimationIter *anim_iter)
321 GdkPixbufFrame *frame;
322 GdkPixbufSimpleAnimIter *iter;
324 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
326 if (iter->current_frame) {
327 frame = iter->current_frame->data;
328 return frame->delay_time - (iter->position - frame->elapsed);
331 return -1; /* show last frame forever */
336 get_pixbuf (GdkPixbufAnimationIter *anim_iter)
338 GdkPixbufSimpleAnimIter *iter;
339 GdkPixbufFrame *frame;
341 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
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;
353 return frame->pixbuf;
357 on_currently_loading_frame (GdkPixbufAnimationIter *anim_iter)
359 GdkPixbufSimpleAnimIter *iter;
361 iter = GDK_PIXBUF_SIMPLE_ANIM_ITER (anim_iter);
363 return iter->current_frame == NULL || iter->current_frame->next == NULL;
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
372 * Creates a new, empty animation.
374 * Returns: a newly allocated #GdkPixbufSimpleAnim
378 GdkPixbufSimpleAnim *
379 gdk_pixbuf_simple_anim_new (gint width,
383 GdkPixbufSimpleAnim *anim;
385 anim = g_object_new (GDK_TYPE_PIXBUF_SIMPLE_ANIM, NULL);
387 anim->height = height;
394 * gdk_pixbuf_simple_anim_add_frame:
395 * @animation: a #GdkPixbufSimpleAnim
396 * @pixbuf: the pixbuf to add
398 * Adds a new frame to @animation. The @pixbuf must
399 * have the dimensions specified when the animation
405 gdk_pixbuf_simple_anim_add_frame (GdkPixbufSimpleAnim *animation,
408 GdkPixbufFrame *frame;
411 g_return_if_fail (animation != NULL);
412 g_return_if_fail (pixbuf != NULL);
414 nframe = g_list_length (animation->frames) + 1;
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));
422 animation->frames = g_list_append (animation->frames, frame);
426 #define __GDK_PIXBUF_SIMPLE_ANIM_C__
427 #include "gdk-pixbuf-aliasdef.c"