src/ascension/scene_node.h

changeset 206
26726b7a89a7
parent 204
be5cf64b5c29
equal deleted inserted replaced
205:d1e44c861426 206:26726b7a89a7
26 */ 26 */
27 27
28 #ifndef ASCENSION_SCENE_NODE_H 28 #ifndef ASCENSION_SCENE_NODE_H
29 #define ASCENSION_SCENE_NODE_H 29 #define ASCENSION_SCENE_NODE_H
30 30
31 #include <cx/allocator.h>
31 #include <cx/list.h> 32 #include <cx/list.h>
32 #include <cx/string.h> 33 #include <cx/string.h>
33 34
34 #include "datatypes.h" 35 #include "datatypes.h"
35 #include "transform.h" 36 #include "transform.h"
74 /** 75 /**
75 * Custom flags for this node. 76 * Custom flags for this node.
76 * The #ASC_SCENE_NODE_FLAGS_MASK bits are reserved for general flags. 77 * The #ASC_SCENE_NODE_FLAGS_MASK bits are reserved for general flags.
77 */ 78 */
78 uint32_t flags; 79 uint32_t flags;
80 void *user_data;
81 const CxAllocator *user_data_allocator;
82 /**
83 * A free function that takes the allocator as the first argument and the data as the second.
84 */
85 cx_destructor_func2 user_data_free_func;
79 }; 86 };
80 87
81 88
82 /** 89 /**
83 * The reserved bits for general flags. 90 * The reserved bits for general flags.
101 #define ASC_SCENE_NODE_TRANSFORM_UPDATED 0x08000000 108 #define ASC_SCENE_NODE_TRANSFORM_UPDATED 0x08000000
102 /** 109 /**
103 * Set when the node is not supposed to be shown on screen. 110 * Set when the node is not supposed to be shown on screen.
104 */ 111 */
105 #define ASC_SCENE_NODE_HIDDEN 0x80000000 112 #define ASC_SCENE_NODE_HIDDEN 0x80000000
106
107 // TODO: some functions are prefixed asc_scene_node_ and others just asc_node_
108 113
109 /** 114 /**
110 * Creates an empty node that may serve as a container for other nodes. 115 * Creates an empty node that may serve as a container for other nodes.
111 * 116 *
112 * The free_func of this node will be a simple free(). 117 * The free_func of this node will be a simple free().
129 134
130 /** 135 /**
131 * Calculates the transformation matrix from components. 136 * Calculates the transformation matrix from components.
132 * 137 *
133 * Used internally, usually you never need to call this. 138 * Used internally, usually you never need to call this.
134 * Use asc_node_update_transform() to trigger a recalculation. 139 * Use asc_scene_node_update_transform() to trigger a recalculation.
135 * 140 *
136 * @param node the node 141 * @param node the node
137 * @see asc_node_update_transform() 142 * @see asc_scene_node_update_transform()
138 */ 143 */
139 void asc_scene_node_calculate_transform(AscSceneNode *node); 144 void asc_scene_node_calculate_transform(AscSceneNode *node);
140 145
141 /** 146 /**
142 * Sets the name of a node. 147 * Sets the name of a node.
175 * 180 *
176 * @param node the node to unlink 181 * @param node the node to unlink
177 */ 182 */
178 void asc_scene_node_unlink(AscSceneNode *node); 183 void asc_scene_node_unlink(AscSceneNode *node);
179 184
180 void asc_node_update(AscSceneNode *node); 185 void asc_scene_node_update(AscSceneNode *node);
181 186
182 void asc_node_update_transform(AscSceneNode *node); 187 void asc_scene_node_update_transform(AscSceneNode *node);
183 188
189 void asc_scene_node_allocate_data(AscSceneNode *node, size_t n);
184 190
185 /** 191 /**
186 * This is the z-position a simple 2D element should have to allow 192 * This is the z-position a simple 2D element should have to allow
187 * stacking 2D elements with depth-test. 193 * stacking 2D elements with depth-test.
188 */ 194 */
189 #define ASC_SCENE_2D_DEPTH_OFFSET 0.0078125f 195 #define ASC_SCENE_2D_DEPTH_OFFSET 0.0078125f
190 196
191 static inline void asc_node_set_position(AscSceneNode *node, asc_vec3f position) { 197 static inline void asc_scene_node_set_position(AscSceneNode *node, asc_vec3f position) {
192 node->position = position; 198 node->position = position;
193 asc_node_update_transform(node); 199 asc_scene_node_update_transform(node);
194 } 200 }
195 201
196 static inline void asc_node_set_scale(AscSceneNode *node, asc_vec3f scale) { 202 static inline void asc_scene_node_move(AscSceneNode *node, asc_vec3f offset) {
203 node->position.x += offset.x;
204 node->position.y += offset.y;
205 node->position.z += offset.z;
206 asc_scene_node_update_transform(node);
207 }
208
209 static inline void asc_scene_node_set_scale(AscSceneNode *node, asc_vec3f scale) {
197 node->scale = scale; 210 node->scale = scale;
198 asc_node_update_transform(node); 211 asc_scene_node_update_transform(node);
199 } 212 }
200 213
201 static inline void asc_node_set_origin(AscSceneNode *node, asc_vec3f origin) { 214 static inline void asc_scene_node_set_origin(AscSceneNode *node, asc_vec3f origin) {
202 node->origin = origin; 215 node->origin = origin;
203 asc_node_update_transform(node); 216 asc_scene_node_update_transform(node);
204 } 217 }
205 218
206 static inline void asc_node_set_position2f(AscSceneNode *node, asc_vec2f position) { 219 static inline void asc_scene_node_set_position2f(AscSceneNode *node, asc_vec2f position) {
207 node->position.x = position.x; 220 node->position.x = position.x;
208 node->position.y = position.y; 221 node->position.y = position.y;
209 asc_node_update_transform(node); 222 asc_scene_node_update_transform(node);
210 } 223 }
211 224
212 static inline void asc_node_set_scale2f(AscSceneNode *node, asc_vec2f scale) { 225 static inline void asc_scene_node_move2f(AscSceneNode *node, asc_vec2f offset) {
226 node->position.x += offset.x;
227 node->position.y += offset.y;
228 asc_scene_node_update_transform(node);
229 }
230
231 static inline void asc_scene_node_set_scale2f(AscSceneNode *node, asc_vec2f scale) {
213 node->scale.width = scale.width; 232 node->scale.width = scale.width;
214 node->scale.height = scale.height; 233 node->scale.height = scale.height;
215 asc_node_update_transform(node); 234 asc_scene_node_update_transform(node);
216 } 235 }
217 236
218 static inline void asc_node_set_origin2f(AscSceneNode *node, asc_vec2f origin) { 237 static inline void asc_scene_node_set_origin2f(AscSceneNode *node, asc_vec2f origin) {
219 node->origin.x = origin.x; 238 node->origin.x = origin.x;
220 node->origin.y = origin.y; 239 node->origin.y = origin.y;
221 asc_node_update_transform(node); 240 asc_scene_node_update_transform(node);
222 } 241 }
223 242
224 static inline void asc_node_set_rotation(AscSceneNode *node, asc_transform rotation) { 243 static inline void asc_scene_node_set_rotation(AscSceneNode *node, asc_transform rotation) {
225 memcpy(node->rotation, rotation, ASC_TRANSFORM_SIZE); 244 memcpy(node->rotation, rotation, ASC_TRANSFORM_SIZE);
226 asc_node_update_transform(node); 245 asc_scene_node_update_transform(node);
227 } 246 }
228 247
229 static inline void asc_node_roll_deg(AscSceneNode *node, float angle) { 248 static inline void asc_scene_node_roll_deg(AscSceneNode *node, float angle) {
230 asc_transform r, d; 249 asc_transform r, d;
231 asc_transform_roll(r, asc_rad(angle)); 250 asc_transform_roll(r, asc_rad(angle));
232 asc_transform_apply(d, r, node->rotation); 251 asc_transform_apply(d, r, node->rotation);
233 asc_transform_copy(node->rotation, d); 252 asc_transform_copy(node->rotation, d);
234 asc_node_update_transform(node); 253 asc_scene_node_update_transform(node);
235 } 254 }
236 255
237 #endif 256 #endif

mercurial