comparison libmlk-rpg/rpg/tileset.h @ 243:71b3b7036de7

misc: lot of cleanups, - prefix libraries with libmlk, - move assets from source directories closes #2520, - prefix header guards closes #2519
author David Demelier <markand@malikania.fr>
date Sat, 28 Nov 2020 22:37:30 +0100
parents librpg/rpg/tileset.h@e71039d820a7
children 08ab73b32832
comparison
equal deleted inserted replaced
242:4c24604efcab 243:71b3b7036de7
1 /*
2 * tileset.h -- map tileset definition
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_RPG_TILESET_H
20 #define MOLKO_RPG_TILESET_H
21
22 #include <stdbool.h>
23 #include <stddef.h>
24
25 struct sprite;
26
27 /**
28 * \brief Describe a tile property in a tileset.
29 *
30 * It can contains an animation and a collision mask.
31 */
32 struct tileset_tiledef {
33 unsigned short id; /*!< (+) Tile index. */
34 short x; /*!< (+) Collision region starts in y. */
35 short y; /*!< (+) Collision region starts in y. */
36 unsigned short w; /*!< (+) Collision width. */
37 unsigned short h; /*!< (+) Collision height. */
38 };
39
40 /**
41 * \brief Per tile animation.
42 *
43 * This structure is used to animate tiles by id. Create one for every tile
44 * that must be animated.
45 */
46 struct tileset_animation {
47 unsigned short id; /*!< (*) Tile index. */
48 struct animation *animation; /*!< (+&?) Animation. */
49 };
50
51 /**
52 * \brief Tileset definition.
53 */
54 struct tileset {
55 struct tileset_tiledef *tiledefs; /*!< (+&?) Per tile properties (must be sorted by id). */
56 size_t tiledefsz; /*!< (+) Number of tile properties. */
57 struct tileset_animation *anims; /*!< (+&?) Per tile animations (must be sorted by id). */
58 size_t animsz; /*!< (+) Number of tile animations. */
59 struct sprite *sprite; /*!< (+&) Sprite to generate the terrain. */
60 };
61
62 /**
63 * Tells if the tileset is correctly initialized.
64 *
65 * \param ts the tileset to check (maybe NULL)
66 * \return True if correctly initialized.
67 */
68 bool
69 tileset_ok(const struct tileset *ts);
70
71 /**
72 * Prepare the tileset before use.
73 *
74 * You must call this function once before using it in the rendering of the map
75 * because it will prepare animations.
76 *
77 * \pre ts != NULL
78 * \param ts the tileset to prepare
79 */
80 void
81 tileset_start(struct tileset *ts);
82
83 /**
84 * Update tileset.
85 *
86 * This function will update tileset animations. It is not necessary to call it
87 * if the tileset does not contain any animation.
88 *
89 * \pre ts != NULL
90 * \param ts the tileset to update
91 * \param ticks the elapsed milliseconds since last frame
92 */
93 void
94 tileset_update(struct tileset *ts, unsigned int ticks);
95
96 /**
97 * Draw a tileset cell at the given position.
98 *
99 * If the tileset at the given row, column is animated its animation will be
100 * rendered otherwise it uses the sprite row column.
101 *
102 * The argument r and c must not be out of bounds.
103 *
104 * \pre ts != NULL
105 * \param ts the tileset to use
106 * \param r the row number
107 * \param c the column number
108 * \param x the x coordinate
109 * \param y the y coordinate
110 */
111 void
112 tileset_draw(const struct tileset *ts, unsigned int r, unsigned int c, int x, int y);
113
114 #endif /* !MOLKO_RPG_TILESET_H */