Added animations to node

This commit is contained in:
2023-02-27 09:28:21 +11:00
parent 572b809d1b
commit 2c16c10558
11 changed files with 366 additions and 115 deletions

View File

@@ -13,6 +13,8 @@ void Box_initEngineNode(Box_EngineNode* node, Toy_Interpreter* interpreter, cons
node->capacity = 0;
node->count = 0;
node->texture = NULL;
node->rect = ((SDL_Rect) { 0, 0, 0, 0 });
node->frames = 0;
Toy_initLiteralDictionary(node->functions);
@@ -205,6 +207,7 @@ int Box_loadTextureEngineNode(Box_EngineNode* node, const char* fname) {
SDL_QueryTexture(node->texture, NULL, NULL, &w, &h);
SDL_Rect r = { 0, 0, w, h };
Box_setRectEngineNode(node, r);
Box_setFramesEngineNode(node, 1); //default
return 0;
}
@@ -220,6 +223,36 @@ void Box_setRectEngineNode(Box_EngineNode* node, SDL_Rect rect) {
node->rect = rect;
}
void Box_drawEngineNode(Box_EngineNode* node, SDL_Rect dest) {
SDL_RenderCopy(engine.renderer, node->texture, &node->rect, &dest);
BOX_API SDL_Rect Box_getRectEngineNode(Box_EngineNode* node) {
return node->rect;
}
BOX_API void Box_setFramesEngineNode(Box_EngineNode* node, int frames) {
node->frames = frames;
node->currentFrame = 0; //just in case
}
BOX_API int Box_getFramesEngineNode(Box_EngineNode* node) {
return node->frames;
}
BOX_API void Box_setCurrentFrameEngineNode(Box_EngineNode* node, int currentFrame) {
node->currentFrame = currentFrame;
}
BOX_API int Box_getCurrentFrameEngineNode(Box_EngineNode* node) {
return node->currentFrame;
}
BOX_API void Box_incrementCurrentFrame(Box_EngineNode* node) {
node->currentFrame++;
if (node->currentFrame >= node->frames) {
node->currentFrame = 0;
}
}
void Box_drawEngineNode(Box_EngineNode* node, SDL_Rect dest) {
SDL_Rect src = node->rect;
src.x += src.w * node->currentFrame; //TODO: improve this
SDL_RenderCopy(engine.renderer, node->texture, &src, &dest);
}

View File

@@ -24,7 +24,6 @@ typedef struct Box_private_engineNode {
//my opaque type tag
int tag;
// int _unused0;
//use Toy's memory model
Box_EngineNode** children;
@@ -34,16 +33,18 @@ typedef struct Box_private_engineNode {
//rendering-specific features
SDL_Texture* texture;
SDL_Rect rect;
//TODO: depth
} Box_EngineNode;
int frames;
int currentFrame;
} Box_EngineNode; //TODO: rename this?
BOX_API void Box_initEngineNode(Box_EngineNode* node, Toy_Interpreter* interpreter, const unsigned char* tb, size_t size); //run bytecode, then grab all top-level function literals
BOX_API void Box_pushEngineNode(Box_EngineNode* node, Box_EngineNode* child); //push to the array (prune tombstones when expanding/copying)
BOX_API void Box_freeEngineNode(Box_EngineNode* node); //free and tombstone this node
BOX_API void Box_freeEngineNode(Box_EngineNode* node); //free this node and all children
BOX_API Toy_Literal Box_callEngineNodeLiteral(Box_EngineNode* node, Toy_Interpreter* interpreter, Toy_Literal key, Toy_LiteralArray* args);
BOX_API Toy_Literal Box_callEngineNode(Box_EngineNode* node, Toy_Interpreter* interpreter, const char* fnName, Toy_LiteralArray* args); //call "fnName" on this node, and only this node, if it exists
//for calling various lifecycle functions
BOX_API void Box_callRecursiveEngineNodeLiteral(Box_EngineNode* node, Toy_Interpreter* interpreter, Toy_Literal key, Toy_LiteralArray* args);
BOX_API void Box_callRecursiveEngineNode(Box_EngineNode* node, Toy_Interpreter* interpreter, const char* fnName, Toy_LiteralArray* args); //call "fnName" on this node, and all children, if it exists
@@ -51,5 +52,12 @@ BOX_API int Box_loadTextureEngineNode(Box_EngineNode* node, const char* fname);
BOX_API void Box_freeTextureEngineNode(Box_EngineNode* node);
BOX_API void Box_setRectEngineNode(Box_EngineNode* node, SDL_Rect rect);
//TODO: getRect
BOX_API SDL_Rect Box_getRectEngineNode(Box_EngineNode* node);
BOX_API void Box_setFramesEngineNode(Box_EngineNode* node, int frames);
BOX_API int Box_getFramesEngineNode(Box_EngineNode* node);
BOX_API void Box_setCurrentFrameEngineNode(Box_EngineNode* node, int currentFrame);
BOX_API int Box_getCurrentFrameEngineNode(Box_EngineNode* node);
BOX_API void Box_incrementCurrentFrame(Box_EngineNode* node);
BOX_API void Box_drawEngineNode(Box_EngineNode* node, SDL_Rect dest);

View File

@@ -113,6 +113,97 @@ static int nativeInitNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume
return 0;
}
static int nativePushNode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
//checks
if (arguments->count != 2) {
interpreter->errorOutput("Incorrect number of arguments passed to pushNode\n");
return -1;
}
Toy_Literal child = Toy_popLiteralArray(arguments);
Toy_Literal parent = Toy_popLiteralArray(arguments);
Toy_Literal parentIdn = parent;
if (TOY_IS_IDENTIFIER(parent) && Toy_parseIdentifierToValue(interpreter, &parent)) {
Toy_freeLiteral(parentIdn);
}
Toy_Literal childIdn = child;
if (TOY_IS_IDENTIFIER(child) && Toy_parseIdentifierToValue(interpreter, &child)) {
Toy_freeLiteral(childIdn);
}
if (!TOY_IS_OPAQUE(parent) || !TOY_IS_OPAQUE(child)) {
interpreter->errorOutput("Incorrect argument type passed to pushNode\n");
Toy_freeLiteral(parent);
Toy_freeLiteral(child);
return -1;
}
//push the node
Box_EngineNode* parentNode = TOY_AS_OPAQUE(parent);
Box_EngineNode* childNode = TOY_AS_OPAQUE(child);
Box_pushEngineNode(parentNode, childNode);
//no return value
Toy_freeLiteral(parent);
Toy_freeLiteral(child);
return 0;
}
static int nativeGetChildNode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
//checks
if (arguments->count != 2) {
interpreter->errorOutput("Incorrect number of arguments passed to getChildNode\n");
return -1;
}
Toy_Literal index = Toy_popLiteralArray(arguments);
Toy_Literal parent = Toy_popLiteralArray(arguments);
Toy_Literal parentIdn = parent;
if (TOY_IS_IDENTIFIER(parent) && Toy_parseIdentifierToValue(interpreter, &parent)) {
Toy_freeLiteral(parentIdn);
}
Toy_Literal indexIdn = index;
if (TOY_IS_IDENTIFIER(index) && Toy_parseIdentifierToValue(interpreter, &index)) {
Toy_freeLiteral(indexIdn);
}
if (!TOY_IS_OPAQUE(parent) || !TOY_IS_INTEGER(index)) {
interpreter->errorOutput("Incorrect argument type passed to getChildNode\n");
Toy_freeLiteral(parent);
Toy_freeLiteral(index);
return -1;
}
//push the node
Box_EngineNode* parentNode = TOY_AS_OPAQUE(parent);
int intIndex = TOY_AS_INTEGER(index);
if (intIndex < 0 || intIndex >= parentNode->count) {
interpreter->errorOutput("index out of bounds in getChildNode\n");
Toy_freeLiteral(parent);
Toy_freeLiteral(index);
return -1;
}
Box_EngineNode* childNode = parentNode->children[intIndex];
Toy_Literal child = TOY_TO_OPAQUE_LITERAL(childNode, childNode->tag);
Toy_pushLiteralArray(&interpreter->stack, child);
//no return value
Toy_freeLiteral(parent);
Toy_freeLiteral(child);
Toy_freeLiteral(index);
return 1;
}
static int nativeFreeChildNode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
if (arguments->count != 2) {
interpreter->errorOutput("Incorrect number of arguments passed to freeChildNode\n");
@@ -162,101 +253,10 @@ static int nativeFreeChildNode(Toy_Interpreter* interpreter, Toy_LiteralArray* a
return 0;
}
static int nativePushNode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
//checks
if (arguments->count != 2) {
interpreter->errorOutput("Incorrect number of arguments passed to pushNode\n");
return -1;
}
Toy_Literal child = Toy_popLiteralArray(arguments);
Toy_Literal parent = Toy_popLiteralArray(arguments);
Toy_Literal parentIdn = parent;
if (TOY_IS_IDENTIFIER(parent) && Toy_parseIdentifierToValue(interpreter, &parent)) {
Toy_freeLiteral(parentIdn);
}
Toy_Literal childIdn = child;
if (TOY_IS_IDENTIFIER(child) && Toy_parseIdentifierToValue(interpreter, &child)) {
Toy_freeLiteral(childIdn);
}
if (!TOY_IS_OPAQUE(parent) || !TOY_IS_OPAQUE(child)) {
interpreter->errorOutput("Incorrect argument type passed to pushNode\n");
Toy_freeLiteral(parent);
Toy_freeLiteral(child);
return -1;
}
//push the node
Box_EngineNode* parentNode = TOY_AS_OPAQUE(parent);
Box_EngineNode* childNode = TOY_AS_OPAQUE(child);
Box_pushEngineNode(parentNode, childNode);
//no return value
Toy_freeLiteral(parent);
Toy_freeLiteral(child);
return 0;
}
static int nativeGetNodeChild(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
//checks
if (arguments->count != 2) {
interpreter->errorOutput("Incorrect number of arguments passed to getNodeChild\n");
return -1;
}
Toy_Literal index = Toy_popLiteralArray(arguments);
Toy_Literal parent = Toy_popLiteralArray(arguments);
Toy_Literal parentIdn = parent;
if (TOY_IS_IDENTIFIER(parent) && Toy_parseIdentifierToValue(interpreter, &parent)) {
Toy_freeLiteral(parentIdn);
}
Toy_Literal indexIdn = index;
if (TOY_IS_IDENTIFIER(index) && Toy_parseIdentifierToValue(interpreter, &index)) {
Toy_freeLiteral(indexIdn);
}
if (!TOY_IS_OPAQUE(parent) || !TOY_IS_INTEGER(index)) {
interpreter->errorOutput("Incorrect argument type passed to getNodeChild\n");
Toy_freeLiteral(parent);
Toy_freeLiteral(index);
return -1;
}
//push the node
Box_EngineNode* parentNode = TOY_AS_OPAQUE(parent);
int intIndex = TOY_AS_INTEGER(index);
if (intIndex < 0 || intIndex >= parentNode->count) {
interpreter->errorOutput("index out of bounds in getNodeChild\n");
Toy_freeLiteral(parent);
Toy_freeLiteral(index);
return -1;
}
Box_EngineNode* childNode = parentNode->children[intIndex];
Toy_Literal child = TOY_TO_OPAQUE_LITERAL(childNode, childNode->tag);
Toy_pushLiteralArray(&interpreter->stack, child);
//no return value
Toy_freeLiteral(parent);
Toy_freeLiteral(child);
Toy_freeLiteral(index);
return 1;
}
static int nativeGetNodeParent(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
static int nativeGetParentNode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
//checks
if (arguments->count != 1) {
interpreter->errorOutput("Incorrect number of arguments passed to getNodeParent\n");
interpreter->errorOutput("Incorrect number of arguments passed to getParentNode\n");
return -1;
}
@@ -268,7 +268,7 @@ static int nativeGetNodeParent(Toy_Interpreter* interpreter, Toy_LiteralArray* a
}
if (!TOY_IS_OPAQUE(nodeLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to getNodeParent\n");
interpreter->errorOutput("Incorrect argument type passed to getParentNode\n");
Toy_freeLiteral(nodeLiteral);
return -1;
}
@@ -384,9 +384,9 @@ static int nativeFreeTexture(Toy_Interpreter* interpreter, Toy_LiteralArray* arg
return 0;
}
static int nativeSetRect(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
static int nativeSetNodeRect(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
if (arguments->count != 5) {
interpreter->errorOutput("Incorrect number of arguments passed to setRect\n");
interpreter->errorOutput("Incorrect number of arguments passed to setNodeRect\n");
return -1;
}
@@ -424,7 +424,7 @@ static int nativeSetRect(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen
//check argument types
if (!TOY_IS_OPAQUE(nodeLiteral) || !TOY_IS_INTEGER(x) || !TOY_IS_INTEGER(y) || !TOY_IS_INTEGER(w) || !TOY_IS_INTEGER(h)) {
interpreter->errorOutput("Incorrect argument type passed to setRect\n");
interpreter->errorOutput("Incorrect argument type passed to setNodeRect\n");
Toy_freeLiteral(nodeLiteral);
Toy_freeLiteral(x);
Toy_freeLiteral(y);
@@ -449,7 +449,187 @@ static int nativeSetRect(Toy_Interpreter* interpreter, Toy_LiteralArray* argumen
return 0;
}
//TODO: get x, y, w, h
static int nativeSetNodeFrames(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
if (arguments->count != 2) {
interpreter->errorOutput("Incorrect number of arguments passed to setNodeFrames\n");
return -1;
}
//extract the arguments
Toy_Literal framesLiteral = Toy_popLiteralArray(arguments);
Toy_Literal nodeLiteral = Toy_popLiteralArray(arguments);
Toy_Literal nodeIdn = nodeLiteral;
if (TOY_IS_IDENTIFIER(nodeLiteral) && Toy_parseIdentifierToValue(interpreter, &nodeLiteral)) {
Toy_freeLiteral(nodeIdn);
}
Toy_Literal frameLiteralIdn = framesLiteral;
if (TOY_IS_IDENTIFIER(framesLiteral) && Toy_parseIdentifierToValue(interpreter, &framesLiteral)) {
Toy_freeLiteral(frameLiteralIdn);
}
//check argument types
if (!TOY_IS_OPAQUE(nodeLiteral) || !TOY_IS_INTEGER(framesLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to setNodeFrames\n");
Toy_freeLiteral(nodeLiteral);
Toy_freeLiteral(framesLiteral);
return -1;
}
//actually set
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral);
Box_setFramesEngineNode(node, TOY_AS_INTEGER(framesLiteral));
//cleanup
Toy_freeLiteral(nodeLiteral);
Toy_freeLiteral(framesLiteral);
return 0;
}
static int nativeGetNodeFrames(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
if (arguments->count != 1) {
interpreter->errorOutput("Incorrect number of arguments passed to getNodeFrames\n");
return -1;
}
//extract the arguments
Toy_Literal nodeLiteral = Toy_popLiteralArray(arguments);
Toy_Literal nodeIdn = nodeLiteral;
if (TOY_IS_IDENTIFIER(nodeLiteral) && Toy_parseIdentifierToValue(interpreter, &nodeLiteral)) {
Toy_freeLiteral(nodeIdn);
}
//check argument types
if (!TOY_IS_OPAQUE(nodeLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to getNodeFrames\n");
Toy_freeLiteral(nodeLiteral);
return -1;
}
//actually get
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral);
Toy_Literal framesLiteral = TOY_TO_INTEGER_LITERAL(node->frames);
Toy_pushLiteralArray(interpreter, framesLiteral);
//cleanup
Toy_freeLiteral(nodeLiteral);
Toy_freeLiteral(framesLiteral);
return 1;
}
static int nativeSetCurrentNodeFrame(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
if (arguments->count != 2) {
interpreter->errorOutput("Incorrect number of arguments passed to setCurrentNodeFrame\n");
return -1;
}
//extract the arguments
Toy_Literal currentFrameLiteral = Toy_popLiteralArray(arguments);
Toy_Literal nodeLiteral = Toy_popLiteralArray(arguments);
Toy_Literal nodeIdn = nodeLiteral;
if (TOY_IS_IDENTIFIER(nodeLiteral) && Toy_parseIdentifierToValue(interpreter, &nodeLiteral)) {
Toy_freeLiteral(nodeIdn);
}
Toy_Literal currentFrameLiteralIdn = currentFrameLiteral;
if (TOY_IS_IDENTIFIER(currentFrameLiteral) && Toy_parseIdentifierToValue(interpreter, &currentFrameLiteral)) {
Toy_freeLiteral(currentFrameLiteralIdn);
}
//check argument types
if (!TOY_IS_OPAQUE(nodeLiteral) || !TOY_IS_INTEGER(currentFrameLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to setCurrentNodeFrame\n");
Toy_freeLiteral(nodeLiteral);
Toy_freeLiteral(currentFrameLiteral);
return -1;
}
//actually set
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral);
Box_setFramesEngineNode(node, TOY_AS_INTEGER(currentFrameLiteral));
//cleanup
Toy_freeLiteral(nodeLiteral);
Toy_freeLiteral(currentFrameLiteral);
return 0;
}
static int nativeGetCurrentNodeFrame(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
if (arguments->count != 1) {
interpreter->errorOutput("Incorrect number of arguments passed to getCurrentNodeFrame\n");
return -1;
}
//extract the arguments
Toy_Literal nodeLiteral = Toy_popLiteralArray(arguments);
Toy_Literal nodeIdn = nodeLiteral;
if (TOY_IS_IDENTIFIER(nodeLiteral) && Toy_parseIdentifierToValue(interpreter, &nodeLiteral)) {
Toy_freeLiteral(nodeIdn);
}
//check argument types
if (!TOY_IS_OPAQUE(nodeLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to getCurrentNodeFrame\n");
Toy_freeLiteral(nodeLiteral);
return -1;
}
//actually get
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral);
Toy_Literal currentFrameLiteral = TOY_TO_INTEGER_LITERAL(node->currentFrame);
Toy_pushLiteralArray(interpreter, currentFrameLiteral);
//cleanup
Toy_freeLiteral(nodeLiteral);
Toy_freeLiteral(currentFrameLiteral);
return 1;
}
static int nativeIncrementCurrentNodeFrame(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
if (arguments->count != 1) {
interpreter->errorOutput("Incorrect number of arguments passed to incrementCurrentNodeFrame\n");
return -1;
}
//extract the arguments
Toy_Literal nodeLiteral = Toy_popLiteralArray(arguments);
Toy_Literal nodeIdn = nodeLiteral;
if (TOY_IS_IDENTIFIER(nodeLiteral) && Toy_parseIdentifierToValue(interpreter, &nodeLiteral)) {
Toy_freeLiteral(nodeIdn);
}
//check argument types
if (!TOY_IS_OPAQUE(nodeLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to incrementCurrentNodeFrame\n");
Toy_freeLiteral(nodeLiteral);
return -1;
}
//actually get
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral);
Box_incrementCurrentFrame(node);
//cleanup
Toy_freeLiteral(nodeLiteral);
return 0;
}
static int nativeDrawNode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
if (arguments->count != 3 && arguments->count != 5) {
@@ -614,17 +794,23 @@ int Box_hookNode(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Liter
Natives natives[] = {
{"loadNode", nativeLoadNode},
{"initNode", nativeInitNode},
{"freeChildNode", nativeFreeChildNode},
{"pushNode", nativePushNode},
{"getNodeChild", nativeGetNodeChild},
{"getNodeParent", nativeGetNodeParent},
{"getChildNode", nativeGetChildNode},
{"freeChildNode", nativeFreeChildNode},
{"getParentNode", nativeGetParentNode},
{"loadTexture", nativeLoadTexture},
{"freeTexture", nativeFreeTexture},
{"setRect", nativeSetRect},
{"setNodeRect", nativeSetNodeRect},
//get rect
{"setNodeFrames", nativeSetNodeFrames},
{"getNodeFrames", nativeGetNodeFrames},
{"setCurrentNodeFrame", nativeSetCurrentNodeFrame},
{"getCurrentNodeFrame", nativeGetCurrentNodeFrame},
{"incrementCurrentNodeFrame", nativeIncrementCurrentNodeFrame},
{"drawNode", nativeDrawNode},
{"callNodeFn", nativeCallNodeFn},
//get rect, get node var, create empty node, get child count, get root node
//TODO: get rect, get node var, create empty node, get child count, get root node
{NULL, NULL},
};