Mercurial > molko
annotate libcore/core/map.h @ 136:30b68089ae70
core: rework actions and a bit of drawables, closes #2492
In the effort of having as less as possible memory allocation in libcore, the
usage of actions and drawable no longer copy the original source parameter to
let user pass a heap allocated variable or a static storage one.
Update predefined drawable and actions to match these new needs.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 13 Oct 2020 09:38:44 +0200 |
parents | c679e08b32b2 |
children | b386d25832c8 |
rev | line source |
---|---|
27
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
1 /* |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
2 * map.h -- game map |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
3 * |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr> |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
5 * |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
6 * Permission to use, copy, modify, and/or distribute this software for any |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
7 * purpose with or without fee is hereby granted, provided that the above |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
8 * copyright notice and this permission notice appear in all copies. |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
9 * |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
17 */ |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
18 |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
19 #ifndef MOLKO_MAP_H |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
20 #define MOLKO_MAP_H |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
21 |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
22 /** |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
23 * \file map.h |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
24 * \brief Game map. |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
25 */ |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
26 |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
27 #include <stdbool.h> |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
28 #include <stdio.h> |
27
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
29 |
94
ed72843a7194
core: simplify font/texture interfaces
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
30 #include "texture.h" |
27
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
31 |
33 | 32 /** |
33 * \brief Max title length for a map. | |
34 */ | |
27
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
35 #define MAP_TITLE_MAX 32 |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
36 |
33 | 37 /** |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
38 * \brief Max filename for tilesets. |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
39 */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
40 #define MAP_TILESET_MAX FILENAME_MAX |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
41 |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
42 /** |
33 | 43 * \brief Map layer. |
44 */ | |
27
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
45 struct map_layer { |
46
b815621df3e3
core: remove all fixed width integers, closes #2460
David Demelier <markand@malikania.fr>
parents:
42
diff
changeset
|
46 unsigned short *tiles; /*!< (RO) Array of tiles, depending on the map size. */ |
27
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
47 }; |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
48 |
33 | 49 /** |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
50 * \brief Map definition structure. |
50
402aa7dcffe1
core: implement map origin, closes #2462 @1h
David Demelier <markand@malikania.fr>
parents:
46
diff
changeset
|
51 * |
402aa7dcffe1
core: implement map origin, closes #2462 @1h
David Demelier <markand@malikania.fr>
parents:
46
diff
changeset
|
52 * This structure only defines the map characteristics. It does not have any |
402aa7dcffe1
core: implement map origin, closes #2462 @1h
David Demelier <markand@malikania.fr>
parents:
46
diff
changeset
|
53 * logic and is left for game state. |
33 | 54 */ |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
55 struct map_data { |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
56 char title[MAP_TITLE_MAX]; /*!< (RW) The map title. */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
57 char tileset[MAP_TILESET_MAX]; /*!< (RO) Name of tileset to use. */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
58 int origin_x; /*!< (RO) Where the player starts in X. */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
59 int origin_y; /*!< (RO) Where the player starts in Y. */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
60 unsigned int real_w; /*!< (RO) Real width in pixels. */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
61 unsigned int real_h; /*!< (RO) Real height in pixels. */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
62 unsigned int w; /*!< (RO) Map width in cells. */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
63 unsigned int h; /*!< (RO) Map height in cells. */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
64 unsigned short tile_w; /*!< (RO) Pixels per cell (width). */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
65 unsigned short tile_h; /*!< (RO) Pixels per cell (height). */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
66 struct map_layer layers[2]; /*!< (RO) Layers (background, foreground). */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
67 }; |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
68 |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
69 /** |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
70 * \brief High level map object. |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
71 * |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
72 * This structure reference a map and perform drawing operations. |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
73 */ |
27
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
74 struct map { |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
75 struct map_data *data; /*!< (RW, ref) Map data. */ |
131 | 76 struct texture *tileset; /*!< (RW, ref) Tileset to use. */ |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
77 struct texture picture; /*!< (RO) Map drawn into a picture. */ |
27
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
78 }; |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
79 |
33 | 80 /** |
124
7b84764741b6
core: allows loading maps from memory, closes #2489
David Demelier <markand@malikania.fr>
parents:
121
diff
changeset
|
81 * Open a map defintion. |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
82 * |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
83 * \pre data != NULL |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
84 * \pre path != NULL |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
85 * \param data the map defintion to fill |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
86 * \param path the path to the map |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
87 * \return True if successfully loaded. |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
88 */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
89 bool |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
90 map_data_open(struct map_data *data, const char *path); |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
91 |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
92 /** |
124
7b84764741b6
core: allows loading maps from memory, closes #2489
David Demelier <markand@malikania.fr>
parents:
121
diff
changeset
|
93 * Open map data definition from memory. |
7b84764741b6
core: allows loading maps from memory, closes #2489
David Demelier <markand@malikania.fr>
parents:
121
diff
changeset
|
94 * |
7b84764741b6
core: allows loading maps from memory, closes #2489
David Demelier <markand@malikania.fr>
parents:
121
diff
changeset
|
95 *\pre data != NULL |
7b84764741b6
core: allows loading maps from memory, closes #2489
David Demelier <markand@malikania.fr>
parents:
121
diff
changeset
|
96 *\pre buf != NULL |
7b84764741b6
core: allows loading maps from memory, closes #2489
David Demelier <markand@malikania.fr>
parents:
121
diff
changeset
|
97 *\param data the map definition to fill |
7b84764741b6
core: allows loading maps from memory, closes #2489
David Demelier <markand@malikania.fr>
parents:
121
diff
changeset
|
98 *\param buf the source buffer |
7b84764741b6
core: allows loading maps from memory, closes #2489
David Demelier <markand@malikania.fr>
parents:
121
diff
changeset
|
99 *\param bufsz the source buffer size |
7b84764741b6
core: allows loading maps from memory, closes #2489
David Demelier <markand@malikania.fr>
parents:
121
diff
changeset
|
100 */ |
7b84764741b6
core: allows loading maps from memory, closes #2489
David Demelier <markand@malikania.fr>
parents:
121
diff
changeset
|
101 bool |
7b84764741b6
core: allows loading maps from memory, closes #2489
David Demelier <markand@malikania.fr>
parents:
121
diff
changeset
|
102 map_data_openmem(struct map_data *data, const void *buf, size_t bufsz); |
7b84764741b6
core: allows loading maps from memory, closes #2489
David Demelier <markand@malikania.fr>
parents:
121
diff
changeset
|
103 |
7b84764741b6
core: allows loading maps from memory, closes #2489
David Demelier <markand@malikania.fr>
parents:
121
diff
changeset
|
104 /** |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
105 * Dispose the map definition data. |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
106 * |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
107 * \pre data != NULL |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
108 * \param data the map definition |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
109 */ |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
110 void |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
111 map_data_finish(struct map_data *data); |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
112 |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
113 /** |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
114 * Initialize this map. |
33 | 115 * |
116 * \pre map != NULL | |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
117 * \pre data != NULL |
131 | 118 * \pre tileset != NULL && texture_ok(tileset) |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
119 * \param map the map to initialize |
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
120 * \param data the definition to reference |
131 | 121 * \param tileset the tileset to use |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
122 * \return False on errors. |
33 | 123 */ |
27
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
124 bool |
131 | 125 map_init(struct map *map, |
126 struct map_data *data, | |
127 struct texture *tileset); | |
27
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
128 |
33 | 129 /** |
130 * Render a map. | |
131 * | |
132 * \pre map != NULL | |
133 * \param map the map to render | |
42
22a09a5ee476
core: add better map check and unit tests
David Demelier <markand@malikania.fr>
parents:
41
diff
changeset
|
134 * \param srcx the x coordinate region |
22a09a5ee476
core: add better map check and unit tests
David Demelier <markand@malikania.fr>
parents:
41
diff
changeset
|
135 * \param srcy the y coordinate region |
33 | 136 */ |
27
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
137 void |
42
22a09a5ee476
core: add better map check and unit tests
David Demelier <markand@malikania.fr>
parents:
41
diff
changeset
|
138 map_draw(struct map *map, int srcx, int srcy); |
28
783841af4033
core: implement painter API, closes #2451 @1h
David Demelier <markand@malikania.fr>
parents:
27
diff
changeset
|
139 |
33 | 140 /** |
41
3996f873a54b
core: implement walksprite, closes #2455
David Demelier <markand@malikania.fr>
parents:
33
diff
changeset
|
141 * Force map repaint on its texture. |
3996f873a54b
core: implement walksprite, closes #2455
David Demelier <markand@malikania.fr>
parents:
33
diff
changeset
|
142 * |
3996f873a54b
core: implement walksprite, closes #2455
David Demelier <markand@malikania.fr>
parents:
33
diff
changeset
|
143 * \pre map != NULL |
3996f873a54b
core: implement walksprite, closes #2455
David Demelier <markand@malikania.fr>
parents:
33
diff
changeset
|
144 * \param map the map to repaint |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
145 * \warning This function does not render anything on the screen. |
41
3996f873a54b
core: implement walksprite, closes #2455
David Demelier <markand@malikania.fr>
parents:
33
diff
changeset
|
146 */ |
3996f873a54b
core: implement walksprite, closes #2455
David Demelier <markand@malikania.fr>
parents:
33
diff
changeset
|
147 void |
3996f873a54b
core: implement walksprite, closes #2455
David Demelier <markand@malikania.fr>
parents:
33
diff
changeset
|
148 map_repaint(struct map *map); |
3996f873a54b
core: implement walksprite, closes #2455
David Demelier <markand@malikania.fr>
parents:
33
diff
changeset
|
149 |
3996f873a54b
core: implement walksprite, closes #2455
David Demelier <markand@malikania.fr>
parents:
33
diff
changeset
|
150 /** |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
151 * Dispose map resources. |
33 | 152 * |
153 * \pre map != NULL | |
98
c7e993455985
map: split the map structure into two definitions
David Demelier <markand@malikania.fr>
parents:
94
diff
changeset
|
154 * \param map the map to close |
33 | 155 */ |
28
783841af4033
core: implement painter API, closes #2451 @1h
David Demelier <markand@malikania.fr>
parents:
27
diff
changeset
|
156 void |
94
ed72843a7194
core: simplify font/texture interfaces
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
157 map_finish(struct map *map); |
27
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
158 |
607bd90aba63
core: finalize map converter and opener, closes #2448 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
159 #endif /* !MOLKO_MAP_H */ |