From 3b5b399f4fdecb2fb2f53f8cd6d649eddb71026d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 27 Jan 2023 07:04:21 +0000 Subject: [PATCH] Tweaks while I sort stuff out --- Toy | 2 +- assets/scripts/entity.toy | 4 ++-- box/lib_node.c | 35 ++++++++++++++++++++++++++--------- box/lib_runner.c | 7 ------- source/main.c | 31 +++++++++++++++++++++++-------- 5 files changed, 52 insertions(+), 27 deletions(-) diff --git a/Toy b/Toy index c86c580..f4469fc 160000 --- a/Toy +++ b/Toy @@ -1 +1 @@ -Subproject commit c86c5800a75249ed45f82dc3497d7eba4e394fbd +Subproject commit f4469fc53dbe593fe787061c5da4e1fee511246b diff --git a/assets/scripts/entity.toy b/assets/scripts/entity.toy index 358963f..332cb61 100644 --- a/assets/scripts/entity.toy +++ b/assets/scripts/entity.toy @@ -11,7 +11,7 @@ var y: int = 50; var xspeed: int = 0; var yspeed: int = 0; -//accessors +//accessors - variables are private, functions are public fn getX(node: opaque) { return x; } @@ -24,7 +24,7 @@ fn getY(node: opaque) { fn onInit(node: opaque) { print "render.toy:onInit() called"; - node.loadTexture("assets/sprites/character.png"); + node.loadTexture("sprites:/character.png"); parent = node.getNodeParent(); } diff --git a/box/lib_node.c b/box/lib_node.c index c962eca..8587580 100644 --- a/box/lib_node.c +++ b/box/lib_node.c @@ -34,6 +34,12 @@ static int nativeLoadNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume Toy_Literal filePathLiteral = Toy_getFilePathLiteral(interpreter, &drivePathLiteral); + if (!TOY_IS_STRING(filePathLiteral)) { + Toy_freeLiteral(drivePathLiteral); + Toy_freeLiteral(filePathLiteral); + return -1; + } + Toy_freeLiteral(drivePathLiteral); //not needed anymore //load the new node @@ -287,12 +293,12 @@ static int nativeLoadTexture(Toy_Interpreter* interpreter, Toy_LiteralArray* arg } //extract the arguments - Toy_Literal fname = Toy_popLiteralArray(arguments); + Toy_Literal drivePathLiteral = Toy_popLiteralArray(arguments); Toy_Literal nodeLiteral = Toy_popLiteralArray(arguments); - Toy_Literal fnameIdn = fname; - if (TOY_IS_IDENTIFIER(fname) && Toy_parseIdentifierToValue(interpreter, &fname)) { - Toy_freeLiteral(fnameIdn); + Toy_Literal drivePathLiteralIdn = drivePathLiteral; + if (TOY_IS_IDENTIFIER(drivePathLiteral) && Toy_parseIdentifierToValue(interpreter, &drivePathLiteral)) { + Toy_freeLiteral(drivePathLiteralIdn); } Toy_Literal nodeIdn = nodeLiteral; @@ -301,13 +307,23 @@ static int nativeLoadTexture(Toy_Interpreter* interpreter, Toy_LiteralArray* arg } //check argument types - if (!TOY_IS_STRING(fname) || !TOY_IS_OPAQUE(nodeLiteral)) { + if (!TOY_IS_STRING(drivePathLiteral) || !TOY_IS_OPAQUE(nodeLiteral)) { interpreter->errorOutput("Incorrect argument type passed to loadTextureEngineNode\n"); - Toy_freeLiteral(fname); + Toy_freeLiteral(drivePathLiteral); Toy_freeLiteral(nodeLiteral); return -1; } + Toy_Literal filePathLiteral = Toy_getFilePathLiteral(interpreter, &drivePathLiteral); + + if (!TOY_IS_STRING(filePathLiteral)) { + Toy_freeLiteral(drivePathLiteral); + Toy_freeLiteral(filePathLiteral); + return -1; + } + + Toy_freeLiteral(drivePathLiteral); //not needed anymore + //actually load TODO: number the opaques, and check the numbers Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); @@ -315,16 +331,17 @@ static int nativeLoadTexture(Toy_Interpreter* interpreter, Toy_LiteralArray* arg Box_freeTextureEngineNode(node); } - if (Box_loadTextureEngineNode(node, Toy_toCString(TOY_AS_STRING(fname))) != 0) { + if (Box_loadTextureEngineNode(node, Toy_toCString(TOY_AS_STRING(filePathLiteral))) != 0) { interpreter->errorOutput("Failed to load the texture into the EngineNode\n"); - Toy_freeLiteral(fname); + Toy_freeLiteral(filePathLiteral); Toy_freeLiteral(nodeLiteral); return -1; } //cleanup - Toy_freeLiteral(fname); + Toy_freeLiteral(filePathLiteral); Toy_freeLiteral(nodeLiteral); + return 0; } diff --git a/box/lib_runner.c b/box/lib_runner.c index f544c62..ee1c5ef 100644 --- a/box/lib_runner.c +++ b/box/lib_runner.c @@ -618,13 +618,6 @@ Toy_Literal Toy_getFilePathLiteral(Toy_Interpreter* interpreter, Toy_Literal* dr Toy_deleteRefString(path); Toy_deleteRefString(drivePath); - //check for file extensions - if (!(filePath[realLength - 5] == '.' && filePath[realLength - 4] == 't' && filePath[realLength - 3] == 'o' && filePath[realLength - 2] == 'y')) { - interpreter->errorOutput("Bad script file extension (expected .toy)\n"); //TODO: add a bytecode version too - TOY_FREE_ARRAY(char, filePath, realLength + 1); - return TOY_TO_NULL_LITERAL; - } - //check for break-out attempts for (int i = 0; i < realLength - 1; i++) { if (filePath[i] == '.' && filePath[i + 1] == '.') { diff --git a/source/main.c b/source/main.c index 8a33d59..60e0d89 100644 --- a/source/main.c +++ b/source/main.c @@ -10,16 +10,31 @@ int main(int argc, char* argv[]) { //the drive system uses a LiteralDictionary, which must be initialized with this Toy_initDriveDictionary(); - //create a pair of literals, the first for the drive name, the second for the path - Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("scripts")); - Toy_Literal pathLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("assets/scripts")); + { + //create a pair of literals, the first for the drive name, the second for the path + Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("scripts")); + Toy_Literal pathLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("assets/scripts")); - //set these within the drive dictionary - Toy_setLiteralDictionary(Toy_getDriveDictionary(), driveLiteral, pathLiteral); + //set these within the drive dictionary + Toy_setLiteralDictionary(Toy_getDriveDictionary(), driveLiteral, pathLiteral); - //these literals are no longer needed - Toy_freeLiteral(driveLiteral); - Toy_freeLiteral(pathLiteral); + //these literals are no longer needed + Toy_freeLiteral(driveLiteral); + Toy_freeLiteral(pathLiteral); + } + + { + //create a pair of literals, the first for the drive name, the second for the path + Toy_Literal driveLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("sprites")); + Toy_Literal pathLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("assets/sprites")); + + //set these within the drive dictionary + Toy_setLiteralDictionary(Toy_getDriveDictionary(), driveLiteral, pathLiteral); + + //these literals are no longer needed + Toy_freeLiteral(driveLiteral); + Toy_freeLiteral(pathLiteral); + } //run the rest of your program Box_initEngine();