DirectX Dynamic Vertex Buffer

Hey guys, I have a question on DirectX. I've read multiple guides and.. well I'm not quite the fastest learner but anyhow I can't seem to get a direct answer as to how I would go about setting up a dynamic vertex buffer.

I'm designing a 2D platform game, each tile is 2 triangles or 6 vertices.

If players were to spawn, that would cause the amount of vertices on-screen to increase by another 6.

The dilemma I'm running into is more of a design issue. Where am I to store these new vertices?

Also, is it a good idea to house all of the vertices in a giant array and feed it in the vertex buffer once or to seperate vertex buffers every X amount of vertices?

My current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

void Engine::OnCreate(HWND hWnd) {
	OwnerHandle = hWnd;
  // A custom class, takes X, Y, WIDTH, HEIGHT, Z, RHW, COLOR parameters. It creates appropriate vertex information based off those parameters.
	TEST_BOX.Initialize(100, 100, 1400, 1400, 1, 0.5, 0xff00ffff);
	DX9Interface = Direct3DCreate9(D3D_SDK_VERSION);
	D3DPRESENT_PARAMETERS DX9Params;
	ZeroMemory(&DX9Params, sizeof(DX9Params));
	DX9Params.Windowed = true;
	DX9Params.BackBufferWidth = 1920;
	DX9Params.BackBufferHeight = 1080;
	DX9Params.SwapEffect = D3DSWAPEFFECT_DISCARD;
	DX9Params.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; // VSYNC OFF = D3DPRESENT_INTERVAL_IMMEDIATE, VSYNC ON = D3DPRESENT_INTERVAL_DEFAULT
	DX9Params.hDeviceWindow = OwnerHandle;
	DX9Params.MultiSampleType = D3DMULTISAMPLE_8_SAMPLES;
	if (FAILED(DX9Interface->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, OwnerHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &DX9Params, &DX9Device))) {
		MessageBox(NULL, L"Failed to initialize DirectX device.", L"DirectX", MB_OK | MB_ICONERROR);
		exit(2);
	}
	DX9Device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, true);

	LPDIRECT3DVERTEXBUFFER9 g_pVB;

	DX9Device->CreateVertexBuffer(3* sizeof(TEST_BOX.Vertices), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL);

	VOID* pVertices;
	g_pVB->Lock( 0, sizeof(TEST_BOX.Vertices), (void**)&pVertices, 0);

	memcpy(pVertices, TEST_BOX.Vertices, sizeof(TEST_BOX.Vertices));
	DX9Device->SetStreamSource(0, g_pVB, 0, sizeof(UI::Vertex));
	DX9Device->SetFVF(D3DFVF_CUSTOMVERTEX);

	g_pVB->Unlock();
	g_pVB->Release();
	Initialized = true;
}


My actual drawing code:

1
2
3
4
5
6
7
void Engine::OnDraw(HWND hWnd) {
	DX9Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
	DX9Device->BeginScene();
	DX9Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
	DX9Device->EndScene();
	DX9Device->Present(NULL, NULL, NULL, NULL);
}


Edit: Should mention, this is DirectX 9 code. Reason I decided not to go with DirectX 10 or 11 was because it's a 2D platform and not everyone has DX10 or DX11 cards.

Any help would be greatly appreciated. :)

Mark.
Last edited on
Topic archived. No new replies allowed.