Jump to content
  • Advertisement

Archived

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

RatLord

getting annoyed - overlays in D3D

This topic is 5588 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

My games coming along well - got physics, collision, particles you name it.... Whats giving me the most grief - my bloody overlay. text isnt a problem what I need is a short example of a 2d overlay for a 3D game - A simple cross hair in the middle of the screen will do - I understand what im supposed to be doing but just cant get it to display.... Once I have one working example Ill be fine as I can see what i have done wrong... Anyone going to be a saint and help me out? Code or a link to some code would be great. or email it to feyratboy@hotmail.com thanks in advance you kind, kind people

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
here is the sprite class I have made maybe it will help

// sprite.h

  
struct SPRITE_VERTEX
{
float x, y, z;
DWORD color;
float u, v;
};
const DWORD D3DFVF_SPRITE = D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1;

class CSprite
{

public:

CSprite();
~CSprite() { Destroy(); }

HRESULT Create(LPDIRECT3DDEVICE9 pDevice, const int nWidth, const int nHeight, const int NumFrames = 1, const DWORD dwColor = 0xFFFFFFFF);
void Destroy();

HRESULT LoadFrame(int nFrame, LPCTSTR pSrcFile, DWORD dwColorKey = 0);

HRESULT Draw(int x, int y);
void UpdateAnimation();

void SetDimensions(const int nWidth, const int nHeight);
void SetPosition(int x, int y);
void SetVisibility(bool bVisible);
void SetAnimationSpeed(int nAnimSpeed);
void SetCurrentFrame(int nFrame);

protected:

HRESULT CreateVertexBuffer();
HRESULT UpdateVertices();

protected:

IDirect3DDevice9* m_pDevice;

IDirect3DVertexBuffer9* m_pVertexBuffer;

int m_X;
int m_Y;

int m_nWidth;
int m_nHeight;

DWORD m_dwColor;

int m_NumFrames;
LPDIRECT3DTEXTURE9* m_Frames;

bool m_bVisible;

int m_nCurrentFrame;

bool m_bAnimating;
int m_nAnimSpeed;

bool m_bOneTimeAnim;

};


// sprite.cpp

  
#include <d3dx9.h>
#include "sprite.h"

CSprite::CSprite()
{
m_pDevice = NULL;

m_pVertexBuffer = NULL;

m_X = 0;
m_Y = 0;

m_nWidth = 0;
m_nHeight = 0;

m_dwColor = 0;

m_NumFrames = 0;
m_Frames = NULL;

m_bVisible = TRUE;

m_nCurrentFrame = 0;

m_bAnimating = TRUE;
m_nAnimSpeed = 0;
}

HRESULT CSprite::Create(IDirect3DDevice9* pDevice, const int nWidth, const int nHeight, const int NumFrames, const DWORD dwColor)
{

m_pDevice = pDevice;

m_nWidth = nWidth;
m_nHeight = nHeight;

m_dwColor = dwColor;

if( SUCCEEDED( CreateVertexBuffer() ) )
{
if( FAILED( UpdateVertices() ) )
{
return E_FAIL;
}
}
else
{
return E_FAIL;
}

m_NumFrames = NumFrames;
m_Frames = new LPDIRECT3DTEXTURE9[m_NumFrames];

for( int i=0; i<m_NumFrames; i++ )
{
m_Frames[i] = NULL;
}

m_bVisible = true;

return D3D_OK;
}

void CSprite::Destroy()
{
if( m_Frames )
{
for( int i=0; i<m_NumFrames; i++ )
{
if( m_Frames[i] )
{
m_Frames[i]->Release();
m_Frames[i] = NULL;
}
}
delete[]* m_Frames;
}

if( m_pVertexBuffer )
{
m_pVertexBuffer->Release();
m_pVertexBuffer = NULL;
}
}

HRESULT CSprite::LoadFrame(int nFrame, LPCTSTR pSrcFile, DWORD dwColorKey)
{

if( nFrame < 1 )
return E_FAIL;
else
nFrame--;

if( m_Frames[nFrame] )
{
m_Frames[nFrame]->Release();
m_Frames[nFrame] = NULL;
}

HRESULT hr = D3D_OK;

hr = D3DXCreateTextureFromFileEx(
m_pDevice, // d3d device

pSrcFile, // file name

D3DX_DEFAULT, // width

D3DX_DEFAULT, // height

D3DX_DEFAULT, // mip levels

0, // usage

D3DFMT_UNKNOWN, // format

D3DPOOL_MANAGED, // pool

D3DX_DEFAULT, // filter

D3DX_DEFAULT, // mip filter

dwColorKey, // color key

NULL, // image info

NULL, // palette

&m_Frames[nFrame] // d3d texture

);

return hr;
}

HRESULT CSprite::Draw(int x, int y)
{

if( !m_bVisible )
{
return D3D_OK;
}

SetPosition(x, y);

m_pDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(SPRITE_VERTEX));
m_pDevice->SetFVF(D3DFVF_SPRITE);

m_pDevice->SetTexture(0, m_Frames[m_nCurrentFrame]);
m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

m_pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

return D3D_OK;
}

void CSprite::UpdateAnimation()
{
static int nUpdateCounter = 0;

if( !m_bAnimating )
{
return;
}

if( nUpdateCounter < m_nAnimSpeed )
{
nUpdateCounter++;
}
else
{
nUpdateCounter = 0;

if( m_nCurrentFrame == (m_NumFrames-1) )
{
m_nCurrentFrame = 0;
if( m_bOneTimeAnim )
m_bAnimating = false;
}
else
{
m_nCurrentFrame++;
}
}
}

void CSprite::SetDimensions(const int nWidth, const int nHeight)
{
bool bUpdate = FALSE;

if( nWidth > 0 )
{
if( m_nWidth != nWidth )
{
m_nWidth = nWidth;
bUpdate = true;
}
}

if( nHeight > 0 )
{
if( m_nHeight != nHeight )
{
m_nHeight = nHeight;
bUpdate = true;
}
}

if( bUpdate ) { UpdateVertices(); }
}

void CSprite::SetPosition(int x, int y)
{
D3DVIEWPORT9 d3dViewport;
m_pDevice->GetViewport(&d3dViewport);

m_X = x;
m_Y = y;

x -= (d3dViewport.Width / 2) - (m_nWidth / 2);
y -= (d3dViewport.Height / 2) - (m_nHeight / 2);

D3DXMATRIX matMove;
D3DXMatrixTranslation(&matMove, (float)x, (float)-y, 0.0f);

m_pDevice->SetTransform(D3DTS_WORLD, &matMove);
}

void CSprite::SetVisibility(bool bVisible)
{
m_bVisible = bVisible;
}

void CSprite::SetAnimationSpeed(int nAnimSpeed)
{
m_bAnimating = true;
m_nAnimSpeed = nAnimSpeed;
}

void CSprite::SetCurrentFrame(int nFrame)
{
if( nFrame < 1 )
return;
else
nFrame--;

m_nCurrentFrame = nFrame;
}

HRESULT CSprite::CreateVertexBuffer()
{
return m_pDevice->CreateVertexBuffer(4*sizeof(SPRITE_VERTEX), 0, D3DFVF_SPRITE, D3DPOOL_DEFAULT, &m_pVertexBuffer, NULL);
}

HRESULT CSprite::UpdateVertices()
{

SPRITE_VERTEX* pVertices = NULL;
m_pVertexBuffer->Lock(0, 4*sizeof(SPRITE_VERTEX), (VOID**)&pVertices, 0);

// Set the positions of the vertices

pVertices[0].x = -(m_nWidth) / 2.0f;
pVertices[0].y = -(m_nHeight) / 2.0f;

pVertices[1].x = -(m_nWidth) / 2.0f;
pVertices[1].y = m_nHeight / 2.0f;

pVertices[2].x = (m_nWidth) / 2.0f;
pVertices[2].y = -(m_nHeight) / 2.0f;

pVertices[3].x = (m_nWidth) / 2.0f;
pVertices[3].y = m_nHeight / 2.0f;

pVertices[0].z = 1.0f;
pVertices[1].z = 1.0f;
pVertices[2].z = 1.0f;
pVertices[3].z = 1.0f;

// Set the color of the vertices

pVertices[0].color = m_dwColor;
pVertices[1].color = m_dwColor;
pVertices[2].color = m_dwColor;
pVertices[3].color = m_dwColor;

// Set the texture coordinates of the vertices

pVertices[0].u = 0.0f;
pVertices[0].v = 1.0f;

pVertices[1].u = 0.0f;
pVertices[1].v = 0.0f;

pVertices[2].u = 1.0f;
pVertices[2].v = 1.0f;

pVertices[3].u = 1.0f;
pVertices[3].v = 0.0f;

m_pVertexBuffer->Unlock();

return D3D_OK;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
and oh ya this is also code I use to setup for drawing the sprites


  
void CDirect3D::Begin2D()
{
D3DXMATRIX matIdentity;
D3DXMATRIX matOrtho;

// Reset the world and view matrices

D3DXMatrixIdentity(&matIdentity);
m_pd3dDevice->SetTransform(D3DTS_WORLD, &matIdentity);
m_pd3dDevice->SetTransform(D3DTS_VIEW, &matIdentity);

// Get the current viewport

D3DVIEWPORT9 d3dViewport;
m_pd3dDevice->GetViewport(&d3dViewport);

// Setup the orthogonal projection matrix

D3DXMatrixOrthoLH(&matOrtho, (float)d3dViewport.Width, (float)d3dViewport.Height, 0.0f, 1.0f);
m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matOrtho);

// Make sure the z-buffer and lighting are disabled

m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
}

void CDirect3D::EnableAlphaBlend()
{
m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
}

void CDirect3D::DisableAlphaBlend()
{
m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
}

Share this post


Link to post
Share on other sites
Ok thanks Ill have a go tommorrow. Funny thing is Ive got 3d sprites fine - just cant get the 2d overlay ones working - had a quick look and your code is 2d sprites so it should provide the clue im looking for in there somewhere

much appreciated.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!