Create a Direct3D device

xiaoxiao2021-03-06  16

/ / -------------------------------------------------------------------------------------------- ----------------------------- // File: createDevice.cpp //// DESC: this is the first tutorial for use Direct3D This is the first step using Direct 3D. Here, what we do is to create a Direct3D here. Equipment and use it to fill // window .//// copyright (c) Microsoft Corporation. All Rights Reserved.//------------------------- -------------------------------------------------- - # include

/ / -------------------------------------------------------------------------------------------- ----------------------------- // Global Variables / / Define global variables / / ---------- -------------------------------------------------- ----------------- LPDIRECT3D9 g_pd3d = null; // used to create the d3ddevice use to create D3D devices LPDIRECT3DDEVICE9 g_pd3ddevice = null; // urnted device Drawing device

/ / -------------------------------------------------------------------------------------------- ----------------------------- // name: initd3d () // desc: initializes Direct3D // Initialization Direct3D // -------------------------------------------------- ------------------------- HRESULT INITD3D (HWND HWND) {// Create The D3D Object, Which Is Needed to create the d3ddevice. // creation D3D object, this object is required when creating D3DDevice (null == (g_pd3d = Direct3dcreate9 (D3D_SDK_VERSION))) Return E_FAIL;

// set up the structure used to create the d3ddevice. Most parameters are // zeroed out. We set windowed to true, since we want to do d3d in a // window, and then set the swapect to "discard", Which IS The Mosting The Back Buffer To The Display. And // We Request A Back Buffer Format That Matches The Current Desktop Display // Format. // Settings to create a structure of D3DDevice. Most parameters (return to Zero / Initialization) // We set the Windowed to True, so we can run the D3D program in the window and set the swapect to "discard", which guarantees the highest work efficiency of the buffer // and us also it requires a suitable buffer to the current display state D3DPRESENT_PARAMETERS d3dpp; ZeroMemory (& d3dpp, sizeof (d3dpp)); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;. // Create the Direct3D device Here we are using the default adapter (most // systems only have one, unless they have multiple graphics hardware cards // installed) and requesting the HAL (which is saying we want the hardware // device rather than a software one). Software vertex processing is // specified since we know it will work on all cards. On cards that support // hardware vertex processing, though, we would see a big performance gain // by specifying hardware vertex processing. / / Create a Direct3D device. Here we use the default display adapter (most system only one, unless you // installed multiple graphics cards), and apply for a HAL (that is, declare that we need a hardware device instead of software) // software processing You can work on all graphics cards. However, if you specify the hardware processing, we can get better performance on the graphics card that supports hard // pieces. IF (Failed (g_pd3d-> createDevice (d3dadapter_default, d3ddevtype_hal, hwnd, d3dcreate_software_vertexprocessing , & D3DPP, & g_pd3ddevice))) {RETURN E_FAIL;}

// Device State Would Normal BE SET Here // Most of the device is set here Return S_OK;}

/ / -------------------------------------------------------------------------------------------- ----------------------------- // name: cleanup () // desc: Releases all previously initialized Objects // Releases All Resources / / -------------------------------------------------------------------------------------------- ----------------------------- void cleanup () {if (g_pd3ddevice! = Null) g_pd3dde -> release ();

IF (g_pd3d! = null) g_pd3d-> release ();

/ / -------------------------------------------------------------------------------------------- ----------------------------- // name: render () // desc: Draws the Scene // Draw Scene // -------------------------------------------------- -------------------------- void render () {if (null == g_pd3ddevice) return;

// Clear the Backbuffer to a blue color // Buffer G_PD3DDEVICE-> CLEAR (0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB (0, 0, 255), 1.0F, 0);

// begin the Scene // Start Draw Scene IF (succeededed (g_pd3ddevice-> beginscene ()))) {// rendering of scene objects can happen here // Scenario action can occur here // end the footne // End Scene G_PD3DDEVICE-> endscene ();

// Present the backbuffer contents to the display // Rear buffer g_pd3ddevice-> present (null, null, null, null) on the display;}

/ / -------------------------------------------------------------------------------------------- ----------------------------- // name: msgproc () // desc: The window's message handler // message processing // -------------------------------------------------- --------------------------- Lresult WinApi MsgProc (HWnd Hwnd, Uint Msg, WPARAM WPARAM, LPARAM LPARAM) {Switch (MSG) {Case WM_DESTROY: CLEANUP (); PostquitMessage (0); Return 0;

Case WM_Paint: Render (); ValidateRect (hwnd, null); Return 0;}

Return DefWindowProc (HWND, MSG, WPARAM, LPARAM);

/ / -------------------------------------------------------------------------------------------- ----------------------------- // name: WinMain () // desc: The application's entry point // main function. Program Inlet // ---------------------------------------------- ----------------------------- Int Winst, Hinstance, LPSTR, INT) {// register the window Class // Register window WNDCLASSEX WC = {SizeOf (WNDCLASSEX), CS_CLASSDC, MSGProc, 0L, 0L, GetModuleHandle (NULL), NULL, NULL, NULL, NULL, "D3D Tutorial", NULL}; RegisterClassex (& WC);

// Create the application's window // Create a window HWND hWnd = CreateWindow ( "D3D Tutorial", "D3D Tutorial 01: CreateDevice", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow (), NULL, wc.hInstance, NULL );

// Initialize Direct3D // Initialization Direct3D IF (successd3d (hwnd))) {// show the window // display window showWindow (hwnd, sw_showdefault); UpdateWindow (hwnd);

// Enter the message loop // Start message loop MSG msg; while (GetMessage (& MSG, NULL, 0, 0)) {TranslateMessage (& MSG); DispatchMessage (& MSG);}}

UnregisterClass ("D3D Tutorial"; Return 0;}

转载请注明原文地址:https://www.9cbs.com/read-41300.html

New Post(0)