Mercurial > molko
comparison librpg/rpg/map.h @ 197:852d0b7817ce
rpg: map, extreme cleanup, closes #2508 @4h
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 09 Nov 2020 10:37:36 +0100 |
parents | c577c15df07f |
children | 70e6ed74940d |
comparison
equal
deleted
inserted
replaced
196:658ee50b8bcb | 197:852d0b7817ce |
---|---|
22 /** | 22 /** |
23 * \file map.h | 23 * \file map.h |
24 * \brief Game map. | 24 * \brief Game map. |
25 */ | 25 */ |
26 | 26 |
27 #include <stdbool.h> | |
28 #include <stdio.h> | |
29 | |
30 #include <core/texture.h> | 27 #include <core/texture.h> |
31 | 28 |
32 /** | 29 #include "walksprite.h" |
33 * \brief Max title length for a map. | 30 |
34 */ | 31 struct sprite; |
35 #define MAP_TITLE_MAX 32 | 32 struct state; |
33 | |
34 union event; | |
36 | 35 |
37 /** | 36 /** |
38 * \brief Max filename for tilesets. | 37 * \brief Map layer type. |
39 */ | 38 */ |
40 #define MAP_TILESET_MAX FILENAME_MAX | 39 enum map_layer_type { |
40 MAP_LAYER_TYPE_BACKGROUND, /*!< Background layer. */ | |
41 MAP_LAYER_TYPE_FOREGROUND, /*!< Foreground layer. */ | |
42 MAP_LAYER_TYPE_ABOVE, /*!< Above foreground layer. */ | |
43 MAP_LAYER_TYPE_NUM /*!< Number of layers. */ | |
44 }; | |
41 | 45 |
42 /** | 46 /** |
43 * \brief Map layer. | 47 * \brief Map layer. |
44 */ | 48 */ |
45 struct map_layer { | 49 struct map_layer { |
46 unsigned short *tiles; /*!< (+) Array of tiles, depending on the map size. */ | 50 unsigned short *tiles; /*!< (+&) Array of tiles, depending on the map size. */ |
47 }; | 51 }; |
48 | 52 |
49 /** | 53 /** |
50 * \brief Map definition structure. | 54 * \brief Map object. |
51 * | 55 * |
52 * This structure only defines the map characteristics. It does not have any | 56 * The map object is used to move a player within the map according to the |
53 * logic and is left for game state. | 57 * tilesets and collisions masks. |
58 * | |
59 * By itself, a map does not know how to be loaded from a file and must be done | |
60 * from an helper like \ref map_file. | |
54 */ | 61 */ |
55 struct map_data { | 62 struct map { |
56 char title[MAP_TITLE_MAX]; /*!< (+) The map title. */ | 63 const char *title; /*!< (+) Map title name. */ |
57 char tileset[MAP_TILESET_MAX]; /*!< (+) Name of tileset to use. */ | |
58 int origin_x; /*!< (+) Where the player starts in X. */ | 64 int origin_x; /*!< (+) Where the player starts in X. */ |
59 int origin_y; /*!< (+) Where the player starts in Y. */ | 65 int origin_y; /*!< (+) Where the player starts in Y. */ |
60 unsigned int real_w; /*!< (-) Real width in pixels. */ | 66 unsigned int real_w; /*!< (-) Real width in pixels. */ |
61 unsigned int real_h; /*!< (-) Real height in pixels. */ | 67 unsigned int real_h; /*!< (-) Real height in pixels. */ |
62 unsigned int w; /*!< (-) Map width in cells. */ | 68 unsigned int w; /*!< (-) Map width in cells. */ |
63 unsigned int h; /*!< (-) Map height in cells. */ | 69 unsigned int h; /*!< (-) Map height in cells. */ |
64 unsigned short tile_w; /*!< (-) Pixels per cell (width). */ | 70 unsigned short tile_w; /*!< (-) Pixels per cell (width). */ |
65 unsigned short tile_h; /*!< (-) Pixels per cell (height). */ | 71 unsigned short tile_h; /*!< (-) Pixels per cell (height). */ |
66 struct map_layer layers[2]; /*!< (+) Layers (background, foreground). */ | 72 struct sprite *tileset; /*!< (+&) Tileset to use. */ |
73 struct texture picture; /*!< (-) Map drawn into a texture. */ | |
74 | |
75 /* Player. */ | |
76 struct sprite *player_sprite; /*!< (+) The sprite to use */ | |
77 struct walksprite player_ws; /*!< (-) Walking sprite for moving the player. */ | |
78 int player_x; /*!< (+) Player position in x */ | |
79 int player_y; /*!< (+) Player position in y */ | |
80 int player_angle; /*!< (+) Player angle (see walksprite) */ | |
81 unsigned int player_movement; /*!< (*) Current player movements. */ | |
82 | |
83 /* View to zoom/locate. */ | |
84 int view_x; /*!< (+) Position in x */ | |
85 int view_y; /*!< (+) Position in y */ | |
86 unsigned int view_w; /*!< (+) View width */ | |
87 unsigned int view_h; /*!< (+) View height */ | |
88 | |
89 /* View margin. */ | |
90 int margin_x; /*!< (+) View margin in x. */ | |
91 int margin_y; /*!< (+) View margin in y. */ | |
92 unsigned int margin_w; /*!< (+) Margin width. */ | |
93 unsigned int margin_h; /*!< (+) Margin height. */ | |
94 | |
95 /** | |
96 * Different tile layers. | |
97 */ | |
98 struct map_layer layers[MAP_LAYER_TYPE_NUM]; | |
67 }; | 99 }; |
68 | 100 |
69 /** | 101 /** |
70 * \brief High level map object. | 102 * Initialize the map. |
71 * | 103 * |
72 * This structure reference a map and perform drawing operations. | 104 * This function will re-generate the terrain and center the view to the player |
105 * position. | |
106 * | |
107 * \pre map != NULL | |
108 * \param map the map to initialize | |
73 */ | 109 */ |
74 struct map { | 110 bool |
75 struct map_data *data; /*!< (+&) Map data. */ | 111 map_init(struct map *map); |
76 struct texture *tileset; /*!< (+&) Tileset to use. */ | |
77 struct texture picture; /*!< (-) Map drawn into a picture. */ | |
78 }; | |
79 | 112 |
80 /** | 113 /** |
81 * Open a map defintion. | 114 * Handle an event. |
82 * | 115 * |
83 * \pre data != NULL | 116 * \pre map != NULL |
84 * \pre path != NULL | 117 * \pre ev != NULL |
85 * \param data the map defintion to fill | 118 * \param map the map |
86 * \param path the path to the map | 119 * \param ev the event to handle |
87 * \return True if successfully loaded. | |
88 */ | 120 */ |
89 bool | 121 void |
90 map_data_open(struct map_data *data, const char *path); | 122 map_handle(struct map *map, const union event *ev); |
91 | 123 |
92 /** | 124 /** |
93 * Open map data definition from memory. | 125 * Update the map. |
94 * | 126 * |
95 *\pre data != NULL | 127 * \pre map != NULL |
96 *\pre buf != NULL | 128 * \param map the map |
97 *\param data the map definition to fill | 129 * \param ticks ellapsed milliseconds since last frame |
98 *\param buf the source buffer | |
99 *\param bufsz the source buffer size | |
100 */ | |
101 bool | |
102 map_data_openmem(struct map_data *data, const void *buf, size_t bufsz); | |
103 | |
104 /** | |
105 * Dispose the map definition data. | |
106 * | |
107 * \pre data != NULL | |
108 * \param data the map definition | |
109 */ | 130 */ |
110 void | 131 void |
111 map_data_finish(struct map_data *data); | 132 map_update(struct map *map, unsigned int ticks); |
112 | |
113 /** | |
114 * Initialize this map. | |
115 * | |
116 * \pre map != NULL | |
117 * \pre data != NULL | |
118 * \pre tileset != NULL && texture_ok(tileset) | |
119 * \param map the map to initialize | |
120 * \param data the definition to reference | |
121 * \param tileset the tileset to use | |
122 * \return False on errors. | |
123 */ | |
124 bool | |
125 map_init(struct map *map, | |
126 struct map_data *data, | |
127 struct texture *tileset); | |
128 | 133 |
129 /** | 134 /** |
130 * Render a map. | 135 * Render a map. |
131 * | 136 * |
132 * \pre map != NULL | 137 * \pre map != NULL |
133 * \param map the map to render | 138 * \param map the map to render |
134 * \param srcx the x coordinate region | |
135 * \param srcy the y coordinate region | |
136 */ | 139 */ |
137 void | 140 void |
138 map_draw(struct map *map, int srcx, int srcy); | 141 map_draw(const struct map *map); |
139 | 142 |
140 /** | 143 /** |
141 * Force map repaint on its texture. | 144 * Force map repaint on its texture. |
142 * | 145 * |
143 * \pre map != NULL | 146 * \pre map != NULL |
146 */ | 149 */ |
147 void | 150 void |
148 map_repaint(struct map *map); | 151 map_repaint(struct map *map); |
149 | 152 |
150 /** | 153 /** |
154 * Convert the map into a game state. | |
155 * | |
156 * Both objects must exist until the state is no longer used. | |
157 * | |
158 * \pre map != NULL | |
159 * \pre state != NULL | |
160 * \param map the map to use | |
161 * \param state the state to fill | |
162 * \post state->data is set to map | |
163 * \post state->handle is set | |
164 * \post state->update is set | |
165 * \post state->draw is set | |
166 */ | |
167 void | |
168 map_state(struct map *map, struct state *state); | |
169 | |
170 /** | |
151 * Dispose map resources. | 171 * Dispose map resources. |
152 * | 172 * |
153 * \pre map != NULL | 173 * \pre map != NULL |
154 * \param map the map to close | 174 * \param map the map to close |
155 */ | 175 */ |