根据 DirectX 8.1 SDK samples/Multimedia/Direct3D/Tutorials/Tut01_CreateDevice 翻译
保留原文档的所有注释
//-----------------------------------------------------------------------------
// File: CreateDevice.cpp
//
// Desc: This is the first tutorial for using Direct3D. In this tutorial, all
// we are doing is creating a Direct3D device and using it to clear the
// window.
//-----------------------------------------------------------------------------
program Tut01_CreateDevice;
uses
Windows,
Messages,
Direct3D8 in 'JEDI/Direct3D8.pas',
DXTypes in 'JEDI/DXTypes.pas';
const
NULL=nil;
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
var
g_pD3D :IDirect3D8 = NULL; // Used to create the D3DDevice
g_pd3dDevice:IDirect3DDevice8 = NULL; // Our rendering device
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
function InitD3D( hWnd: HWND ) :HRESULT;
var
d3ddm :TD3DDisplayMode;
d3dpp :TD3DPresentParameters;
begin
// Create the D3D object.
g_pD3D:=Direct3DCreate8(D3D_SDK_VERSION);
if g_pD3D=nil then begin
Result:=E_FAIL;
exit;
end;
// Get the current desktop display mode
if not (g_pD3D.GetAdapterDisplayMode( D3DADAPTER_DEFAULT, d3ddm )=S_OK) then begin
Result:=E_FAIL;
exit;
end;
// 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 SwapEffect to "discard", which is the most
// efficient method of presenting the back buffer to the display. And
// we request a back buffer format that matches the current desktop display
// format.
Fillchar( d3dpp, sizeof(d3dpp), 0 );
d3dpp.Windowed := TRUE;
d3dpp.SwapEffect := D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat := d3ddm.Format;
// 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 the D3DDevice
if not ( g_pD3D.CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
d3dpp, g_pd3dDevice )=S_OK) then begin
Result:=E_FAIL;
exit;
end;
// Device state would normally be set here
Result:=S_OK;
end;
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
procedure Cleanup;
begin
g_pd3dDevice:=nil;
g_pD3D :=nil;
end;
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
procedure Render;
begin
if( NULL = g_pd3dDevice ) then exit;
// Clear the backbuffer to a blue color
g_pd3dDevice.Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0, 0 );
// Begin the scene
g_pd3dDevice.BeginScene();
// Rendering of scene objects can happen here
// End the scene
g_pd3dDevice.EndScene();
// Present the backbuffer contents to the display
g_pd3dDevice.Present( NULL, NULL, 0, NULL );
end;
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
function MsgProc( h_Wnd : THandle; aMSG : Cardinal; wParam : Cardinal; lParam : Integer ) : LRESULT; stdcall;
begin
case aMSG of
WM_DESTROY:
PostQuitMessage( 0 );
WM_PAINT :
begin
Render;
ValidateRect( h_Wnd, NULL);
end;
end;
Result :=DefWindowProc(h_Wnd, aMSG, wParam, lParam);
end;
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
function WinMain( hInst :LongWord ) :Integer;
var
wc :TWndClassEx;
hWnd :THandle;
aMsg :TMsg;
begin
// Register the window class
FillChar(wc,sizeof(wc),0);
wc.cbSize:=sizeof(wc);
wc.style:=CS_CLASSDC;
wc.lpfnWndProc:=@MsgProc;
wc.cbClsExtra:=0;
wc.cbWndExtra:=0;
wc.hInstance:=hInst;
wc.hIcon:=0;//LoadIcon(hInstance, 'MAINICON');
wc.hCursor:=0;//LoadCursor(hInstance, MakeIntResource(200));
wc.hbrBackground:=0;
wc.lpszMenuName:=nil;
wc.lpszClassName:='D3D Tutorial';
wc.hIconSm:=0;//LoadIcon(hInstance, 'MAINICON');
RegisterClassEx(wc);
// Create the application's window
hWnd := CreateWindow('D3D Tutorial', 'D3D Tutorial 01: CreateDevice',
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), 0, wc.hInstance, NULL );
// Initialize Direct3D
if SUCCEEDED(InitD3D(hWnd)) then begin
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// Enter the message loop
while GetMessage(aMsg, 0, 0, 0) do begin
TranslateMessage(aMsg);
DispatchMessage(aMsg);
end
end;
// Clean up everything and exit the app
Cleanup;
UnregisterClass( 'D3D Tutorial', wc.hInstance );
Result:=0;
end;
begin
WinMain(hInstance);
Halt(0);
end.