DirectX and Sprites

This is a discussion on DirectX and Sprites within the Game Programming forums, part of the General Programming Boards category; So this is what I am thinking, when you are creating a level containing multiple enemies if you load the ...

  1. #1
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540

    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?

  2. #2
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    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

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.

    CAnimFrame
    This is one frame of animation and has two members. Time and texture.

    CAnimSeq
    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.

    CAnimSeqContainer
    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.

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    I might be misunderstanding something with DirectX, but where do you store the DirectX sprite object with this model?

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    In the animation frame class.

    IDirect3DTexture9 *Texture;

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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).

  7. #7
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Nevermind, I got it. The LPD3DXSPRITE object simply provides functions for drawing and transforming sprites defined by the IDirect3DTexture9 object.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direct3D10 Sprites Alpha Blending
    By DarkMasterBosel in forum Game Programming
    Replies: 0
    Last Post: 01-17-2009, 06:17 AM
  2. D3DXMatrixRotationY and 2D sprites
    By andyhunter in forum Game Programming
    Replies: 3
    Last Post: 12-22-2004, 01:41 PM
  3. tiles/textures and sprites
    By X PaYnE X in forum Game Programming
    Replies: 12
    Last Post: 05-01-2004, 05:23 PM
  4. DirectX engine nearing completion
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 01-12-2004, 04:07 PM
  5. HOWTO: rotate sprites
    By Carlos in forum Game Programming
    Replies: 11
    Last Post: 01-28-2003, 04:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21