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.