DX Learning Notes (Initializing direct 3D)

Source: Internet
Author: User
Tags comments win32 timedelta

The first is the header file, where the polygon defines 3 functions, namely the initialization function Initd3d (), the message loop function Entermsgloop, the window procedure function WndProc ()

The remaining release () Delete () is defined as a template function.

Release () to facilitate the release of the interface, and set to null, and delete () is to facilitate the deletion, and empty the pointer.

A lot of comments (after all, the weak is Win32 novice, many concepts have to rely on the online Baidu to understand)

D3dUtility.h

#ifndef __d3dutilityh__
#define __D3DUTILITYH__

#include <d3dx9.h>
#include <string>

Namespace D3D
{
	bool Initd3d (
		hinstance hinstance,//Handle of current application instance
		int width, int height,//Background cache surface width and height
		bool windowed,//whether windowing 
		D3ddevtype DeviceType,//D3D device type  hardware (HAL) or reference (REF) or software (SW)
		idirect3ddevice9** device);//To create devices (for output)

	int Entermsgloop (
		bool (*ptr_display) (float timedelta));

	LRESULT CALLBACK WndProc (
		hwnd hwnd,
		UINT msg,
		WPARAM WPARAM,
		LPARAM LPARAM);

	Template<class t>void Release (T t)
	{
		if (t)
		{
			t->release ();
			t = 0;
		}
	}
	Template<class t>void Delete (T-t)
	{
		if (t)
		{
			delete T; 
			t = 0;
}}} #endif//__d3dutilityh__

D3dUtility.cpp

Four steps to implement D3D initialization.

Step 1: Create the Idirect3d9 object
Step 2: Verify the hardware vertex calculation
Step 3: Populate the initial value of the D3DPRESENT_PARAMETERS structure
Step 4: Create the device (in addition to implementing the program's message loop, Entermsgloop (the rest of the notes supplement a lot I won't say much)

#include "d3dUtility.h" bool D3d::initd3d (hinstance hinstance, int width, int height, bool windowed, D3ddevtype Devicetyp

	E, IDirect3DDevice9 **device) {wndclass WC; Wc.style = Cs_hredraw | cs_vredraw;//window type (when horizontal length, vertical length, or position change, redraw entire window) Wc.lpfnwndproc = (WNDPROC) d3d::wndproc;//window processing function Wc.cbclsextra = 0;// Window class no extension Wc.cbwndextra = 0;//window instance no extension wc.hinstance = hinstance;//instance Handle Wc.hicon = LoadIcon (0, idi_application);//Window minimized The icon is the default icon wc.hcursor = LoadCursor (0, Idc_arrow);//window with arrow cursor Wc.hbrbackground = (hbrush) getstockobject (White_brush);//Window The background color is white wc.lpszmenuname = 0;//window no menu wc.lpszclassname = "Direct3d9app"; Window class name if (!
		RegisterClass (&AMP;WC)//If registration fails, issue a warning {:: MessageBox (0, "registerclass ()-falied", 0, 0);
		/** MessageBox (HWND hwnd,lpctstr lptext,lpctstr lpcaption,uint utype);
	HWnd: A message box has a window Lptext: The contents of a message box lpcaption: The caption of a message box Utype: A button in the specified flag to display a message box and an icon (default, only one confirmation button) **/return false;
	} hwnd hwnd = 0; hwnd =:: CreateWindow ("Direct3d9app", "Direct3D9app ", ws_ex_topmost,//Settings window style, set the window to the top 0,0,width,height, 0/* parent window */, 0/* menu */, hinstance,0/* extension */);
		if (!hwnd) {:: MessageBox (0, "CreateWindow ()-falied", 0, 0);
	return false;
	}:: ShowWindow (hwnd, sw_show);

	:: UpdateWindow (HWND);

	HRESULT hr = 0;
	Step 1: Create the Idirect3d9 object Idirect3d9 *d3d9 = 0;

	D3d9 = Direct3dcreate9 (d3d_sdk_version);
		if (!d3d9) {:: MessageBox (0, "direct3dcreate9 ()-FAILED", 0, 0);
	return false;
	}//Step 2: Verify the hardware vertex calculation D3DCAPS9 caps;
	D3d9->getdevicecaps (D3dadapter_default, DeviceType, &caps);
	Caps will return information that is loaded with the current graphics device capability int VP = 0; if (Caps.
	Devcaps & d3ddevcaps_hwtransformandlight) VP = d3dcreate_hardware_vertexprocessing;

	else VP = d3dcreate_software_vertexprocessing;
	Step 3: Populate the initial value of the d3dpresent_parameters structure d3dpresent_parameters D3DPP; D3DPP.
	Backbufferwidth = width; D3DPP.
	Backbufferheight = height; D3DPP.
	Backbufferformat = D3DFMT_A8R8G8B8; D3DPP.
	BackBufferCount = 1; D3DPP.
	multisamplequality = 0; D3DPP. MultiSampleType = D3dmulTisample_none; D3DPP.
	SwapEffect = D3dswapeffect_discard;
	D3dpp.hdevicewindow = hwnd; D3DPP.
	windowed = windowed; D3DPP. EnableAutoDepthStencil = true;//If set to True, D3D automatically creates and maintains a deep cache or template cache d3dpp. Autodepthstencilformat = D3DFMT_D24S8;//D24S8 uses 24 bits to represent the depth and retains 8 bits for the template to use D3DPP.
	Flags = 0; D3DPP. FullScreen_RefreshRateInHz = d3dpresent_rate_default;//refresh rate is set to the default value of D3DPP. Presentationinterval = d3dpresent_interval_immediate;//When the background cache in the swap chain switches to the foreground, the maximum rate is set to commit now//Step 4: Create device hr = d3d9->created		   Evice (d3dadapter_default,//Master Adapter DeviceType, hwnd, VP, &AMP;D3DPP, Device); Returns the created device if (FAILED (HR)) {D3DPP.

		Autodepthstencilformat = D3dfmt_d16;

		hr = D3d9->createdevice (D3dadapter_default, DeviceType, hwnd, VP, &AMP;D3DPP, Device);
			if (FAILED (HR)) {d3d9->release ();
			:: MessageBox (0, "createdevice-failed", 0, 0);
		return false;

	}} d3d9->release (); 
return true;
	} int D3d::entermsgloop (bool (*ptr_display) (float Timedelta)) {MSG msg; :: ZeroMemory (&msg, SizeoF (MSG));

	static float Lasttime = (float) timegettime ();
			while (msg.message! = wm_quit) {if (::P eekmessage (&msg, 0, 0, 0, Pm_remove)) {:: TranslateMessage (&AMP;MSG);
			::D ispatchmessage (&AMP;MSG);//Send Message} else {float currtime = (float) timegettime ();

			float Timedelta = (currtime-lasttime) *0.001f;

			Ptr_display (Timedelta);
		Lasttime = Currtime;
	

 }} return msg.wparam;//from a message indicating exit}

D3dInit.cpp

The display () function is implemented primarily here. The main implementation details are added in the comments, the only thing that needs to be emphasized--to be aware of whether you are creating a console or Win32 application. If the console needs to be converted into a Win32 application

#include "d3dUtility.h" idirect3ddevice9* Device = 0;

BOOL Setup () {return true;} void Cleanup () {} bool Display (float Timedelta) {if (Device) {device->clear (0, 0, d3dclear_target |
		D3dclear_zbuffer, 0x00000000, 1.0f, 0);
		/** Clear (DWORD count,const d3drect *prects,dword Flags, DWORD Color, float Z, DWORD stencil); The number of rectangles in the Count prect array prect The array of screen rectangles to perform the cleanup operation.
	        This parameter allows us to clear only some areas of the surface.
			FLAGS specifies the surface to be cleared. We can clear one or more of the following surfaces. 
		D3dclear_target draws the target surface, usually referred to as the background cache d3dclear_zbuffer depth Cache d3dclear_strncil Template Cache color Specifies the value to be set for how the target body is set for the color Z-depth cache
Stencil template Cache To set the value **/device->present (0, 0, 0, 0);//submit Background Cache} return true;
		} LRESULT CALLBACK D3d::wndproc (HWND hwnd, UINT MSG, WPARAM WPARAM, LPARAM LPARAM) {switch (msg) {case Wm_destroy:
		::P ostquitmessage (0);
	Break
		Case Wm_keydown:if (WParam = = Vk_escape)::D Estroywindow (HWND);
	Break
} return::D Efwindowproc (hwnd, MSG, WParam, LParam); } int WINAPI WinMain (hinstance hinstance, HInstance PrevInstance, PSTR cmdline, int showcmd) {if (!d3d::initd3d, HINSTANCE, 640, True, 480, D3ddevtype_hal
		Evice)) {:: MessageBox (0, "initd3d ()-FAILED", 0, 0);
	return 0; } if (!
		Setup ()) {:: MessageBox (0, "setup ()-FAILED", 0, 0);
	return 0;

	} d3d::entermsgloop (Display);

	Cleanup ();

	Device->release ();
return 0; }




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.