DirectX 9 C++ - Drawing a triangle

Started by
1 comment, last by murray732 14 years, 5 months ago
Hi, I'm currently learning the basics of DirectX 9 using the website www.directxtutorial.com. The link to the specific tutorial used is: http://www.directxtutorial.com/Tutorial9/B-Direct3DBasics/dx9B4.aspx#still I'm trying to create a basic screen that just displays a colorful triangle. My code differs from the tutorial's slightly by having large code chunks put into functions and some other modifications that make switching to fullscreen easier. I've looked over this code for hours now and just can't figure out where i've went wrong. The code compiles with 0 errors and 0 warnings, just doesn't show the triangle on the screen. Here's the code:

#include <windows.h>
#include <d3d9.h>

#pragma comment(lib, "d3d9.lib")

// Definitions
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define FULLSCREEN false
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

LONG APIENTRY WindowProc(HWND hWnd, UINT message, WPARAM
							wParam, LPARAM lParam);

//Function Prototypes
void initWindow(HINSTANCE hInstance);
HWND createWindow(HINSTANCE hInstance);
void initD3D(HWND hWnd);
void cleanD3D();
void render_frame();
void initGraphics();

//Global Variables
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;

// Structures
struct CUSTOMVERTEX {
	float x, y, z, RHW;
	DWORD color;
};

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
					LPSTR lpCmdLine, int nCmdShow)
{
	HWND hWnd;
	
	initWindow(hInstance);
	hWnd = createWindow(hInstance);
	ShowWindow(hWnd, nCmdShow);

	initD3D(hWnd);	

	MSG msg;

	while (true)
	{

		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		if (msg.message == WM_QUIT)
			break;

		render_frame();
	}

	return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message,
							WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_KEYDOWN:
				if (wParam == VK_ESCAPE)
					PostQuitMessage(0);
				break; 

		case WM_DESTROY:
				PostQuitMessage(0);
				break;

		default:
				return DefWindowProc(hWnd, message, wParam, lParam);
				break;
	}

	return 0;
}

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

	ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));

	if (FULLSCREEN)
		d3dpp.Windowed = false;
	else
		d3dpp.Windowed = true;
	d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
	d3dpp.hDeviceWindow = hWnd;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferWidth = SCREEN_WIDTH;
	d3dpp.BackBufferHeight = SCREEN_HEIGHT;

	d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
				D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);

	initGraphics();
}

void cleanD3D()
{
	v_buffer->Release();
	d3ddev->Release();
	d3d->Release();
}

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

	d3ddev->BeginScene();

	d3ddev->SetFVF(CUSTOMFVF);
	d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
	d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

	d3ddev->EndScene();

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

void initWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wc;

	ZeroMemory(&wc, sizeof(WNDCLASSEX));

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WindowProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	// wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wc.lpszClassName = L"WindowClass";

	RegisterClassEx(&wc);
}

HWND createWindow(HINSTANCE hInstance)
{
	HWND hWnd;
	DWORD style;
	int x, y;

	if (FULLSCREEN)
	{
		style = WS_EX_TOPMOST | WS_POPUP;
		x = 0, y = 0;
	}
	else
	{
		style = WS_OVERLAPPEDWINDOW;
		x = 50, y = 50;
	}

	hWnd = CreateWindowEx(NULL, L"WindowClass", L"Murray", style,
						x, y, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, 
						NULL, hInstance, NULL);

	return hWnd;
}
void initGraphics()
{
	CUSTOMVERTEX vertices[] = 
	{
		{400.0f, 50.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255)},
		{100.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0)},
		{700.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0)}
	};

	d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED,
							&v_buffer, NULL);

	VOID* pVoid;

	v_buffer->Lock(0, 0, &pVoid, 0);
	memcpy(pVoid, vertices, sizeof(vertices));
	v_buffer->Unlock();
}

Any feedback is extremely appreciated! Thanks, Murray Edit: Used CODE tags instead of SOURCE tags.
Advertisement
Back-face culling is turned on, and your triangle is facing away from the screen. Change the order of the vertices:
CUSTOMVERTEX vertices[] = {	{700.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0)},	{100.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0)},	{400.0f, 50.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255)}};
Ah, thank you that worked.

I didn't know about back-face culling. I thought that I could put the vertices in whatever order I wanted, heh.

Thanks again,

Murray

This topic is closed to new replies.

Advertisement