In the previous chapter we learned how to build the DirectX development environment in C++win32 project, then write code to initialize DirectX bar O (∩_∩) o~. First you create a WIN32 program, click Run you can see a window Windows program, I created the project called Initdirectx, such as
First configure the DirectX development environment, and then click Enter InitDirectX.cpp to write the initialization code.
I. Introduction of header File D3d9.h
#include <d3d9.h>
Second, define the interface object of DirectX, device object
Lpdirect3d9 g_pd3d = NULL; // defining D3d9 Interface Objects Lpdirect3ddevice9 g_pd3ddevice = NULL; // defining D3D9 Interface Device pointers
Third, write initialize DirectX function initd3d
//Initialize the Direct3D methodHRESULT INITD3D (hwnd hwnd) {//Creating a DirectX Interface object if(NULL = = (G_pd3d =Direct3dcreate9 (d3d_sdk_version))) { returnE_fail; } //Empty Memoryd3dpresent_parameters D3DPP; ZeroMemory (&D3DPP,sizeof(D3DPP)); D3DPP. Windowed= TRUE;//true window display mode, false full screen display modeD3DPP. SwapEffect = D3dswapeffect_discard;//specifies that the contents of the back buffer are copied to the foreground cache and are generally set to D3dswapeffect_discardD3DPP. Backbufferformat = D3dfmt_unknown;//set the Save pixel format for the back buffer//Create a device if(FAILED (g_pd3d->CreateDevice (D3dadapter_default, D3ddevtype_hal, HWnd, d3dcreate_software_vertexprocessing,&D3DPP, &g_pd3ddevice))) { returnE_fail; } returnS_OK;}
Iv. writing whether DirectX functions
// Release DirectX VOID Cleanup () { if(g_pd3ddevice! = NULL) { g_pd3ddevice-N-Release (); } if (G_pd3d! = NULL) { g_pd3d, Release ();} }
Write the render main function, and draw everything in this function later
//RenderingVOID Render () {if(NULL = =g_pd3ddevice) { return; } //clear the scene with a colorG_pd3ddevice->clear (0, Null,d3dclear_target,d3dcolor_xrgb (255,168,165),1.0f,0); if(SUCCEEDED (g_pd3ddevice->BeginScene ())) { //Start writing the rendering section and draw the object hereg_pd3ddevice-EndScene (); } g_pd3ddevice-Present (null,null,null,null);}
VI. Add the initialization initd3d function to the InitInstance function and pass in the window handle
Join in window processing function, render render function, Cleanup release function
=.= then run the program you can see the window is a piece of yellow, I removed the window's menu bar (wcex.lpszmenuname = NULL;)
=.= goodbye
DirectX Initialize DirectX (first way)