constructor TMainForm.Create(AOwner: TComponent); var pa: TD3DPresentParameters; matProj, matView: TD3DXMatrix; eye, at, up: TD3DVector; vertices: PVertices; begin inherited; FDirect3D := Direct3DCreate9(D3D_SDK_VERSION); if not Assigned(FDirect3D) then begin Application.MessageBox('Create Direct3D Failure', 'Error', MB_OK or MB_ICONERROR); Application.Terminate; end;
if not Assigned(FD3DDevice) then begin Application.MessageBox('Create Direct3DDevice Failure', 'Error', MB_OK or MB_ICONERROR); Application.Terminate; end;
FD3DDevice.CreateVertexBuffer(SizeOf(TVertex) * 3, 0, VERTEX_FVF, D3DPOOL_DEFAULT, FVertexBuf, nil); if not Assigned(FVertexBuf) then begin Application.MessageBox('Create Vertex Buffer Failure', 'Error', MB_OK or MB_ICONERROR); Application.Terminate; end;
procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin FKeyDown := True; end;
procedure TMainForm.AppIdle(Sender: TObject; var Done: Boolean); var matWorld: TD3DMatrix; begin D3DXMatrixIdentity(matWorld); FD3DDevice.SetTransform(D3DTS_WORLD, matWorld);
FD3DDevice.Clear(0, nil, D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER, $FF000000, 1, 0); if Succeeded(FD3DDevice.BeginScene) then begin FD3DDevice.SetFVF(VERTEX_FVF); FD3DDevice.SetStreamSource(0, FVertexBuf, 0, SizeOf(TVertex)); FD3DDevice.DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1); FD3DDevice.EndScene; FD3DDevice.SetStreamSource(0, nil, 0, 0); FD3DDevice.Present(nil, nil, Handle, nil); end;
if FKeyDown then begin GetScreen; FKeyDown := False; end;
Done := False; end;
procedure TMainForm.GetScreen; type TColor4 = packed record b, g, r, a: Byte; end; PColor4Array = ^TColor4Array; TColor4Array = array[0..0] of TColor4;
TColor3 = packed record b, g, r: Byte; end; PColor3Array = ^TColor3Array; TColor3Array = array[0..0] of TColor3;
var i, j: Integer; bf: IDirect3DSurface9; r: TRect; lr: TD3DLockedRect; bmp: TBitmap; src: PColor4Array; dest: PColor3Array; begin r := Rect(0, 0, ClientWidth, ClientHeight); FD3DDevice.GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, bf); bf.LockRect(lr, nil, D3DLOCK_READONLY); bmp := TBitmap.Create; bmp.Width := ClientWidth; bmp.Height := ClientHeight; bmp.PixelFormat := pf24Bit; for i := 0 to ClientHeight - 1 do begin src := lr.pBits; dest := bmp.ScanLine[i]; for j := 0 to ClientWidth - 1 do begin dest[j].b := src[j].b; dest[j].g := src[j].g; dest[j].r := src[j].r; end; lr.pBits := Pointer(Integer(lr.pBits) + lr.Pitch); end; bf.UnlockRect; bf := nil; bmp.SaveToFile(ExtractFilePath(Application.ExeName) + 'Screen.bmp'); bmp.Free; end;