//-----------------------------------------------------------------------------//File:CreateDevice.cpp////Desc:this is the first tutorial for using Direct3D. In this tutorial, all//We are doing was creating a Direct3D device and using it to clear the//window.////Copyright (c) Microsoft Corporation. All rights reserved.//-----------------------------------------------------------------------------#include <d3d9.h>#pragmaWarning (disable:4996)//Disable deprecated warning#include<strsafe.h>#pragmaWarning (default:4996)//-----------------------------------------------------------------------------//Global Variables//-----------------------------------------------------------------------------Lpdirect3d9 g_pd3d = NULL;//used to create the D3DDeviceLpdirect3ddevice9 g_pd3ddevice = NULL;//Our rendering device//-----------------------------------------------------------------------------//Name:initd3d ()//desc:initializes Direct3D//-----------------------------------------------------------------------------HRESULT INITD3D (hwnd hwnd) {//Create the D3D object, which is needed to create the D3DDevice. if(NULL = = (G_pd3d =Direct3dcreate9 (d3d_sdk_version))) returnE_fail; //Set up the structure used to create the D3DDevice. Most parameters is//zeroed out. We set windowed to TRUE, since we want to does D3D in a//window, and then set the ' SwapEffect to ' discard, which is the most//efficient method of presenting the back buffer to the display. and//We request a back buffer format this matches the current desktop display//format.d3dpresent_parameters D3DPP; ZeroMemory (&D3DPP,sizeof(D3DPP)); D3DPP. Windowed=TRUE; D3DPP. SwapEffect=D3dswapeffect_discard; D3DPP. Backbufferformat=D3dfmt_unknown; //Create the Direct3D device. Here we is using the default adapter ( most//Systems only has 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'll work on all cards. On cards This support//hardware vertex processing, though, we would see a big performance gain//By specifying hardware vertex processing. if(FAILED (g_pd3d->CreateDevice (D3dadapter_default, D3ddevtype_hal, HWnd, D3dcreate_software_vertex Processing,&D3DPP, &g_pd3ddevice)) ) { returnE_fail; } //Device state would normally BES set here returnS_OK;}//-----------------------------------------------------------------------------//Name:cleanup ()//desc:releases all previously initialized objects//-----------------------------------------------------------------------------VOID Cleanup () {if(G_pd3ddevice! =NULL) g_pd3ddevice-Release (); if(G_pd3d! =NULL) G_pd3d-Release ();}//-----------------------------------------------------------------------------//Name:render ()//desc:draws the scene//-----------------------------------------------------------------------------VOID Render () {if(NULL = =g_pd3ddevice)return; //Clear the backbuffer to a blue colorG_pd3ddevice->clear (0, NULL, D3dclear_target, D3dcolor_xrgb (0,0,255),1.0f,0 ); //Begin the scene if(SUCCEEDED (g_pd3ddevice->BeginScene ())) { //Rendering of Scene objects can happen here//End the sceneG_pd3ddevice->EndScene (); } //Present the Backbuffer contents to the displayG_pd3ddevice->Present (null , NULL, NULL, NULL);}//-----------------------------------------------------------------------------//Name:msgproc ()//desc:the window ' s Message Handler//-----------------------------------------------------------------------------LRESULT WINAPI Msgproc (HWND hwnd, UINT MSG, WPARAM WPARAM, LPARAM LPARAM) {Switch(msg) { CaseWm_destroy:cleanup (); PostQuitMessage (0 ); return 0; CaseWm_paint:render (); ValidateRect (HWnd, NULL); return 0; } returnDefWindowProc (hWnd, MSG, WParam, LParam);}//-----------------------------------------------------------------------------//Name:wwinmain ()//desc:the application ' s entry point//-----------------------------------------------------------------------------int WINAPI wWinMain (hinstance hInst, hinstance, LPWStr, INT) {unreferenced_parameter (hInst); //Register the window classWndclassex WC = { sizeof(Wndclassex), CS_CLASSDC, Msgproc,0L,0L, GetModuleHandle (null), NULL, NULL , NULL, NULL, L"D3D Tutorial", NULL}; RegisterClassEx (&WC); //Create The application ' s windowHWND hwnd = CreateWindow (L"D3D Tutorial", L"D3D Tutorial 01:createdevice", Ws_overlappedwindow, -, -, -, -, NULL, NULL, wc.hinstance, NULL); //Initialize Direct3D if(SUCCEEDED (Initd3d (hWnd))) {//Show the windowShowWindow (hWnd, Sw_showdefault); UpdateWindow (HWND); //Enter the message loopmsg msg; while(GetMessage (&msg, NULL,0,0) {translatemessage (&msg); DispatchMessage (&msg); }} unregisterclass (L"D3D Tutorial", wc.hinstance); return 0;}
D3D Creating a Window