Compare commits

...

3 Commits

Author SHA1 Message Date
Dendy 01e4d7c9ec Improve inheritance system 2021-02-06 15:20:24 +01:00
Dendy 9e702bcdbb Add Node2D 2021-02-06 13:16:18 +01:00
Blumi 643e2e5ba4 Add Node2D 2021-02-06 13:15:27 +01:00
6 changed files with 67 additions and 15 deletions

View File

@ -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:

View File

@ -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;
}

View File

@ -5,22 +5,27 @@
#include <string.h>
NOD_Node *NOD_CreateNode(char *name){
NOD_Node *node = malloc(sizeof(NOD_Node));
return NOD_CreateNodeEX("Node", name);
}
if(node != NULL){
node->name = name;
NOD_Node *NOD_CreateNodeEX(char* type, char *name){
NOD_Node *node = malloc(sizeof(NOD_Node));
node->parent = NULL;
node->childv = NULL;
node->childc = 0;
}
if(node != NULL){
node->name = name;
node->type = type;
return node;
node->parent = NULL;
node->childv = NULL;
node->childc = 0;
}
return node;
}
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);
}

View File

@ -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);

21
src/node2d/node2d.c Normal file
View File

@ -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;
}

13
src/node2d/node2d.h Normal file
View File

@ -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);