# How to draw 2 cubes instead fo one? (DirectX9)

0

Here my code draws one cube:

``````void CreateCube(
LPDIRECT3DDEVICE9 &ptr_dxDevice,
LPDIRECT3DINDEXBUFFER9 &ptr_indexBuffer,
LPDIRECT3DVERTEXBUFFER9 &ptr_vertexBuffer,
int cubeNumber
)
{
// create the vertices using the CUSTOMVERTEX struct
CUSTOMVERTEX vertices[] =
{
{ -0.2f,  0.2f, -0.2f, 0xffff0000,},
{  0.2f,  0.2f, -0.2f, 0xff00ff00,},
{ -0.2f, -0.2f, -0.2f, 0xffff0000,},
{  0.2f, -0.2f, -0.2f, 0xff00ff00,},
{ -0.2f,  0.2f,  0.2f, 0xffff0000,},
{  0.2f,  0.2f,  0.2f, 0xff00ff00,},
{ -0.2f, -0.2f,  0.2f, 0xffff0000,},
{  0.2f, -0.2f,  0.2f, 0xff00ff00,},
};

// create a vertex buffer interface called v_buffer
ptr_dxDevice->CreateVertexBuffer(
8 * sizeof(CUSTOMVERTEX),
0,
drawFVF,
D3DPOOL_MANAGED,
&ptr_vertexBuffer,
NULL);

VOID* pVoid;    // a void pointer

// lock v_buffer and load the vertices into it
ptr_vertexBuffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
ptr_vertexBuffer->Unlock();

// create the indices using an int array
short indices[] =
{
0, 1, 2,    // side 1
2, 1, 3,
4, 0, 6,    // side 2
6, 0, 2,
7, 5, 6,    // side 3
6, 5, 4,
3, 1, 7,    // side 4
7, 1, 5,
4, 5, 0,    // side 5
0, 5, 1,
3, 7, 2,    // side 6
2, 7, 6,
};

// create an index buffer interface called i_buffer
ptr_dxDevice->CreateIndexBuffer(36 * sizeof(short),
0,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&ptr_indexBuffer,
NULL);

// lock i_buffer and load the indices into it
ptr_indexBuffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, indices, sizeof(indices));
ptr_indexBuffer->Unlock();

ptr_dxDevice->SetFVF(drawFVF);

// select the vertex and index buffers to use
ptr_dxDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(CUSTOMVERTEX));
ptr_dxDevice->SetIndices(ptr_indexBuffer);

// draw the cube
ptr_dxDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
}
``````

When I tries to use this code to create 2 or more cubes I got memory violation error. I'm using vertex bufefr and index Buffer.

PLease, help me to fix this code - what should I do to be able to draw 2 or more cubes ?

Thank you

P.S. Using DirectX9 Visual Studio 2017 Today is Thursday

c++
visual-c++
directx