Started on input, tired

This commit is contained in:
2022-10-19 20:42:33 +01:00
parent 951453640f
commit 893430469b
7 changed files with 116 additions and 22 deletions

View File

@@ -1,6 +1,23 @@
import engine; 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 //this function must always be called, or the engine won't run
initWindow("Airport Game", 800, 600, false); initWindow("Airport Game", 800, 600, false);
//kick off the logic of the scene graph
loadRootNode("assets/scripts/root.toy"); loadRootNode("assets/scripts/root.toy");

View File

@@ -6,8 +6,8 @@ fn onInit(node: opaque) {
} }
fn onStep(node: opaque) { fn onStep(node: opaque) {
print node.getNodeTag(); //print node.getNodeTag();
print node.getNodeParent().getNodeTag(); //print node.getNodeParent().getNodeTag();
} }
fn onFree(node: opaque) { fn onFree(node: opaque) {

View File

@@ -2,6 +2,7 @@
#include "lib_engine.h" #include "lib_engine.h"
#include "lib_render.h" #include "lib_render.h"
#include "lib_input.h"
#include "lib_standard.h" #include "lib_standard.h"
#include "repl_tools.h" #include "repl_tools.h"
@@ -43,6 +44,7 @@ void initEngine() {
initInterpreter(&engine.interpreter); initInterpreter(&engine.interpreter);
injectNativeHook(&engine.interpreter, "engine", hookEngine); injectNativeHook(&engine.interpreter, "engine", hookEngine);
injectNativeHook(&engine.interpreter, "render", hookRender); injectNativeHook(&engine.interpreter, "render", hookRender);
injectNativeHook(&engine.interpreter, "input", hookInput);
injectNativeHook(&engine.interpreter, "standard", hookStandard); injectNativeHook(&engine.interpreter, "standard", hookStandard);
size_t size = 0; size_t size = 0;

View File

@@ -198,8 +198,6 @@ static int nativeLoadNode(Interpreter* interpreter, LiteralArray* arguments) {
initEngineNode(node, &inner, tb, size); initEngineNode(node, &inner, tb, size);
//NOTE: initNode() must be called manually
// return the node // return the node
Literal nodeLiteral = TO_OPAQUE_LITERAL(node, node->tag); Literal nodeLiteral = TO_OPAQUE_LITERAL(node, node->tag);
pushLiteralArray(&interpreter->stack, nodeLiteral); pushLiteralArray(&interpreter->stack, nodeLiteral);

71
core/lib_input.c Normal file
View File

@@ -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;
}

6
core/lib_input.h Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
#include "interpreter.h"
int hookInput(Interpreter* interpreter, Literal identifier, Literal alias);

View File

@@ -58,8 +58,6 @@ static int nativeLoadRenderNode(Interpreter* interpreter, LiteralArray* argument
initRenderNode(node, &inner, tb, size); initRenderNode(node, &inner, tb, size);
//NOTE: initNode() must be called manually
// return the node // return the node
Literal nodeLiteral = TO_OPAQUE_LITERAL(node, node->tag); Literal nodeLiteral = TO_OPAQUE_LITERAL(node, node->tag);
pushLiteralArray(&interpreter->stack, nodeLiteral); 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)) { 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"); interpreter->errorOutput("Incorrect argument type passed to setRectRenderNode\n");
freeLiteral(nodeLiteral); freeLiteral(nodeLiteral);
freeLiteral(xi); freeLiteral(x);
freeLiteral(yi); freeLiteral(y);
freeLiteral(wi); freeLiteral(w);
freeLiteral(hi); freeLiteral(h);
return -1; return -1;
} }
@@ -211,13 +209,15 @@ static int nativeSetRectRenderNode(Interpreter* interpreter, LiteralArray* argum
//cleanup //cleanup
freeLiteral(nodeLiteral); freeLiteral(nodeLiteral);
freeLiteral(xi); freeLiteral(x);
freeLiteral(yi); freeLiteral(y);
freeLiteral(wi); freeLiteral(w);
freeLiteral(hi); freeLiteral(h);
return 0; return 0;
} }
//TODO: get x, y, w, h
static int nativeDrawRenderNode(Interpreter* interpreter, LiteralArray* arguments) { static int nativeDrawRenderNode(Interpreter* interpreter, LiteralArray* arguments) {
if (arguments->count != 3 && arguments->count != 5) { if (arguments->count != 3 && arguments->count != 5) {
interpreter->errorOutput("Incorrect number of arguments passed to drawRenderNode\n"); 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))) { 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"); interpreter->errorOutput("Incorrect argument type passed to drawRenderNode\n");
freeLiteral(nodeLiteral); freeLiteral(nodeLiteral);
freeLiteral(xi); freeLiteral(x);
freeLiteral(yi); freeLiteral(y);
freeLiteral(wi); freeLiteral(w);
freeLiteral(hi); freeLiteral(h);
return -1; return -1;
} }
@@ -288,10 +288,10 @@ static int nativeDrawRenderNode(Interpreter* interpreter, LiteralArray* argument
//cleanup //cleanup
freeLiteral(nodeLiteral); freeLiteral(nodeLiteral);
freeLiteral(xi); freeLiteral(x);
freeLiteral(yi); freeLiteral(y);
freeLiteral(wi); freeLiteral(w);
freeLiteral(hi); freeLiteral(h);
return 0; return 0;
} }