D3d Texture Wrapper == ARRRGGGGHHH

This is a discussion on D3d Texture Wrapper == ARRRGGGGHHH within the Game Programming forums, part of the General Programming Boards category; This is getting too frustrating for me. I've tried to figure this out myself and I've tried to stop myself ...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Angry D3d Texture Wrapper == ARRRGGGGHHH

    This is getting too frustrating for me. I've tried to figure this out myself and I've tried to stop myself from pasting so much code here but I don't have a choice:

    Code:
    // SPRITE.H
    
    struct SPRITE_INFO
    {
    	SPRITE_INFO();
    	~SPRITE_INFO();
    	
    	SPRITE_INFO( const SPRITE_INFO& temp );
    	SPRITE_INFO& operator = ( const SPRITE_INFO& temp );
    
    	short Texture;
    	D3DXVECTOR3* Position;
    	D3DCOLOR Color;
    
    	void SetColor( WORD r, WORD g, WORD b );
    	void SetPosition( float x, float y, float z );
    };
    
    struct TEXTURE
    {
    	TEXTURE( IDirect3DDevice9* Device, LPCSTR sFilename, float Dim_X, float Dim_Y );
    	~TEXTURE();
    	
    	TEXTURE( const TEXTURE& temp );
    	TEXTURE& operator = ( const TEXTURE& temp );
    
    	IDirect3DTexture9* Texture;
    	D3DXVECTOR3* Center;
    	LPCSTR Filename;
    };
    
    class SPRITE
    {
    public:
    	SPRITE();
    	SPRITE( IDirect3DDevice9* Dev );
    	~SPRITE();
    
    	ID3DXSprite* Sprite;
    
    	std::vector<TEXTURE> Texture;
    	std::vector<SPRITE_INFO> SpriteInfo;
    	
    	
    	void SetDevice( IDirect3DDevice9* Dev );
    	void OnLostDevice();
    	void OnResetDevice();
    	void AddTexture( LPCSTR Filename, float Dim_X, float Dim_Y );
    	void Draw();
    
    private:
    	IDirect3DDevice9* Device;
    	bool inited : 1;
    };//testsprite( gd3dDevice );
    Code:
    // SPRITE.CPP
    
    // ###################### SPRITE_INFO ######################
    
    SPRITE_INFO::SPRITE_INFO()
    {
    	// Default color
    	Position = new D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
    	SetColor( 255, 255, 255 );
    };
    
    SPRITE_INFO::~SPRITE_INFO()
    {
    	delete Position;
    };
    
    SPRITE_INFO::SPRITE_INFO( const SPRITE_INFO& temp )
    {
    	Position = new D3DXVECTOR3;
    	*Position = *temp.Position;
    };
    
    SPRITE_INFO& SPRITE_INFO::operator = ( const SPRITE_INFO& temp )
    {
    	if ( this != &temp )
    	{
    		*Position = *temp.Position;
    	}
    	return *this;
    };
    
    void SPRITE_INFO::SetColor( WORD r, WORD g, WORD b )
    {
    	Color = D3DCOLOR_XRGB( r, g, b );
    };
    
    void SPRITE_INFO::SetPosition( float x, float y, float z )
    {
    	*Position = D3DXVECTOR3( x, y, z );
    };
    
    
    // ###################### TEXTURE ######################
    
    TEXTURE::TEXTURE( IDirect3DDevice9* Device, LPCSTR sFilename, float Dim_X, float Dim_Y )
    {
    	Filename = sFilename;
    	Center = new D3DXVECTOR3( Dim_X, Dim_Y, 0.0f);
    	HR( D3DXCreateTextureFromFile( Device, Filename, &Texture ) );
    };
    
    TEXTURE::~TEXTURE()
    {
    	ReleaseCOM( Texture );
    	delete Center;
    };
    
    TEXTURE::TEXTURE( const TEXTURE& temp )
    {
    	Center = new D3DXVECTOR3;
    	*Center = *temp.Center;
    
    
    	IDirect3DDevice9* Device;
    	Texture->GetDevice( &Device );
    	HR( D3DXCreateTextureFromFile( Device, Filename, &Texture ) );
    
    	*Texture = *temp.Texture;
    };
    
    TEXTURE& TEXTURE::operator = ( const TEXTURE& temp )
    {
    	if ( this != &temp )
    	{
    		*Center = *temp.Center;
    		*Texture = *temp.Texture;
    	}
    	return *this;
    };
    
    
    // ###################### SPRITE ######################
    
    SPRITE::SPRITE()
    {
    	inited = false;
    };
    
    SPRITE::SPRITE( IDirect3DDevice9* Dev )
    {
    	inited = false;
    	SetDevice( Dev );
    };
    
    SPRITE::~SPRITE()
    {
    	ReleaseCOM( Sprite );
    };
    
    void SPRITE::SetDevice( IDirect3DDevice9* Dev )
    {
    	if ( !inited )
    	{
    		inited = true;
    		Device = Dev;
    		HR( D3DXCreateSprite( Device, &Sprite ) );
    	}
    };
    
    void SPRITE::OnLostDevice()
    {
    	HR( Sprite->OnLostDevice() );
    };
    
    void SPRITE::OnResetDevice()
    {
    	// Call the onResetDevice of other objects.
    	HR( Sprite->OnResetDevice() );
    
    	// This code sets texture filters, which helps to smooth out distortions
    	// when you scale a texture.  
    	HR( Device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ) );
    	HR( Device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ) );
    	HR( Device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ) );
    	
    	// The following code specifies an alpha test and reference value.
    	HR( Device->SetRenderState(D3DRS_ALPHAREF, 10 ) );
    	HR( Device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER ) );
    
    	// The following code is used to setup alpha blending.
    	HR( Device->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ) );
    	HR( Device->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ) );
    	HR( Device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ) );
    	HR( Device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ) );
    
    	// Indicates that we are using 2D texture coordinates.
    	HR( Device->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 ) );
    };
    
    void SPRITE::AddTexture( LPCSTR Filename, float Dim_X, float Dim_Y )
    {
    	Texture.push_back( TEXTURE( Device, Filename, Dim_X, Dim_Y ) );
    };
    
    void SPRITE::Draw()
    {
    	// Clear the back buffer and depth buffer.
    	HR( Sprite->Begin( D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE ) );
    	
    	for ( unsigned short i = 0 ; i < SpriteInfo.size() ; i++ )
    	{
    		//D3DXMATRIX T;
    		//D3DXMatrixTranslation( &T, 0.0f, 0.0f, 0.0f );
    		//HR( Sprite->SetTransform( &T ) );
    
    		HR( Sprite->Draw( Texture.at( i ).Texture, NULL, Texture.at( i ).Center, SpriteInfo.at( i ).Position, SpriteInfo.at( i ).Color ) );
    		HR( Sprite->Flush() );
    	}
    
    	HR( Sprite->End() );
    };
    This is my first attempt for a simple wrapper for a 2D game. It compiles fine but I get a box popup when I run it titled "Unexpected error encountered" and says "File:"... and thats it. When I debug, it says

    "There is no source code available for the current location". If I can't get this to work, any dreams I have of making a 2D game with textures will perish.

    I much appreciate any help.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    If you are compiling in anything except unicode the DX_TRACE and other functions won't report correctly.

    You can still get information by using DXGetErrorString() and others like it.

    Code:
    TEXTURE::TEXTURE( const TEXTURE& temp )
    {
    	Center = new D3DXVECTOR3;
    	*Center = *temp.Center;
    
    
    	IDirect3DDevice9* Device;
    	Texture->GetDevice( &Device );
    	HR( D3DXCreateTextureFromFile( Device, Filename, &Texture ) );
    
    	*Texture = *temp.Texture;
    };
    This looks suspicious. D3DXCreateTextureFromFile() will create a new IDirect3DTexture9 interface pointer for you. Then you proceed to assign it to a pointer in temp. You just leaked one COM resource right there. If you were just going to shallow copy the one in temp to the current one why did you ask D3DX to create a new one? Also you don't need semicolon's after the closing braces of functions.

    Code:
    HR( Device->SetRenderState(D3DRS_ALPHAREF, 10 ) );
    HR( Device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER ) );
    
    // The following code is used to setup alpha blending.
    HR( Device->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ) );
    HR( Device->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ) );
    HR( Device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ) );
    HR( Device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ) );
    Are you trying to do alpha testing here or alpha blending? Enabling both for one object seems a bit odd.

    Code:
    HR( Sprite->Begin( D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE ) );
    This may draw nothing depending on how you have your system setup.

    Code:
    void SPRITE::AddTexture( LPCSTR Filename, float Dim_X, float Dim_Y )
    {
    	Texture.push_back( TEXTURE( Device, Filename, Dim_X, Dim_Y ) );
    };
    Why not have a texture manager that returns a texture ID when you add textures to it's list? Then you store the ID in the Sprite object and during render you ask for the texture based on the ID you were given at creation time.

    Why have the sprite keep track of the actual texture objects? Isn't animation just a list of texture ID's or texture u,v's?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulator
    By MasterAchilles in forum C Programming
    Replies: 10
    Last Post: 11-30-2008, 09:31 PM
  2. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 PM
  3. Separate long string into multiple arrays
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-27-2007, 01:57 AM
  4. One quick question...
    By Kross7 in forum C++ Programming
    Replies: 10
    Last Post: 04-13-2007, 09:50 PM
  5. Replies: 22
    Last Post: 11-08-2001, 10:01 PM

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