I've finally started coding my first D3D game, a breakout clone. It's been going pretty smooth until now. I was thinking of having the ball explode when it goes past the bat, but I am having trouble making this work. I have a mesh called 'Ball' that I load from an .X file. I was thinking about creating a dynamic vertex buffer and filling it with the vertex data from the Ball mesh, that way I could have the vertices explode out and change colors. Not sure if I am on the right track, or even if it can work this way but here is what I have so far.
I have these objects for the Ball and the dynamic buffer
Code:
CMesh m_BallMesh;
CVertexBuffer m_ExplosionBuffer;
My vertex buffer and custom vertex structure
Code:
bool CVertexBuffer::CreateBuffer(LPDIRECT3DDEVICE9 pDevice,
UINT numVertices,
DWORD FVF,
UINT VertexSize,
bool dynamic) {
Release();
m_numVertices = numVertices;
m_FVF = FVF;
m_VertexSize = VertexSize;
D3DPOOL pool = dynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
DWORD usage = dynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : D3DUSAGE_WRITEONLY;
if (FAILED(pDevice->CreateVertexBuffer(m_numVertices * m_VertexSize,
usage,
m_FVF,
pool,
&m_pVB,
NULL ) ) ) {
SHOWERROR("CreateVertexBuffer failed.", __FILE__, __LINE__);
return false;
}
return true;
}
typedef struct PositionNormalTextured {
public:
PositionNormalTextured()
: X(0), Y(0), Z(0), Nx(0), Ny(0), Nz(0), Tu(0), Tv(0) { }
PositionNormalTextured(float x, float y, float z,
float nx, float ny, float nz,
float tu, float tv)
: X(x), Y(y), Z(z), Nx(nx), Ny(ny), Nz(nz), Tu(tu), Tv(tv) { }
float X, Y, Z;
float Nx, Ny, Nz;
float Tu, Tv;
} PositionNormalTextured;
And then I copy the vertex data from the mesh to the vertex buffer
Code:
// Create the buffer
m_ExplosionBuffer.CreateBuffer(pDevice,
m_BallMesh.GetMesh()->GetNumVertices(),
D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX0,
sizeof(cstmVertex::PositionNormalTextured),
true);
// Lock the mesh vertex buffer and get pointer to its memory
void** pVB;
m_BallMesh.GetMesh()->LockVertexBuffer(0, pVB);
// Copy the buffer data to the ExplosionBuffer
m_ExplosionBuffer.SetData(m_BallMesh.GetMesh()->GetNumVertices(),
pVB,
0);
m_BallMesh.GetMesh()->UnlockVertexBuffer();
But I get a segmentation fault when I get to the SetData function, which copies the data from pVB to m_ExplosionBuffer. I assumed that the size of m_ExplosionBuffer does not match the size of pVB, but everything looks ok to me.
What am I missing here? Or am I even on the right track?