🎉 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!

Pong and Direct3D

Started by
7 comments, last by Tom Sloper 2 years, 2 months ago

I am learning DX9 and C++. I am making a pong game using Direct3D. I am trying to move the player paddle using the move_player variable in WMKEYDOWN and wParam==VKUP. I think I am having a scoping problem. Here is some of my code.

float move_player = 0.0f;

// 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_player -= 50.0f;
			break;
		}
		else if (wParam == VK_DOWN)
		{
			move_player++;
			break;
		}
		break;
	}
	break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

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), },
		{ 30.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), },
		{ 30.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
	
		{ 770.0f, 250.0f + move_player, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
		{ 800.0f, 250.0f + move_player, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
		{ 770.0f, 350.0f + move_player, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
		{ 800.0f, 350.0f + move_player, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },

		{ 390.0f, 290.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
		{ 410.0f, 290.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
		{ 390.0f, 310.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
		{ 410.0f, 310.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },

	};

	// create a vertex buffer interface called v_buffer
	d3ddev->CreateVertexBuffer(12 * 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
if (wParam == VK_UP)
		{
			move_player -= 50.0f;
			break;
		}
		else if (wParam == VK_DOWN)
		{
			move_player++;
			break;
		}

Updating positions from message handling is tightly coupled but correct. However, in this case:

  • If “up” moves the paddle 50 units and “down” 1 unit there's probably a mistake.
  • You don't use move_player at all after updating its value.

Omae Wa Mou Shindeiru

He states that he thinks the “problem” is related to scoping but never says what the problem is, other than a vague reference to moving the paddle.

I guess people will start taking turns figuring out a solution to the unstated problem.

Maybe we can keep bumping this with “solutions” every time it scrolls off the front page.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

It's Pong so there's no scoping issue. Maybe Lorenzo's comment will help Phil with the real problem.

-- Tom Sloper -- sloperama.com

Tom Sloper said:

It's Pong so there's no scoping issue. Maybe Lorenzo's comment will help Phil with the real problem.

What is the real problem Tom?

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

fleabay said:

What is the real problem Tom?

Unnecessary commentary. Let's see how it goes.

-- Tom Sloper -- sloperama.com

@LorenzoGatti can you go into more depth on your answer?

And we're done. Lorenzo's answer does not need explaining.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement