Renamed Box_EngineNode to Box_Node

This commit is contained in:
2023-06-14 23:20:36 +10:00
parent 887a3d063e
commit bfb985a08e
7 changed files with 177 additions and 177 deletions

View File

@@ -139,7 +139,7 @@ xcopy "$(SolutionDir)Toy\repl\repl_tools.*" "$(SolutionDir)box" /Y /I /E</Comman
<ItemGroup> <ItemGroup>
<ClCompile Include="box\box_common.c" /> <ClCompile Include="box\box_common.c" />
<ClCompile Include="box\box_engine.c" /> <ClCompile Include="box\box_engine.c" />
<ClCompile Include="box\box_engine_node.c" /> <ClCompile Include="box\box_node.c" />
<ClCompile Include="box\dbg_profiler.c" /> <ClCompile Include="box\dbg_profiler.c" />
<ClCompile Include="box\lib_about.c" /> <ClCompile Include="box\lib_about.c" />
<ClCompile Include="box\lib_engine.c" /> <ClCompile Include="box\lib_engine.c" />
@@ -153,7 +153,7 @@ xcopy "$(SolutionDir)Toy\repl\repl_tools.*" "$(SolutionDir)box" /Y /I /E</Comman
<ItemGroup> <ItemGroup>
<ClInclude Include="box\box_common.h" /> <ClInclude Include="box\box_common.h" />
<ClInclude Include="box\box_engine.h" /> <ClInclude Include="box\box_engine.h" />
<ClInclude Include="box\box_engine_node.h" /> <ClInclude Include="box\box_node.h" />
<ClInclude Include="box\dbg_profiler.h" /> <ClInclude Include="box\dbg_profiler.h" />
<ClInclude Include="box\lib_about.h" /> <ClInclude Include="box\lib_about.h" />
<ClInclude Include="box\lib_engine.h" /> <ClInclude Include="box\lib_engine.h" />

View File

@@ -82,8 +82,8 @@ void Box_initEngine() {
void Box_freeEngine() { void Box_freeEngine() {
//clear existing root node //clear existing root node
if (engine.rootNode != NULL) { if (engine.rootNode != NULL) {
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onFree", NULL); Box_callRecursiveNode(engine.rootNode, &engine.interpreter, "onFree", NULL);
Box_freeEngineNode(engine.rootNode); Box_freeNode(engine.rootNode);
engine.rootNode = NULL; engine.rootNode = NULL;
} }
@@ -114,8 +114,8 @@ static inline void execLoadRootNode() {
//free the existing root node //free the existing root node
if (engine.rootNode != NULL) { if (engine.rootNode != NULL) {
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onFree", NULL); Box_callRecursiveNode(engine.rootNode, &engine.interpreter, "onFree", NULL);
Box_freeEngineNode(engine.rootNode); Box_freeNode(engine.rootNode);
engine.rootNode = NULL; engine.rootNode = NULL;
} }
@@ -126,7 +126,7 @@ static inline void execLoadRootNode() {
free((void*)source); free((void*)source);
//allocate the new root node //allocate the new root node
engine.rootNode = TOY_ALLOCATE(Box_EngineNode, 1); engine.rootNode = TOY_ALLOCATE(Box_Node, 1);
//BUGFIX: make an inner-interpreter //BUGFIX: make an inner-interpreter
Toy_Interpreter inner; Toy_Interpreter inner;
@@ -146,10 +146,10 @@ static inline void execLoadRootNode() {
Toy_setInterpreterAssert(&inner, engine.interpreter.assertOutput); Toy_setInterpreterAssert(&inner, engine.interpreter.assertOutput);
Toy_setInterpreterError(&inner, engine.interpreter.errorOutput); Toy_setInterpreterError(&inner, engine.interpreter.errorOutput);
Box_initEngineNode(engine.rootNode, &inner, tb, size); Box_initNode(engine.rootNode, &inner, tb, size);
//immediately call onLoad() after running the script - for loading other nodes //immediately call onLoad() after running the script - for loading other nodes
Box_callEngineNode(engine.rootNode, &inner, "onLoad", NULL); Box_callNode(engine.rootNode, &inner, "onLoad", NULL);
//cache the scope for later freeing //cache the scope for later freeing
engine.rootNode->scope = inner.scope; engine.rootNode->scope = inner.scope;
@@ -163,7 +163,7 @@ static inline void execLoadRootNode() {
engine.nextRootNodeFilename = TOY_TO_NULL_LITERAL; engine.nextRootNodeFilename = TOY_TO_NULL_LITERAL;
//init the new node-tree //init the new node-tree
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onInit", NULL); Box_callRecursiveNode(engine.rootNode, &engine.interpreter, "onInit", NULL);
} }
static inline void execEvents() { static inline void execEvents() {
@@ -213,7 +213,7 @@ static inline void execEvents() {
//call the function //call the function
Toy_pushLiteralArray(&args, eventLiteral); Toy_pushLiteralArray(&args, eventLiteral);
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onKeyDown", &args); Box_callRecursiveNode(engine.rootNode, &engine.interpreter, "onKeyDown", &args);
Toy_freeLiteral(Toy_popLiteralArray(&args)); Toy_freeLiteral(Toy_popLiteralArray(&args));
//push to the event list //push to the event list
@@ -240,7 +240,7 @@ static inline void execEvents() {
//call the function //call the function
Toy_pushLiteralArray(&args, eventLiteral); Toy_pushLiteralArray(&args, eventLiteral);
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onKeyUp", &args); Box_callRecursiveNode(engine.rootNode, &engine.interpreter, "onKeyUp", &args);
Toy_freeLiteral(Toy_popLiteralArray(&args)); Toy_freeLiteral(Toy_popLiteralArray(&args));
//push to the event list //push to the event list
@@ -261,7 +261,7 @@ static inline void execEvents() {
Toy_pushLiteralArray(&args, mouseXRel); Toy_pushLiteralArray(&args, mouseXRel);
Toy_pushLiteralArray(&args, mouseYRel); Toy_pushLiteralArray(&args, mouseYRel);
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onMouseMotion", &args); Box_callRecursiveNode(engine.rootNode, &engine.interpreter, "onMouseMotion", &args);
Toy_freeLiteral(mouseX); Toy_freeLiteral(mouseX);
Toy_freeLiteral(mouseY); Toy_freeLiteral(mouseY);
@@ -312,7 +312,7 @@ static inline void execEvents() {
Toy_pushLiteralArray(&args, mouseY); Toy_pushLiteralArray(&args, mouseY);
Toy_pushLiteralArray(&args, mouseButton); Toy_pushLiteralArray(&args, mouseButton);
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onMouseButtonDown", &args); Box_callRecursiveNode(engine.rootNode, &engine.interpreter, "onMouseButtonDown", &args);
Toy_freeLiteral(mouseX); Toy_freeLiteral(mouseX);
Toy_freeLiteral(mouseY); Toy_freeLiteral(mouseY);
@@ -362,7 +362,7 @@ static inline void execEvents() {
Toy_pushLiteralArray(&args, mouseY); Toy_pushLiteralArray(&args, mouseY);
Toy_pushLiteralArray(&args, mouseButton); Toy_pushLiteralArray(&args, mouseButton);
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onMouseButtonUp", &args); Box_callRecursiveNode(engine.rootNode, &engine.interpreter, "onMouseButtonUp", &args);
Toy_freeLiteral(mouseX); Toy_freeLiteral(mouseX);
Toy_freeLiteral(mouseY); Toy_freeLiteral(mouseY);
@@ -383,7 +383,7 @@ static inline void execEvents() {
Toy_pushLiteralArray(&args, mouseX); Toy_pushLiteralArray(&args, mouseX);
Toy_pushLiteralArray(&args, mouseY); Toy_pushLiteralArray(&args, mouseY);
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onMouseWheel", &args); Box_callRecursiveNode(engine.rootNode, &engine.interpreter, "onMouseWheel", &args);
Toy_freeLiteral(mouseX); Toy_freeLiteral(mouseX);
Toy_freeLiteral(mouseY); Toy_freeLiteral(mouseY);
@@ -404,7 +404,7 @@ static inline void execEvents() {
static inline void execStep() { static inline void execStep() {
if (engine.rootNode != NULL) { if (engine.rootNode != NULL) {
//steps //steps
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onStep", NULL); Box_callRecursiveNode(engine.rootNode, &engine.interpreter, "onStep", NULL);
} }
} }
@@ -461,7 +461,7 @@ void Box_execEngine() {
Dbg_stopTimer(&dbgTimer); Dbg_stopTimer(&dbgTimer);
Dbg_startTimer(&dbgTimer, "onDraw() calls"); Dbg_startTimer(&dbgTimer, "onDraw() calls");
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onDraw", NULL); Box_callRecursiveNode(engine.rootNode, &engine.interpreter, "onDraw", NULL);
Dbg_stopTimer(&dbgTimer); Dbg_stopTimer(&dbgTimer);
Dbg_startTimer(&dbgTimer, "render screen"); Dbg_startTimer(&dbgTimer, "render screen");

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#include "box_common.h" #include "box_common.h"
#include "box_engine_node.h" #include "box_node.h"
#include "toy_interpreter.h" #include "toy_interpreter.h"
#include "toy_literal_array.h" #include "toy_literal_array.h"
@@ -12,7 +12,7 @@
//the base engine object, which represents the state of the game //the base engine object, which represents the state of the game
typedef struct Box_private_engine { typedef struct Box_private_engine {
//engine stuff //engine stuff
Box_EngineNode* rootNode; Box_Node* rootNode;
Toy_Literal nextRootNodeFilename; Toy_Literal nextRootNodeFilename;
clock_t simTime; clock_t simTime;
clock_t realTime; clock_t realTime;

View File

@@ -1,74 +0,0 @@
#pragma once
#include "box_common.h"
#include "toy_literal_dictionary.h"
#include "toy_interpreter.h"
#define OPAQUE_TAG_ENGINE_NODE 1001
//forward declare
typedef struct Box_private_engineNode Box_EngineNode;
// typedef void (*EngineNodeCallback)(void*);
//the node object, which forms a tree
typedef struct Box_private_engineNode {
//function for releasing memory NOTE: removed, because it's not needed with only 1 node type - I've left them commented out because I might need them soon
// EngineNodeCallback freeMemory;
//toy functions, stored in a dict for flexibility
Toy_LiteralDictionary* functions;
//point to the parent
Box_EngineNode* parent;
//BUGFIX: hold the node's scope so it can be popped
Toy_Scope* scope;
//my opaque type tag
int tag;
//use Toy's memory model
Box_EngineNode** children;
int capacity;
int count; //includes tombstones
int childCount;
//rendering-specific features
SDL_Texture* texture;
SDL_Rect rect;
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 this node and all children
BOX_API Box_EngineNode* Box_getChildEngineNode(Box_EngineNode* node, int index);
BOX_API void Box_freeChildEngineNode(Box_EngineNode* node, int index);
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
BOX_API int Box_getChildCountEngineNode(Box_EngineNode* node);
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);
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_setTextEngineNode(Box_EngineNode* node, TTF_Font* font, const char* text, SDL_Color color);
BOX_API void Box_drawEngineNode(Box_EngineNode* node, SDL_Rect dest);

View File

@@ -1,15 +1,15 @@
#include "box_engine_node.h" #include "box_node.h"
#include "box_engine.h" #include "box_engine.h"
#include "toy_memory.h" #include "toy_memory.h"
void Box_initEngineNode(Box_EngineNode* node, Toy_Interpreter* interpreter, const unsigned char* tb, size_t size) { void Box_initNode(Box_Node* node, Toy_Interpreter* interpreter, const unsigned char* tb, size_t size) {
//init //init
// node->freeMemory = freeMemory; // node->freeMemory = freeMemory;
node->functions = TOY_ALLOCATE(Toy_LiteralDictionary, 1); node->functions = TOY_ALLOCATE(Toy_LiteralDictionary, 1);
node->parent = NULL; node->parent = NULL;
node->scope = NULL; node->scope = NULL;
node->tag = OPAQUE_TAG_ENGINE_NODE; node->tag = OPAQUE_TAG_NODE;
node->children = NULL; node->children = NULL;
node->capacity = 0; node->capacity = 0;
node->count = 0; node->count = 0;
@@ -41,13 +41,13 @@ void Box_initEngineNode(Box_EngineNode* node, Toy_Interpreter* interpreter, cons
} }
} }
void Box_pushEngineNode(Box_EngineNode* node, Box_EngineNode* child) { void Box_pushNode(Box_Node* node, Box_Node* child) {
//push to the array //push to the array
if (node->count + 1 > node->capacity) { if (node->count + 1 > node->capacity) {
int oldCapacity = node->capacity; int oldCapacity = node->capacity;
node->capacity = TOY_GROW_CAPACITY(oldCapacity); node->capacity = TOY_GROW_CAPACITY(oldCapacity);
node->children = TOY_GROW_ARRAY(Box_EngineNode*, node->children, oldCapacity, node->capacity); node->children = TOY_GROW_ARRAY(Box_Node*, node->children, oldCapacity, node->capacity);
} }
//assign //assign
@@ -60,18 +60,18 @@ void Box_pushEngineNode(Box_EngineNode* node, Box_EngineNode* child) {
node->childCount++; node->childCount++;
} }
void Box_freeEngineNode(Box_EngineNode* node) { void Box_freeNode(Box_Node* node) {
if (node == NULL) { if (node == NULL) {
return; //NO-OP return; //NO-OP
} }
//free this node's children //free this node's children
for (int i = 0; i < node->count; i++) { for (int i = 0; i < node->count; i++) {
Box_freeEngineNode(node->children[i]); Box_freeNode(node->children[i]);
} }
//free the pointer array to the children //free the pointer array to the children
TOY_FREE_ARRAY(Box_EngineNode*, node->children, node->capacity); TOY_FREE_ARRAY(Box_Node*, node->children, node->capacity);
if (node->functions != NULL) { if (node->functions != NULL) {
Toy_freeLiteralDictionary(node->functions); Toy_freeLiteralDictionary(node->functions);
@@ -83,14 +83,14 @@ void Box_freeEngineNode(Box_EngineNode* node) {
} }
if (node->texture != NULL) { if (node->texture != NULL) {
Box_freeTextureEngineNode(node); Box_freeTextureNode(node);
} }
//free this node's memory //free this node's memory
TOY_FREE(Box_EngineNode, node); TOY_FREE(Box_Node, node);
} }
Box_EngineNode* Box_getChildEngineNode(Box_EngineNode* node, int index) { Box_Node* Box_getChildNode(Box_Node* node, int index) {
if (index < 0 || index > node->count) { if (index < 0 || index > node->count) {
return NULL; return NULL;
} }
@@ -98,20 +98,20 @@ Box_EngineNode* Box_getChildEngineNode(Box_EngineNode* node, int index) {
return node->children[index]; return node->children[index];
} }
void Box_freeChildEngineNode(Box_EngineNode* node, int index) { void Box_freeChildNode(Box_Node* node, int index) {
//get the child node //get the child node
Box_EngineNode* childNode = node->children[index]; Box_Node* childNode = node->children[index];
//free the node //free the node
if (childNode != NULL) { if (childNode != NULL) {
Box_freeEngineNode(childNode); Box_freeNode(childNode);
node->childCount--; node->childCount--;
} }
node->children[index] = NULL; node->children[index] = NULL;
} }
Toy_Literal Box_callEngineNodeLiteral(Box_EngineNode* node, Toy_Interpreter* interpreter, Toy_Literal key, Toy_LiteralArray* args) { Toy_Literal Box_callNodeLiteral(Box_Node* node, Toy_Interpreter* interpreter, Toy_Literal key, Toy_LiteralArray* args) {
Toy_Literal ret = TOY_TO_NULL_LITERAL; Toy_Literal ret = TOY_TO_NULL_LITERAL;
//if this fn exists //if this fn exists
@@ -147,18 +147,18 @@ Toy_Literal Box_callEngineNodeLiteral(Box_EngineNode* node, Toy_Interpreter* int
return ret; return ret;
} }
Toy_Literal Box_callEngineNode(Box_EngineNode* node, Toy_Interpreter* interpreter, const char* fnName, Toy_LiteralArray* args) { Toy_Literal Box_callNode(Box_Node* node, Toy_Interpreter* interpreter, const char* fnName, Toy_LiteralArray* args) {
//call "fnName" on this node, and all children, if it exists //call "fnName" on this node, and all children, if it exists
Toy_Literal key = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefString(fnName)); Toy_Literal key = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefString(fnName));
Toy_Literal ret = Box_callEngineNodeLiteral(node, interpreter, key, args); Toy_Literal ret = Box_callNodeLiteral(node, interpreter, key, args);
Toy_freeLiteral(key); Toy_freeLiteral(key);
return ret; return ret;
} }
void Box_callRecursiveEngineNodeLiteral(Box_EngineNode* node, Toy_Interpreter* interpreter, Toy_Literal key, Toy_LiteralArray* args) { void Box_callRecursiveNodeLiteral(Box_Node* node, Toy_Interpreter* interpreter, Toy_Literal key, Toy_LiteralArray* args) {
//if this fn exists //if this fn exists
if (Toy_existsLiteralDictionary(node->functions, key)) { if (Toy_existsLiteralDictionary(node->functions, key)) {
Toy_Literal fn = Toy_getLiteralDictionary(node->functions, key); Toy_Literal fn = Toy_getLiteralDictionary(node->functions, key);
@@ -190,25 +190,25 @@ void Box_callRecursiveEngineNodeLiteral(Box_EngineNode* node, Toy_Interpreter* i
//recurse to the (non-tombstone) children //recurse to the (non-tombstone) children
for (int i = 0; i < node->count; i++) { for (int i = 0; i < node->count; i++) {
if (node->children[i] != NULL) { if (node->children[i] != NULL) {
Box_callRecursiveEngineNodeLiteral(node->children[i], interpreter, key, args); Box_callRecursiveNodeLiteral(node->children[i], interpreter, key, args);
} }
} }
} }
void Box_callRecursiveEngineNode(Box_EngineNode* node, Toy_Interpreter* interpreter, const char* fnName, Toy_LiteralArray* args) { void Box_callRecursiveNode(Box_Node* node, Toy_Interpreter* interpreter, const char* fnName, Toy_LiteralArray* args) {
//call "fnName" on this node, and all children, if it exists //call "fnName" on this node, and all children, if it exists
Toy_Literal key = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefString(fnName)); Toy_Literal key = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefString(fnName));
Box_callRecursiveEngineNodeLiteral(node, interpreter, key, args); Box_callRecursiveNodeLiteral(node, interpreter, key, args);
Toy_freeLiteral(key); Toy_freeLiteral(key);
} }
int Box_getChildCountEngineNode(Box_EngineNode* node) { int Box_getChildCountNode(Box_Node* node) {
return node->childCount; return node->childCount;
} }
int Box_loadTextureEngineNode(Box_EngineNode* node, const char* fname) { int Box_loadTextureNode(Box_Node* node, const char* fname) {
SDL_Surface* surface = IMG_Load(fname); SDL_Surface* surface = IMG_Load(fname);
if (surface == NULL) { if (surface == NULL) {
@@ -226,52 +226,52 @@ int Box_loadTextureEngineNode(Box_EngineNode* node, const char* fname) {
int w, h; int w, h;
SDL_QueryTexture(node->texture, NULL, NULL, &w, &h); SDL_QueryTexture(node->texture, NULL, NULL, &w, &h);
SDL_Rect r = { 0, 0, w, h }; SDL_Rect r = { 0, 0, w, h };
Box_setRectEngineNode(node, r); Box_setRectNode(node, r);
Box_setFramesEngineNode(node, 1); //default Box_setFramesNode(node, 1); //default
return 0; return 0;
} }
void Box_freeTextureEngineNode(Box_EngineNode* node) { void Box_freeTextureNode(Box_Node* node) {
if (node->texture != NULL) { if (node->texture != NULL) {
SDL_DestroyTexture(node->texture); SDL_DestroyTexture(node->texture);
node->texture = NULL; node->texture = NULL;
} }
} }
void Box_setRectEngineNode(Box_EngineNode* node, SDL_Rect rect) { void Box_setRectNode(Box_Node* node, SDL_Rect rect) {
node->rect = rect; node->rect = rect;
} }
SDL_Rect Box_getRectEngineNode(Box_EngineNode* node) { SDL_Rect Box_getRectNode(Box_Node* node) {
return node->rect; return node->rect;
} }
void Box_setFramesEngineNode(Box_EngineNode* node, int frames) { void Box_setFramesNode(Box_Node* node, int frames) {
node->frames = frames; node->frames = frames;
node->currentFrame = 0; //just in case node->currentFrame = 0; //just in case
} }
int Box_getFramesEngineNode(Box_EngineNode* node) { int Box_getFramesNode(Box_Node* node) {
return node->frames; return node->frames;
} }
void Box_setCurrentFrameEngineNode(Box_EngineNode* node, int currentFrame) { void Box_setCurrentFrameNode(Box_Node* node, int currentFrame) {
node->currentFrame = currentFrame; node->currentFrame = currentFrame;
} }
int Box_getCurrentFrameEngineNode(Box_EngineNode* node) { int Box_getCurrentFrameNode(Box_Node* node) {
return node->currentFrame; return node->currentFrame;
} }
void Box_incrementCurrentFrame(Box_EngineNode* node) { void Box_incrementCurrentFrame(Box_Node* node) {
node->currentFrame++; node->currentFrame++;
if (node->currentFrame >= node->frames) { if (node->currentFrame >= node->frames) {
node->currentFrame = 0; node->currentFrame = 0;
} }
} }
void Box_setTextEngineNode(Box_EngineNode* node, TTF_Font* font, const char* text, SDL_Color color) { void Box_setTextNode(Box_Node* node, TTF_Font* font, const char* text, SDL_Color color) {
SDL_Surface* surface = TTF_RenderText_Solid(font, text, color); SDL_Surface* surface = TTF_RenderText_Solid(font, text, color);
node->texture = SDL_CreateTextureFromSurface(engine.renderer, surface); node->texture = SDL_CreateTextureFromSurface(engine.renderer, surface);
@@ -284,7 +284,7 @@ void Box_setTextEngineNode(Box_EngineNode* node, TTF_Font* font, const char* tex
} }
void Box_drawEngineNode(Box_EngineNode* node, SDL_Rect dest) { void Box_drawNode(Box_Node* node, SDL_Rect dest) {
if (!node->texture) return; if (!node->texture) return;
SDL_Rect src = node->rect; SDL_Rect src = node->rect;
src.x += src.w * node->currentFrame; //TODO: improve this src.x += src.w * node->currentFrame; //TODO: improve this

74
box/box_node.h Normal file
View File

@@ -0,0 +1,74 @@
#pragma once
#include "box_common.h"
#include "toy_literal_dictionary.h"
#include "toy_interpreter.h"
#define OPAQUE_TAG_NODE 1001
//forward declare
typedef struct Box_private_node Box_Node;
// typedef void (*Box_NodeCallback)(void*);
//the node object, which forms a tree
typedef struct Box_private_node {
//function for releasing memory NOTE: removed, because it's not needed with only 1 node type - I've left them commented out because I might need them soon
// Box_NodeCallback freeMemory;
//toy functions, stored in a dict for flexibility
Toy_LiteralDictionary* functions;
//point to the parent
Box_Node* parent;
//BUGFIX: hold the node's scope so it can be popped
Toy_Scope* scope;
//my opaque type tag
int tag;
//use Toy's memory model
Box_Node** children;
int capacity;
int count; //includes tombstones
int childCount;
//rendering-specific features
SDL_Texture* texture;
SDL_Rect rect;
int frames;
int currentFrame;
} Box_Node; //TODO: rename this?
BOX_API void Box_initNode(Box_Node* node, Toy_Interpreter* interpreter, const unsigned char* tb, size_t size); //run bytecode, then grab all top-level function literals
BOX_API void Box_pushNode(Box_Node* node, Box_Node* child); //push to the array (prune tombstones when expanding/copying)
BOX_API void Box_freeNode(Box_Node* node); //free this node and all children
BOX_API Box_Node* Box_getChildNode(Box_Node* node, int index);
BOX_API void Box_freeChildNode(Box_Node* node, int index);
BOX_API Toy_Literal Box_callNodeLiteral(Box_Node* node, Toy_Interpreter* interpreter, Toy_Literal key, Toy_LiteralArray* args);
BOX_API Toy_Literal Box_callNode(Box_Node* 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_callRecursiveNodeLiteral(Box_Node* node, Toy_Interpreter* interpreter, Toy_Literal key, Toy_LiteralArray* args);
BOX_API void Box_callRecursiveNode(Box_Node* node, Toy_Interpreter* interpreter, const char* fnName, Toy_LiteralArray* args); //call "fnName" on this node, and all children, if it exists
BOX_API int Box_getChildCountNode(Box_Node* node);
BOX_API int Box_loadTextureNode(Box_Node* node, const char* fname);
BOX_API void Box_freeTextureNode(Box_Node* node);
BOX_API void Box_setRectNode(Box_Node* node, SDL_Rect rect);
BOX_API SDL_Rect Box_getRectNode(Box_Node* node);
BOX_API void Box_setFramesNode(Box_Node* node, int frames);
BOX_API int Box_getFramesNode(Box_Node* node);
BOX_API void Box_setCurrentFrameNode(Box_Node* node, int currentFrame);
BOX_API int Box_getCurrentFrameNode(Box_Node* node);
BOX_API void Box_incrementCurrentFrame(Box_Node* node);
BOX_API void Box_setTextNode(Box_Node* node, TTF_Font* font, const char* text, SDL_Color color);
BOX_API void Box_drawNode(Box_Node* node, SDL_Rect dest);

View File

@@ -1,6 +1,6 @@
#include "lib_node.h" #include "lib_node.h"
#include "box_engine_node.h" #include "box_node.h"
#include "box_engine.h" #include "box_engine.h"
#include "repl_tools.h" #include "repl_tools.h"
@@ -47,7 +47,7 @@ static int nativeLoadNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume
const unsigned char* tb = Toy_compileString((const char*)source, &size); const unsigned char* tb = Toy_compileString((const char*)source, &size);
free((void*)source); free((void*)source);
Box_EngineNode* node = TOY_ALLOCATE(Box_EngineNode, 1); Box_Node* node = TOY_ALLOCATE(Box_Node, 1);
//BUGFIX: make an -interpreter //BUGFIX: make an -interpreter
Toy_Interpreter inner; Toy_Interpreter inner;
@@ -67,10 +67,10 @@ static int nativeLoadNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume
Toy_setInterpreterAssert(&inner, interpreter->assertOutput); Toy_setInterpreterAssert(&inner, interpreter->assertOutput);
Toy_setInterpreterError(&inner, interpreter->errorOutput); Toy_setInterpreterError(&inner, interpreter->errorOutput);
Box_initEngineNode(node, &inner, tb, size); Box_initNode(node, &inner, tb, size);
//immediately call onLoad() after running the script - for loading other nodes //immediately call onLoad() after running the script - for loading other nodes
Box_callEngineNode(node, &inner, "onLoad", NULL); Box_callNode(node, &inner, "onLoad", NULL);
// return the node // return the node
Toy_Literal nodeLiteral = TOY_TO_OPAQUE_LITERAL(node, node->tag); Toy_Literal nodeLiteral = TOY_TO_OPAQUE_LITERAL(node, node->tag);
@@ -93,27 +93,27 @@ static int nativeInitNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume
return -1; return -1;
} }
Toy_Literal node = Toy_popLiteralArray(arguments); Toy_Literal nodeLiteral = Toy_popLiteralArray(arguments);
Toy_Literal nodeIdn = node; Toy_Literal nodeIdn = nodeLiteral;
if (TOY_IS_IDENTIFIER(node) && Toy_parseIdentifierToValue(interpreter, &node)) { if (TOY_IS_IDENTIFIER(nodeLiteral) && Toy_parseIdentifierToValue(interpreter, &nodeLiteral)) {
Toy_freeLiteral(nodeIdn); Toy_freeLiteral(nodeIdn);
} }
//check argument types //check argument types
if (!TOY_IS_OPAQUE(node)) { if (!TOY_IS_OPAQUE(nodeLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to initNode\n"); interpreter->errorOutput("Incorrect argument type passed to initNode\n");
Toy_freeLiteral(node); Toy_freeLiteral(nodeLiteral);
return -1; return -1;
} }
Box_EngineNode* engineNode = TOY_AS_OPAQUE(node); Box_Node* node = TOY_AS_OPAQUE(nodeLiteral);
//init the new node (and ONLY this node) //init the new node (and ONLY this node)
Box_callEngineNode(engineNode, &engine.interpreter, "onInit", NULL); Box_callNode(node, &engine.interpreter, "onInit", NULL);
//cleanup //cleanup
Toy_freeLiteral(node); Toy_freeLiteral(nodeLiteral);
return 0; return 0;
} }
@@ -145,10 +145,10 @@ static int nativePushNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume
} }
//push the node //push the node
Box_EngineNode* parentNode = TOY_AS_OPAQUE(parent); Box_Node* parentNode = TOY_AS_OPAQUE(parent);
Box_EngineNode* childNode = TOY_AS_OPAQUE(child); Box_Node* childNode = TOY_AS_OPAQUE(child);
Box_pushEngineNode(parentNode, childNode); Box_pushNode(parentNode, childNode);
//no return value //no return value
Toy_freeLiteral(parent); Toy_freeLiteral(parent);
@@ -185,7 +185,7 @@ static int nativeGetChildNode(Toy_Interpreter* interpreter, Toy_LiteralArray* ar
} }
//push the node //push the node
Box_EngineNode* parentNode = TOY_AS_OPAQUE(parent); Box_Node* parentNode = TOY_AS_OPAQUE(parent);
int intIndex = TOY_AS_INTEGER(index); int intIndex = TOY_AS_INTEGER(index);
if (intIndex < 0 || intIndex >= parentNode->count) { if (intIndex < 0 || intIndex >= parentNode->count) {
@@ -195,7 +195,7 @@ static int nativeGetChildNode(Toy_Interpreter* interpreter, Toy_LiteralArray* ar
return -1; return -1;
} }
Box_EngineNode* childNode = Box_getChildEngineNode(parentNode, intIndex); Box_Node* childNode = Box_getChildNode(parentNode, intIndex);
Toy_Literal child; Toy_Literal child;
if (childNode == NULL) { if (childNode == NULL) {
@@ -236,7 +236,7 @@ static int nativeFreeChildNode(Toy_Interpreter* interpreter, Toy_LiteralArray* a
return -1; return -1;
} }
Box_EngineNode* node = TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = TOY_AS_OPAQUE(nodeLiteral);
int idx = TOY_AS_INTEGER(indexLiteral); int idx = TOY_AS_INTEGER(indexLiteral);
//check bounds //check bounds
@@ -248,8 +248,8 @@ static int nativeFreeChildNode(Toy_Interpreter* interpreter, Toy_LiteralArray* a
} }
//TODO: differentiate between onFree() and freeing memory //TODO: differentiate between onFree() and freeing memory
Box_callRecursiveEngineNode(node, interpreter, "onFree", NULL); Box_callRecursiveNode(node, interpreter, "onFree", NULL);
Box_freeChildEngineNode(node, idx); Box_freeChildNode(node, idx);
//cleanup //cleanup
Toy_freeLiteral(nodeLiteral); Toy_freeLiteral(nodeLiteral);
@@ -278,8 +278,8 @@ static int nativeGetParentNode(Toy_Interpreter* interpreter, Toy_LiteralArray* a
} }
//push the node //push the node
Box_EngineNode* node = TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = TOY_AS_OPAQUE(nodeLiteral);
Box_EngineNode* parent = node->parent; Box_Node* parent = node->parent;
Toy_Literal parentLiteral = TOY_TO_NULL_LITERAL; Toy_Literal parentLiteral = TOY_TO_NULL_LITERAL;
if (parent != NULL) { if (parent != NULL) {
@@ -316,8 +316,8 @@ static int nativeGetChildNodeCount(Toy_Interpreter* interpreter, Toy_LiteralArra
} }
//get the count //get the count
Box_EngineNode* node = TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = TOY_AS_OPAQUE(nodeLiteral);
int childCount = Box_getChildCountEngineNode(node); int childCount = Box_getChildCountNode(node);
Toy_Literal childCountLiteral = TOY_TO_INTEGER_LITERAL(childCount); Toy_Literal childCountLiteral = TOY_TO_INTEGER_LITERAL(childCount);
Toy_pushLiteralArray(&interpreter->stack, childCountLiteral); Toy_pushLiteralArray(&interpreter->stack, childCountLiteral);
@@ -368,14 +368,14 @@ static int nativeLoadNodeTexture(Toy_Interpreter* interpreter, Toy_LiteralArray*
Toy_freeLiteral(drivePathLiteral); //not needed anymore Toy_freeLiteral(drivePathLiteral); //not needed anymore
//actually load TODO: number the opaques, and check the tag //actually load TODO: number the opaques, and check the tag
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
if (node->texture != NULL) { if (node->texture != NULL) {
Box_freeTextureEngineNode(node); Box_freeTextureNode(node);
} }
if (Box_loadTextureEngineNode(node, Toy_toCString(TOY_AS_STRING(filePathLiteral))) != 0) { if (Box_loadTextureNode(node, Toy_toCString(TOY_AS_STRING(filePathLiteral))) != 0) {
interpreter->errorOutput("Failed to load the texture into the EngineNode\n"); interpreter->errorOutput("Failed to load the texture into the Box_Node\n");
Toy_freeLiteral(filePathLiteral); Toy_freeLiteral(filePathLiteral);
Toy_freeLiteral(nodeLiteral); Toy_freeLiteral(nodeLiteral);
return -1; return -1;
@@ -410,10 +410,10 @@ static int nativeFreeNodeTexture(Toy_Interpreter* interpreter, Toy_LiteralArray*
} }
//actually load TODO: number the opaques, and check the numbers //actually load TODO: number the opaques, and check the numbers
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
if (node->texture != NULL) { if (node->texture != NULL) {
Box_freeTextureEngineNode(node); Box_freeTextureNode(node);
} }
//cleanup //cleanup
@@ -472,10 +472,10 @@ static int nativeSetNodeRect(Toy_Interpreter* interpreter, Toy_LiteralArray* arg
} }
//actually set //actually set
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
SDL_Rect r = {TOY_AS_INTEGER(x), TOY_AS_INTEGER(y), TOY_AS_INTEGER(w), TOY_AS_INTEGER(h)}; SDL_Rect r = {TOY_AS_INTEGER(x), TOY_AS_INTEGER(y), TOY_AS_INTEGER(w), TOY_AS_INTEGER(h)};
Box_setRectEngineNode(node, r); Box_setRectNode(node, r);
//cleanup //cleanup
Toy_freeLiteral(nodeLiteral); Toy_freeLiteral(nodeLiteral);
@@ -509,7 +509,7 @@ static int nativeGetNodeRectX(Toy_Interpreter* interpreter, Toy_LiteralArray* ar
} }
//actually get //actually get
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
Toy_Literal resultLiteral = TOY_TO_INTEGER_LITERAL(node->rect.x); Toy_Literal resultLiteral = TOY_TO_INTEGER_LITERAL(node->rect.x);
Toy_pushLiteralArray(&interpreter->stack, resultLiteral); Toy_pushLiteralArray(&interpreter->stack, resultLiteral);
@@ -542,7 +542,7 @@ static int nativeGetNodeRectY(Toy_Interpreter* interpreter, Toy_LiteralArray* ar
} }
//actually get //actually get
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
Toy_Literal resultLiteral = TOY_TO_INTEGER_LITERAL(node->rect.y); Toy_Literal resultLiteral = TOY_TO_INTEGER_LITERAL(node->rect.y);
Toy_pushLiteralArray(&interpreter->stack, resultLiteral); Toy_pushLiteralArray(&interpreter->stack, resultLiteral);
@@ -575,7 +575,7 @@ static int nativeGetNodeRectW(Toy_Interpreter* interpreter, Toy_LiteralArray* ar
} }
//actually get //actually get
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
Toy_Literal resultLiteral = TOY_TO_INTEGER_LITERAL(node->rect.w); Toy_Literal resultLiteral = TOY_TO_INTEGER_LITERAL(node->rect.w);
Toy_pushLiteralArray(&interpreter->stack, resultLiteral); Toy_pushLiteralArray(&interpreter->stack, resultLiteral);
@@ -608,7 +608,7 @@ static int nativeGetNodeRectH(Toy_Interpreter* interpreter, Toy_LiteralArray* ar
} }
//actually get //actually get
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
Toy_Literal resultLiteral = TOY_TO_INTEGER_LITERAL(node->rect.h); Toy_Literal resultLiteral = TOY_TO_INTEGER_LITERAL(node->rect.h);
Toy_pushLiteralArray(&interpreter->stack, resultLiteral); Toy_pushLiteralArray(&interpreter->stack, resultLiteral);
@@ -648,9 +648,9 @@ static int nativeSetNodeFrames(Toy_Interpreter* interpreter, Toy_LiteralArray* a
} }
//actually set //actually set
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
Box_setFramesEngineNode(node, TOY_AS_INTEGER(framesLiteral)); Box_setFramesNode(node, TOY_AS_INTEGER(framesLiteral));
//cleanup //cleanup
Toy_freeLiteral(nodeLiteral); Toy_freeLiteral(nodeLiteral);
@@ -681,7 +681,7 @@ static int nativeGetNodeFrames(Toy_Interpreter* interpreter, Toy_LiteralArray* a
} }
//actually get //actually get
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
Toy_Literal framesLiteral = TOY_TO_INTEGER_LITERAL(node->frames); Toy_Literal framesLiteral = TOY_TO_INTEGER_LITERAL(node->frames);
Toy_pushLiteralArray(&interpreter->stack, framesLiteral); Toy_pushLiteralArray(&interpreter->stack, framesLiteral);
@@ -723,9 +723,9 @@ static int nativeSetCurrentNodeFrame(Toy_Interpreter* interpreter, Toy_LiteralAr
} }
//actually set //actually set
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
Box_setCurrentFrameEngineNode(node, TOY_AS_INTEGER(currentFrameLiteral)); Box_setCurrentFrameNode(node, TOY_AS_INTEGER(currentFrameLiteral));
//cleanup //cleanup
Toy_freeLiteral(nodeLiteral); Toy_freeLiteral(nodeLiteral);
@@ -756,7 +756,7 @@ static int nativeGetCurrentNodeFrame(Toy_Interpreter* interpreter, Toy_LiteralAr
} }
//actually get //actually get
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
Toy_Literal currentFrameLiteral = TOY_TO_INTEGER_LITERAL(node->currentFrame); Toy_Literal currentFrameLiteral = TOY_TO_INTEGER_LITERAL(node->currentFrame);
Toy_pushLiteralArray(&interpreter->stack, currentFrameLiteral); Toy_pushLiteralArray(&interpreter->stack, currentFrameLiteral);
@@ -790,7 +790,7 @@ static int nativeIncrementCurrentNodeFrame(Toy_Interpreter* interpreter, Toy_Lit
} }
//actually get //actually get
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
Box_incrementCurrentFrame(node); Box_incrementCurrentFrame(node);
@@ -854,7 +854,7 @@ static int nativeDrawNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume
} }
//actually render //actually render
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
SDL_Rect r = {TOY_AS_INTEGER(x), TOY_AS_INTEGER(y), 0, 0}; SDL_Rect r = {TOY_AS_INTEGER(x), TOY_AS_INTEGER(y), 0, 0};
if (TOY_IS_INTEGER(w) && TOY_IS_INTEGER(h)) { if (TOY_IS_INTEGER(w) && TOY_IS_INTEGER(h)) {
@@ -866,7 +866,7 @@ static int nativeDrawNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume
r.h = node->rect.h; r.h = node->rect.h;
} }
Box_drawEngineNode(node, r); Box_drawNode(node, r);
//cleanup //cleanup
Toy_freeLiteral(nodeLiteral); Toy_freeLiteral(nodeLiteral);
@@ -992,8 +992,8 @@ static int nativeSetNodeText(Toy_Interpreter* interpreter, Toy_LiteralArray* arg
SDL_Color color = (SDL_Color){ .r = TOY_AS_INTEGER(rLiteral), .g = TOY_AS_INTEGER(gLiteral), .b = TOY_AS_INTEGER(bLiteral), .a = TOY_AS_INTEGER(aLiteral) }; SDL_Color color = (SDL_Color){ .r = TOY_AS_INTEGER(rLiteral), .g = TOY_AS_INTEGER(gLiteral), .b = TOY_AS_INTEGER(bLiteral), .a = TOY_AS_INTEGER(aLiteral) };
//actually set //actually set
Box_EngineNode* node = (Box_EngineNode*)TOY_AS_OPAQUE(nodeLiteral); Box_Node* node = (Box_Node*)TOY_AS_OPAQUE(nodeLiteral);
Box_setTextEngineNode(node, font, Toy_toCString(TOY_AS_STRING(textLiteral)), color); Box_setTextNode(node, font, Toy_toCString(TOY_AS_STRING(textLiteral)), color);
//cleanup //cleanup
TTF_CloseFont(font); TTF_CloseFont(font);
@@ -1071,7 +1071,7 @@ static int nativeCallNodeFn(Toy_Interpreter* interpreter, Toy_LiteralArray* argu
Toy_Literal fnNameIdentifier = TOY_TO_IDENTIFIER_LITERAL(Toy_copyRefString(TOY_AS_STRING(fnName))); Toy_Literal fnNameIdentifier = TOY_TO_IDENTIFIER_LITERAL(Toy_copyRefString(TOY_AS_STRING(fnName)));
//call the function //call the function
Toy_Literal result = Box_callEngineNodeLiteral(TOY_AS_OPAQUE(nodeLiteral), interpreter, fnNameIdentifier, &extraArgs); Toy_Literal result = Box_callNodeLiteral(TOY_AS_OPAQUE(nodeLiteral), interpreter, fnNameIdentifier, &extraArgs);
Toy_pushLiteralArray(&interpreter->stack, result); Toy_pushLiteralArray(&interpreter->stack, result);