Mercurial > molko
comparison libcore/core/drawable.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 | 9301c7c84471 |
children | fea0cc899931 |
comparison
equal
deleted
inserted
replaced
135:eadfed7674ac | 136:30b68089ae70 |
---|---|
17 */ | 17 */ |
18 | 18 |
19 #ifndef MOLKO_DRAWABLE_H | 19 #ifndef MOLKO_DRAWABLE_H |
20 #define MOLKO_DRAWABLE_H | 20 #define MOLKO_DRAWABLE_H |
21 | 21 |
22 /** | |
23 * \file drawable.h | |
24 * \brief Automatic drawable objects. | |
25 */ | |
26 | |
22 #include <stdbool.h> | 27 #include <stdbool.h> |
23 | 28 |
24 /** | 29 /** |
25 * \brief Maximum number of drawable object into a stack. | 30 * \brief Maximum number of drawable object into a stack. |
26 */ | 31 */ |
27 #define DRAWABLE_STACK_MAX 128 | 32 #define DRAWABLE_STACK_MAX 128 |
28 | |
29 struct animation; | |
30 | 33 |
31 /** | 34 /** |
32 * \brief Abstract drawable object. | 35 * \brief Abstract drawable object. |
33 * | 36 * |
34 * This structure is used to | 37 * This structure is used to |
62 */ | 65 */ |
63 void (*finish)(struct drawable *dw); | 66 void (*finish)(struct drawable *dw); |
64 }; | 67 }; |
65 | 68 |
66 /** | 69 /** |
67 * Update the object | 70 * Shortcut for dw->update (if not NULL). |
68 * | 71 * |
69 * \pre dw != NULL | 72 * \pre dw != NULL |
70 * \param dw the drawable object | 73 * \param dw the drawable object |
71 * \param ticks elapsed milliseconds since last frame | 74 * \param ticks elapsed milliseconds since last frame |
72 * \return true if the drawable ended | 75 * \return true if the drawable ended |
73 */ | 76 */ |
74 bool | 77 bool |
75 drawable_update(struct drawable *dw, unsigned int ticks); | 78 drawable_update(struct drawable *dw, unsigned int ticks); |
76 | 79 |
77 /** | 80 /** |
78 * Draw the drawable. | 81 * Shortcut for dw->draw (if not NULL). |
79 * | 82 * |
80 * \pre dw != NULL | 83 * \pre dw != NULL |
81 * \param dw the drawable object | 84 * \param dw the drawable object |
82 */ | 85 */ |
83 void | 86 void |
84 drawable_draw(struct drawable *dw); | 87 drawable_draw(struct drawable *dw); |
85 | 88 |
86 /** | 89 /** |
87 * Dispose internal resources if necessary. | 90 * Shortcut for dw->finish (if not NULL). |
88 * | 91 * |
89 * \pre dw != NULL | 92 * \pre dw != NULL |
90 * \param dw the drawable object | 93 * \param dw the drawable object |
91 */ | 94 */ |
92 void | 95 void |
93 drawable_finish(struct drawable *dw); | 96 drawable_finish(struct drawable *dw); |
94 | 97 |
95 /** | 98 /** |
96 * Create a drawable from an animation. | |
97 * | |
98 * The animation is copied verbatim (as such internal resources must be kept | |
99 * valid). | |
100 * | |
101 * \pre dw != NULL | |
102 * \pre an the animation | |
103 * \param dw the drawable | |
104 * \param an the animation | |
105 * \param x x position on screen | |
106 * \param y y position on screen | |
107 */ | |
108 void | |
109 drawable_from_animation(struct drawable *dw, | |
110 const struct animation *an, | |
111 int x, | |
112 int y); | |
113 | |
114 /** | |
115 * \brief Stack of drawable objects. | 99 * \brief Stack of drawable objects. |
116 * | 100 * |
117 * This stack of drawable object can be used to store drawable objects within | 101 * This stack of drawable object can be used to store drawable objects within |
118 * a specific transition (state, battle, menu, etc). | 102 * a specific transition (state, battle, menu, etc). |
119 * | 103 * |
120 * You can add, clear, and update and draw them. | 104 * You can add, clear, update and draw them. |
121 */ | 105 */ |
122 struct drawable_stack { | 106 struct drawable_stack { |
123 struct drawable objects[DRAWABLE_STACK_MAX]; /*!< (RW) Drawables. */ | 107 struct drawable *objects[DRAWABLE_STACK_MAX]; /*!< (RW) Drawables. */ |
124 }; | 108 }; |
125 | 109 |
126 /** | 110 /** |
127 * Initialize the stack. | 111 * Initialize the stack. |
128 * | 112 * |
133 drawable_stack_init(struct drawable_stack *st); | 117 drawable_stack_init(struct drawable_stack *st); |
134 | 118 |
135 /** | 119 /** |
136 * Add a drawable object into the stack. | 120 * Add a drawable object into the stack. |
137 * | 121 * |
138 * The drawable object internals are copied verbatim into the stack. | 122 * The drawable object must be kept alive until the stack uses it. |
139 * | 123 * |
140 * \pre st != NULL | 124 * \pre st != NULL |
141 * \pre dw != NULL | 125 * \pre dw != NULL |
142 * \param st the stack | 126 * \param st the stack |
143 * \param dw the drawable to copy from | 127 * \param dw the drawable to reference |
144 * \return true if the drawable was placed correctly and false if there wasn't | 128 * \return true if the drawable was placed correctly and false if there wasn't |
145 * enough room. | 129 * enough room. |
146 */ | 130 */ |
147 bool | 131 bool |
148 drawable_stack_add(struct drawable_stack *st, const struct drawable *dw); | 132 drawable_stack_add(struct drawable_stack *st, struct drawable *dw); |
149 | 133 |
150 /** | 134 /** |
151 * Update all drawable objects. | 135 * Update all drawable objects. |
152 * | 136 * |
153 * Also remove drawable objects if they were finished. | 137 * Also remove drawable objects if they were finished. |