🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

dx9 pong

Started by
6 comments, last by Tom Sloper 1 year, 5 months ago

I am trying to build a pong game using dx9. I am able to draw a paddle on the left side of the screen. I want to draw a paddle on the right side of the screen. I am very new at dx9.

void render_frame(void)
{
	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

	d3ddev->BeginScene();

	// select which vertex format we are using
	d3ddev->SetFVF(CUSTOMFVF);

	// 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_TRIANGLELIST, 0, 2);

	d3ddev->EndScene();

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


// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
	v_buffer->Release();    // close and release the vertex buffer
	d3ddev->Release();    // close and release the 3D device
	d3d->Release();    // close and release Direct3D
}

// this is the function that puts the 3D models into video RAM
void init_graphics(void)
{
		// create the vertices using the CUSTOMVERTEX struct
		CUSTOMVERTEX vertices[] =
		{
			{ 0.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 25.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 0.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 25.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 25.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 0.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
		};
	// create a vertex buffer interface called v_buffer
	d3ddev->CreateVertexBuffer(6 * sizeof(CUSTOMVERTEX),
		0,
		CUSTOMFVF,
		D3DPOOL_MANAGED,
		&v_buffer,
		NULL);

	VOID* pVoid;    // a void pointer

	// lock v_buffer and load the vertices into it
	v_buffer->Lock(0, 0, (void**)&pVoid, 0);
	memcpy(pVoid, vertices, sizeof(vertices));
	v_buffer->Unlock();
}
  
Advertisement

well, I was able to draw two paddles like I asked above.

pbivens67 said:
like I asked above.

There was no question.

-- Tom Sloper -- sloperama.com

well, I have drawn two paddles and one ball, however I am unsure of how to move the paddles, here is the code I am using to render the paddles and the keyboard input.

int move_paddle = 0;

// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_DESTROY:
	{
		PostQuitMessage(0);
		return 0;
	} 
	case WM_KEYDOWN:
	{
		if (wParam == VK_UP)
		{
			move_paddle--;
			ValidateRect(hWnd, NULL);
			return 0;
		}
		if (wParam == VK_DOWN)
		{
			move_paddle++;
			ValidateRect(hWnd, NULL);
			return 0;
		}
		break;
	}
	break;
	}

	return DefWindowProc(hWnd, message, wParam, lParam);
}
void init_graphics(void)
{
		// create the vertices using the CUSTOMVERTEX struct
		CUSTOMVERTEX vertices[] =
		{
			{ 775.0f, 250.0f + move_paddle, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
			{ 800.0f, 250.0f + move_paddle, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
			{ 775.0f, 350.0f + move_paddle, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
			{ 800.0f, 250.0f + move_paddle, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
			{ 800.0f, 350.0f + move_paddle, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
			{ 775.0f, 350.0f + move_paddle, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },

			{ 0.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 25.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 0.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 25.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 25.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 0.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },

			{ 395.0f, 295.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
			{ 405.0f, 295.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
			{ 395.0f, 305.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
			{ 405.0f, 295.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
			{ 405.0f, 305.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
			{ 395.0f, 305.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
		};
void init_graphics(void)
{
		// create the vertices using the CUSTOMVERTEX struct
		CUSTOMVERTEX vertices[] =
		{
			{ 775.0f, 250.0f + move_paddle, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
			{ 800.0f, 250.0f + move_paddle, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
			{ 775.0f, 350.0f + move_paddle, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
			{ 800.0f, 250.0f + move_paddle, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
			{ 800.0f, 350.0f + move_paddle, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
			{ 775.0f, 350.0f + move_paddle, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },

			{ 0.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 25.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 0.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 25.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 25.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
			{ 0.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },

			{ 395.0f, 295.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
			{ 405.0f, 295.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
			{ 395.0f, 305.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
			{ 405.0f, 295.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
			{ 405.0f, 305.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
			{ 395.0f, 305.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
		};

Just in case you missed it, DX9 is more than 20 years old, was superceded in 2006, and passed its official “end of life” over a decade ago. If you use it, much of the functionality needs to be emulated on modern cards because it is so far out of date.

Most of the tools are out of support as well.

But if it works for you, go for it I guess.

I am using at a platform before I learn dx11.-

Thread locked because two more threads were created by the OP on the same topic.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement