Compare commits
3 Commits
48908719fa
...
01e4d7c9ec
Author | SHA1 | Date |
---|---|---|
Dendy | 01e4d7c9ec | |
Dendy | 9e702bcdbb | |
Blumi | 643e2e5ba4 |
2
Makefile
2
Makefile
|
@ -6,7 +6,7 @@
|
|||
|
||||
CC="tcc"
|
||||
|
||||
main: src/main.c src/node.c
|
||||
main: src/main.c src/node/node.c src/node2d/node2d.c
|
||||
tcc -o $@ -I src $^
|
||||
|
||||
clean:
|
||||
|
|
11
src/main.c
11
src/main.c
|
@ -1,6 +1,7 @@
|
|||
#include<stdio.h>
|
||||
|
||||
#include "node.h"
|
||||
#include "node/node.h"
|
||||
#include "node2d/node2d.h"
|
||||
|
||||
int main(int argc, char* *argv){
|
||||
NOD_Node *distros = NOD_CreateNode("Distros");
|
||||
|
@ -20,6 +21,12 @@ int main(int argc, char* *argv){
|
|||
NOD_Node *openbsd = NOD_CreateNode("OpenBSD");
|
||||
NOD_Node *debianbsd = NOD_CreateNode("Debian GNU/kFreeBSD");
|
||||
|
||||
NOD_Node2D *point = NOD_CreateNode2D("Point2D");
|
||||
point->pos.x = 2;
|
||||
printf("The X value of '%s' is %d!\n", point->node.name, 3);
|
||||
|
||||
NOD_NodeAddChild(debian, point);
|
||||
|
||||
NOD_NodeAddChild(distros, linoox);
|
||||
NOD_NodeAddChild(distros, bsd);
|
||||
|
||||
|
@ -43,8 +50,10 @@ int main(int argc, char* *argv){
|
|||
|
||||
//NOD_NodeUnparent(child2);
|
||||
|
||||
|
||||
NOD_PrintNodeTree(distros);
|
||||
|
||||
NOD_DestroyNodeBranch(distros);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -5,10 +5,15 @@
|
|||
#include <string.h>
|
||||
|
||||
NOD_Node *NOD_CreateNode(char *name){
|
||||
return NOD_CreateNodeEX("Node", name);
|
||||
}
|
||||
|
||||
NOD_Node *NOD_CreateNodeEX(char* type, char *name){
|
||||
NOD_Node *node = malloc(sizeof(NOD_Node));
|
||||
|
||||
if(node != NULL){
|
||||
node->name = name;
|
||||
node->type = type;
|
||||
|
||||
node->parent = NULL;
|
||||
node->childv = NULL;
|
||||
|
@ -20,7 +25,7 @@ NOD_Node *NOD_CreateNode(char *name){
|
|||
|
||||
void NOD_DestroyNode(NOD_Node *node){
|
||||
if(node != NULL){
|
||||
printf("NOD_Info: Deleting node \"%s\"\n", node->name);
|
||||
/*printf("NOD_Info: Deleting node \"%s\"\n", node->name);*/
|
||||
/** Cleanup dependencies on this node by its parent */
|
||||
NOD_NodeUnparent(node);
|
||||
free(node);
|
||||
|
@ -128,9 +133,9 @@ void NOD_NodeUnparent(NOD_Node *node){
|
|||
|
||||
/* Free allocated memory if last child */
|
||||
if(i + 1 == parent->childc){
|
||||
fprintf(stderr,
|
||||
/*fprintf(stderr,
|
||||
"NOD_Debug: Freeing last child \"%s\" of vector \"%s\"...\n",
|
||||
node->name, parent->name);
|
||||
node->name, parent->name);*/
|
||||
parent->childv = realloc(parent->childv, sizeof(node) *parent->childc);
|
||||
parent->childc--;
|
||||
}
|
||||
|
@ -171,7 +176,7 @@ void nodetree_print_branch(NOD_Node *node, char* level){
|
|||
strcat(level, "n");
|
||||
}
|
||||
}
|
||||
printf("%s\n", node->name);
|
||||
printf("%s (%s)\n", node->name, node->type);
|
||||
|
||||
/** Recurse */
|
||||
for(int i = 0; i < node->childc; i++){
|
||||
|
@ -189,7 +194,7 @@ void NOD_PrintNodeTree(NOD_Node *node){
|
|||
if(node != NULL){
|
||||
char* level = strdup("");
|
||||
|
||||
printf("%s\n", node->name);
|
||||
printf("%s (%s)\n", node->name, node->type);
|
||||
for(int i = 0; i < node->childc; i++){
|
||||
nodetree_print_branch(node->childv[i], level);
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
typedef struct node{
|
||||
char *name;
|
||||
char *type;
|
||||
|
||||
/** childc keeps track of allocated space,
|
||||
not amount of children. For that use
|
||||
|
@ -11,9 +12,12 @@ typedef struct node{
|
|||
struct node* *childv;
|
||||
struct node *parent;
|
||||
|
||||
void* data;
|
||||
|
||||
} NOD_Node;
|
||||
|
||||
NOD_Node *NOD_CreateNode(char *name);
|
||||
NOD_Node *NOD_CreateNodeEX(char *type, char *name);
|
||||
void NOD_DestroyNode(NOD_Node *node);
|
||||
void NOD_DestroyNodeBranch(NOD_Node *node);
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
#include "node2d.h"
|
||||
|
||||
NOD_Node2D *NOD_CreateNode2D(char *name){
|
||||
NOD_Node2D *node = NULL;
|
||||
NOD_Node *plain_node = NULL;
|
||||
|
||||
plain_node = NOD_CreateNodeEX("Node2D", name);
|
||||
|
||||
if(plain_node != NULL){
|
||||
plain_node = realloc(plain_node, sizeof(NOD_Node2D));
|
||||
|
||||
if(plain_node != NULL){
|
||||
node = (NOD_Node2D*)plain_node;
|
||||
|
||||
node->pos.x = 0;
|
||||
node->pos.y = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
#include"../node/node.h"
|
||||
#include"../types.h"
|
||||
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
typedef struct node_2d{
|
||||
NOD_Node node;
|
||||
vector2Di pos;
|
||||
} NOD_Node2D;
|
||||
|
||||
NOD_Node2D *NOD_CreateNode2D(char *name);
|
||||
void NOD_DestroyNode2D(NOD_Node *node);
|
Loading…
Reference in New Issue