DirectX installation and VS2010 testing based on the programming basis

Source: Internet
Author: User
Tags win32

C + + An important application is game development, and game development will have to talk about DirectX, a lot of knowledge about DirectX I have not this qualification to talk about, just started to learn, the first lesson is the same as most new things environment installation configuration.

Learn DirectX game development of course to install is the DirectX SDK, the download of the DX SDK address: http://www.microsoft.com/download/en/details.aspx?displaylang=en &id=6812 no money the latest is June 2010 version, I installed February 2010 version, and then is an IDE, always use VS, so of course preferred VS2010 slightly.

As for the installation there is no majority, after the installation is configured.

1. First copy the files from the Lib and include directories in the DirectX SDK installation directory to the LIB and include directories in the VS2010 installation directory;

2. Open VS2010 Create a new win32--> Win32 project, and then in the application settings, attach the option set to "Empty Project" as shown in figure:

After you create a project, add a class to paste the code:

#pragma comment (lib, "D3d9.lib") #pragma comment (lib, "D3dx9.lib") #pragma comment (lib, "Winmm.lib") #include <
windows.h> #include <d3dx9.h> #include <MMSystem.h> lpdirect3d9 g_pd3d;
Lpdirect3ddevice9 g_pd3ddevice;
Lpdirect3dvertexbuffer9 G_PVB;    
	struct customvertex{FLOAT x, y, Z;
DWORD color;
}; #define D3DFVF_CUSTOMVERTEX (d3dfvf_xyz| D3dfvf_diffuse) HRESULT Initobject () {CustomVertex triangle[] = {{ -1.0f,-1.0f, 0.0f, 0xffff0000,}    
	, {1.0f,-1.0f, 0.0f, 0XFF0000FF,}, {0.0f, 1.0f, 0.0f, 0xFFFFFFFF,}}; if (FAILED (G_pd3ddevice->createvertexbuffer (3*sizeof (CustomVertex), 0, D3dfvf_customvertex, D3dpool_default,    
	&AMP;G_PVB, NULL))) return E_FAIL;    
	void* pvertices;    
	if (FAILED (G_pvb->lock (0,sizeof (triangle), &pvertices, 0)) return E_FAIL;    
	memcpy (pvertices, triangle, sizeof (triangle));    
	G_pvb->unlock ();
return S_OK; } HRESULT Initd3d (hwnd hwnd) {G_pD3D = Direct3dcreate9 (d3d_sdk_version);    
	if (NULL = = G_pd3d) return e_fail;    
	D3dpresent_parameters D3DPP;    
	ZeroMemory (&AMP;D3DPP, sizeof (D3DPP)); D3DPP.    
	windowed = TRUE; D3DPP.    
	SwapEffect = D3dswapeffect_discard; D3DPP.    
	Backbufferformat = D3dfmt_unknown; if (FAILED (G_pd3d->createdevice (D3dadapter_default, D3ddevtype_hal, HWnd, D3        
		Dcreate_software_vertexprocessing, &AMP;D3DPP, &g_pd3ddevice))) {    
	return E_FAIL;    
	} g_pd3ddevice->setrenderstate (D3drs_cullmode, D3dcull_none); Turn off D3D Lighting, since we is providing our own vertex colors g_pd3ddevice->setrenderstate (d3drs_lightin    
	G, FALSE);    
	if (FAILED (Initobject ())) return E_FAIL;
return S_OK;    
	} void Setupmatrices () {D3dxmatrix matworld;    
	UINT itime = timegettime ()% 1000;    
FLOAT Fangle = itime * (2.0f * d3dx_pi)/1000.0f;	D3dxmatrixrotationy (&matworld, Fangle);    
	G_pd3ddevice->settransform (D3dts_world, &matworld);    
	D3dxvector3 veyept (0.0f, 3.0f, -5.0f);    
	D3dxvector3 vlookatpt (0.0f, 0.0f, 0.0f);    
	D3dxvector3 vUp (0.0f, 1.0f, 0.0f);    
	D3dxmatrixa16 Matview;    
	D3DXMATRIXLOOKATLH (&matview, &veyept, &AMP;VLOOKATPT, &vup);    
	G_pd3ddevice->settransform (D3dts_view, &matview);    
	D3dxmatrix Matpoj;    
	D3DXMATRIXPERSPECTIVEFOVLH (&matpoj, D3dx_pi/4, 1.0f, 1.0f, 100.0f);
G_pd3ddevice->settransform (D3dts_projection, &AMP;MATPOJ);    
	} void Render () {g_pd3ddevice->clear (0,null, D3dclear_target, D3dcolor_xrgb (0, 0, 255), 1.0f, 0);    
	G_pd3ddevice->beginscene ();    
	Setupmatrices ();    
	G_pd3ddevice->setstreamsource (0, G_PVB, 0, sizeof (CustomVertex));    
	G_PD3DDEVICE-&GT;SETFVF (D3dfvf_customvertex);    
	G_pd3ddevice->drawprimitive (d3dpt_trianglelist, 0, 1);    
	G_pd3ddevice->endscene (); G_pd3ddevice->present (NULL, NULL, NULL, NULL);    
	} void Cleanup () {if (g_pd3ddevice) g_pd3ddevice->release ();    
	if (G_PD3D) g_pd3d->release ();
if (G_PVB) g_pvb->release (); } LRESULT WINAPI Msgproc (HWND hwnd, UINT MSG, WPARAM WPARAM, LPARAM LPARAM) {switch (msg) {case Wm_destro        
		Y:postquitmessage (0);    
	return 0;        
		Case Wm_paint:validaterect (HWnd, NULL);   
	return 0;
} return DefWindowProc (HWnd, MSG, WParam, LParam); } int WINAPI WinMain (__in hinstance hinstance, __in_opt hinstance hprevinstance, __in_opt LPSTR lpcmdline, __in INT NSHOWC MD) {Wndclassex WC = {sizeof (wndclassex), CS_CLASSDC, Msgproc, 0, 0, getmodulehandle (null), NULL, NULL, NULL, NU    
	LL, L "Direct3D", NULL};    
	RegisterClassEx (&AMP;WC); Create the application window hwnd hwnd = CreateWindow (L "Direct3D", L "learn", Ws_overlappedwindow, 10 0, GetDesktopWindow (), NULL, Wc.hinstance, NULL);    
	ShowWindow (HWnd, sw_show);        
		if (SUCCEEDED (Initd3d (HWND))) {ShowWindow (hwnd, Sw_showdefault);       
		UpdateWindow (HWND);        
		MSG msg;    
		ZeroMemory (&msg, sizeof (msg));           
				while (msg.message! = wm_quit) {if (PeekMessage (&msg, NULL, 0, 0, pm_remove)) {     
				TranslateMessage (&AMP;MSG);      
			DispatchMessage (&AMP;MSG);       
		} else Render ();    
	}} unregisterclass (L "Direct3D", Wc.hinstance);
	Cleanup ();
return nshowcmd; }

Well, run the following result:


This is successful, the code is learning later, this method eliminates the click Open link http://www.cnblogs.com/lovestudy/archive/2012/04/26/2471228.html in the method mentioned in many of the steps, And instead of having to create a new project every time, you'll need to add the following code to each new project:

#pragma comment (lib, "D3d9.lib")
#pragma comment (lib, "D3dx9.lib")
#pragma comment (lib, "Winmm.lib")

Can complete the configuration, just start very simple, continue to refuel it.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.