diff --git a/assets/scripts/init.toy b/assets/scripts/init.toy index d589ddf..7721b08 100644 --- a/assets/scripts/init.toy +++ b/assets/scripts/init.toy @@ -1,6 +1,23 @@ import engine; +import input; + +/* +//input settings, mapping SDL2's virtual keys to event names +mapInputEventToKey("character_up", "w", 0); //event, keysym, keymod +mapInputEventToKey("character_left", "a", 0); //event, keysym, keymod +mapInputEventToKey("character_down", "s", 0); //event, keysym, keymod +mapInputEventToKey("character_right", "d", 0); //event, keysym, keymod + +mapInputEventToSpecial("character_up", "arrow_up"); //event, special +mapInputEventToSpecial("character_left", "arrow_left"); //event, special +mapInputEventToSpecial("character_down", "arrow_down"); //event, special +mapInputEventToSpecial("character_right", "arrow_right"); //event, special + +mapInputEventToKey("character_jump", " ", 0); //event, keysym, keymod +*/ //this function must always be called, or the engine won't run initWindow("Airport Game", 800, 600, false); +//kick off the logic of the scene graph loadRootNode("assets/scripts/root.toy"); diff --git a/assets/scripts/render.toy b/assets/scripts/render.toy index 99a7f96..28d4e96 100644 --- a/assets/scripts/render.toy +++ b/assets/scripts/render.toy @@ -6,8 +6,8 @@ fn onInit(node: opaque) { } fn onStep(node: opaque) { - print node.getNodeTag(); - print node.getNodeParent().getNodeTag(); + //print node.getNodeTag(); + //print node.getNodeParent().getNodeTag(); } fn onFree(node: opaque) { diff --git a/core/engine.c b/core/engine.c index 69ef17b..d85e2e0 100644 --- a/core/engine.c +++ b/core/engine.c @@ -2,6 +2,7 @@ #include "lib_engine.h" #include "lib_render.h" +#include "lib_input.h" #include "lib_standard.h" #include "repl_tools.h" @@ -43,6 +44,7 @@ void initEngine() { initInterpreter(&engine.interpreter); injectNativeHook(&engine.interpreter, "engine", hookEngine); injectNativeHook(&engine.interpreter, "render", hookRender); + injectNativeHook(&engine.interpreter, "input", hookInput); injectNativeHook(&engine.interpreter, "standard", hookStandard); size_t size = 0; diff --git a/core/lib_engine.c b/core/lib_engine.c index 27679ce..da8dcd8 100644 --- a/core/lib_engine.c +++ b/core/lib_engine.c @@ -198,8 +198,6 @@ static int nativeLoadNode(Interpreter* interpreter, LiteralArray* arguments) { initEngineNode(node, &inner, tb, size); - //NOTE: initNode() must be called manually - // return the node Literal nodeLiteral = TO_OPAQUE_LITERAL(node, node->tag); pushLiteralArray(&interpreter->stack, nodeLiteral); diff --git a/core/lib_input.c b/core/lib_input.c new file mode 100644 index 0000000..7e9971b --- /dev/null +++ b/core/lib_input.c @@ -0,0 +1,71 @@ +#include "lib_input.h" + +#include "memory.h" + +//TODO: native input calls + +//call the hook +typedef struct Natives { + char* name; + NativeFn fn; +} Natives; + +int hookInput(Interpreter* interpreter, Literal identifier, Literal alias) { + //build the natives list + Natives natives[] = { + // {"mapInputEventToKey", nativeMapInputEventToKey}, + // {"mapInputEventToMouse", nativeMapInputEventToMouse}, + // {"mapInputEventToSpecial", nativeMapInputEventToSpecial}, + {NULL, NULL} + }; + + //store the library in an aliased dictionary + if (!IS_NULL(alias)) { + //make sure the name isn't taken + if (isDelcaredScopeVariable(interpreter->scope, alias)) { + interpreter->errorOutput("Can't override an existing variable\n"); + freeLiteral(alias); + return false; + } + + //create the dictionary to load up with functions + LiteralDictionary* dictionary = ALLOCATE(LiteralDictionary, 1); + initLiteralDictionary(dictionary); + + //load the dict with functions + for (int i = 0; natives[i].name; i++) { + Literal name = TO_STRING_LITERAL(copyString(natives[i].name, strlen(natives[i].name)), strlen(natives[i].name)); + Literal func = TO_FUNCTION_LITERAL((void*)natives[i].fn, 0); + func.type = LITERAL_FUNCTION_NATIVE; + + setLiteralDictionary(dictionary, name, func); + + freeLiteral(name); + freeLiteral(func); + } + + //build the type + Literal type = TO_TYPE_LITERAL(LITERAL_DICTIONARY, true); + Literal strType = TO_TYPE_LITERAL(LITERAL_STRING, true); + Literal fnType = TO_TYPE_LITERAL(LITERAL_FUNCTION_NATIVE, true); + TYPE_PUSH_SUBTYPE(&type, strType); + TYPE_PUSH_SUBTYPE(&type, fnType); + + //set scope + Literal dict = TO_DICTIONARY_LITERAL(dictionary); + declareScopeVariable(interpreter->scope, alias, type); + setScopeVariable(interpreter->scope, alias, dict, false); + + //cleanup + freeLiteral(dict); + freeLiteral(type); + return 0; + } + + //default + for (int i = 0; natives[i].name; i++) { + injectNativeFn(interpreter, natives[i].name, natives[i].fn); + } + + return 0; +} diff --git a/core/lib_input.h b/core/lib_input.h new file mode 100644 index 0000000..1b92478 --- /dev/null +++ b/core/lib_input.h @@ -0,0 +1,6 @@ +#pragma once + +#include "interpreter.h" + +int hookInput(Interpreter* interpreter, Literal identifier, Literal alias); + diff --git a/core/lib_render.c b/core/lib_render.c index 7327d39..2d8689b 100644 --- a/core/lib_render.c +++ b/core/lib_render.c @@ -58,8 +58,6 @@ static int nativeLoadRenderNode(Interpreter* interpreter, LiteralArray* argument initRenderNode(node, &inner, tb, size); - //NOTE: initNode() must be called manually - // return the node Literal nodeLiteral = TO_OPAQUE_LITERAL(node, node->tag); pushLiteralArray(&interpreter->stack, nodeLiteral); @@ -196,10 +194,10 @@ static int nativeSetRectRenderNode(Interpreter* interpreter, LiteralArray* argum if (!IS_OPAQUE(nodeLiteral) || !IS_INTEGER(x) || !IS_INTEGER(y) || !IS_INTEGER(w) || !IS_INTEGER(h)) { interpreter->errorOutput("Incorrect argument type passed to setRectRenderNode\n"); freeLiteral(nodeLiteral); - freeLiteral(xi); - freeLiteral(yi); - freeLiteral(wi); - freeLiteral(hi); + freeLiteral(x); + freeLiteral(y); + freeLiteral(w); + freeLiteral(h); return -1; } @@ -211,13 +209,15 @@ static int nativeSetRectRenderNode(Interpreter* interpreter, LiteralArray* argum //cleanup freeLiteral(nodeLiteral); - freeLiteral(xi); - freeLiteral(yi); - freeLiteral(wi); - freeLiteral(hi); + freeLiteral(x); + freeLiteral(y); + freeLiteral(w); + freeLiteral(h); return 0; } +//TODO: get x, y, w, h + static int nativeDrawRenderNode(Interpreter* interpreter, LiteralArray* arguments) { if (arguments->count != 3 && arguments->count != 5) { interpreter->errorOutput("Incorrect number of arguments passed to drawRenderNode\n"); @@ -264,10 +264,10 @@ static int nativeDrawRenderNode(Interpreter* interpreter, LiteralArray* argument if (!IS_OPAQUE(nodeLiteral) || !IS_INTEGER(x) || !IS_INTEGER(y) || (!IS_INTEGER(w) && !IS_NULL(w)) || (!IS_INTEGER(h) && !IS_NULL(h))) { interpreter->errorOutput("Incorrect argument type passed to drawRenderNode\n"); freeLiteral(nodeLiteral); - freeLiteral(xi); - freeLiteral(yi); - freeLiteral(wi); - freeLiteral(hi); + freeLiteral(x); + freeLiteral(y); + freeLiteral(w); + freeLiteral(h); return -1; } @@ -288,10 +288,10 @@ static int nativeDrawRenderNode(Interpreter* interpreter, LiteralArray* argument //cleanup freeLiteral(nodeLiteral); - freeLiteral(xi); - freeLiteral(yi); - freeLiteral(wi); - freeLiteral(hi); + freeLiteral(x); + freeLiteral(y); + freeLiteral(w); + freeLiteral(h); return 0; }