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+=
#directories
export GAME_SOURCEDIR=source
export GAME_OUTDIR=out
export GAME_OBJDIR=obj
export VAMP_SOURCEDIR=source
export VAMP_OUTDIR=out
export VAMP_OBJDIR=obj
#Override Toy's build destination
export TOY_SOURCEDIR=Toy/source
export TOY_OUTDIR=../$(GAME_OUTDIR)
export TOY_OBJDIR=$(GAME_OBJDIR)
export TOY_OUTDIR=../$(VAMP_OUTDIR)
export TOY_OBJDIR=$(VAMP_OBJDIR)
#targets
all: $(GAME_OUTDIR) Toy source
all: $(VAMP_OUTDIR) Toy source
.PHONY: source
source:
$(MAKE) -C $(GAME_SOURCEDIR)
$(MAKE) -C $(VAMP_SOURCEDIR)
.PHONY: Toy
Toy:
$(MAKE) -C $(TOY_SOURCEDIR)
#util targets
$(GAME_OUTDIR):
mkdir $(GAME_OUTDIR)
$(VAMP_OUTDIR):
mkdir $(VAMP_OUTDIR)
#util commands
.PHONY: clean
+34 -8
View File
@@ -14,8 +14,9 @@ static Toy_Table* spriteTable = NULL;
static Toy_Array* actorArray = NULL;
//callbacks
static void api_loadSprite(Toy_VM* vm) {
static void api_loadSprite(Toy_VM* vm, Toy_FunctionNative* self) {
//key, file, width, height -> null
(void)self;
if (!IsWindowReady()) {
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);
}
static void api_spawnActorAt(Toy_VM* vm) {
static void api_spawnActorAt(Toy_VM* vm, Toy_FunctionNative* self) {
//sprite, onStep, x, y -> void
(void)self;
//check for initialization
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
ActorData* newActorPtr = NULL;
for (unsigned int i = 0; i < actorArray->count; i++) {
ActorData* mData = (ActorData*)TOY_VALUE_AS_OPAQUE(actorArray->data[i]);
if (!mData->enabled) { //if this actor is dead, steal the slot
newActorPtr = mData;
ActorData* actorData = (ActorData*)TOY_VALUE_AS_OPAQUE(actorArray->data[i]);
if (!actorData->enabled) { //if this actor is dead, steal the slot
//free the dead actor's internals
actorData->sprite = NULL;
if (actorData->onStep != NULL) {
Toy_freeFunction(actorData->onStep);
actorData->onStep = NULL;
}
newActorPtr = actorData;
break;
}
}
@@ -219,6 +228,20 @@ void freeActorAPI(Toy_VM* vm) {
Toy_freeTable(spriteTable);
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);
}
@@ -254,7 +277,6 @@ void processActors(Toy_VM* vm) {
//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);
//run
@@ -381,7 +403,9 @@ ActorData* spawnActorAt(Toy_Bucket** bucketHandle, Toy_Value key, Toy_Function*
}
//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 x = Toy_popStack(&vm->stack);
@@ -394,7 +418,9 @@ static void attr_actorSetX(Toy_VM* vm) {
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 y = Toy_popStack(&vm->stack);
+8 -4
View File
@@ -73,7 +73,9 @@ static Toy_Function* onFrame = NULL;
static Toy_Function* onClose = NULL;
//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 height = Toy_popStack(&vm->stack);
Toy_Value width = Toy_popStack(&vm->stack);
@@ -97,7 +99,9 @@ void initScreen(Toy_VM* vm) {
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 valueOnFrame = Toy_popStack(&vm->stack);
Toy_Value valueOnReady = Toy_popStack(&vm->stack);
@@ -147,8 +151,8 @@ typedef struct CallbackPairs {
} CallbackPairs;
static CallbackPairs callbackPairs[] = {
{"initScreen", initScreen},
{"initLoop", initLoop},
{"initScreen", api_initScreen},
{"initLoop", api_initLoop},
{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
#directories
REPL_ROOTDIR=..
REPL_REPLDIR=.
REPL_SOURCEDIR=$(REPL_ROOTDIR)/$(TOY_SOURCEDIR)
GAME_ROOTDIR=..
GAME_SOURCEDIR=.
GAME_TOYDIR=$(GAME_ROOTDIR)/$(TOY_SOURCEDIR)
REPL_OUTDIR=$(REPL_ROOTDIR)/$(GAME_OUTDIR)
REPL_OBJDIR=$(TOY_OBJDIR)
GAME_OUTDIR=$(GAME_ROOTDIR)/$(VAMP_OUTDIR)
GAME_OBJDIR=$(VAMP_OBJDIR)
#file names
REPL_REPLFILES=$(wildcard $(REPL_REPLDIR)/*.c)
REPL_OBJFILES=$(addprefix $(REPL_OBJDIR)/,$(notdir $(REPL_REPLFILES:.c=.o)))
REPL_TARGETNAME=vampires
GAME_SOURCEFILES=$(wildcard $(GAME_SOURCEDIR)/*.c)
GAME_OBJFILES=$(addprefix $(GAME_OBJDIR)/,$(notdir $(GAME_SOURCEFILES:.c=.o)))
GAME_TARGETNAME=vampires
#file extensions
ifeq ($(OS),Windows_NT)
REPL_TARGETEXT=.exe
GAME_TARGETEXT=.exe
else
REPL_TARGETEXT=.out
GAME_TARGETEXT=.out
endif
#linker fix
LDFLAGS+=-L$(realpath $(REPL_OUTDIR))
LDFLAGS+=-L$(realpath $(GAME_OUTDIR))
#build the object files, compile the test cases, and run
all: build link
#targets for each step
.PHONY: build
build: $(REPL_OBJDIR) $(REPL_OBJFILES)
build: $(GAME_OBJDIR) $(GAME_OBJFILES)
.PHONY: link
link: $(REPL_OUTDIR) $(REPL_OUTDIR)/$(REPL_TARGETNAME)$(REPL_TARGETEXT)
link: $(GAME_OUTDIR) $(GAME_OUTDIR)/$(GAME_TARGETNAME)$(GAME_TARGETEXT)
#util targets
$(REPL_OUTDIR):
mkdir $(REPL_OUTDIR)
$(GAME_OUTDIR):
mkdir $(GAME_OUTDIR)
$(REPL_OBJDIR):
mkdir $(REPL_OBJDIR)
$(GAME_OBJDIR):
mkdir $(GAME_OBJDIR)
#compilation steps
$(REPL_OBJDIR)/%.o: $(REPL_REPLDIR)/%.c
$(CC) -c -o $@ $< $(addprefix -I,$(REPL_REPLDIR)) $(addprefix -I,$(REPL_SOURCEDIR)) $(CFLAGS)
$(GAME_OBJDIR)/%.o: $(GAME_SOURCEDIR)/%.c
$(CC) -c -o $@ $< $(addprefix -I,$(GAME_SOURCEDIR)) $(addprefix -I,$(GAME_TOYDIR)) $(CFLAGS)
$(REPL_OUTDIR)/$(REPL_TARGETNAME)$(REPL_TARGETEXT): $(REPL_OBJFILES)
$(CC) -DTOY_IMPORT $(CFLAGS) -o $@ $(REPL_OBJFILES) $(LDFLAGS) $(LIBS)
$(GAME_OUTDIR)/$(GAME_TARGETNAME)$(GAME_TARGETEXT): $(GAME_OBJFILES)
$(CC) -DTOY_IMPORT $(CFLAGS) -o $@ $(GAME_OBJFILES) $(LDFLAGS) $(LIBS)
ifeq ($(shell uname),Darwin) #dylib fix
otool -L $@
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 $@
endif