3
« on: November 22, 2008, 09:26:10 am »
I've had a few adventures in program design because of this very topic while working on my game. I decided that I would make the engine as if I was only making the engine and then passing it off to another programmer (who also happened to be me) to use it to make a game. Naturally, I built it with a separation between logic and graphics since the second programmer should never have to interface with SFML, and the design of the whole thing relies on the extendability of certain objects.
I decided that an easy way to render things was to create a sprite for each entity each frame, and I found that the approach could create performance problems if you're using a very lot of them. It's about the easiest thing you could set up while keeping a separation between graphics and logic, but it's not the best way of doing it.
I've got an Athlon X2 5200+, and I could have around 2,400 of them being created and destroyed at 60 frames a second while using only a little over half of my CPU power. If you don't have a lot of entities, then, although it's not the most efficient method, it won't create any problems.
The part I didn't like about it, though, was how it made me put a lot of data into my entities, which also made the constructors take around 11 arguments, which can be a bit of a pain to work with.
That didn't fit my desires entirely, though, as it limited me to one sprite per entity, and, as mentioned before, it's not really efficient. What I'm doing now is creating a structure that allows me to control and render a group of sprites as a single entity and allow it to be referenced by a unique name. Load it once before it's used, then just put it in a tree of sorts and search out your desired sprite sets each time an entity is created, returning a pointer to be kept inside the entity it's being associated with.
Searching a tree for an object containing sprites once when an item is created rather than creating a sprite every time an item is rendered is definitely more efficient, and the use of pointers to reference the stored sprites lets you cut down on memory use. It also allows you to reduce the amount of graphical information your entities need to contain, which also makes their constructors simpler.
This is the best solution I have come up with for this problem.