DirectX and Sprites
So this is what I am thinking, when you are creating a level containing multiple enemies if you load the sprites for each enemy you are going to eat up a lot of memory and kill framerate. So the best way to implement this using directX would be to create a container for your enemies implementing just individual coordinates, vector information, and probably transformation matrix information and then just use pointers to statically loaded sprites. You would then draw those sprites based on the information gained from your container. This way you would only have a sprite loaded for each type of enemy vice each enemy. Am I on the right track here?
That's one way of doing it.
If I were you, I'd create a class that has the loaded sprites, and then a vector of the enemy info.
This way you can easily control the loading of the sprites and creation of the surface that you use, and it will make your enemy info easier to use to draw (less confusion with surfaces).
That's just my idea, but it sounds (to me at least) that you're on the right track :).
Of course, we're going to get a guru in here who'll totally prove me wrong about everything :p
In a matter of speaking yes you are on the right track.
That is essentially how I've done it except I'm stuck on the timing information. I don't really want the timing relative to the animation class, but I also don't want the timer so sep from the class that you must call update for the timer, and update for the animation. So here is what I've come up with.
This is one frame of animation and has two members. Time and texture.
This is one sequence of animation. It is composed of multiple CAnimFrame's. It's update function controls the animation and returns the correct TEXTURE to the calling function based on the timeDelta passed to it. So this class stores a lot of things.
- Frame accumulation times for each user of this class
- Frame position information for each user of this class
- Vector of CAnimFrames each having their own frame times. This class checks the time in CAnimFrames and if the accumulated time for this user indicates a frame switch is needed, it increments frame position by 1 frame and returns that texture to the caller.
This is simply a vector of CAnimSeqs. It's very simple to use and [hopefully] if I've coded it right only one instance of this class exists. To add a CAnimSeq to the container you simply call Add() and it returns an ID number. To update all animations you simply call Update() and provide the frame time. It in turn calls all Updates for all CAnimSeq in the vector.
The render function then simply must retrieve the correct texture from CAnimSeqContainer and/or the class doing the rendering must have the ID of the animation sequence that the class us using. Once it knows this it simply calls GetTexture(ID) and the correct texture is returned. After that it can correctly render the sprite.
There are a lot of issues I'm not particularly happy about. There are a lot of calls in this structure which translates to a lot of function call overhead. There is a lot of pointer swapping/switching/copying in this structure which also degrades performance. But so far it's the best I could come up with.
Maybe this will help you in your design. It's quite complex when you break it all down and try to implement it as efficiently as possible.
I might be misunderstanding something with DirectX, but where do you store the DirectX sprite object with this model?
In the animation frame class.
OK I must be confused because I have been using the texture object and the sprite object (LPD3DXSPRITE lpSprite;).
I realize when I call draw() I pass only the texture but I call draw from the sprite object.
Oh wait a minute, you only need to create 1 sprite object to obtain the methods to draw the texture. Is that the correct idea? I must have misunderstood the example I had (it only used 1 picture).
Nevermind, I got it. The LPD3DXSPRITE object simply provides functions for drawing and transforming sprites defined by the IDirect3DTexture9 object.