From 14602b075064cbd50f8b1e652b40b229a7557be6 Mon Sep 17 00:00:00 2001 From: Ratstail91 Date: Tue, 28 Feb 2023 05:59:44 +1100 Subject: [PATCH] All of a sudden, it's lagging like hell --- assets/scripts/init.toy | 2 +- assets/scripts/scene.toy | 15 +++++++++++ assets/scripts/tilemap/layer-background.toy | 25 ++++++++++-------- assets/scripts/tilemap/layer-walls.toy | 25 ++++++++++-------- assets/scripts/tilemap/tilemap.toy | 18 +++---------- box/lib_engine.c | 29 ++++++++++++++++++--- box/lib_node.c | 2 +- 7 files changed, 74 insertions(+), 42 deletions(-) diff --git a/assets/scripts/init.toy b/assets/scripts/init.toy index 8241504..eddaf2c 100644 --- a/assets/scripts/init.toy +++ b/assets/scripts/init.toy @@ -28,4 +28,4 @@ mapInputEventToKeyUp("character_right", "right"); //event, keysym initWindow("Airport Game", 1080, 720, false); //kick off the logic of the scene graph -loadRootNode("scripts:/children_test.toy"); +loadRootNode("scripts:/scene.toy"); diff --git a/assets/scripts/scene.toy b/assets/scripts/scene.toy index 41ed902..fa8db4f 100644 --- a/assets/scripts/scene.toy +++ b/assets/scripts/scene.toy @@ -21,3 +21,18 @@ fn onInit(node: opaque) { //tilemapNode.callNodeFn("loadLayer", "layer-walls.toy"); //TODO: remove this //tilemapNode.callNodeFn("loadLayer", "layer-maze.toy"); } + + +//global accessors +fn getTileWidth(node: opaque): int { return 64; } +fn getTileHeight(node: opaque): int { return 64; } + +fn getRoomWidth(node: opaque): int { return 10; } +fn getRoomHeight(node: opaque): int { return 10; } + +fn getLevelXCount(node: opaque): int { return 9; } +fn getLevelYCount(node: opaque): int { return 9; } + +//TODO: move this into the engine +fn getScreenWidth(node: opaque): int { return 1920; } +fn getScreenHeight(node: opaque): int { return 1080; } diff --git a/assets/scripts/tilemap/layer-background.toy b/assets/scripts/tilemap/layer-background.toy index 76b43f2..577e7be 100644 --- a/assets/scripts/tilemap/layer-background.toy +++ b/assets/scripts/tilemap/layer-background.toy @@ -1,17 +1,8 @@ //this is one layer import standard; +import engine; import node; -//TODO: reference these from a global source (root?) -var tileWidth: int const = 64; -var tileHeight: int const = 64; - -var roomWidth: int const = 10; -var roomHeight: int const = 10; - -var levelXCount: int const = 9; -var levelYCount: int const = 9; - //util to generate and init a child node of a given parent fn makeChildSprite(parent: opaque, spriteName: string) { @@ -32,9 +23,21 @@ fn onInit(node: opaque) { fn drawLayer(node: opaque, camX, camY, camW, camH, depth) { + //get the constants from root + var root: opaque = getRootNode(); + + var tileWidth: int const = root.callNodeFn("getTileWidth"); + var tileHeight: int const = root.callNodeFn("getTileHeight"); + + var roomWidth: int const = root.callNodeFn("getRoomWidth"); + var roomHeight: int const = root.callNodeFn("getRoomHeight"); + + var levelXCount: int const = root.callNodeFn("getLevelXCount"); + var levelYCount: int const = root.callNodeFn("getLevelYCount"); + //calc the modifier ratio to offset things var mod: float = float tileWidth / (tileWidth - depth); - + var tileWidth_mod: float = tileWidth * mod; var tileHeight_mod: float = tileHeight * mod; var camX_mod: float = (camX - camW) * mod + camW/2; diff --git a/assets/scripts/tilemap/layer-walls.toy b/assets/scripts/tilemap/layer-walls.toy index 288d265..cc1f41b 100644 --- a/assets/scripts/tilemap/layer-walls.toy +++ b/assets/scripts/tilemap/layer-walls.toy @@ -1,17 +1,8 @@ //this is one layer import standard; +import engine; import node; -//TODO: reference these from a global source (root?) -var tileWidth: int const = 64; -var tileHeight: int const = 64; - -var roomWidth: int const = 10; -var roomHeight: int const = 10; - -var levelXCount: int const = 9; -var levelYCount: int const = 9; - //util to generate and init a child node of a given parent fn makeChildSprite(parent: opaque, spriteName: string) { @@ -32,9 +23,21 @@ fn onInit(node: opaque) { fn drawLayer(node: opaque, camX, camY, camW, camH, depth) { + //get the constants from root + var root: opaque = getRootNode(); + + var tileWidth: int const = root.callNodeFn("getTileWidth"); + var tileHeight: int const = root.callNodeFn("getTileHeight"); + + var roomWidth: int const = root.callNodeFn("getRoomWidth"); + var roomHeight: int const = root.callNodeFn("getRoomHeight"); + + var levelXCount: int const = root.callNodeFn("getLevelXCount"); + var levelYCount: int const = root.callNodeFn("getLevelYCount"); + //calc the modifier ratio to offset things var mod: float = float tileWidth / (tileWidth - depth); - + var tileWidth_mod: int = round(tileWidth * mod); var tileHeight_mod: int = round(tileHeight * mod); var camX_mod: int = round((camX - camW) * mod + camW/2); diff --git a/assets/scripts/tilemap/tilemap.toy b/assets/scripts/tilemap/tilemap.toy index 22e3763..14614cd 100644 --- a/assets/scripts/tilemap/tilemap.toy +++ b/assets/scripts/tilemap/tilemap.toy @@ -1,24 +1,11 @@ //this file manages the tilemap-related utilities import standard; +import engine; import node; var camX: float = 0; var camY: float = 0; -//TODO: reference these from a global source (root?) -var tileWidth: int const = 64; -var tileHeight: int const = 64; - -var roomWidth: int const = 10; -var roomHeight: int const = 10; - -var levelXCount: int const = 9; -var levelYCount: int const = 9; - -var screenWidth: int const = 1080; -var screenHeight: int const = 720; - - //util to generate and init a child node of a given parent fn makeChild(parent: opaque, fname: string) { var child: opaque = loadNode(fname); @@ -44,6 +31,9 @@ fn onDraw(node: opaque) { print stepCounter; stepCounter = 0; + var screenWidth: int const = getRootNode().callNodeFn("getScreenWidth"); + var screenHeight: int const = getRootNode().callNodeFn("getScreenHeight"); + //iterate over each layer, passing in the screen dimensions for (var c = 0; c < node.getChildNodeCount(); c++) { node.getChildNode(c).callNodeFn("drawLayer", camX, camY, screenWidth, screenHeight, c * 4.0); diff --git a/box/lib_engine.c b/box/lib_engine.c index bd036a9..678d30c 100644 --- a/box/lib_engine.c +++ b/box/lib_engine.c @@ -80,7 +80,6 @@ static int nativeInitWindow(Toy_Interpreter* interpreter, Toy_LiteralArray* argu return 0; } -//TODO: perhaps a returns argument would be better? static int nativeLoadRootNode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { if (arguments->count != 1) { interpreter->errorOutput("Incorrect number of arguments passed to loadRootNode\n"); @@ -127,7 +126,7 @@ static int nativeLoadRootNode(Toy_Interpreter* interpreter, Toy_LiteralArray* ar const unsigned char* tb = Toy_compileString((const char*)source, &size); free((void*)source); - engine.rootNode = TOY_ALLOCATE(Box_EngineNode, 1); + Box_EngineNode* rootNode = TOY_ALLOCATE(Box_EngineNode, 1); //BUGFIX: make an inner-interpreter Toy_Interpreter inner; @@ -147,10 +146,13 @@ static int nativeLoadRootNode(Toy_Interpreter* interpreter, Toy_LiteralArray* ar Toy_setInterpreterAssert(&inner, interpreter->assertOutput); Toy_setInterpreterError(&inner, interpreter->errorOutput); - Box_initEngineNode(engine.rootNode, &inner, tb, size); + Box_initEngineNode(rootNode, &inner, tb, size); //init the new node (and ONLY this node) - Box_callEngineNode(engine.rootNode, &engine.interpreter, "onInit", NULL); + Box_callEngineNode(rootNode, &engine.interpreter, "onInit", NULL); + + //NOW it's non-null + engine.rootNode = rootNode; //cleanup while(inner.scope) { @@ -164,6 +166,24 @@ static int nativeLoadRootNode(Toy_Interpreter* interpreter, Toy_LiteralArray* ar return 0; } +static int nativeGetRootNode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { + if (arguments->count != 0) { + interpreter->errorOutput("Incorrect number of arguments passed to getRootNode\n"); + return -1; + } + + if (engine.rootNode == NULL) { + interpreter->errorOutput("Can't access root node until after initialization\n"); + return -1; + } + + Toy_Literal resultLiteral = TOY_TO_OPAQUE_LITERAL(engine.rootNode, engine.rootNode->tag); + + Toy_pushLiteralArray(&interpreter->stack, resultLiteral); + + return 1; +} + //call the hook typedef struct Natives { char* name; @@ -175,6 +195,7 @@ int Box_hookEngine(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Lit Natives natives[] = { {"initWindow", nativeInitWindow}, {"loadRootNode", nativeLoadRootNode}, + {"getRootNode", nativeGetRootNode}, {NULL, NULL} }; diff --git a/box/lib_node.c b/box/lib_node.c index 9531289..febb1f1 100644 --- a/box/lib_node.c +++ b/box/lib_node.c @@ -842,7 +842,7 @@ int Box_hookNode(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Liter {"drawNode", nativeDrawNode}, {"callNodeFn", nativeCallNodeFn}, - //TODO: get rect, get node var, create empty node, get root node + //TODO: get rect, get node var, create empty node {NULL, NULL}, };