DirectX Game Programming Game framework

Source: Internet
Author: User
Tags bool

Follow the previous two articles to write the game framework code for DirectX

The beginning of DirectX game programming

DirectX Game Programming 3D space, vertex caching and Index caching

Development environment: VS2012, DirectX SDK (June), C + +


External interface:

BOOL Directcdemo (hinstance hinstance, Renderfunc renderfunc);

Where Renderfunc is the drawing function, defined as

typedef void (*RENDERFUNC) ();
To draw a tetrahedron that is shaded by vertices, the code can be as follows

int WINAPI WinMain (hinstance hinstance,
	hinstance hprevinstance,
	LPSTR lpcmdline,
	int ncmdshow)
{
	Directcdemo (hinstance, Rendervertex);
	return 0;
}

By looking at the vertex data in the drawing function, the definition of the index data and the drawing process can be a glimpse of our encapsulation code, the drawing process is simple as follows:

0 Create window, initialize Direct3D.

The following, provides a complete initialization of the Direct3D process

BOOL Gamewnd::initdirect3d (HWND hwnd, BOOL fullscreen) {//Step 1:create D3ddirect3d D3ddirect3d = Direct3dcreate9 (D3D
	_sdk_version);

	D3dpresent_parameters D3DPP;
	Step 2:set d3dpresent_parameters zeromemory (&D3DPP, sizeof (D3DPP)); if (Ismulbackbuffer) d3dpp.
	BackBufferCount = 2; Else D3DPP.
	BackBufferCount = 1; Enable depth and stencil if (Iszbuffer) d3dpp.
	EnableAutoDepthStencil = TRUE; Else D3DPP.

	EnableAutoDepthStencil = FALSE; D3DPP.

	Presentationinterval = d3dpresent_interval_immediate; if (fullscreen) {d3dpp.
		Backbufferwidth = window_width; D3DPP.
		Backbufferheight = window_height; if (color_format_default==32) d3dpp.
		Backbufferformat = D3DFMT_X8R8G8B8; Else D3DPP.
		Backbufferformat = d3dfmt_r5g6b5; D3DPP.
		SwapEffect = D3dswapeffect_flip; D3DPP.
		windowed = FALSE; D3DPP.
	FullScreen_RefreshRateInHz = D3dpresent_rate_default; } else {d3dpp.
		windowed = TRUE;
		Always the default display mode in + bits. D3DPP. Backbufferformat = d3dfmt_x8r8G8B8; D3DPP.
	SwapEffect = D3dswapeffect_discard;

	} d3ddevtype devtype = D3ddevtype_hal; Set depth and stencil format if (Isstencilbuffer && iszbuffer) {d3dpp.

		Autodepthstencilformat = D3DFMT_D24S8; if (FAILED (D3ddirect3d->checkdeviceformat (D3dadapter_default,devtype, D3DPP. Backbufferformat, D3dusage_depthstencil, D3drtype_surface, D3DPP. Autodepthstencilformat)) {D3DPP.

			Autodepthstencilformat = D3DFMT_D24X4S4; if (FAILED (D3ddirect3d->checkdeviceformat (D3dadapter_default,devtype, D3DPP. Backbufferformat, D3dusage_depthstencil, D3drtype_surface, D3DPP. Autodepthstencilformat)) {//Device does not support Stencilbuffer}}}//Does not use stencil buffer els E if (!isstencilbuffer && iszbuffer) {d3dpp.

		Autodepthstencilformat = D3DFMT_D32; if (FAILED (D3ddirect3d->checkdeviceformat (D3dadapter_default,devtype, D3DPP. Backbufferformat, D3dusage_depthstencil, D3drtype_surface, D3DPP. Autodepthstencilformat)) {D3DPP.
			Autodepthstencilformat = d3dfmt_d24x8; if (FAILED (D3ddirect3d->checkdeviceformat (D3dadapter_default,devtype, D3DPP. Backbufferformat, D3dusage_depthstencil, D3drtype_surface, D3DPP.
			Autodepthstencilformat)) {//Device does not support required depth buffer return false; }}}//Step 3:create D3ddirect3d device HRESULT hr = D3ddirect3d->createdevice (D3dadapter_default, Devtype, H

	WND, D3dcreate_hardware_vertexprocessing, &D3DPP, &d3ddevice); if (FAILED (hr)) hr = D3ddirect3d->createdevice (D3dadapter_default, Devtype, hwnd, d3dcreate_mixed_vertexprocessing

	, &D3DPP, &d3ddevice); if (FAILED (hr)) hr = D3ddirect3d->createdevice (D3dadapter_default, Devtype, hwnd, d3dcreate_software_vertexprocess

	ING, &D3DPP, &d3ddevice); if (!
		D3DDevice) {//was wasn't able to create Direct3D9 device.
	return false;

	} d3ddevice->getdevicecaps (&d3dcaps);
return true;
 }

Some points to note:

If automatic deep cache is turned on, template cache management must be formatted with a deep cache, template cache, or the device failed to be created

D3DPP. EnableAutoDepthStencil

D3DPP. Autodepthstencilformat = D3DFMT_D24S8;  Set both the depth cache and the template cache bit number
D3DPP. Autodepthstencilformat = d3dfmt_d24x8;  Only deep cache is turned on

1 defining vertex data, we already know that we can use the flexible vertex format (flexible Vertex FORMAT,FVF) to customize the vertex format, and our standard vertex format is defined as

d3dfvf_xyz | D3dfvf_normal | D3dfvf_diffuse | D3dfvf_tex1
Structure is

struct Vertex
{
	Vertex () {}
	Vertex (float x, float y, float z,
		float nx, float NY, float NZ,
		Color c,
  float u, float v): position (x, y, z), Normal (NX,NY,NZ), color (c), Texcoords (u,v)
	{
	}
	position3d position;
	position3d Normal;
	Color color;
	position2d texcoords;
};
2 The index is defined by vertex data, which is actually a short index array, the type of index data to be specified at the time of drawing

3 Setting the camera's projection (project), view matrix

4 render the material, which will set up many of the parameters of DirectX, such as switching lighting, drawing grids, depth testing, template testing, etc.

5 draws vertices, sets the vertex format, and provides a simpler vertex-drawing function Drawindexedprimitiveup

HRESULT Drawindexedprimitiveup (
  [in]       d3dprimitivetype primitivetype,   //Basic primitive type, base triangle list D3dpt_ Trianglelist
  [in]       uint             minvertexindex,  //minimum vertex index, typically 0
  [in]       uint             due to the use of 0-based vertex index Numvertices,   //number of vertices, not number of indexes, 4 (tetrahedron) [in
  ]       UINT             primitivecount,  //number of entities, Triangle
  number in Demo [in] const void             *pindexdata,  //Index data
  [in]       d3dformat        Indexdataformat,  // Index data type short is a
  [in] const void             *pvertexstreamzerodata,  //Vertex data [in
  ]       UINT             Vertexstreamzerostride  //Vertex structure size sizeof (VERTEX)
);


Code Address Git:git@code.csdn.net:hrlnldy/directxdemo.git

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.