diff --git a/src/main.c b/src/main.c index 4c1b947..cae67b4 100644 --- a/src/main.c +++ b/src/main.c @@ -25,10 +25,23 @@ int main(int argc, char* *argv){ NOD_Node2D *point2 = NOD_CreateNode2D("Point2D2"); point->pos.x = 2; - printf("The X value of '%s' is %d!\n", point->node.name, 3); + point->pos.y = 10; + printf("The X value of '%s' is %d!\n", point->node.name, point->pos.x); + printf("The Y value of '%s' is %d!\n", point->node.name, point->pos.y); + printf("\n"); + + point2->pos.x = 10; + point2->pos.y = 20; + printf("The X value of '%s' is %d!\n", point2->node.name, point2->pos.x); + printf("The Y value of '%s' is %d!\n", point2->node.name, point2->pos.y); + printf("\n"); NOD_NodeAddChild(point, point2); + printf("The true X value of '%s' is %d!\n", point2->node.name, NOD_GetAbsPosNode2D(point2).x); + printf("The true Y value of '%s' is %d!\n", point2->node.name, NOD_GetAbsPosNode2D(point2).y); + printf("\n"); + printf("The parent of '%s' is '%s'.\n", NOD(point2)->name, NOD(NOD(point2)->parent)->name); NOD_PrintNodeTree(point); diff --git a/src/node/node.h b/src/node/node.h index 6519abf..4e3ac99 100644 --- a/src/node/node.h +++ b/src/node/node.h @@ -30,11 +30,13 @@ void NOD_DestroyNodeBranch(NOD_Node *node); #define NOD_NodeAddChild(x, y) _nod_add_child(NOD(x), NOD(y)) #define NOD_NodeUnparent(x) _nod_unparent(NOD(x)) -#define NOD_PrintNodeTree(x) _nod_print_tree(NOD(x)) -#define NOD_NodeChildCount(x) _nod_child_count(NOD(x)); void _nod_add_child(NOD_Node *parent, NOD_Node *child); void _nod_unparent(NOD_Node *child); + +#define NOD_PrintNodeTree(x) _nod_print_tree(NOD(x)) +#define NOD_NodeChildCount(x) _nod_child_count(NOD(x)); + void _nod_print_tree(NOD_Node *node); int _nod_child_count(NOD_Node *node); diff --git a/src/node2d/node2d.c b/src/node2d/node2d.c index 5cd1789..3f834e4 100644 --- a/src/node2d/node2d.c +++ b/src/node2d/node2d.c @@ -4,7 +4,7 @@ NOD_Node2D *NOD_CreateNode2D(char *name){ NOD_Node2D *node = NULL; NOD_Node *plain_node = NULL; - plain_node = NOD_CreateNodeEX("Node2D", name); + plain_node = NOD_CreateNodeEX(_NOD_2D_TYPE, name); if(plain_node != NULL){ plain_node = realloc(plain_node, sizeof(NOD_Node2D)); @@ -19,3 +19,28 @@ NOD_Node2D *NOD_CreateNode2D(char *name){ return node; } + +vector2Di NOD_GetAbsPosNode2D(NOD_Node2D *node){ + vector2Di final_pos = {0, 0}; + + if(node != NULL){ + final_pos = node->pos; + + NOD_Node *parent = NOD(node)->parent; + + /* Skip non Node2D nodes */ + while(parent != NULL && strcmp(parent->type, _NOD_2D_TYPE) != 0){ + parent = parent->parent; + } + + /* Recurse to get the pos of the rest of the ascendence */ + if(parent != NULL){ + vector2Di parent_pos = NOD_GetAbsPosNode2D((NOD_Node2D *)parent); + + final_pos.x += parent_pos.x; + final_pos.y += parent_pos.y; + } + } + + return final_pos; +} diff --git a/src/node2d/node2d.h b/src/node2d/node2d.h index 7a015cd..bbbe3b2 100644 --- a/src/node2d/node2d.h +++ b/src/node2d/node2d.h @@ -3,6 +3,9 @@ #include #include +#include + +#define _NOD_2D_TYPE "NODE_2D" typedef struct node_2d{ NOD_Node node; @@ -11,3 +14,5 @@ typedef struct node_2d{ NOD_Node2D *NOD_CreateNode2D(char *name); void NOD_DestroyNode2D(NOD_Node *node); + +vector2Di NOD_GetAbsPosNode2D(NOD_Node2D *node);