Updated Toy and APIs, fixed a leak from Actor 'onStep'

This commit is contained in:
2026-05-20 15:41:40 +10:00
parent eb8115aa7e
commit 465530923e
5 changed files with 74 additions and 44 deletions
+1 -1
Submodule Toy updated: 6901b9a6c9...750ebd1f99
+9 -9
View File
@@ -5,29 +5,29 @@
#LDFLAGS+= #LDFLAGS+=
#directories #directories
export GAME_SOURCEDIR=source export VAMP_SOURCEDIR=source
export GAME_OUTDIR=out export VAMP_OUTDIR=out
export GAME_OBJDIR=obj export VAMP_OBJDIR=obj
#Override Toy's build destination #Override Toy's build destination
export TOY_SOURCEDIR=Toy/source export TOY_SOURCEDIR=Toy/source
export TOY_OUTDIR=../$(GAME_OUTDIR) export TOY_OUTDIR=../$(VAMP_OUTDIR)
export TOY_OBJDIR=$(GAME_OBJDIR) export TOY_OBJDIR=$(VAMP_OBJDIR)
#targets #targets
all: $(GAME_OUTDIR) Toy source all: $(VAMP_OUTDIR) Toy source
.PHONY: source .PHONY: source
source: source:
$(MAKE) -C $(GAME_SOURCEDIR) $(MAKE) -C $(VAMP_SOURCEDIR)
.PHONY: Toy .PHONY: Toy
Toy: Toy:
$(MAKE) -C $(TOY_SOURCEDIR) $(MAKE) -C $(TOY_SOURCEDIR)
#util targets #util targets
$(GAME_OUTDIR): $(VAMP_OUTDIR):
mkdir $(GAME_OUTDIR) mkdir $(VAMP_OUTDIR)
#util commands #util commands
.PHONY: clean .PHONY: clean
+34 -8
View File
@@ -14,8 +14,9 @@ static Toy_Table* spriteTable = NULL;
static Toy_Array* actorArray = NULL; static Toy_Array* actorArray = NULL;
//callbacks //callbacks
static void api_loadSprite(Toy_VM* vm) { static void api_loadSprite(Toy_VM* vm, Toy_FunctionNative* self) {
//key, file, width, height -> null //key, file, width, height -> null
(void)self;
if (!IsWindowReady()) { if (!IsWindowReady()) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't load actor sprites before the window has been initialized" TOY_CC_RESET "\n"); fprintf(stderr, TOY_CC_ERROR "ERROR: Can't load actor sprites before the window has been initialized" TOY_CC_RESET "\n");
@@ -79,8 +80,9 @@ static void api_loadSprite(Toy_VM* vm) {
Toy_freeValue(height); Toy_freeValue(height);
} }
static void api_spawnActorAt(Toy_VM* vm) { static void api_spawnActorAt(Toy_VM* vm, Toy_FunctionNative* self) {
//sprite, onStep, x, y -> void //sprite, onStep, x, y -> void
(void)self;
//check for initialization //check for initialization
if (spriteTable == NULL || actorArray == NULL) { if (spriteTable == NULL || actorArray == NULL) {
@@ -141,9 +143,16 @@ static void api_spawnActorAt(Toy_VM* vm) {
//find an existing spot for the new actor, overwriting a dead one if able //find an existing spot for the new actor, overwriting a dead one if able
ActorData* newActorPtr = NULL; ActorData* newActorPtr = NULL;
for (unsigned int i = 0; i < actorArray->count; i++) { for (unsigned int i = 0; i < actorArray->count; i++) {
ActorData* mData = (ActorData*)TOY_VALUE_AS_OPAQUE(actorArray->data[i]); ActorData* actorData = (ActorData*)TOY_VALUE_AS_OPAQUE(actorArray->data[i]);
if (!mData->enabled) { //if this actor is dead, steal the slot if (!actorData->enabled) { //if this actor is dead, steal the slot
newActorPtr = mData; //free the dead actor's internals
actorData->sprite = NULL;
if (actorData->onStep != NULL) {
Toy_freeFunction(actorData->onStep);
actorData->onStep = NULL;
}
newActorPtr = actorData;
break; break;
} }
} }
@@ -219,6 +228,20 @@ void freeActorAPI(Toy_VM* vm) {
Toy_freeTable(spriteTable); Toy_freeTable(spriteTable);
spriteTable = NULL; spriteTable = NULL;
//unload each actor's members
for (unsigned int i = 0; i < actorArray->count; i++) {
ActorData* actorData = (ActorData*)TOY_VALUE_AS_OPAQUE(actorArray->data[i]);
//free the dead actor's internals
actorData->sprite = NULL;
if (actorData->onStep != NULL) {
Toy_freeFunction(actorData->onStep);
actorData->onStep = NULL;
}
actorData->position = (Vector2){0,0};
actorData->enabled = false;
}
actorArray = Toy_resizeArray(actorArray, 0); actorArray = Toy_resizeArray(actorArray, 0);
} }
@@ -254,7 +277,6 @@ void processActors(Toy_VM* vm) {
//inject this actor as a parameter //inject this actor as a parameter
subVM.scope = Toy_pushScope(&subVM.memoryBucket, subVM.scope);
Toy_declareScope(subVM.scope, name, paramType, Toy_copyValue(&subVM.memoryBucket, actorArray->data[i]), true); Toy_declareScope(subVM.scope, name, paramType, Toy_copyValue(&subVM.memoryBucket, actorArray->data[i]), true);
//run //run
@@ -381,7 +403,9 @@ ActorData* spawnActorAt(Toy_Bucket** bucketHandle, Toy_Value key, Toy_Function*
} }
//opaque handler //opaque handler
static void attr_actorSetX(Toy_VM* vm) { static void attr_actorSetX(Toy_VM* vm, Toy_FunctionNative* self) {
(void)self;
Toy_Value compound = Toy_popStack(&vm->stack); Toy_Value compound = Toy_popStack(&vm->stack);
Toy_Value x = Toy_popStack(&vm->stack); Toy_Value x = Toy_popStack(&vm->stack);
@@ -394,7 +418,9 @@ static void attr_actorSetX(Toy_VM* vm) {
actor->position.x = TOY_VALUE_AS_INTEGER(x); actor->position.x = TOY_VALUE_AS_INTEGER(x);
} }
static void attr_actorSetY(Toy_VM* vm) { static void attr_actorSetY(Toy_VM* vm, Toy_FunctionNative* self) {
(void)self;
Toy_Value compound = Toy_popStack(&vm->stack); Toy_Value compound = Toy_popStack(&vm->stack);
Toy_Value y = Toy_popStack(&vm->stack); Toy_Value y = Toy_popStack(&vm->stack);
+8 -4
View File
@@ -73,7 +73,9 @@ static Toy_Function* onFrame = NULL;
static Toy_Function* onClose = NULL; static Toy_Function* onClose = NULL;
//game API definitions //game API definitions
void initScreen(Toy_VM* vm) { void api_initScreen(Toy_VM* vm, Toy_FunctionNative* self) {
(void)self;
Toy_Value caption = Toy_popStack(&vm->stack); Toy_Value caption = Toy_popStack(&vm->stack);
Toy_Value height = Toy_popStack(&vm->stack); Toy_Value height = Toy_popStack(&vm->stack);
Toy_Value width = Toy_popStack(&vm->stack); Toy_Value width = Toy_popStack(&vm->stack);
@@ -97,7 +99,9 @@ void initScreen(Toy_VM* vm) {
Toy_freeValue(caption); Toy_freeValue(caption);
} }
void initLoop(Toy_VM* vm) { void api_initLoop(Toy_VM* vm, Toy_FunctionNative* self) {
(void)self;
Toy_Value valueOnClose = Toy_popStack(&vm->stack); Toy_Value valueOnClose = Toy_popStack(&vm->stack);
Toy_Value valueOnFrame = Toy_popStack(&vm->stack); Toy_Value valueOnFrame = Toy_popStack(&vm->stack);
Toy_Value valueOnReady = Toy_popStack(&vm->stack); Toy_Value valueOnReady = Toy_popStack(&vm->stack);
@@ -147,8 +151,8 @@ typedef struct CallbackPairs {
} CallbackPairs; } CallbackPairs;
static CallbackPairs callbackPairs[] = { static CallbackPairs callbackPairs[] = {
{"initScreen", initScreen}, {"initScreen", api_initScreen},
{"initLoop", initLoop}, {"initLoop", api_initLoop},
{NULL, NULL}, {NULL, NULL},
}; };
+22 -22
View File
@@ -9,54 +9,54 @@ ifeq ($(shell uname),Darwin) #make sure there's enough space for the dylib fix
endif endif
#directories #directories
REPL_ROOTDIR=.. GAME_ROOTDIR=..
REPL_REPLDIR=. GAME_SOURCEDIR=.
REPL_SOURCEDIR=$(REPL_ROOTDIR)/$(TOY_SOURCEDIR) GAME_TOYDIR=$(GAME_ROOTDIR)/$(TOY_SOURCEDIR)
REPL_OUTDIR=$(REPL_ROOTDIR)/$(GAME_OUTDIR) GAME_OUTDIR=$(GAME_ROOTDIR)/$(VAMP_OUTDIR)
REPL_OBJDIR=$(TOY_OBJDIR) GAME_OBJDIR=$(VAMP_OBJDIR)
#file names #file names
REPL_REPLFILES=$(wildcard $(REPL_REPLDIR)/*.c) GAME_SOURCEFILES=$(wildcard $(GAME_SOURCEDIR)/*.c)
REPL_OBJFILES=$(addprefix $(REPL_OBJDIR)/,$(notdir $(REPL_REPLFILES:.c=.o))) GAME_OBJFILES=$(addprefix $(GAME_OBJDIR)/,$(notdir $(GAME_SOURCEFILES:.c=.o)))
REPL_TARGETNAME=vampires GAME_TARGETNAME=vampires
#file extensions #file extensions
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
REPL_TARGETEXT=.exe GAME_TARGETEXT=.exe
else else
REPL_TARGETEXT=.out GAME_TARGETEXT=.out
endif endif
#linker fix #linker fix
LDFLAGS+=-L$(realpath $(REPL_OUTDIR)) LDFLAGS+=-L$(realpath $(GAME_OUTDIR))
#build the object files, compile the test cases, and run #build the object files, compile the test cases, and run
all: build link all: build link
#targets for each step #targets for each step
.PHONY: build .PHONY: build
build: $(REPL_OBJDIR) $(REPL_OBJFILES) build: $(GAME_OBJDIR) $(GAME_OBJFILES)
.PHONY: link .PHONY: link
link: $(REPL_OUTDIR) $(REPL_OUTDIR)/$(REPL_TARGETNAME)$(REPL_TARGETEXT) link: $(GAME_OUTDIR) $(GAME_OUTDIR)/$(GAME_TARGETNAME)$(GAME_TARGETEXT)
#util targets #util targets
$(REPL_OUTDIR): $(GAME_OUTDIR):
mkdir $(REPL_OUTDIR) mkdir $(GAME_OUTDIR)
$(REPL_OBJDIR): $(GAME_OBJDIR):
mkdir $(REPL_OBJDIR) mkdir $(GAME_OBJDIR)
#compilation steps #compilation steps
$(REPL_OBJDIR)/%.o: $(REPL_REPLDIR)/%.c $(GAME_OBJDIR)/%.o: $(GAME_SOURCEDIR)/%.c
$(CC) -c -o $@ $< $(addprefix -I,$(REPL_REPLDIR)) $(addprefix -I,$(REPL_SOURCEDIR)) $(CFLAGS) $(CC) -c -o $@ $< $(addprefix -I,$(GAME_SOURCEDIR)) $(addprefix -I,$(GAME_TOYDIR)) $(CFLAGS)
$(REPL_OUTDIR)/$(REPL_TARGETNAME)$(REPL_TARGETEXT): $(REPL_OBJFILES) $(GAME_OUTDIR)/$(GAME_TARGETNAME)$(GAME_TARGETEXT): $(GAME_OBJFILES)
$(CC) -DTOY_IMPORT $(CFLAGS) -o $@ $(REPL_OBJFILES) $(LDFLAGS) $(LIBS) $(CC) -DTOY_IMPORT $(CFLAGS) -o $@ $(GAME_OBJFILES) $(LDFLAGS) $(LIBS)
ifeq ($(shell uname),Darwin) #dylib fix ifeq ($(shell uname),Darwin) #dylib fix
otool -L $@ otool -L $@
install_name_tool -add_rpath @executable_path/. $@ install_name_tool -add_rpath @executable_path/. $@
install_name_tool -change $(REPL_OUTDIR)/libToy.dylib @executable_path/libToy.dylib $@ install_name_tool -change $(GAME_OUTDIR)/libToy.dylib @executable_path/libToy.dylib $@
otool -L $@ otool -L $@
endif endif