From aec5fcab5534b24d547415f8cecd51211ed114ba Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 7 Nov 2022 05:22:42 +0000 Subject: [PATCH] Added variable argument count to callNode --- assets/scripts/child.toy | 11 ----- assets/scripts/{render.toy => entity.toy} | 59 ++++++++++++----------- assets/scripts/init.toy | 1 + assets/scripts/root.toy | 10 ++-- core/engine_node.c | 7 +-- core/lib_node.c | 36 ++++++++++++-- 6 files changed, 73 insertions(+), 51 deletions(-) delete mode 100644 assets/scripts/child.toy rename assets/scripts/{render.toy => entity.toy} (98%) diff --git a/assets/scripts/child.toy b/assets/scripts/child.toy deleted file mode 100644 index 3d29a96..0000000 --- a/assets/scripts/child.toy +++ /dev/null @@ -1,11 +0,0 @@ -fn onInit(node: opaque) { - print "child.toy:onInit() called"; -} - -fn onStep(node: opaque) { - print "child.toy:onStep()"; -} - -fn onFree(node: opaque) { - print "child.toy:onFree() called"; -} diff --git a/assets/scripts/render.toy b/assets/scripts/entity.toy similarity index 98% rename from assets/scripts/render.toy rename to assets/scripts/entity.toy index 0d7a527..358963f 100644 --- a/assets/scripts/render.toy +++ b/assets/scripts/entity.toy @@ -28,6 +28,35 @@ fn onInit(node: opaque) { parent = node.getNodeParent(); } +fn onStep(node: opaque) { + x += xspeed; + y += yspeed; +} + +fn onFree(node: opaque) { + print "render.toy:onFree() called"; + + node.freeTexture(); +} + +fn onDraw(node: opaque) { +// print "render.toy:onDraw() called"; + + var px = parent.callNode("getX"); + var py = parent.callNode("getY"); + + if (px == null) { + px = 0; + } + + if (py == null) { + py = 0; + } + + node.drawNode(x + px, y + py, 100, 100); +} + +//event functions fn onKeyDown(node: opaque, event: string) { if (event == "character_up") { yspeed -= SPEED; @@ -70,32 +99,4 @@ fn onKeyUp(node: opaque, event: string) { xspeed = 0; return; } -} - -fn onStep(node: opaque) { - x += xspeed; - y += yspeed; -} - -fn onFree(node: opaque) { - print "render.toy:onFree() called"; - - node.freeTexture(); -} - -fn onDraw(node: opaque) { -// print "render.toy:onDraw() called"; - - var px = parent.callNode("getX"); - var py = parent.callNode("getY"); - - if (px == null) { - px = 0; - } - - if (py == null) { - py = 0; - } - - node.drawNode(x + px, y + py, 100, 100); -} +} \ No newline at end of file diff --git a/assets/scripts/init.toy b/assets/scripts/init.toy index c3434c5..9ddcb25 100644 --- a/assets/scripts/init.toy +++ b/assets/scripts/init.toy @@ -23,6 +23,7 @@ mapInputEventToKeyUp("character_left", "left"); //event, keysym mapInputEventToKeyUp("character_down", "down"); //event, keysym mapInputEventToKeyUp("character_right", "right"); //event, keysym + //this function must always be called, or the engine won't run initWindow("Airport Game", 800, 600, false); diff --git a/assets/scripts/root.toy b/assets/scripts/root.toy index 75b54fd..c5cc326 100644 --- a/assets/scripts/root.toy +++ b/assets/scripts/root.toy @@ -2,22 +2,22 @@ import engine; import node; +//util to generate and init a child node of a parent fn _makeChild(parent: opaque, fname: string) { var child: opaque = loadNode(fname); parent.pushNode(child); child.initNode(); } -//root node can load the whole scene, and essentially act as the scene object +//NOTE: root node can load the whole scene, and essentially act as the scene object fn onInit(node: opaque) { print "root.toy:onInit() called"; //make a child -// node.makeChild("assets/scripts/child.toy"); -// node.makeChild("assets/scripts/child.toy"); - node.makeChild("assets/scripts/render.toy"); + node.makeChild("assets/scripts/entity.toy"); - node.getNodeChild(0).makeChild("assets/scripts/render.toy"); + //give the child a child + node.getNodeChild(0).makeChild("assets/scripts/entity.toy"); } fn onStep(node: opaque) { diff --git a/core/engine_node.c b/core/engine_node.c index 920f0b0..ef462d7 100644 --- a/core/engine_node.c +++ b/core/engine_node.c @@ -114,14 +114,15 @@ Literal callEngineNodeLiteral(EngineNode* node, Interpreter* interpreter, Litera initLiteralArray(&arguments); initLiteralArray(&returns); - pushLiteralArray(&arguments, n); - + //feed the arguments in backwards! if (args) { - for (int i = 0; i < args->count; i++) { + for (int i = args->count -1; i >= 0; i--) { pushLiteralArray(&arguments, args->literals[i]); } } + pushLiteralArray(&arguments, n); + callLiteralFn(interpreter, fn, &arguments, &returns); ret = popLiteralArray(&returns); diff --git a/core/lib_node.c b/core/lib_node.c index 03d033a..3441d78 100644 --- a/core/lib_node.c +++ b/core/lib_node.c @@ -534,11 +534,40 @@ static int nativeGetNodeTag(Interpreter* interpreter, LiteralArray* arguments) { static int nativeCallNode(Interpreter* interpreter, LiteralArray* arguments) { //checks - if (arguments->count != 2) { - interpreter->errorOutput("Incorrect number of arguments passed to callEngineNode\n"); + if (arguments->count < 2) { + interpreter->errorOutput("Too few arguments passed to callEngineNode\n"); return -1; } + LiteralArray extraArgs; + initLiteralArray(&extraArgs); + + LiteralArray flippedExtraArgs; + initLiteralArray(&flippedExtraArgs); + + //extract the extra arg values + while (arguments->count > 2) { + Literal tmp = popLiteralArray(arguments); + + Literal idn = tmp; //there's almost certainly a better way of doing all of this stuff + if (IS_IDENTIFIER(tmp) && parseIdentifierToValue(interpreter, &tmp)) { + freeLiteral(idn); + } + + pushLiteralArray(&flippedExtraArgs, tmp); + freeLiteral(tmp); + } + + //correct the order + while (flippedExtraArgs.count) { + Literal tmp = popLiteralArray(&flippedExtraArgs); + pushLiteralArray(&extraArgs, tmp); + freeLiteral(tmp); + } + + freeLiteralArray(&flippedExtraArgs); + + //back on track Literal fnName = popLiteralArray(arguments); Literal nodeLiteral = popLiteralArray(arguments); @@ -563,11 +592,12 @@ static int nativeCallNode(Interpreter* interpreter, LiteralArray* arguments) { Literal fnNameIdentifier = TO_IDENTIFIER_LITERAL(copyString(strptr, strlen(strptr)), strlen(strptr)); //call the function - Literal result = callEngineNodeLiteral(AS_OPAQUE(nodeLiteral), interpreter, fnNameIdentifier, NULL); + Literal result = callEngineNodeLiteral(AS_OPAQUE(nodeLiteral), interpreter, fnNameIdentifier, &extraArgs); pushLiteralArray(&interpreter->stack, result); //cleanup + freeLiteralArray(&extraArgs); freeLiteral(nodeLiteral); freeLiteral(fnName); freeLiteral(result);