Code:
//IN THE HEADER
IDirect3DDevice8 *m_pD3DDevice;
IDirect3DVertexBuffer8 *m_pVBuffer2D;
//THIS CODE INITIALIZES ONE CUSTOM VERTEX TYPE THE FIRST
//TIME BECAUSE I CAN'T WORK WITH AN EMPTY VERTEX BUFFER
HRESULT cObject2D::InitVBuffer()
{
HRESULT Attempt;
CUSTOMVERTEX *VBufferVertices;
Attempt = m_pD3DDevice->CreateVertexBuffer(1 * sizeof(CUSTOMVERTEX), 0,
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_pVBuffer2D);
if(FAILED(Attempt))
return E_FAIL;
Attempt = m_pVBuffer2D->Lock(0, 1 * sizeof(CUSTOMVERTEX), (BYTE**)&VBufferVertices, 0);
if(FAILED(Attempt))
return E_FAIL;
VBufferVertices[0].vPosition = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
VBufferVertices[0].Colour = D3DCOLOR_XRGB(0, 0, 0);
m_pVBuffer2D->Unlock();
m_pNumV += 1;
AddBox(D3DXVECTOR3(50.0f, 0.0f, 1.0f), 40.0f, 40.0f);
AddBox(D3DXVECTOR3(0.0f, 50.0f, 1.0f), 40.0f, 40.0f);
return S_OK;
}
//HERE'S WHERE ALL THE MAGIC HAPPENS
HRESULT cObject2D::AddBox(D3DXVECTOR3 Coords, float fWidth, float fHeight)
{
IDirect3DVertexBuffer8 *VBufferStorage = NULL;
CUSTOMVERTEX *VBufferVertices = NULL;
HRESULT Attempt;
//CREATE TEMP BUFFER TO HOLD OLD DATA
Attempt = m_pD3DDevice->CreateVertexBuffer(m_pNumV * sizeof(CUSTOMVERTEX), 0,
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT,
&VBufferStorage);
if(FAILED(Attempt))
return E_FAIL;
//COPY OLD DATA TO TEMP BUFFER
*VBufferStorage = *m_pVBuffer2D;
//CLEAR OLD BUFFER AND NULL IT
if(m_pVBuffer2D != NULL)
{
m_pVBuffer2D->Release();
m_pVBuffer2D = NULL;
}
//RE-CREATE OLD BUFFER FOR NEW SHAPE (SIZE OF: 4 NEW VERTICES + TEMP BUFFER)
Attempt = m_pD3DDevice->CreateVertexBuffer(sizeof(CUSTOMVERTEX) * (4 + m_pNumV), 0,
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT,
&m_pVBuffer2D);
if(FAILED(Attempt))
return E_FAIL;
//COPY THE OLD DATA BACK INTO THE NEW AND IMPROVED BUFFER
*m_pVBuffer2D = *VBufferStorage;
//LOCK THE LAST 4 POSITIONS OF THE NEW AND IMPROVED BUFFER
Attempt = m_pVBuffer2D->Lock(0, 0, (BYTE**)&VBufferVertices, 0);
if(FAILED(Attempt))
return E_FAIL;
VBufferVertices[m_pNumV + 0].vPosition = Coords + (D3DXVECTOR3(-fWidth / 2, +fHeight / 2, 0.0f));
VBufferVertices[m_pNumV + 1].vPosition = Coords + (D3DXVECTOR3(+fWidth / 2, +fHeight / 2, 0.0f));
VBufferVertices[m_pNumV + 2].vPosition = Coords + (D3DXVECTOR3(-fWidth / 2, -fHeight / 2, 0.0f));
VBufferVertices[m_pNumV + 3].vPosition = Coords + (D3DXVECTOR3(+fWidth / 2, -fHeight / 2, 0.0f));
VBufferVertices[m_pNumV + 0].Colour = D3DCOLOR_XRGB(255, 0, 0);
VBufferVertices[m_pNumV + 1].Colour = D3DCOLOR_XRGB(0, 255, 0);
VBufferVertices[m_pNumV + 2].Colour = D3DCOLOR_XRGB(0, 0, 255);
VBufferVertices[m_pNumV + 3].Colour = D3DCOLOR_XRGB(0, 255, 255);
m_pVBuffer2D->Unlock();
m_pNumV += 4;
//CLEAN OUT UNNECCESSARY VARIABLES, JUST FOR KICKS
VBufferVertices = NULL;
VBufferStorage->Release();
VBufferStorage = NULL;
//SUCCESSFULLY TACKED 4 MORE VERTICES TO THE END OF OUR BUFFER, REPORT SUCCESS
return S_OK;
}
//NOT AS IMPORTANT, JUST SHOWING WHAT I'M RENDERING WITH
void cObject2D::RenderAll()
{
m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, false);
m_pD3DDevice->SetTransform(D3DTS_PROJECTION, &m_pMatProj);
m_pD3DDevice->SetTransform(D3DTS_WORLD, &m_pMatView);
m_pD3DDevice->SetTransform(D3DTS_VIEW, &m_pMatView);
m_pD3DDevice->SetStreamSource(0, m_pVBuffer2D, sizeof(CUSTOMVERTEX));
m_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 1, 2);
}
If you actually read it all, you'll see that I've tried to implement code that will update the Vertex Buffer on the fly with the correct number of vertices, allowing me to add a box simply through a function. Now, the first time that AddBox() gets called, it works just fine, no hitches, I can see the square. But, if I call it a second time (as shown in the code), then my screen is all blank (just the black window shows). Now, there are a few things that've come up when I've tested this code...