Drawing a rectangle in Direct X

Jimmyt1988 picture Jimmyt1988 · Apr 7, 2013 · Viewed 13.9k times · Source

Using the tutorial here, I have managed to get a red triangle up on my screen: http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-4

CUSTOMVERTEX OurVertices[] =
{
    { 0, 0, 0, 1.0f, D3DCOLOR_XRGB( 127, 0, 0 ) },
    { WIDTH, 0, 0, 1.0f, D3DCOLOR_XRGB( 127, 0, 0 ) },
    { 0, 300, 0, 1.0f, D3DCOLOR_XRGB( 127, 0, 0 ) },
    { WIDTH, 300, 0, 1.0f, D3DCOLOR_XRGB( 127, 0, 0 ) }
};

d3dDevice->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
    0,
    CUSTOMFVF,
    D3DPOOL_MANAGED,
    &vBuffer,
    NULL);

VOID* pVoid;    // the void* we were talking about

vBuffer->Lock(0, 0, (void**)&pVoid, 0);    // locks v_buffer, the buffer we made earlier
memcpy(pVoid, OurVertices, sizeof(OurVertices));    // copy vertices to the vertex buffer
vBuffer->Unlock();    // unlock v_buffer

d3dDevice->SetFVF(CUSTOMFVF);
d3dDevice->SetStreamSource(0, vBuffer, 0, sizeof(CUSTOMVERTEX));
d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

But you can see that I really want to be drawing a rectangle.

I have changed the Primitive to draw 2 triangles and extended the buffer size to 4*size of my custom vertex but I can't really say I understand how to get it from my triangle to my rectangle I would like:

enter image description here

Is there a better way of drawing a rectangle rather than using a quad considering I just want to sling some text on top of it something like this:

http://1.bp.blogspot.com/-6HjFVnrVM94/TgRq8oP4U-I/AAAAAAAAAKk/i8N0OZU999E/s1600/monkey_island_screen.jpg

Answer

Jimmyt1988 picture Jimmyt1988 · Apr 7, 2013

I had to exend my buffer to allow for 4 vertex array size:

d3dDevice->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),
    0,
    CUSTOMFVF,
    D3DPOOL_MANAGED,
    &vBuffer,
    NULL);

And then changed the draw primitive from TRIANGLELIST to STRIP extending the amount of triangles drawn to 2

d3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 0, 2 );

Source: http://www.mdxinfo.com/tutorials/tutorial4.php