Small fixes to prevent possible issues down the road

Thanks @objc
This commit is contained in:
2026-05-02 09:16:37 +10:00
parent 96f7942049
commit 9d35e4862e
2 changed files with 28 additions and 4 deletions
+9 -4
View File
@@ -127,11 +127,14 @@ int main() {
//initialize the monster object pool and run the setup function //initialize the monster object pool and run the setup function
initMonsterObjectPool(&vm); initMonsterObjectPool(&vm);
//run the onStep function (or die if it's undefined) //setup
Toy_bindVM(&vm, invokeOnReady, NULL); Toy_bindVM(&vm, invokeOnReady, NULL);
Toy_runVM(&vm); Toy_runVM(&vm);
Toy_resetVM(&vm, true, false); Toy_resetVM(&vm, true, false);
//onStep is called each frame
Toy_bindVM(&vm, invokeOnStep, NULL);
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
//input //input
if (IsKeyDown(KEY_UP)) player.position.y -= 5.0f; if (IsKeyDown(KEY_UP)) player.position.y -= 5.0f;
@@ -139,10 +142,8 @@ int main() {
if (IsKeyDown(KEY_LEFT)) player.position.x -= 5.0f; if (IsKeyDown(KEY_LEFT)) player.position.x -= 5.0f;
if (IsKeyDown(KEY_RIGHT)) player.position.x += 5.0f; if (IsKeyDown(KEY_RIGHT)) player.position.x += 5.0f;
//run the onStep function (or die if it's undefined) //run the onStep function
Toy_bindVM(&vm, invokeOnStep, NULL);
Toy_runVM(&vm); Toy_runVM(&vm);
Toy_resetVM(&vm, true, false);
//drawing //drawing
BeginDrawing(); BeginDrawing();
@@ -157,6 +158,10 @@ int main() {
EndDrawing(); EndDrawing();
} }
//clear onStep
Toy_resetVM(&vm, true, false);
//cleanup
Toy_bindVM(&vm, invokeOnFinished, NULL); Toy_bindVM(&vm, invokeOnFinished, NULL);
Toy_runVM(&vm); Toy_runVM(&vm);
Toy_resetVM(&vm, true, false); Toy_resetVM(&vm, true, false);
+19
View File
@@ -31,6 +31,12 @@ static Toy_Array* monsterArray = NULL;
static void loadMonsterSprite(Toy_VM* vm) { static void loadMonsterSprite(Toy_VM* vm) {
//key, file, width, height -> null //key, file, width, height -> null
//check for initialization
if (spriteTable == NULL || monsterArray == NULL) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for monster system hasn't been initialized" TOY_CC_RESET "\n");
return;
}
//check parameter count //check parameter count
if (vm->stack->count < 4) { if (vm->stack->count < 4) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Not enough parameters found in 'loadMonsterSprite'" TOY_CC_RESET "\n"); fprintf(stderr, TOY_CC_ERROR "ERROR: Not enough parameters found in 'loadMonsterSprite'" TOY_CC_RESET "\n");
@@ -80,6 +86,12 @@ static void loadMonsterSprite(Toy_VM* vm) {
static void spawnMonsterAt(Toy_VM* vm) { static void spawnMonsterAt(Toy_VM* vm) {
//sprite, x, y -> void //sprite, x, y -> void
//check for initialization
if (spriteTable == NULL || monsterArray == NULL) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for monster system hasn't been initialized" TOY_CC_RESET "\n");
return;
}
//check parameter count //check parameter count
if (vm->stack->count < 3) { if (vm->stack->count < 3) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Not enough parameters found in 'spawnMonsterAt'" TOY_CC_RESET "\n"); fprintf(stderr, TOY_CC_ERROR "ERROR: Not enough parameters found in 'spawnMonsterAt'" TOY_CC_RESET "\n");
@@ -180,6 +192,13 @@ void initMonsterObjectPool(Toy_VM* vm) {
void freeMonsterObjectPool(Toy_VM* vm) { void freeMonsterObjectPool(Toy_VM* vm) {
(void)vm; (void)vm;
//free the GL textures
for (unsigned int i = 0; i < spriteTable->capacity; i++) {
if (TOY_VALUE_IS_OPAQUE(spriteTable->data[i].value)) {
UnloadTexture(((MonsterSprite*)TOY_VALUE_AS_OPAQUE(spriteTable->data[i].value))->texture);
}
}
Toy_freeTable(spriteTable); Toy_freeTable(spriteTable);
spriteTable = NULL; spriteTable = NULL;