From 86b5ed63ec833de784a021366ac0ca4402ed7a8f Mon Sep 17 00:00:00 2001 From: Ratstail91 Date: Fri, 16 Jun 2023 04:11:03 +1000 Subject: [PATCH] Fixed a node sorting bug --- assets/scripts/empty.toy | 4 ---- assets/scripts/gameplay/scene.toy | 16 +++++----------- box/box_node.c | 26 ++++++++++---------------- 3 files changed, 15 insertions(+), 31 deletions(-) diff --git a/assets/scripts/empty.toy b/assets/scripts/empty.toy index cd8b2b4..79bacfa 100644 --- a/assets/scripts/empty.toy +++ b/assets/scripts/empty.toy @@ -1,5 +1 @@ //this file is a polyfill TODO: fix this - -fn getRealPos(node: opaque) { - return [0, 0]; -} \ No newline at end of file diff --git a/assets/scripts/gameplay/scene.toy b/assets/scripts/gameplay/scene.toy index b96709c..484faeb 100644 --- a/assets/scripts/gameplay/scene.toy +++ b/assets/scripts/gameplay/scene.toy @@ -130,21 +130,15 @@ fn depthComparator(lhs: opaque, rhs: opaque) { //for sorting by depth var lhsPos = lhs.callNodeFn("getRealPos"); var rhsPos = rhs.callNodeFn("getRealPos"); - var result = null; + if (lhsPos == null || rhsPos == null) { //BUGFIX: children without that function + return true; + } if (lhsPos[1] == rhsPos[1]) { //BUGFIX: prevent z-fighting - if (lhsPos[0] == rhsPos[0]) { - result = true; - } - else { - result = lhsPos[0] < rhsPos[0]; - } - } - else { - result = lhsPos[1] < rhsPos[1]; + return lhsPos[0] <= rhsPos[0]; } - return result; + return lhsPos[1] < rhsPos[1]; } fn getCameraPos(node: opaque) { diff --git a/box/box_node.c b/box/box_node.c index 2c82842..7b543c2 100644 --- a/box/box_node.c +++ b/box/box_node.c @@ -128,16 +128,15 @@ static void recursiveLiteralQuicksortUtil(Toy_Interpreter* interpreter, Box_Node //iterate through the array for (int checker = 0; checker < count - 1; checker++) { - //if node is null, it is always sorted to the end - if (ptr[checker] == NULL) { - swapUtil(&ptr[checker], &ptr[checker + 1]); - runner++; - continue; + //if node is null, it is always "sorted" to the end + while (ptr[checker] == NULL && count > 0) { + swapUtil(&ptr[checker], &ptr[count - 1]); + count--; } - if (ptr[checker + 1] == NULL) { - runner++; - continue; + //base case + if (count < 2) { + return; } Toy_LiteralArray arguments; @@ -147,7 +146,7 @@ static void recursiveLiteralQuicksortUtil(Toy_Interpreter* interpreter, Box_Node Toy_initLiteralArray(&returns); Toy_pushLiteralArray(&arguments, TOY_TO_OPAQUE_LITERAL(ptr[checker], OPAQUE_TAG_NODE)); - Toy_pushLiteralArray(&arguments, TOY_TO_OPAQUE_LITERAL(ptr[checker + 1], OPAQUE_TAG_NODE)); + Toy_pushLiteralArray(&arguments, TOY_TO_OPAQUE_LITERAL(ptr[count - 1], OPAQUE_TAG_NODE)); Toy_callLiteralFn(interpreter, fnCompare, &arguments, &returns); @@ -167,13 +166,8 @@ static void recursiveLiteralQuicksortUtil(Toy_Interpreter* interpreter, Box_Node swapUtil(&ptr[runner], &ptr[count - 1]); //recurse on each end - if (runner > 0) { - recursiveLiteralQuicksortUtil(interpreter, &ptr[0], runner, fnCompare); - } - - if (runner < count) { - recursiveLiteralQuicksortUtil(interpreter, &ptr[runner + 1], count - runner - 1, fnCompare); - } + recursiveLiteralQuicksortUtil(interpreter, &ptr[0], runner, fnCompare); + recursiveLiteralQuicksortUtil(interpreter, &ptr[runner + 1], count - runner - 1, fnCompare); } BOX_API void Box_sortChildrenNode(Box_Node* node, Toy_Interpreter* interpreter, Toy_Literal fnCompare) {