Here is what it takes in DirectX:
Code:
#ifndef C2DOVERLAY
#define C2DOVERLAY
#include <string>
#include <d3d9.h>
struct TLVertex
{
D3DXVECTOR3 m_Position;
float m_fRhw;
float m_fTu,m_fTv;
static const DWORD m_dwFVF;
TLVertex(D3DXVECTOR3 pos,float u,float v):m_Position(pos),m_fRhw(1.0f),m_fTu(u),m_fTv(v) {}
};
class C2DOverlay
{
TLVertex m_pVerts[4];
IDirect3DTexture9 *m_pTexture;
IDirect3DDevice9 *m_pDevice;
float m_fSize;
public:
C2DOverlay(void):m_pTexture(NULL),m_pDevice(NULL),m_fSize(0.0f) {}
~C2DOverlay(void)
{
//If we sucessfully allocated an object from COM, hand it back now
if (Texture) Texture->Release();
Texture=NULL;
}
void Create(IDirect3DDevice9 *_device,float _size);
void Render(float _timeDelta);
};
#endif
Code:
#include "C2DOverlay.h"
const DWORD TLVertex::FVF=D3DFVF_XYZRHW | D3DFVF_TEX1;
void C2DOverlay::Create(IDirect3DDevice9 *_device,float _size,std::string _TextureFile)
{
m_pDevice=_device;
m_fSize=_size;
float size2=m_fSize*.5f;
m_pVerts[0]=TLVertex(D3DXVECTOR3(-size2,-size2,0.0f,0.0f,0.0f);
m_pVerts[1]=TLVertex(D3DXVECTOR3(size2,-size2,0.0f,1.0f,0.0f);
m_pVerts[2]=TLVertex(D3DXVECTOR3(-size2,size2,0.0f,0.0f,1.0f);
m_pVerts[3]=TLVertex(D3DXVECTOR3(size2,size2,0.0f,1.0f,1.0f);
if (FAILED(D3DXCreateTextureFromFile(m_pDevice,_TextureFile.c_str(),&m_pTexture)))
{
MessageBox(0,0,"C2DOverlay::Create() - Failed to load texture");
return;
}
}
void C2DOverlay::Render(float _timeDelta)
{
//Insert code to translate overlay here
//.....
//.....
//Render overlay
m_pDevice->SetTexture(0,&m_pTexture);
m_pDevice->SetFVF(TLVertex::FVF);
m_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2 ,m_pVerts,sizeof(TLVertex));
}
That should do the trick. Render your vertices as XYZRHW. This tells Direct3D NOT to send the vertices down the rendering pipeline. In essence you are saying 'I've already transformed these vertexes to screen space....don't touch them.' What this translates to is that the x and y components of your vertices are rendered as is - in screen space - just like in a normal 2D game. So it's fairly straightforward how to move the object around in screen space so I omitted that code.
You can also use irregular shaped textures by using the correct alpha blending states.
m_pDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA );
m_pDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALP HA);
You probably don't want to do this in each object's render function as it would be too slow. Turn alpha blending on, render all objects that use that type of alpha blending and then turn it off if need be or simply change the blending states to do different types of alpha blending.
Hope this helps.