Hi all,
I''ve got the following class:
#ifndef CPANE_H
#define CPANE_H
#include "StdAfx.h"
class CPane
{
//Private member variables
private:
//Generic
int miWidth; //Viewport / window width
int miHeight; //Viewport / window height
int miTextures; //Counter of loaded textures
int miMaxTextures; //Maximum number of textures
//Pane element
struct ELEMENT {
LPDIRECT3DTEXTURE9 mptex;
int x, y;
float xSize, ySize;
};
ELEMENT* pElements;
//Custom vertex
struct PANEVERTEX {
D3DXVECTOR3 position;
D3DXVECTOR3 vecNorm;
FLOAT tu, tv;
FLOAT tu2, tv2;
};
// static const DWORD D3DFVF_PANEVERTEX = (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX2);
DWORD D3DFVF_PANEVERTEX;
//DirectX
LPDIRECT3DDEVICE9 mpdev;
LPDIRECT3DVERTEXBUFFER9 mpvb;
//Private member functions
private:
HRESULT InitPane();
//Implementation
public:
HRESULT Create(LPDIRECT3DDEVICE9 pDev, int iWidth, int iHeight);
HRESULT LoadTexture(int x, int y, float xs, float ys, LPCSTR szFileName);
HRESULT Render();
void CleanUp();
//Defaults
public:
CPane();
~CPane();
};
#endif
#include "CPane.h"
CPane::CPane()
{
//Objects
mpvb = NULL;
mpdev = NULL;
//Textures
miMaxTextures = 32;
miTextures = 0;
pElements = new ELEMENT[miMaxTextures];
D3DFVF_PANEVERTEX = (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX2);
}
CPane::~CPane()
{
CleanUp();
}
void CPane::CleanUp()
{
//Objects
SAFE_RELEASE(mpvb);
//Textures
SAFE_DELETE(pElements);
//Reset textures count
miTextures = 0;
}
HRESULT CPane::Create(LPDIRECT3DDEVICE9 pDev, int iWidth, int iHeight)
{
CleanUp();
//Get our rendering device
mpdev = pDev;
if (!mpdev)
return E_FAIL;
//Save the window dimensions
miWidth = iWidth;
miHeight = iHeight;
//3D projection matrix
D3DXMATRIX matProj;
D3DXMatrixOrthoLH(&matProj, (float)miWidth, (float)miHeight, 0, 1);
mpdev->SetTransform(D3DTS_PROJECTION, &matProj);
//Turn off culling
mpdev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
//Turn off lighting
mpdev->SetRenderState(D3DRS_LIGHTING, FALSE);
//Turn off z-buffering (no 2d graphics discarded at render time)
mpdev->SetRenderState(D3DRS_ZENABLE, FALSE);
//Turn off z-buffer writing (2d graphics not to alter z-buffer)
mpdev->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
//Initialize the vertex buffer
return InitPane();
}
HRESULT CPane::InitPane()
{
PANEVERTEX* pVertices;
//Create the interface object buffer
if(FAILED(mpdev->CreateVertexBuffer(4*sizeof(PANEVERTEX), 0, D3DFVF_PANEVERTEX, D3DPOOL_DEFAULT, &mpvb, NULL)))
return E_FAIL;
//Lock the buffer for editing
if(FAILED(mpvb->Lock(0, 0, (void**)&pVertices, 0)))
return E_FAIL;
//Create a quad made up of 3 vertices (triangle strips)
//
// 1 3
// X--------X
// |x |
// | x |
// | x |
// | x |
// | x |
// | x |
// | x |
// | x|
// X--------X
// 0 2
//
pVertices[0].position = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
pVertices[0].tu = 0.0f;
pVertices[0].tv = 1.0f;
pVertices[0].tu2 = 0.0f;
pVertices[0].tv2 = 1.0f;
pVertices[0].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
pVertices[1].position = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
pVertices[1].tu = 0.0f;
pVertices[1].tv = 0.0f;
pVertices[1].tu2 = 0.0f;
pVertices[1].tv2 = 0.0f;
pVertices[1].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
pVertices[2].position = D3DXVECTOR3( 1.0f, 0.0f, 0.0f );
pVertices[2].tu = 1.0f;
pVertices[2].tv = 1.0f;
pVertices[2].tu2 = 1.0f;
pVertices[2].tv2 = 1.0f;
pVertices[2].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
pVertices[3].position = D3DXVECTOR3( 1.0f, 1.0f, 0.0f );
pVertices[3].tu = 1.0f;
pVertices[3].tv = 0.0f;
pVertices[3].tu2 = 1.0f;
pVertices[3].tv2 = 0.0f;
pVertices[3].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
//Done editing, unlock the buffer
mpvb->Unlock();
return D3D_OK;
}
HRESULT CPane::LoadTexture(int x, int y, float xs, float ys, LPCSTR szFileName)
{
//LPDIRECT3DTEXTURE9 mptex;
//Do we have a device?
if (!mpdev)
return E_FAIL;
//Have we reached the maximum textures?
if (miTextures == miMaxTextures)
return E_FAIL;
//Load the texture from file
if (FAILED(D3DXCreateTextureFromFile(mpdev, szFileName, &pElements[miTextures].mptex))) {
if (miTextures > 0)
miTextures--;
return E_FAIL;
}
//Set the appropriate values in the structure
//pElements[miTextures].mptex = mptex;
pElements[miTextures].x = x;
pElements[miTextures].y = y;
pElements[miTextures].xSize = xs;
pElements[miTextures].ySize = ys;
//Increment the number of textures we''re using
miTextures++;
return D3D_OK;
}
HRESULT CPane::Render()
{
D3DXMATRIX matWorld, matRotation, matTranslation, matScale;
float fXPos, fYPos;
//Do we have a device?
if (!mpdev)
return E_FAIL;
for (int i = 0; i < miTextures; i++) {
//Set default position, scale, rotation
D3DXMatrixIdentity(&matTranslation);
//Scale the sprite
D3DXMatrixScaling(&matScale, pElements[i].xSize, pElements[i].ySize, 1.0f);
D3DXMatrixMultiply(&matTranslation, &matTranslation, &matScale);
//Rotate the sprite
D3DXMatrixRotationZ(&matRotation, 0.0f);
D3DXMatrixMultiply(&matWorld, &matTranslation, &matRotation);
//Calculate the position in screen-space
fXPos = (float)(-(miWidth / 2) + pElements[i].x);
fYPos = (float)(-(miHeight / 2) - pElements[i].y + pElements[i].ySize);
//Move the sprite
matWorld._41 = fXPos;
matWorld._42 = fYPos;
//Set matrix
mpdev->SetTransform(D3DTS_WORLD, &matWorld);
mpdev->SetTexture(0, pElements[i].mptex);
mpdev->SetStreamSource(0, mpvb, 0, sizeof(PANEVERTEX));
mpdev->SetFVF(D3DFVF_PANEVERTEX);
mpdev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
//Dereference texture
mpdev->SetTexture(0, NULL);
}
return D3D_OK;
}
my main render function is the following:
HRESULT CEngine::Render()
{
if (!mpdev)
return E_FAIL;
mpdev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(0,0,0,0), 1.0f, 0);
if (SUCCEEDED(mpdev->BeginScene())) {
//mptile->Render();
if (mppane)
mppane->Render();
UpdateStats();
mpdev->EndScene();
}
mpdev->Present(NULL, NULL, NULL, NULL);
return D3D_OK;
}
I am loading a 256x256 tga and placing and 0,0.
The damn bitmap is not being rendered on the screen and it''s driving me crazy... HELP!!!!
Any suggestions??
Ivan