The simplest example of D3D
Last Update:2018-07-25
Source: Internet
Author: User
The simplest of D3D
Example //=============================================================================//DESC: Simplest Direct3D program,/// /must install D3D SDK///-> linker-> Input-> additional dependency option-> d3d9.lib//VC tools-> Option-> VC + + directory dropdown box Select library file with include file add DX in Clude and Lib directory//============================================================================= #include < d3d9.h> //-----------------------------------------------------------------------------/global variable//- ----------------------------------------------------------------------------lpdirect3d9 g_pd3d = NULL; Direct3D object lpdirect3ddevice9 g_pd3ddevice = NULL; Direct3D Device Objects //-----------------------------------------------------------------------------// Desc: Initialize Direct3D//-----------------------------------------------------------------------------HRESULT Initd3d ( HwnD hWnd) { //Create Direct3D object that is used to create Direct3D device Objects if (NULL = G_pd3d = Direct3 DCreate9 (d3d_sdk_version)) return e_fail; //Set up d3dpresent_parameters structure, ready to create Direct3D device object D3dpresent_ PARAMETERS D3DPP; ZeroMemory (&D3DPP, sizeof (D3DPP)); D3DPP. windowed = TRUE; D3DPP. SwapEffect = D3dswapeffect_discard; D3DPP. Backbufferformat = D3dfmt_unknown; //Create Direct3D Device Objects if FAILED (G_pd3d->createdevice (d3dadapter_ DEFAULT, D3ddevtype_hal, HWnd, d3dcreate_software_vertexprocessing, &D3DPP, &g_ Pd3ddevice)) { return e_fail; } &nbs P return S_OK; } //-----------------------------------------------------------------------------//DESC: Releasing created objects//--- --------------------------------------------------------------------------VOID Cleanup () { //Release Direct3D device Object if (g_pd3ddevice!= NULL) g_ Pd3ddevice->release (); //Release Direct3D Object if (g_pd3d!= NULL) & nbsp; g_pd3d->release (); } //-----------------------------------------------------------------------------//DESC: Rendering graphics//----- ------------------------------------------------------------------------VOID Render () { // Empty back buffers g_pd3ddevice->clear (0, NULL, D3dclear_target, D3dcolor_xrgb (), 170), 1.0f, 0); //Start drawing graphics in background buffers if (SUCCEEDED (G_pd3ddevice->beginscene) ) { //here in the background buffer draw graphics //end of rendering graphics g_pd3ddevice-> in the background buffer EndScene (); } //The graphics drawn in the background buffer will be submitted to the foreground buffer display g_pd3ddevice->present ( NULL, NULL, NULL, NULL); } //-----------------------------------------------------------------------------//DESC: Message processing//----- ------------------------------------------------------------------------lresult WINAPI Msgproc (HWND hwnd, UINT MSG, WPARAM WPARAM, LPARAM LPARAM) { switch (msg) { case Wm_destroy: & nbsp; Cleanup (); postquitmessage (0); return 0; case WM_PAINT: Render (); ValidateRect (hWnd, NULL); return 0; } return DefWindowProc (hWnd, MSG, WParam, LParam); } //-----------------------------------------------------------------------------//DESC: Program Entry//----- ------------------------------------------------------------------------INT winapi WinMain (hinstance hinst, HINSTANCE, LPSTR, INT) { //Register window class Wndclassex WC = {sizeof (wndclassex), CS_CLASSDC, Msgproc, 0L, 0L, GetModuleHandle (NULL), NULL, NULL, NULL, NULL, L "ClassName", NULL}; RegisterClassEx (&WC); //Create window hwnd hwnd = CreateWindow (L "ClassName", L "simplest Direct3D program", &NBSP;&N bsp; Ws_overlappedwindow, MB, NULL, NULL, wc.hinstance, NULL); //initialization of Direct3D if (SUCCEEDED (Initd3d (hWnd)) { //Show main window ShowWindow (hWnd, Sw_ Showdefault); UpdateWindow (HWND); /Enter message loop MSG msg; ZeroMemory (&msg, sizeof (msg)); while (msg.message!=wm_quit) { if ( PeekMessage (&msg, NULL, 0U, 0U, pm_remove)) & nbsp; { TranslateMessage (&msg); DispatchMessage (&MSG); } Else { Render (); //Render Graphics } } } Unregisterclass (L "ClassName", wc.hinstance); return 0; }