Mercurial > molko
comparison src/libmlk-rpg/rpg/tileset.c @ 320:8f9937403749
misc: improve loading of data
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 01 Oct 2021 20:30:00 +0200 |
parents | libmlk-rpg/rpg/tileset.c@d01e83210ca2 |
children | 460c78706989 |
comparison
equal
deleted
inserted
replaced
319:b843eef4cc35 | 320:8f9937403749 |
---|---|
1 /* | |
2 * tileset.c -- map tileset definition | |
3 * | |
4 * Copyright (c) 2020-2021 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 #include <assert.h> | |
20 #include <stdlib.h> | |
21 | |
22 #include <core/animation.h> | |
23 #include <core/sprite.h> | |
24 | |
25 #include "tileset.h" | |
26 | |
27 static inline int | |
28 anim_cmp(const void *d1, const void *d2) | |
29 { | |
30 const struct tileset_animation *mtd1 = d1; | |
31 const struct tileset_animation *mtd2 = d2; | |
32 | |
33 if (mtd1->id < mtd2->id) | |
34 return -1; | |
35 if (mtd1->id > mtd2->id) | |
36 return 1; | |
37 | |
38 return 0; | |
39 } | |
40 | |
41 static inline const struct tileset_animation * | |
42 find(const struct tileset *ts, unsigned int r, unsigned int c) | |
43 { | |
44 const struct tileset_animation key = { | |
45 .id = c + (r * ts->sprite->ncols) | |
46 }; | |
47 | |
48 return bsearch(&key, ts->anims, ts->animsz, sizeof (key), anim_cmp); | |
49 } | |
50 | |
51 int | |
52 tileset_ok(const struct tileset *ts) | |
53 { | |
54 return ts && sprite_ok(ts->sprite); | |
55 } | |
56 | |
57 void | |
58 tileset_start(struct tileset *ts) | |
59 { | |
60 for (size_t i = 0; i < ts->animsz; ++i) { | |
61 struct tileset_animation *ta = &ts->anims[i]; | |
62 | |
63 if (ta->animation) | |
64 animation_start(ta->animation); | |
65 } | |
66 } | |
67 | |
68 void | |
69 tileset_update(struct tileset *ts, unsigned int ticks) | |
70 { | |
71 for (size_t i = 0; i < ts->animsz; ++i) { | |
72 struct tileset_animation *ta = &ts->anims[i]; | |
73 | |
74 if (!ta->animation) | |
75 continue; | |
76 | |
77 if (animation_update(ta->animation, ticks)) | |
78 animation_start(ta->animation); | |
79 } | |
80 } | |
81 | |
82 void | |
83 tileset_draw(const struct tileset *ts, unsigned int r, unsigned int c, int x, int y) | |
84 { | |
85 assert(ts); | |
86 | |
87 const struct tileset_animation *ta; | |
88 | |
89 if ((ta = find(ts, r, c))) | |
90 animation_draw(ta->animation, x, y); | |
91 else | |
92 sprite_draw(ts->sprite, r, c, x, y); | |
93 } |