diff --git a/Airport.vcxproj b/Airport.vcxproj index f3d6c6e..0d3ed90 100644 --- a/Airport.vcxproj +++ b/Airport.vcxproj @@ -23,13 +23,13 @@ + - diff --git a/assets/scripts/children_test.toy b/assets/scripts/children_test.toy index 84397a6..bce6337 100644 --- a/assets/scripts/children_test.toy +++ b/assets/scripts/children_test.toy @@ -1,24 +1,22 @@ import node; -//util to generate and init a child node of a given parent -fn makeChild(parent: opaque, fname: string) { +//looks like a polyfill +fn loadChild(parent: opaque, fname: string) { var child: opaque = loadNode(fname); parent.pushNode(child); - child.initNode(); return child; } -fn onInit(node: opaque) { +fn onLoad(node: opaque) { for (var i = 0; i < 20; i++) { - node.makeChild("scripts:/tilemap/tile.toy"); + node.loadChild("scripts:/empty.toy"); } node.freeChildNode(10); var n = node.getChildNode(10); for (var i = 0; i < 20; i++) { - //this would originally prune tombstones... - node.makeChild("scripts:/tilemap/tile.toy"); + node.loadChild("scripts:/empty.toy"); } print node.getChildNodeCount(); diff --git a/assets/scripts/empty.toy b/assets/scripts/empty.toy new file mode 100644 index 0000000..60d8c7b --- /dev/null +++ b/assets/scripts/empty.toy @@ -0,0 +1 @@ +//this file is a polyfill \ No newline at end of file diff --git a/assets/scripts/scene.toy b/assets/scripts/scene.toy index 756f206..9974a3b 100644 --- a/assets/scripts/scene.toy +++ b/assets/scripts/scene.toy @@ -1,17 +1,20 @@ //the overarching scene import node; +//debugging tools +var stepCounter: int = 0; +var stepTracker: [int] = [0, 0, 0]; + //util to generate and init a child node of a given parent fn makeChild(parent: opaque, fname: string) { var child: opaque = loadNode(fname); parent.pushNode(child); - child.initNode(); return child; } - -fn onInit(node: opaque) { +//lifecycle functions +fn onLoad(node: opaque) { //load the tile map node var tilemapNode = node.makeChild("scripts:/tilemap/tilemap.toy"); @@ -22,6 +25,22 @@ fn onInit(node: opaque) { //tilemapNode.callNodeFn("loadLayer", "layer-maze.toy"); } +//debugging code +fn onStep(node: opaque) { + stepCounter++; +} + +fn onDraw(node: opaque) { + //round off - too many skips means it's crappy anyway + if (stepCounter > stepTracker.length() - 1) { + stepCounter = stepTracker.length() - 1; + } + + //This logic is just a debugging tool + stepTracker[stepCounter] = stepTracker[stepCounter] + 1; + print stepTracker; + stepCounter = 0; +} //global accessors fn getTileWidth(node: opaque): int { return 64; } diff --git a/assets/scripts/tilemap/layer-background.toy b/assets/scripts/tilemap/layer-background.toy index 577e7be..2bf833c 100644 --- a/assets/scripts/tilemap/layer-background.toy +++ b/assets/scripts/tilemap/layer-background.toy @@ -4,24 +4,25 @@ import engine; import node; -//util to generate and init a child node of a given parent -fn makeChildSprite(parent: opaque, spriteName: string) { - var child: opaque = loadNode("scripts:/tilemap/tile.toy"); +//TODO: replace with an empty node function (polyfill) +fn loadChildEmpty(parent: opaque) { + var child: opaque = loadNode("scripts:/empty.toy"); parent.pushNode(child); - child.initNode(); - - child.loadTexture("sprites:/" + spriteName); - return child; } -fn onInit(node: opaque) { +fn onLoad(node: opaque) { //load the child node, with the tiling back image - node.makeChildSprite("tile-background.png"); + node.loadChildEmpty(); +} + +fn onInit(node: opaque) { + node.getChildNode(0).loadTexture("sprites:/tile-background.png"); } +//drawing util fn drawLayer(node: opaque, camX, camY, camW, camH, depth) { //get the constants from root var root: opaque = getRootNode(); diff --git a/assets/scripts/tilemap/layer-walls.toy b/assets/scripts/tilemap/layer-walls.toy index cc1f41b..432d7f8 100644 --- a/assets/scripts/tilemap/layer-walls.toy +++ b/assets/scripts/tilemap/layer-walls.toy @@ -4,24 +4,25 @@ import engine; import node; -//util to generate and init a child node of a given parent -fn makeChildSprite(parent: opaque, spriteName: string) { - var child: opaque = loadNode("scripts:/tilemap/tile.toy"); +//TODO: replace with an empty node function (polyfill) +fn loadChildEmpty(parent: opaque) { + var child: opaque = loadNode("scripts:/empty.toy"); parent.pushNode(child); - child.initNode(); - - child.loadTexture("sprites:/" + spriteName); - return child; } -fn onInit(node: opaque) { +fn onLoad(node: opaque) { //load the child node, with the tiling back image - node.makeChildSprite("tile-wall.png"); + node.loadChildEmpty(); +} + +fn onInit(node: opaque) { + node.getChildNode(0).loadTexture("sprites:/tile-wall.png"); } +//drawing util fn drawLayer(node: opaque, camX, camY, camW, camH, depth) { //get the constants from root var root: opaque = getRootNode(); diff --git a/assets/scripts/tilemap/tile.toy b/assets/scripts/tilemap/tile.toy deleted file mode 100644 index 29c12b7..0000000 --- a/assets/scripts/tilemap/tile.toy +++ /dev/null @@ -1,2 +0,0 @@ -//EMPTY - diff --git a/assets/scripts/tilemap/tilemap.toy b/assets/scripts/tilemap/tilemap.toy index 7d5dbed..3ab0708 100644 --- a/assets/scripts/tilemap/tilemap.toy +++ b/assets/scripts/tilemap/tilemap.toy @@ -6,44 +6,25 @@ import node; var camX: float = 0; var camY: float = 0; -var stepCounter: int = 0; -var stepTracker: [int] = [0, 0, 0, 0, 0, 0]; - -//util to generate and init a child node of a given parent -fn makeChild(parent: opaque, fname: string) { +//util to generate a child node of a given parent +fn loadChild(parent: opaque, fname: string) { var child: opaque = loadNode(fname); parent.pushNode(child); - child.initNode(); return child; } fn loadLayer(node: opaque, layerName: string) { //load the given layer as a child - var layerNode = node.makeChild("scripts:/tilemap/" + layerName); + var layerNode = node.loadChild("scripts:/tilemap/" + layerName); } +//lifecycle functions fn onStep(node: opaque) { - stepCounter++; - camX--; camY--; } fn onDraw(node: opaque) { - var tmp = stepCounter; - print stepCounter; - stepCounter = 0; - - if (tmp > 5) { - tmp = 5; - } - - print typeof (stepTracker[tmp] + 1); - - //TODO: index[]++ - stepTracker[tmp] = stepTracker[tmp] + 1; - print stepTracker; - var screenWidth: int const = getRootNode().callNodeFn("getScreenWidth"); var screenHeight: int const = getRootNode().callNodeFn("getScreenHeight"); diff --git a/box/box_engine.c b/box/box_engine.c index 49ee200..6e833ea 100644 --- a/box/box_engine.c +++ b/box/box_engine.c @@ -63,12 +63,17 @@ void Box_initEngine() { Toy_injectNativeHook(&engine.interpreter, "node", Box_hookNode); Toy_injectNativeHook(&engine.interpreter, "input", Box_hookInput); + //run the init size_t size = 0; const unsigned char* source = Toy_readFile("./assets/scripts/init.toy", &size); const unsigned char* tb = Toy_compileString((const char*)source, &size); free((void*)source); Toy_runInterpreter(&engine.interpreter, tb, size); + + //init the node-tree + Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onInit", NULL); + } void Box_freeEngine() { diff --git a/box/lib_engine.c b/box/lib_engine.c index b68f0e0..8aab366 100644 --- a/box/lib_engine.c +++ b/box/lib_engine.c @@ -25,7 +25,7 @@ static int nativeInitWindow(Toy_Interpreter* interpreter, Toy_LiteralArray* argu } if (arguments->count != 4) { - fatalError("Incorrect number of arguments passed to initEngine\n"); + fatalError("Incorrect number of arguments passed to initWindow\n"); } //extract the arguments @@ -34,9 +34,29 @@ static int nativeInitWindow(Toy_Interpreter* interpreter, Toy_LiteralArray* argu Toy_Literal screenWidth = Toy_popLiteralArray(arguments); Toy_Literal caption = Toy_popLiteralArray(arguments); + Toy_Literal captionIdn = caption; + if (TOY_IS_IDENTIFIER(caption) && Toy_parseIdentifierToValue(interpreter, &caption)) { + Toy_freeLiteral(captionIdn); + } + + Toy_Literal screenWidthIdn = screenWidth; + if (TOY_IS_IDENTIFIER(screenWidth) && Toy_parseIdentifierToValue(interpreter, &screenWidth)) { + Toy_freeLiteral(screenWidthIdn); + } + + Toy_Literal screenHeightIdn = screenHeight; + if (TOY_IS_IDENTIFIER(screenHeight) && Toy_parseIdentifierToValue(interpreter, &screenHeight)) { + Toy_freeLiteral(screenHeightIdn); + } + + Toy_Literal fscreenIdn = fscreen; + if (TOY_IS_IDENTIFIER(fscreen) && Toy_parseIdentifierToValue(interpreter, &fscreen)) { + Toy_freeLiteral(fscreenIdn); + } + //check argument types if (!TOY_IS_STRING(caption) || !TOY_IS_INTEGER(screenWidth) || !TOY_IS_INTEGER(screenHeight) || !TOY_IS_BOOLEAN(fscreen)) { - fatalError("Incorrect argument type passed to initEngine\n"); + fatalError("Incorrect argument type passed to initWindow\n"); } //init the window @@ -133,7 +153,7 @@ static int nativeLoadRootNode(Toy_Interpreter* interpreter, Toy_LiteralArray* ar //init the inner interpreter manually Toy_initLiteralArray(&inner.literalCache); - inner.scope = Toy_pushScope(NULL); + inner.scope = Toy_pushScope(interpreter->scope); inner.bytecode = tb; inner.length = size; inner.count = 0; @@ -148,16 +168,15 @@ static int nativeLoadRootNode(Toy_Interpreter* interpreter, Toy_LiteralArray* ar Box_initEngineNode(rootNode, &inner, tb, size); - //init the new node (and ONLY this node) - Box_callEngineNode(rootNode, &engine.interpreter, "onInit", NULL); + //immediately call onLoad() after running the script - for loading other nodes + Box_callEngineNode(rootNode, &engine.interpreter, "onLoad", NULL); //NOW it's non-null engine.rootNode = rootNode; //cleanup - while(inner.scope) { - inner.scope = Toy_popScope(inner.scope); - } + Toy_popScope(inner.scope); + inner.scope = NULL; Toy_freeLiteralArray(&inner.stack); Toy_freeLiteralArray(&inner.literalCache); diff --git a/box/lib_node.c b/box/lib_node.c index febb1f1..2c8f1ab 100644 --- a/box/lib_node.c +++ b/box/lib_node.c @@ -50,14 +50,14 @@ static int nativeLoadNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume Box_EngineNode* node = TOY_ALLOCATE(Box_EngineNode, 1); - //BUGFIX: make an inner-interpreter + //BUGFIX: make an -interpreter Toy_Interpreter inner; //init the inner interpreter manually Toy_initLiteralArray(&inner.literalCache); Toy_initLiteralArray(&inner.stack); inner.hooks = interpreter->hooks; - inner.scope = Toy_pushScope(NULL); + inner.scope = Toy_pushScope(interpreter->scope); inner.bytecode = tb; inner.length = size; inner.count = 0; @@ -70,6 +70,9 @@ static int nativeLoadNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume Box_initEngineNode(node, &inner, tb, size); + //immediately call onLoad() after running the script - for loading other nodes + Box_callEngineNode(node, &engine.interpreter, "onLoad", NULL); + // return the node Toy_Literal nodeLiteral = TOY_TO_OPAQUE_LITERAL(node, node->tag); Toy_pushLiteralArray(&interpreter->stack, nodeLiteral);