I make code to draw square by Direct3D 9:
// create the vertices using the CUSTOMVERTEX struct
struct CustomVertex { float X, Y, Z, RHW; unsigned int Color; } vertices[4] = {
{ 10.0f, 10.0f, 0.0f, 1.0f, 0xFFFFFFFF },
{ 10.0f + 20.0f, 10.0f, 0.0f, 1.0f, 0xFFFFFFFF },
{ 10.0f, 10.0f + 20.0f, 0.0f, 1.0f, 0xFFFFFFFF },
{ 10.0f + 20.0f, 10.0f + 20.0f, 0.0f, 1.0f, 0xFFFFFFFF }
};
// the pointer to the vertex buffer
IDirect3DVertexBuffer9 *v_buffer;
// create a vertex buffer interface called v_buffer
d3ddev->CreateVertexBuffer(4 * sizeof(CustomVertex), 0, (D3DFVF_XYZRHW | D3DFVF_DIFFUSE), D3DPOOL_MANAGED, &v_buffer, NULL);
// a void pointer
void *pVoid;
// lock v_buffer and load the vertices into it
v_buffer->Lock(0, 0, &pVoid, 0);
memcpy(pVoid, vertices, 4 * sizeof(CustomVertex));
v_buffer->Unlock();
// select which vertex format we are using
d3ddev->SetFVF((D3DFVF_XYZRHW | D3DFVF_DIFFUSE));
// select the vertex buffer to display
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CustomVertex));
// copy the vertex buffer to the back buffer
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
// close and release the vertex buffer
v_buffer->Release();
And I found memory leak, may be imperceptible, but when draw 40 shapes, leakage will be noticed, but idk exactly where and why.
NOTE: I use the entire code inside a loop, like this:
while () {
// the code
}
Why? Because I need to draw a shape at a certain time, and then stop drawing it.
ADDITIONAL NOTE: Idk the code is suitable for this way, or there is better way.