Well, I've made the breakthrough of getting a texture on the screen, and I can even move it around. But that's the problem... it's only one texture and adding more doesn't seem to do much help.
Sorry to bombard you with so much code... but this is as simplified as I can get:
Code:
// d3dTexture.h
#include "d3dApp.h"
#ifndef _D3DTEXTURE_H
#define _D3DTEXTURE_H
#include <vector>
class TEXTURE
{
public:
TEXTURE();
~TEXTURE();
virtual void SetDevice( IDirect3DDevice9* tDevice );
virtual void OnLostDevice();
virtual void OnResetDevice();
UINT AddTexture( const char* Filename, float dimx, float dimy );
IDirect3DTexture9* GetTexture( int Tex_ID );
D3DXVECTOR3 GetCenter( int Tex_ID );
UINT TotalTextures();
protected:
std::vector< IDirect3DTexture9* > Container;
std::vector< D3DXVECTOR3 > CenterContainer;
IDirect3DTexture9* Temp;
IDirect3DDevice9* Device;
};
#endif
// d3dTexture.cpp
#include "d3dTexture.h"
TEXTURE::TEXTURE()
{
Device = NULL;
};
TEXTURE::~TEXTURE()
{
// delete all textures in the container
for ( unsigned int i = 0; i < Container.size(); i++ )
ReleaseCOM( Container.at( i ) );
};
void TEXTURE::SetDevice( IDirect3DDevice9* tDevice )
{
Device = tDevice;
};
void TEXTURE::OnLostDevice()
{
// Nothing
};
void TEXTURE::OnResetDevice()
{
// The following code specifies an alpha test and reference value.
HR(gd3dDevice->SetRenderState( D3DRS_ALPHAREF, 10 ) );
HR(gd3dDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATER ) );
// The following code is used to setup alpha blending.
HR( gd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ) );
HR( gd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ) );
HR( gd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ) );
HR( gd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ) );
// Indicates that we are using 2D texture coordinates.
HR(gd3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 ) );
// This code sets texture filters, which helps to smooth out distortions
// when you scale a texture.
HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR));
HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR));
HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR));
};
UINT TEXTURE::AddTexture( const char* Filename, float dimx, float dimy )
{
HRESULT result = D3DXCreateTextureFromFile( Device, Filename, &Temp );
// if the texture worked, add details
if ( result == D3D_OK )
{
CenterContainer.push_back( D3DXVECTOR3( dimx / 2.0f, dimy / 2.0f, 0.0f) );
Container.push_back( Temp );
return Container.size();
}
// loading texture failed
std::string ts = Filename;
ts += " texture could not open.";
MessageBox( NULL, ts.c_str(), "Error", 0 );
return 0;
};
IDirect3DTexture9* TEXTURE::GetTexture( int Tex_ID )
{
return Container.at( Tex_ID );
};
D3DXVECTOR3 TEXTURE::GetCenter( int Tex_ID )
{
return CenterContainer.at( Tex_ID );
};
UINT TEXTURE::TotalTextures()
{
return Container.size();
};
Code:
// d3dSprite.h
#include "d3dApp.h"
#ifndef _D3DSPRITE_H
#define _D3DSPRITE_H
#include "d3dTexture.h"
class SPRITE : public TEXTURE
{
public:
void OnLostDevice();
void OnResetDevice();
void SetDevice( IDirect3DDevice9* tDevice );
void DrawAll();
UINT AddSprite( const char* Filename, float dimx, float dimy, float posx, float posy );
bool AddSprite( UINT Tex_ID, float dimx, float dimy, float posx, float posy );
D3DXVECTOR3 GetPosition( int Spr_ID );
void SetPosition( int Spr_ID, float x, float y, float z );
D3DCOLOR GetColor( int Spr_ID );
void SetColor( int Spr_ID, short r, short g, short b );
void SetColor( int Spr_ID, D3DCOLOR col );
private:
ID3DXSprite* Sprite;
std::vector< int > TextureID;
std::vector< D3DXVECTOR3 > Position;
std::vector< D3DCOLOR > Color;
};
#endif
//d3dSprite.cpp
#include "d3dSprite.h"
void SPRITE::OnLostDevice()
{
HR( Sprite->OnLostDevice() );
};
void SPRITE::OnResetDevice()
{
HR( Sprite->OnResetDevice() );
// The following code specifies an alpha test and reference value.
HR(gd3dDevice->SetRenderState( D3DRS_ALPHAREF, 10 ) );
HR(gd3dDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATER ) );
// The following code is used to setup alpha blending.
HR( gd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ) );
HR( gd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ) );
HR( gd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ) );
HR( gd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ) );
// Indicates that we are using 2D texture coordinates.
HR(gd3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 ) );
// This code sets texture filters, which helps to smooth out distortions
// when you scale a texture.
HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR));
HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR));
HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR));
};
void SPRITE::SetDevice( IDirect3DDevice9* tDevice )
{
Device = tDevice;
HR( D3DXCreateSprite( tDevice, &Sprite ) );
};
void SPRITE::DrawAll()
{
// Clear the back buffer and depth buffer.
HR( Sprite->Begin( D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE ) );
// go through texture IDs and draw corresponding textures
for ( UINT i = 0; i < TotalTextures() - 1; i++ )
{
HR( Sprite->Draw( GetTexture( TextureID.at( i ) ), NULL, &GetCenter( i ), &GetPosition( i ), GetColor( i ) ) );
}
HR( Sprite->Flush() );
HR( Sprite->End() ); // finish drawing
};
UINT SPRITE::AddSprite( const char* Filename, float dimx, float dimy, float posx, float posy )
{
// Add texture
int ret = AddTexture( Filename, dimx, dimy );
// if it worked, add details
if ( ret )
{
TextureID.push_back( ret );
Position.push_back( D3DXVECTOR3( posx, posy, 0.0f ) );
Color.push_back( D3DCOLOR_XRGB( 255, 255, 255 ) );
}
return ret;
};
bool SPRITE::AddSprite( UINT Tex_ID, float dimx, float dimy, float posx, float posy )
{
// if the texture exists, add details
if ( Tex_ID < Container.size() )
{
TextureID.push_back( Tex_ID );
Position.push_back( D3DXVECTOR3( posx, posy, 0.0f ) );
Color.push_back( D3DCOLOR_XRGB( 255, 255, 255 ) );
return true;
}
return false;
};
D3DXVECTOR3 SPRITE::GetPosition( int Spr_ID )
{
return Position.at( Spr_ID );
};
void SPRITE::SetPosition( int Spr_ID, float x, float y, float z )
{
Position.at( Spr_ID ) = D3DXVECTOR3( x, y, z );
};
D3DCOLOR SPRITE::GetColor( int Spr_ID )
{
return Color.at( Spr_ID );
};
void SPRITE::SetColor( int Spr_ID, short r, short g, short b )
{
SetColor( Spr_ID, D3DCOLOR_XRGB( r, g, b ) );
};
void SPRITE::SetColor( int Spr_ID, D3DCOLOR col )
{
Color.at( Spr_ID ) = col;
};
Here's a little example of how I use it:
Code:
SPRITE sprite;
sprite.SetDevice( gd3dDevice );
sprite.AddSprite( "test.bmp", 128, 128, 0.0f, 0.0f );
sprite.AddSprite( "test2.bmp", 128, 128, 200.0f, 200.0f );
sprite.DrawAll();
What I've noticed is that it never draws the first texture and the second texture gets the first texture's coordinates(did I stuff up the vectors somewhere?). Just to add to that... all my textures are upsidedown.
Thanks in advance.