Skip to main content
GameDev.net gamedev.net
🔒 Locked

more directx9 tutorial

Started by pbivens67 Mar 18, 2021 at 9:42 PM 10 replies 6.7k views
Original Post
pbivens67
pbivens67

I am still working on the exercises in a direct x9 tutorial. I am trying to get a triangle to fade the colors at the vertices. sorry for all the handholding but I am very new at dx9, I am still reading my tutorial. here is the code I am using.

void initD3D(HWND hWnd)
{
	d3d = Direct3DCreate9(D3D_SDK_VERSION);

	D3DPRESENT_PARAMETERS d3dpp;

	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.hDeviceWindow = hWnd;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferWidth = SCREEN_WIDTH;
	d3dpp.BackBufferHeight = SCREEN_HEIGHT;

	// create a device class using this information and the info from the d3dpp stuct
	d3d->CreateDevice(D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp,
		&d3ddev);

	for (int i = 0; i < 256; i++)
	{
	init_graphics();    // call the function to initialize the triangle
	}
}

int x = 0, count = 0;

// this is the function used to render a single frame
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, 1);

	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[] =
		{
			{ 400.0f, 62.5f, 0.5f, 1.0f, D3DCOLOR_XRGB(x++, 0, 0), },
			{ 650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, x++), },
			{ 150.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, x++, 0), },
		};
	// create a vertex buffer interface called v_buffer
	d3ddev->CreateVertexBuffer(3 * 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();
}
Tom Sloper
Tom Sloper

What happens when you run the code? Do the colors fade at the vertices or not?

-- Tom Sloper    --      sloperama.com
pbivens67
pbivens67

when I run my code it only draws the triangle but the colors are static

Tom Sloper
Tom Sloper

See, it helps if you tell people what your problem is, and what you need help with.

-- Tom Sloper    --      sloperama.com
fleabay
fleabay

@pbivens67 You can't run from them forever…

I just googled for this, the internet is awesome.

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

before I get into shaders I think I should learn the basics of direct x9 first. tom are you good at directx9?

Tom Sloper
Tom Sloper

@pbivens67 no, I am not a programmer. I'm a designer/producer. Don't ask me programming questions. I'm just moderating here.

-- Tom Sloper    --      sloperama.com
fleabay
fleabay

@pbivens67 I use OpenGL. I'm slowly learning Direct3D 11.

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

I am trying to get my paddle sprite to move but it won't move at all. Here is my keyboard input code.

//WindowProc - Handles input sent to the window.
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		} break;
	case WM_CLOSE:
		{
			PostQuitMessage(0);
			return 0;
		} break;
	case WM_KEYDOWN:
	{
		switch (wParam)
		{
		case VK_LEFT:
			move_x--;
			InvalidateRect(hWnd, NULL, FALSE);
			break;
		case VK_RIGHT:
			move_x++;
			InvalidateRect(hWnd, NULL, FALSE);
			break;
		} break;
	} break;

	}

	return DefWindowProc(hWnd, message, wParam, lParam);
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.