Expanded keyboard API, added repl tools to make process

This commit is contained in:
2026-05-27 12:25:32 +10:00
parent 1c1473339f
commit 82d43ae813
8 changed files with 90 additions and 44 deletions
+15 -3
View File
@@ -116,13 +116,25 @@ KeyboardMap keyboardMap[] = {
{0, NULL},
};
KeyboardData keyboardData = { //NOTE: it is just a dummy struct right now so the API looks nice
KeyboardData keyboardData = {
.type = OPAQUE_KEYBOARD,
.callback = &IsKeyDown,
};
KeyboardData keyPressedData = {
.type = OPAQUE_KEY_PRESSED,
.callback = &IsKeyPressed,
};
KeyboardData keyReleasedData = {
.type = OPAQUE_KEY_RELEASED,
.callback = &IsKeyReleased,
};
Toy_Value handleKeyboardAttributes(Toy_VM* vm, Toy_Value compound, Toy_Value attribute) {
(void)vm;
(void)compound;
KeyboardData* kd = (KeyboardData*)TOY_VALUE_AS_OPAQUE(compound);
Toy_String* string = TOY_VALUE_AS_STRING(attribute);
const char* cstr = string->leaf.data;
@@ -130,7 +142,7 @@ Toy_Value handleKeyboardAttributes(Toy_VM* vm, Toy_Value compound, Toy_Value att
//find the mapped value, if available
for (KeyboardMap* ptr = keyboardMap; ptr->cstr != NULL; ptr++) {
if (strlen(ptr->cstr) == strlen(cstr) && strncmp(cstr, ptr->cstr, strlen(ptr->cstr)) == 0) {
bool result = IsKeyPressed(ptr->raykey);
bool result = kd->callback(ptr->raykey);
return TOY_VALUE_FROM_BOOLEAN(result);
}
}
+6
View File
@@ -4,6 +4,9 @@
#include "toy_vm.h"
#include "raylib.h"
//fn pointers
typedef bool (*raykey_callback)(int);
//wraps raylib's 'KeyboardKey' enum to a c-string
typedef struct KeyboardMap {
int raykey;
@@ -15,8 +18,11 @@ extern KeyboardMap keyboardMap[];
//keyboard opaque
typedef struct KeyboardData {
OpaqueType type;
raykey_callback callback;
} KeyboardData;
extern KeyboardData keyboardData;
extern KeyboardData keyPressedData;
extern KeyboardData keyReleasedData;
Toy_Value handleKeyboardAttributes(Toy_VM* vm, Toy_Value compound, Toy_Value attribute);
+24 -4
View File
@@ -11,11 +11,14 @@
#include "keyboard.h"
#include "actor.h"
#include "standard_library.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include "bytecode_inspector.h"
// #include "bucket_inspector.h"
//utils
unsigned char* readFile(char* path, int* size) {
@@ -160,6 +163,8 @@ Toy_Value dispatchOpaqueAttributes(Toy_VM* vm, Toy_Value compound, Toy_Value att
switch(*type) {
case OPAQUE_KEYBOARD:
case OPAQUE_KEY_PRESSED:
case OPAQUE_KEY_RELEASED:
return handleKeyboardAttributes(vm, compound, attribute);
case OPAQUE_ACTOR:
@@ -197,10 +202,22 @@ void initGameAPI(Toy_VM* vm) {
Toy_freeString(key);
}
//declare keyboard opaque
Toy_String* keyboardString = Toy_toString(&vm->memoryBucket, "Keyboard");
Toy_declareScope(vm->scope, keyboardString, TOY_VALUE_OPAQUE, TOY_OPAQUE_FROM_POINTER(&keyboardData), true);
Toy_freeString(keyboardString);
//declare input opaques
{
Toy_String* name = Toy_toString(&vm->memoryBucket, "Keyboard");
Toy_declareScope(vm->scope, name, TOY_VALUE_OPAQUE, TOY_OPAQUE_FROM_POINTER(&keyboardData), true);
Toy_freeString(name);
}
{
Toy_String* name = Toy_toString(&vm->memoryBucket, "KeyPressed");
Toy_declareScope(vm->scope, name, TOY_VALUE_OPAQUE, TOY_OPAQUE_FROM_POINTER(&keyPressedData), true);
Toy_freeString(name);
}
{
Toy_String* name = Toy_toString(&vm->memoryBucket, "KeyReleased");
Toy_declareScope(vm->scope, name, TOY_VALUE_OPAQUE, TOY_OPAQUE_FROM_POINTER(&keyReleasedData), true);
Toy_freeString(name);
}
}
//main file
@@ -221,6 +238,7 @@ int main() {
Toy_initVM(&vm);
Toy_bindVM(&vm, entryCode, NULL);
initStandardLibrary(&vm);
initGameAPI(&vm);
initActorAPI(&vm);
Toy_setOpaqueAttributeHandler(dispatchOpaqueAttributes);
@@ -271,6 +289,8 @@ int main() {
freeActorAPI(&vm);
// inspect_bucket(&vm.memoryBucket);
Toy_freeVM(&vm);
free(entryCode);
+2 -4
View File
@@ -3,9 +3,7 @@
//always first member of any opaques
typedef enum OpaqueType {
OPAQUE_KEYBOARD,
OPAQUE_KEY_PRESSED,
OPAQUE_KEY_RELEASED,
OPAQUE_ACTOR,
} OpaqueType;
typedef struct DummyOpaque {
OpaqueType type;
} DummyOpaque;