I make code to draw square by Direct3D 9:
struct CustomVertex { float X, Y, Z, RHW; CE_uint4 Color; } vertices[4] = {
{ 10.0f, 10.0f, 0.0f, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255)},
{ 10.0f + 20.0f, 10.0f, 0.0f, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) },
{ 10.0f, 10.0f + 20.0f, 0.0f, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) },
{ 10.0f + 20.0f, 10.0f + 20.0f, 0.0f, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) }
};
IDirect3DVertexBuffer9* v_buffer = CE_NULL; // the pointer to the vertex buffer
// create a vertex buffer interface called v_buffer
CE_CHECK_HRESULT(renderer.gDevice()->CreateVertexBuffer(4 * sizeof(CustomVertex), 0, (D3DFVF_XYZRHW | D3DFVF_DIFFUSE), D3DPOOL_MANAGED, &v_buffer, CE_NULL));
void* pVoid; // a void pointer
// lock v_buffer and load the vertices into it
CE_CHECK_HRESULT(v_buffer->Lock(0, 0, &pVoid, 0));
memcpy(pVoid, vertices, 4 * sizeof(CustomVertex));
CE_CHECK_HRESULT(v_buffer->Unlock());
// select which vertex format we are using
CE_CHECK_HRESULT(renderer.gDevice()->SetFVF((D3DFVF_XYZRHW | D3DFVF_DIFFUSE)));
// select the vertex buffer to display
CE_CHECK_HRESULT(renderer.gDevice()->SetStreamSource(0, v_buffer, 0, sizeof(CustomVertex)));
CE_CHECK_HRESULT(renderer.gDevice()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2));
v_buffer->Release(); // close and release the vertex buffer
But is here in this code memory leak, And idk where and how.
NOTE: the code is used in game loop, like this:
while() {
// the code
}
why? Because my project requires this way.
ADDITIONAL NOTE: Idk the code is suitable for this way.