Updated Toy, added a couple util functions
This commit is contained in:
2
Toy
2
Toy
Submodule Toy updated: 168369d897...bd348abf32
@@ -1,3 +1,4 @@
|
||||
import engine;
|
||||
import render;
|
||||
|
||||
fn onInit(node: opaque) {
|
||||
@@ -5,7 +6,8 @@ fn onInit(node: opaque) {
|
||||
}
|
||||
|
||||
fn onStep(node: opaque) {
|
||||
//
|
||||
print node.getNodeTag();
|
||||
print node.getNodeParent().getNodeTag();
|
||||
}
|
||||
|
||||
fn onFree(node: opaque) {
|
||||
@@ -13,5 +15,5 @@ fn onFree(node: opaque) {
|
||||
}
|
||||
|
||||
fn onDraw(node: opaque) {
|
||||
node.drawRenderNode(50, 50);
|
||||
node.drawRenderNode(50, 50, 100, 100);
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 166 B After Width: | Height: | Size: 700 B |
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "memory.h"
|
||||
|
||||
STATIC_ASSERT(sizeof(EngineNode) == 32);
|
||||
STATIC_ASSERT(sizeof(EngineNode) == 48);
|
||||
STATIC_ASSERT(sizeof(EngineNodeCallback) == 8);
|
||||
STATIC_ASSERT(sizeof(LiteralDictionary*) == 8);
|
||||
STATIC_ASSERT(sizeof(EngineNode*) == 8);
|
||||
@@ -17,6 +17,8 @@ void initEngineNode(EngineNode* node, Interpreter* interpreter, void* tb, size_t
|
||||
//init
|
||||
node->freeMemory = freeMemory;
|
||||
node->functions = ALLOCATE(LiteralDictionary, 1);
|
||||
node->parent = NULL;
|
||||
node->tag = OPAQUE_TAG_ENGINE_NODE;
|
||||
node->children = NULL;
|
||||
node->capacity = 0;
|
||||
node->count = 0;
|
||||
@@ -69,6 +71,9 @@ void pushEngineNode(EngineNode* node, EngineNode* child) {
|
||||
|
||||
//assign
|
||||
node->children[node->count++] = child;
|
||||
|
||||
//reverse-assign
|
||||
child->parent = node;
|
||||
}
|
||||
|
||||
void freeEngineNode(EngineNode* node) {
|
||||
@@ -97,7 +102,7 @@ static void callEngineNodeLiteral(EngineNode* node, Interpreter* interpreter, Li
|
||||
//if this fn exists
|
||||
if (existsLiteralDictionary(node->functions, key)) {
|
||||
Literal fn = getLiteralDictionary(node->functions, key);
|
||||
Literal n = TO_OPAQUE_LITERAL(node, -1);
|
||||
Literal n = TO_OPAQUE_LITERAL(node, node->tag);
|
||||
|
||||
LiteralArray arguments;
|
||||
LiteralArray returns;
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
#include "literal_dictionary.h"
|
||||
#include "interpreter.h"
|
||||
|
||||
#define OPAQUE_TAG_ENGINE_NODE 1
|
||||
|
||||
//forward declare
|
||||
typedef struct _engineNode EngineNode;
|
||||
|
||||
typedef void (*EngineNodeCallback)(void*);
|
||||
|
||||
//the node object, which forms a tree
|
||||
@@ -18,6 +19,13 @@ typedef struct _engineNode {
|
||||
//toy functions, stored in a dict for flexibility
|
||||
LiteralDictionary* functions;
|
||||
|
||||
//point to the parent
|
||||
EngineNode* parent;
|
||||
|
||||
//my opaque type tag
|
||||
int tag;
|
||||
int _unused;
|
||||
|
||||
//use Toy's memory model
|
||||
EngineNode** children;
|
||||
int capacity;
|
||||
|
||||
@@ -201,7 +201,7 @@ static int nativeLoadNode(Interpreter* interpreter, LiteralArray* arguments) {
|
||||
//NOTE: initNode() must be called manually
|
||||
|
||||
// return the node
|
||||
Literal nodeLiteral = TO_OPAQUE_LITERAL(node, -1);
|
||||
Literal nodeLiteral = TO_OPAQUE_LITERAL(node, node->tag);
|
||||
pushLiteralArray(&interpreter->stack, nodeLiteral);
|
||||
|
||||
//cleanup
|
||||
@@ -397,7 +397,7 @@ static int nativeGetNode(Interpreter* interpreter, LiteralArray* arguments) {
|
||||
}
|
||||
|
||||
EngineNode* childNode = parentNode->children[intIndex];
|
||||
Literal child = TO_OPAQUE_LITERAL(childNode, -1);
|
||||
Literal child = TO_OPAQUE_LITERAL(childNode, childNode->tag);
|
||||
|
||||
pushLiteralArray(&interpreter->stack, child);
|
||||
|
||||
@@ -409,6 +409,76 @@ static int nativeGetNode(Interpreter* interpreter, LiteralArray* arguments) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nativeGetNodeParent(Interpreter* interpreter, LiteralArray* arguments) {
|
||||
//checks
|
||||
if (arguments->count != 1) {
|
||||
interpreter->errorOutput("Incorrect number of arguments passed to getNodeParent\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
Literal nodeLiteral = popLiteralArray(arguments);
|
||||
|
||||
Literal nodeIdn = nodeLiteral;
|
||||
if (IS_IDENTIFIER(nodeLiteral) && parseIdentifierToValue(interpreter, &nodeLiteral)) {
|
||||
freeLiteral(nodeIdn);
|
||||
}
|
||||
|
||||
if (!IS_OPAQUE(nodeLiteral)) {
|
||||
interpreter->errorOutput("Incorrect argument type passed to getNodeParent\n");
|
||||
freeLiteral(nodeLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//push the node
|
||||
EngineNode* node = AS_OPAQUE(nodeLiteral);
|
||||
EngineNode* parent = node->parent;
|
||||
|
||||
Literal parentLiteral = TO_NULL_LITERAL;
|
||||
if (parent != NULL) {
|
||||
parentLiteral = TO_OPAQUE_LITERAL(parent, parent->tag);
|
||||
}
|
||||
|
||||
pushLiteralArray(&interpreter->stack, parentLiteral);
|
||||
|
||||
//cleanup
|
||||
freeLiteral(parentLiteral);
|
||||
freeLiteral(nodeLiteral);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int nativeGetNodeTag(Interpreter* interpreter, LiteralArray* arguments) {
|
||||
//checks
|
||||
if (arguments->count != 1) {
|
||||
interpreter->errorOutput("Incorrect number of arguments passed to getNodeTag\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
Literal nodeLiteral = popLiteralArray(arguments);
|
||||
|
||||
Literal nodeIdn = nodeLiteral;
|
||||
if (IS_IDENTIFIER(nodeLiteral) && parseIdentifierToValue(interpreter, &nodeLiteral)) {
|
||||
freeLiteral(nodeIdn);
|
||||
}
|
||||
|
||||
if (!IS_OPAQUE(nodeLiteral)) {
|
||||
interpreter->errorOutput("Incorrect argument type passed to getNodeTag\n");
|
||||
freeLiteral(nodeLiteral);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//push the tag
|
||||
Literal tagLiteral = TO_INTEGER_LITERAL( ((EngineNode*)AS_OPAQUE(nodeLiteral))->tag );
|
||||
|
||||
pushLiteralArray(&interpreter->stack, tagLiteral);
|
||||
|
||||
//cleanup
|
||||
freeLiteral(nodeLiteral);
|
||||
freeLiteral(tagLiteral);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
//call the hook
|
||||
typedef struct Natives {
|
||||
char* name;
|
||||
@@ -426,6 +496,8 @@ int hookEngine(Interpreter* interpreter, Literal identifier, Literal alias) {
|
||||
{"_freeChildNode", nativeFreeChildNode},
|
||||
{"_pushNode", nativePushNode},
|
||||
{"_getNode", nativeGetNode},
|
||||
{"_getNodeParent", nativeGetNodeParent},
|
||||
{"_getNodeTag", nativeGetNodeTag},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ static int nativeLoadRenderNode(Interpreter* interpreter, LiteralArray* argument
|
||||
//NOTE: initNode() must be called manually
|
||||
|
||||
// return the node
|
||||
Literal nodeLiteral = TO_OPAQUE_LITERAL(node, -1);
|
||||
Literal nodeLiteral = TO_OPAQUE_LITERAL(node, node->tag);
|
||||
pushLiteralArray(&interpreter->stack, nodeLiteral);
|
||||
|
||||
//cleanup
|
||||
|
||||
@@ -19,6 +19,8 @@ void initRenderNode(RenderNode* node, Interpreter* interpreter, void* tb, size_t
|
||||
//init
|
||||
node->freeMemory = freeMemory;
|
||||
node->functions = ALLOCATE(LiteralDictionary, 1);
|
||||
node->parent = NULL;
|
||||
node->tag = OPAQUE_TAG_RENDER_NODE;
|
||||
node->children = NULL;
|
||||
node->capacity = 0;
|
||||
node->count = 0;
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#define OPAQUE_TAG_RENDER_NODE 2
|
||||
|
||||
typedef struct _renderNode {
|
||||
//function for releasing memory
|
||||
EngineNodeCallback freeMemory;
|
||||
@@ -14,6 +16,13 @@ typedef struct _renderNode {
|
||||
//toy functions, stored in a dict for flexibility
|
||||
LiteralDictionary* functions;
|
||||
|
||||
//point to the parent
|
||||
EngineNode* parent;
|
||||
|
||||
//my opaque type tag
|
||||
int tag;
|
||||
int _unused;
|
||||
|
||||
//use Toy's memory model
|
||||
EngineNode** children;
|
||||
int capacity;
|
||||
|
||||
Reference in New Issue
Block a user