comparison libmlk-rpg/mlk/rpg/tileset-loader.h @ 552:ffd972a3d084

rpg: major rewrite of tilesets - Now tilesets can be opened using a custom allocator/loader. - A default mlk_tileset_loader_file implementation is provided. - Put a simple example-tileset example to demonstrate.
author David Demelier <markand@malikania.fr>
date Tue, 07 Mar 2023 20:45:00 +0100
parents libmlk-rpg/mlk/rpg/tileset-file.h@6e8f6640e05b
children cdbc13ceff85
comparison
equal deleted inserted replaced
551:856c2e96189d 552:ffd972a3d084
1 /*
2 * tileset-loader.h -- abstract tileset loader
3 *
4 * Copyright (c) 2020-2023 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 MLK_RPG_TILESET_LOADER_H
20 #define MLK_RPG_TILESET_LOADER_H
21
22 /**
23 * \file mlk/rpg/tileset-loader.h
24 * \brief Abstract tileset loader
25 *
26 * This module provides a generic way to open tilesets. It uses a callback
27 * system whenever an action has to be taken by the user. by itself, this
28 * module does not alloate nor owns any data.
29 *
30 * It is designed in mind that the loader knows how to decode a tileset data
31 * format file but has no indication on how it should allocate, arrange and
32 * find tileset images and other resources.
33 *
34 * See tileset-file.h for an implementation of this module using files.
35 */
36
37 #include <stddef.h>
38
39 struct mlk_animation;
40 struct mlk_sprite;
41 struct mlk_texture;
42 struct mlk_tileset;
43 struct mlk_tileset_animations;
44 struct mlk_tileset_collision;
45
46 /**
47 * \struct mlk_tileset_loader
48 * \brief Abstract loader structure
49 *
50 * All function pointers must be set.
51 */
52 struct mlk_tileset_loader {
53 /**
54 * (read-write, borrowed, optional)
55 *
56 * Arbitrary user data for callbacks.
57 */
58 void *data;
59
60 /**
61 * (read-write)
62 *
63 * Open a texture from the given ident name.
64 *
65 * \param self this loader
66 * \param ident the texture name (or path)
67 * \return a borrowed texture or NULL on failure
68 */
69 struct mlk_texture * (*init_texture)(struct mlk_tileset_loader *self,
70 const char *ident);
71
72 /**
73 * (read-write)
74 *
75 * Return a sprite that the loader needs.
76 *
77 * \param self this loader
78 * \return a unused sprite
79 * \return a borrowed sprite or NULL on failure
80 */
81 struct mlk_sprite * (*init_sprite)(struct mlk_tileset_loader *self);
82
83 /**
84 * (read-write)
85 *
86 * Return an animation that the loader needs.
87 *
88 * \param self this loader
89 * \return a unused animation
90 * \return a borrowed animation or NULL on failure
91 */
92 struct mlk_animation * (*init_animation)(struct mlk_tileset_loader *self);
93
94 /**
95 * (read-write)
96 *
97 * Expand the collision array by one element.
98 *
99 * \param self this loader
100 * \param array the old array (can be NULL) to reallocate
101 * \param arraysz the new array size (usually +1 than before)
102 * \return a unused animation
103 */
104 struct mlk_tileset_collision * (*expand_collisions)(struct mlk_tileset_loader *self,
105 struct mlk_tileset_collision *array,
106 size_t arraysz);
107
108 /**
109 * (read-write)
110 *
111 * Expand the animation array by one element.
112 *
113 * \param self this loader
114 * \param array the old array (can be NULL) to reallocate
115 * \param arraysz the new array size (usually +1 than before)
116 * \return a unused animation
117 */
118 struct mlk_tileset_animation * (*expand_animations)(struct mlk_tileset_loader *self,
119 struct mlk_tileset_animation *array,
120 size_t arraysz);
121
122 /** \cond MLK_PRIVATE_DECLS */
123 unsigned int tilewidth;
124 unsigned int tileheight;
125 /** \endcond MLK_PRIVATE_DECLS */
126 };
127
128 #if defined(__cplusplus)
129 extern "C" {
130 #endif
131
132 /**
133 * Open a tileset from a filesystem path.
134 *
135 * \pre loader != NULL
136 * \param loader the loader
137 * \param tileset the tileset destination
138 * \param path the path to the tileset file
139 * \return 0 on success or an error code on failure
140 */
141 int
142 mlk_tileset_loader_open(struct mlk_tileset_loader *loader,
143 struct mlk_tileset *tileset,
144 const char *path);
145
146 /**
147 * Open a tileset from a const binary data.
148 *
149 * The binary data must be kept alive until the tileset loader is no longer
150 * used.
151 *
152 * \pre loader != NULL
153 * \param loader the loader
154 * \param tileset the tileset destination
155 * \param data the tileset content
156 * \param datasz the tileset content length
157 * \return 0 on success or an error code on failure
158 */
159 int
160 mlk_tileset_loader_openmem(struct mlk_tileset_loader *loader,
161 struct mlk_tileset *tileset,
162 const void *data,
163 size_t datasz);
164
165 #if defined(__cplusplus)
166 }
167 #endif
168
169 #endif /* !MLK_RPG_TILESET_LOADER_H */