PDA

View Full Version : DirectX با دلفی:قسمت اول



shaniaki
سه شنبه 10 تیر 1382, 08:33 صبح
باعرض پوزش از تاخیر پیش آمده یه هفته است می خوام این SDK DirectX 9 رو که دانلود کردم ببرم خونه هر روز یه مشکلی پیش اومده. به هر شکل امیدوارم تا به حال شما SDK رو خریده باشید(یا دانلود) و اون Header هایی(unit هایی) رو که آدرس سایتشونو گفتم هم دانلود کرده باشید(چند عدد .pas و .dcu) و در قسمت Directories تو Project Options مسیرشونو تو search path داده باشید که هر وقت uses می کنیدشون پیداشون کنه.
خوب از Tutorial ها شروع می کنیم اگر SDK رو نصب کرده باشید یه DirectX Sample Browser براتون نصب می کنه که می تونید Tutorial ها و Sample هایی رو در این قسمت به زبون های C++,C#,VB به همراه Documentation شون (اگر داشته باشند) ببینید. اکثر اون ها توی همون سایتی که گفتم(clootie JEDI) ترجمه شده به دلفیشون هست که شما دانلود کردید.
اولین Tutorial یعنی CreateDevice رو شروع می کنیم. project مربوطه رو (فقط یک dpr) باز کنید و اجرا کنید(آدرس Header ها را فراموش نکرده باشید) . می بینیم که یک Direct3D Device می سازد و فقط یک صفحه آبی را رندر می کند.
نکته: دقت کنید که پیچیدگی های ظاهری که می بینید بیشتر به خاطر این است که در این پروژه تمام کارهای ساخت فرم و Handle کردن Message ها دستی انجام می شود. نسخه ساده شده این برنامه با استفاده از فرم های آماده دلفی را در زیر می آورم.
نکات اصلی این برنامه که شما از Documentation مطالعه خواهید کرد و برای تکمیل بحث اگر میل داشتید به عنوان جواب در زیر اضافه می کنید در توابع InitD3D, Render, Cleanup نهفته است. نکته مهم این است که برای رندر تصویر شما به شیی از نوع Direct3DDevice9 برای ساخت این شیی به شیی از نوع Direct3D9 نیاز خواهید داشت.
توجه به d3dpp که شیی از نوع TD3DPresentParameters بوده وبرای ساخت Direct3DDevice9 به کار می رود دقت کنید. لازم نیست تمام پارامتر های توابع را در ابتدای کار تمام و کمال حفظ باشید. در Documentation مربوط به SDK تمام آن ها را به همراه فرمتشان آورده است.

این هم نسخه ساده شده برنامه با یک فرم آما ده دلفی(مانند وقتی که New Application) می کنید:


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Direct3D9;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormPaint(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
var
g_pD3D: IDirect3D9 = nil; // Used to create the D3DDevice
g_pd3dDevice: IDirect3DDevice9 = nil; // Our rendering device

//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
function InitD3D(hWnd: HWND): HRESULT;
var
d3dpp: TD3DPresentParameters;
begin
Result:= E_FAIL;

// Create the D3D object, which is needed to create the D3DDevice.
g_pD3D := Direct3DCreate9(D3D_SDK_VERSION);
if (g_pD3D = nil) then Exit;

// 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 := 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.
Result:= g_pD3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
@d3dpp, g_pd3dDevice);
if FAILED(Result) 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
if (g_pd3dDevice <> nil) then
{$IFDEF TMT}
g_pd3dDevice.Release;
{$ELSE}
g_pd3dDevice:= nil;
{$ENDIF}

if (g_pD3D <> nil) then
{$IFDEF TMT}
g_pD3D.Release;
{$ELSE}
g_pD3D:= nil;
{$ENDIF}
end;




//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
procedure Render;
begin
if (g_pd3dDevice = nil) then Exit;

// Clear the backbuffer to a blue color
g_pd3dDevice.Clear(0, nil, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0, 0);

// Begin the scene
if (SUCCEEDED(g_pd3dDevice.BeginScene)) then
begin
// Rendering of scene objects can happen here

// End the scene
g_pd3dDevice.EndScene;
end;

// Present the backbuffer contents to the display
g_pd3dDevice.Present(nil, nil, 0, nil);
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
if not SUCCEEDED(InitD3D(Form1.Handle)) then Application.Terminate;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Cleanup;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
Render;
end;

end.

Mohammad_Mnt
سه شنبه 10 تیر 1382, 20:34 عصر
هوراااااااا :D