***************************************************************** * Neveprise Inc. Delphi FAQ * ***************************************************************** * Section: DirectX Topic: Create DD interfaces * * Created: July 27th, 1999 Revised: July 27th, 1999 * * Updates: www.neveprise.de Info: support@neveprise.de * ***************************************************************** Q: How can I create basic DirectDraw and Direct3DRM devices and interfaces? A: If you declare all needed variables in your applications, you can use the function below to init all nessessary devices and interfaces. Function DXCHeck() just interpretes DirectX functions and you can use it later to add more detailed info, such as understandable messages etc. DXStat is a global string that is used by DXCheck to know on which function the error occured. function CreateDXDevices_SD(FormHandle: THandle; Driver: PGUID; out DirectDraw: IDirectDraw2; out Direct3DRM: IDirect3DRM; out Direct3DDevice: IDirect3DRMDevice; out SurfPrim, SurfBack, SurfZBuf: IDirectDrawSurface): boolean; var SurfDesc: TDDSurfaceDesc; Caps: TDDSCaps; PixFmt: TDDPixelFormat; begin Result:=false; //create a DirectDraw instance DXStat:='Create DirectDraw'; //in the next function, the first parameter is nil, so the primary device //will be used. But it is *strongly* recommended to insert there an //existing GUID of a chosen graphics accelerator card! //you can retrieve it by using DirectDrawEnumerate. //How to use it, please look into this FAQ, topic "Enumerating graphic devices" if DXCheck(DirectDrawCreate(nil, DirectDraw1, nil))=false then Exit; //shift it to DirectDraw2 DXStat:='Query DirectDraw2'; if DXCheck(DirectDraw1.QueryInterface(IID_IDirectDraw2, DirectDraw))=false then Exit; //set app's priority and screen mode DXStat:='Set Cooperative level'; if DXCheck(DirectDraw.SetCooperativeLevel(FormHandle, DDSCL_Exclusive+ DDSCL_FullScreen+DDSCL_AllowReboot))=false then Exit; //create primary surface (the "display") and define properties FillChar(SurfDesc,SizeOf(SurfDesc),0); with SurfDesc do begin dwSize:=SizeOf(SurfDesc); //reset record dwFlags:=DDSD_Caps+DDSD_BackBufferCount; //valid fields dwBackBufferCount:=1; // we have one backbuffer (for flipping) //caps: is prim. surface, can flip, 3D able and complex ddsCaps.dwCaps:=DDSCaps_PrimarySurface+DDSCaps_Complex+DDSCaps_Flip+ DDSCaps_3DDevice; end; DXStat:='Create Primary Surface'; if DXCheck(DirectDraw.CreateSurface(SurfDesc, SurfPrim, nil))=false then Exit; //set pixelformat for primary surface FillChar(PixFmt,SizeOf(PixFmt),0); PixFmt.dwSize:=SizeOf(PixFmt); SurfPrim.GetPixelFormat(PixFmt); //create and attach backbuffer surface (the surface that is drawn on) FillChar(Caps, SizeOf(Caps),0); Caps.dwCaps:=DDSCaps_BackBuffer; DXStat:='Create Back Buffer'; SurfPrim.GetAttachedSurface(Caps, SurfBack); //create and attach ZBuffer surface FillChar(SurfDesc,SizeOf(SurfDesc),0); with SurfDesc do begin dwSize:= SizeOf(SurfDesc); dwFlags:= DDSD_Width+DDSD_Height+DDSD_Caps+DDSD_ZBufferBitDepth; dwHeight:= DisplayMode.Height; //set display size x dwWidth:= DisplayMode.Width; //" y //..is a z buffer and stored in adapter's mem ddsCaps.dwCaps:=DDSCaps_ZBuffer+DDSCaps_VideoMemory; //color depth is high colour / 16 bit dwZBufferBitDepth:=DisplayMode.BitDepth; end; //create the z buffer DXStat:='Create ZBuffer'; if DXCheck(DirectDraw.CreateSurface(SurfDesc, SurfZBuf,nil))=false then Exit; //..and attach it DXStat:='Attach ZBuffer'; if DXCheck(SurfBack.AddAttachedSurface(SurfZBuf))=false then Exit; Direct3DRM:=nil; DXStat:='Create Direct3DRM'; if DXCheck(Direct3DRMCreate(Direct3DRM))=false then Exit; //create a Direct3DRMDevice from back buffer data DXStat:='Create D3DRMDevice from Surface'; if DXCheck(Direct3DRM.CreateDeviceFromSurface(@IID_IDirect3DHALDevice, DirectDraw1, SurfBack, Direct3DDevice))=false then Exit; with Direct3DDevice do begin SetQuality(D3DRMRender_Phong); SetTextureQuality(D3DRMTexture_MIPLinear); end; Result:=true; end; Blazko