src/scene.c

changeset 27
4ccf4703999e
parent 21
1a47c57666f5
child 29
1d001eb694dc
     1.1 --- a/src/scene.c	Sun Jan 21 13:34:49 2024 +0100
     1.2 +++ b/src/scene.c	Sun Jan 21 13:58:23 2024 +0100
     1.3 @@ -28,7 +28,6 @@
     1.4  #include "ascension/scene.h"
     1.5  #include "ascension/error.h"
     1.6  
     1.7 -#include <cx/linked_list.h>
     1.8  #include <assert.h>
     1.9  
    1.10  void asc_scene_init(AscScene *scene) {
    1.11 @@ -44,24 +43,25 @@
    1.12  }
    1.13  
    1.14  AscSceneNode *asc_scene_node_create(AscSceneNode *parent) {
    1.15 -    AscSceneNode *node = malloc(sizeof(AscSceneNode));
    1.16 +    // TODO: check if this can remain a calloc or if it's too expensive
    1.17 +    AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
    1.18      assert(node != NULL);
    1.19 -    node->children = cxLinkedListCreateSimple(CX_STORE_POINTERS);
    1.20 -    assert(node->children != NULL);
    1.21 -    node->parent = NULL;
    1.22      asc_scene_node_link(node, parent);
    1.23      return node;
    1.24  }
    1.25  
    1.26  void asc_scene_node_free(AscSceneNode *node) {
    1.27      if (node == NULL) return;
    1.28 -    while (cxListSize(node->children) > 0) {
    1.29 -        AscSceneNode *child = cxListAt(node->children, 0);
    1.30 -        asc_scene_node_free(child);
    1.31 +
    1.32 +    // free the children recursively
    1.33 +    while (node->children != NULL) {
    1.34 +        asc_scene_node_free(node->children);
    1.35      }
    1.36 -    if (node->parent != NULL) {
    1.37 -        cxListFindRemove(node->parent->children, node);
    1.38 -    }
    1.39 +
    1.40 +    // remove this node from its parent
    1.41 +    asc_scene_node_unlink(node);
    1.42 +
    1.43 +    // free the node
    1.44      free(node);
    1.45  }
    1.46  
    1.47 @@ -72,13 +72,26 @@
    1.48      if (node->parent == parent) return;
    1.49      if (node->parent != NULL || parent == NULL) asc_scene_node_unlink(node);
    1.50      if (parent != NULL) {
    1.51 -        cxListAdd(parent->children, node);
    1.52 +        if (parent->children == NULL) {
    1.53 +            parent->children = node;
    1.54 +        } else {
    1.55 +            parent->children->prev = node;
    1.56 +            node->next = parent->children;
    1.57 +        }
    1.58          node->parent = parent;
    1.59      }
    1.60  }
    1.61  
    1.62  void asc_scene_node_unlink(AscSceneNode *node) {
    1.63      if (node->parent == NULL) return;
    1.64 -    cxListFindRemove(node->parent->children, node);
    1.65 -    node->parent = NULL;
    1.66 +    AscSceneNode *left = node->prev;
    1.67 +    AscSceneNode *right = node->next;
    1.68 +    assert(left == NULL || node->parent->children != node);
    1.69 +    if (left != NULL) {
    1.70 +        left->next = right;
    1.71 +    } else {
    1.72 +        node->parent->children = right;
    1.73 +    }
    1.74 +    if (right != NULL) right->prev = left;
    1.75 +    node->parent = node->prev = node->next = NULL;
    1.76  }
    1.77 \ No newline at end of file

mercurial