polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

node.h (2167B)


      1 
      2 #ifndef POLYADVENT_NODE_H
      3 #define POLYADVENT_NODE_H
      4 
      5 #include "resource.h"
      6 #include "mat4.h"
      7 #include <assert.h>
      8 
      9 #define MAX_NODE_CHILDREN 32
     10 
     11 enum node_flags {
     12   NODE_IGNORE_RECALC = 1 << 0
     13 };
     14 
     15 struct node_id {
     16 	struct resource_id id;
     17 };
     18 
     19 struct node {
     20     float pos[3];
     21     float rot[3];
     22     float scale[3];
     23     float mat[16];
     24     float orientation[4];
     25     char label[8];
     26     int needs_recalc;
     27     int flags;
     28     int n_children;
     29     void (*custom_update)(struct node*);
     30     void *custom_update_data;
     31 
     32     struct node_id parent_id;
     33     struct node_id children_ids[MAX_NODE_CHILDREN];
     34 };
     35 
     36 int node_recalc(struct node *root);
     37 void node_attach(struct node_id *node, struct node_id *to);
     38 int node_detach(struct node *node, struct node *from);
     39 void node_detach_from_parent(struct node *node);
     40 void node_mark_for_recalc(struct node *node);
     41 struct node *node_init(struct node *node);
     42 void node_translate(struct node *node, float *p);
     43 void node_forward(struct node *node, float *p);
     44 void node_forward_dir(struct node *node, float *orientation, float *p);
     45 void node_rotate(struct node *node, float *p);
     46 void node_scale(struct node *node, float val);
     47 int node_count(struct node *node);
     48 int node_set_label(struct node *node, const char *label);
     49 
     50 void destroy_node(struct node_id *);
     51 void init_node_manager();
     52 
     53 extern struct resource_manager node_manager;
     54 
     55 static inline struct node *static_nodes()
     56 {
     57 	return (struct node*)node_manager.resources;
     58 }
     59 
     60 static inline struct node *new_uninitialized_node(struct node_id *id)
     61 {
     62 	return new_resource(&node_manager, &id->id);
     63 }
     64 
     65 static inline struct node *new_node(struct node_id *id)
     66 {
     67 	struct node *n = node_init(new_uninitialized_node(id));
     68 	assert((int64_t)id->id.uuid != -1);
     69 	/* debug("new node %llu\n", id->uuid); */
     70 	return n;
     71 }
     72 
     73 static inline struct node *get_node(struct node_id *id)
     74 {
     75 	return get_resource(&node_manager, &id->id);
     76 }
     77 
     78 
     79 static inline int node_needs_recalc(struct node *node)
     80 {
     81 	struct node *parent = get_node(&node->parent_id);
     82 	return (parent && parent->needs_recalc) || node->needs_recalc;
     83 }
     84 
     85 static inline float *node_world(struct node *node)
     86 {
     87 	return &node->mat[M_X];
     88 }
     89 
     90 
     91 #endif /* POLYADVENT_NODE_H */