DirectX使用Draw Primitive绘制多个矩形

时间:2018-01-23 00:35:58

标签: c++ memory-leaks directx

我已经开始在C ++中使用DirectX。我试图在屏幕上绘制多个矩形和三角形并尝试移动它们。我将顶点存储在顶点缓冲区中并绘制一次。然后我多次调用DrawPrimitives绘制矩形。我从How to Draw Two Detached Rectangles in DirectX using the D3DPT_TRIANGLESTRIP Primitive Type看过这件事。问题是当我从Visual Studios" Performance Profiler"检查这个程序的内存消耗时。它从26MB开始,开始逐个增长,如27,28,29,30 ....... 如果我删除DrawPrimitive函数,Visual Studio中的内存消耗将保持不变。我不知道我做错了什么?当屏幕上绘制多个三角形和矩形时,内存消耗是否固定? 我的代码是:

void render_frame()
{
    //ourVertices conntain multiple rectangle
    LPDIRECT3DVERTEXBUFFER9 v_buffer;
    d3ddev->CreateVertexBuffer(numberofrectangles * sizeof(ourVertices),
        0,
        CUSTOMFVF,
        D3DPOOL_MANAGED,
        &v_buffer,
        NULL);
    VOID* pVoid;    // the void* we were talking about

    v_buffer->Lock(0, 0, (void**)&pVoid, 0);    // locks v_buffer, the buffer we made earlier
    memcpy(pVoid, ourVertices, numberofrectangles * sizeof(ourVertices));
    v_buffer->Unlock();

    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->BeginScene();

    // select which vertex format we are using
    // select the vertex buffer to display
    d3ddev->SetFVF(CUSTOMFVF);
    d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(Point));
    for (int i = 0; i< size_of_rectangles_list; i++) {
        // copy the vertex buffer to the back buffer
        d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, i * 4, 2); //here is the problem
    }

    d3ddev->EndScene();

    d3ddev->Present(NULL, NULL, NULL, NULL);

}

1 个答案:

答案 0 :(得分:0)

正如@Retired Ninja在评论中所说,每次拨打Release都需要CreateVertexBuffer

您可以通过在渲染循环中调用v_buffer->Release()(不推荐),或者在循环外创建并重用顶点缓冲区来完成此操作。