Wednesday, July 9, 2008

Object Ownership

I've been working on and off for a couple of days to get some basic mechanics down, and there's been quite a lot of progress. I integrated Chipmunk yesterday, and to make a long story short I now have ten or so little pucks bouncing around an XML-defined playpen. I'm still a bit confused (or rather unhappy) about how Chipmunk manages memory. There's no way to collect all the wall segments into a coherent shape, so I have to add them one by one to the level's space. That's no problem, but the space assumes ownership of all shapes, and destroys them.

However, this also goes for all other objects – for instance the space owns all the puck shapes and bodies, while I need to keep them around to mess with them. When I destroy the space, it also destroys all the added bodies and shapes, but (naturally) leaves non-added bodies alone. So, how do I keep track of which bodies and shapes are currently added to the space when they're destroyed? Should a StaticBody object keep track of its own shapes and bodies? Should a MovingBody? Does it make sense for the walls of a level have its 100+ shapes thrown into a space and forgotten about, but also be added to a StaticBody that sets their mass to +inf?

In short, there are a bunch of resource/ownership issues to be worked out, and I fear I might run into double frees everywhere...

No comments: