comparison src/animation.h @ 7:fbb7101b7bd8

core: implement animations, closes #2439
author David Demelier <markand@malikania.fr>
date Mon, 06 Jan 2020 21:39:16 +0100
parents
children 5519ad48822e
comparison
equal deleted inserted replaced
6:3054723e53d7 7:fbb7101b7bd8
1 /*
2 * animation.h -- basic animations
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef MOLKO_ANIMATION_H
20 #define MOLKO_ANIMATION_H
21
22 /**
23 * \file animation.h
24 * \brief Basic animations.
25 */
26
27 #include <stdbool.h>
28 #include <stdint.h>
29
30 struct sprite;
31
32 /**
33 * \brief Animation object
34 */
35 struct animation {
36 struct sprite *sprite; /* Sprite to use (RW) */
37 uint16_t row; /* current row (RO) */
38 uint16_t column; /* current column (RO) */
39 uint16_t delay; /* delay between frames (RW) */
40 uint16_t elapsed; /* elapsed time since last frame (RO) */
41 };
42
43 /**
44 * Initialize the animation.
45 *
46 * The animation does not take sprite ownership, it must be valid until
47 * animation is no longer used.
48 *
49 * \pre an != NULL
50 * \pre sprite != NULL
51 * \param an the animation
52 * \param sprite the sprite to use
53 * \param delay the delay between frames in milliseconds
54 */
55 void
56 animation_init(struct animation *an, struct sprite *sprite, uint16_t delay);
57
58 /**
59 * Tells if the animation is complete.
60 *
61 * \pre an != NULL
62 * \param an the animation
63 * \return true if the animation has completed
64 */
65 bool
66 animation_is_complete(const struct animation *an);
67
68 /**
69 * Start an animation.
70 *
71 * \pre an != NULL
72 * \param an the animation
73 */
74 void
75 animation_start(struct animation *an);
76
77 /**
78 * Update the animation.
79 *
80 * You must call this function at each loop iteration to update the animation
81 * frame depending on its delay.
82 *
83 * \pre an != NULL
84 * \param an the animation
85 * \param ticks the elapsed ticks since the last call
86 */
87 void
88 animation_update(struct animation *an, unsigned ticks);
89
90 /**
91 * Draw the animation.
92 *
93 * \pre an != NULL
94 * \param an the animation
95 * \param x the X coordinate
96 * \param y the Y coordinate
97 */
98 void
99 animation_draw(struct animation *an, int x, int y);
100
101 #endif /* !MOLKO_ANIMATION_H */