Non-allocating versions of C API functions

,

Hello,

currently some C API functions allocate memory and require the user to free that memory (e.g. moveWithCollisions).
It would be nice to have a version of these functions that accept a buffer+size from the user and return the amount of items that were filled in the buffer.
Something like this:

// Returns the number of collisions happened
int playdate->sprite->moveWithCollisionsNoAlloc(LCDSprite *sprite, float goalX, float goalY, float *actualX, float *actualY, int *len, SpriteCollisionInfo **outCollisions, int outCollisionsCapacity);

This would allow the developer to have complete control over how the memory is allocated and freed (e.g. by using a scratch buffer/stack allocator/etc) and would avoid having to allocate/deallocate memory through the system allocator after the game initialization.

As a bonus, it would also avoid certain pitfalls that are pretty easy to fall into if you're not experience with the Playdate API.

Cheers!

2 Likes

I think many of us using C don't use the sprite system. But I agree that it would be nice to be able to provide a buffer or a custom allocator to the API to allow more memory control without having to re-write the behavior ourselves.