Archived

This topic is now archived and is closed to further replies.

Sprite Handling Class Texture management

This topic is 5013 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all! I've been tinkering around with DX9 sprites and have a class called SpriteObject that I use to simplify using them. This class contains the sprite, screen coord vectors, movement velocities, etc. Here's the class definition from SpriteObject.h:
class SpriteObject
{
public:
	LPD3DXSPRITE Sprite; // The D3DXSprite itself	
	PDIRECT3DTEXTURE9 Texture1; // The primary texture for the sprite
	PDIRECT3DTEXTURE9 Texture2; // The second texture for the sprite
	PDIRECT3DTEXTURE9 Texture3; // The third texture for the sprite
	PDIRECT3DTEXTURE9 Texture4; // The fourth texture for the sprite
	PDIRECT3DTEXTURE9 Texture5; // The fifth texture for the sprite
	PDIRECT3DTEXTURE9 Texture6; // The sixth texture for the sprite
	PDIRECT3DTEXTURE9 Texture7; // The seventh texture for the sprite
	PDIRECT3DTEXTURE9 Texture8; // The eighth texture for the sprite
	PDIRECT3DTEXTURE9 Texture9; // The ninth texture for the sprite
	PDIRECT3DTEXTURE9 Texture10; // The tenth texture for the sprite
	D3DXVECTOR2 ScreenVector; // The vector for the sprite's screen position
	double hVelocity; // The horizontal velocity of the sprite
	double vVelocity; // The vertical velocity of the sprite
	int textureCount; // The number of textures assigned to this sprite
	int width; // The width of the sprite in pixels
	int height; // The height of the sprite in pixels


	SpriteObject(void);
	virtual ~SpriteObject(void);
	void Initialize(LPDIRECT3DDEVICE9 Device);
	int AddTextureFromFile(LPDIRECT3DDEVICE9 Device, LPCTSTR Filename);
	int AddTextureFromResource(LPDIRECT3DDEVICE9 Device, int ResourceID);
	void Draw(int TextureIndex);
};
  
It's pretty self-explanitory. The Draw method just calls the ID3DXSprite->Draw() function using a default set of parameters. If you use the SpriteObject.Draw() method, you can change the texture, but that's it. Otherwise, you can access the SpriteObject's Sprite member and use it's Draw() method to be able to use whatever params you want. But right now I'm looking for opinions and ideas on how to manage the sprite's textures... What I'm wanting to do is change this so that the SpriteObject has "states". So, if it's a cannonball, for example, it could have an Idle state, an InFlight state and an Exploding state. The state would determine what textures to use and so on. So, if it's in the Exploding state, it would use the textures with the explosion animation. Also, I want to add the ability for it to manage it's textures automatically. So my main program code can set it's state to Exploding, for example, and it would begin with the first texture in the Exploding animation series and then it will change the texture until it gets to the end of the animation sequence. Then, depending on the definition of the state it will either switch to a new state (Idle, for instance) or it would loop. The first issue here is this: I currently have it set up to accept up to 10 textures. What I want to do is change it so that it can have however many states you want to create for it in the code. Further, each state will have it's own series of textures. The number of states and the number of textures will be dynamic. I think I need to start by coming up with a way to dynamically store textures. I was thinking of a linked list or perhaps an array. The array could be created when the SpriteObject.Initialize() method is called. This would require knowing how many textures I'll be using at the time that it's initialized, but that's ok. Then, when I add states, I could change it so that there's an array of states and create a state class which contains an array of textures.... but that's for later... Anyway, it comes down to this: What would you guys suggest as the way to store these textures? I don't mind having to tell it how many textures there will be when the object is created, but I'd like for it to be able to support however many are needed rather than having a set number of textures available. I'm not sure that I'm being very clear here... but I've rambled enough... LOL! Any input is appreciated! Thanks, Rattlehead [edited by - Rattlehead on March 23, 2004 5:34:41 AM]

Share this post


Link to post
Share on other sites
Hi
I think you should not store IDirect3DTexture9 pointers in your class. Let''s say you want to change just one texture, then all the references to that texture in your sprites will need to be changed ... this implies keeping a references array for each texture, and its definetely bad.
I suggest you implement a texture manager which loads the textures from a resource file, and store only texture identifiers, but not the texture themselves, in your sprite.

typedef unsigned int TEXID;

class TextureManager
{
std::map mapIdTex;
void Initialize(LPDIRECT3DDEVICE9 pDevice) {
//load the texture from a file
// and assign it an ID
std::string file = "getTheFileName()"
TEXID id = "getTheId()"
mapIdTex[id] = D3DXCreateTextureFromFile(..., file, ...);
....
}
void SetTexture(TEXID tex, LPDIRECT3DDEVICE9 pDevice) {
pDevice->SetTexture(0, mapIdTex[id]);
}
};

What remains undefined is how you associate your texture files with your texture ids. You can decide to hardcode if you dont have too many textures:
#define TEX_BULLET 0
#define TEX_EXPLOSION 1
#define TEX_SMOKE 2
...
and then load the file directly in TextureManager::Initialize()

Now you only need to create a TextureManager instance, call Initialize() when a new device is created. This instance should be available to each object using TextureManager::SetTexture().

and then you could store the textures need by your sprite in such a way:

class SpriteObject
{
enum States {
IDLE,
INFLIGHT,
EXPLODING
};
...
States state;
std::vector textures;
TEXID currentTexture;
void Render(LPDIRECT3DDEVICE9 pDevice) {
textureManager->SetTexture(currentTexture, pDevice);
pDevice->DrawPrimitive(...);
}
void Update(float elapsedTime) {
timer += elapsedTime;
switch (state)
{
....
case INFLIGHT:
currentTexture = TEX_ID_OF_THE_TEXTURE_YOU_NEED;
break;
}
....
}
...
};


The vector is a dynamic array that will contain all the textures used by the sprite. This array is needed only if you want some kind of automated texture selection process.

Err, I don''t know if that was useful or clear, but it might help you.

Tibo

Share this post


Link to post
Share on other sites
Also, you don''t need a D3DXSprite object for each sprite, just one per program.

---------------------------------------------------------
There are 10 kinds of people: those who know binary and those who don''t

Share this post


Link to post
Share on other sites
quote:
Original post by CutterSlade
Also, you don''t need a D3DXSprite object for each sprite, just one per program.



Eh.... How do I do that? I mean, how do I use one D3DXSprite for all of my sprites?

Rattlehead

Share this post


Link to post
Share on other sites
Ok, I think I see what you''re saying... I store the texture and position and so on of each game "actor" seperately. Things such as an enemy or cannonball or whatever. But when I draw them, they all go through the same D3DXSprite object... Right?

Rattlehead

Share this post


Link to post
Share on other sites
It seems so logical when I think about it now, but the name D3DXSprite misled me. Seeing how it is now, what I was doing is a lot like creating a new device for every frame I drew... LOL!

Thanks for the heads up!

Rattlehead

Share this post


Link to post
Share on other sites