private int Present(IntPtr swapChainPtr, int syncInterval, PresentFlags flags) { SwapChain swapChain = (SharpDX.DXGI.SwapChain)swapChainPtr; swapChain.GetDevice(typeof(SharpDX.Direct3D11.Device).GUID, out IntPtr deviceOut); var device = new SharpDX.Direct3D11.Device(deviceOut); DeviceContext deviceContext = new DeviceContext(device); if (this.isFirst) { isFirst = false; this.srvFront = GetShaderResourceView(device, Brushes.Gold); this.srvback = GetShaderResourceView(device, Brushes.Red); var stencilDesc = new DepthStencilStateDescription() { DepthComparison = Comparison.Less, IsDepthEnabled = true, IsStencilEnabled = true, StencilReadMask = 0xFF, StencilWriteMask = 0xFF, FrontFace = new DepthStencilOperationDescription() { FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Increment, PassOperation = StencilOperation.Keep, Comparison = Comparison.Always }, BackFace = new DepthStencilOperationDescription() { FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Decrement, PassOperation = StencilOperation.Keep, Comparison = Comparison.Always } }; stencilDesc.IsDepthEnabled = false; stencilDesc.DepthWriteMask = DepthWriteMask.All; depthStencilStateDisabled = new DepthStencilState(deviceContext.Device, stencilDesc); stencilDesc.IsDepthEnabled = true; stencilDesc.DepthWriteMask = DepthWriteMask.All; stencilDesc.DepthComparison = Comparison.GreaterEqual; stencilDesc.IsStencilEnabled = false; stencilDesc.StencilReadMask = 0xFF; stencilDesc.StencilWriteMask = 0x0; stencilDesc.FrontFace.FailOperation = StencilOperation.Zero; stencilDesc.FrontFace.DepthFailOperation = StencilOperation.Zero; stencilDesc.FrontFace.PassOperation = StencilOperation.Keep; stencilDesc.FrontFace.Comparison = Comparison.Equal; stencilDesc.BackFace.FailOperation = StencilOperation.Zero; stencilDesc.BackFace.DepthFailOperation = StencilOperation.Zero; stencilDesc.BackFace.PassOperation = StencilOperation.Zero; stencilDesc.BackFace.Comparison = Comparison.Never; depthStencilStateReadonly = new DepthStencilState(deviceContext.Device, stencilDesc); } Dx11.DXFont dXFont = new Dx11.DXFont(device, deviceContext); dXFont.Initialize("宋体", 14, FontStyle.Bold, true); DXSprite dXSprite = new DXSprite(device, deviceContext); dXSprite.DrawString(50, 50, "ceshi", 255, 255, 233, 233, dXFont); swapChain.Present(syncInterval, flags); return(SharpDX.Result.Ok.Code); }
public void DrawString(int X, int Y, string text, int R, int G, int B, int A, DXFont F) { SharpDX.Mathematics.Interop.RawColor4 blendFactor = new Color4(1.0f); SharpDX.Mathematics.Interop.RawColor4 backupBlendFactor; int backupMask; var backupBlendState = _deviceContext.OutputMerger.GetBlendState(out backupBlendFactor, out backupMask); _deviceContext.OutputMerger.SetBlendState(_transparentBS, blendFactor); BeginBatch(F.GetFontSheetSRV()); int length = text.Length; int posX = X; int posY = Y; Color4 color; Vector4 Vec = new Vector4(R > 0 ? (float)(R / 255.0f) : 0.0f, G > 0 ? (float)(G / 255.0f) : 0.0f, B > 0 ? (float)(B / 255.0f) : 0.0f, A > 0 ? (float)(A / 255.0f) : 0.0f); color = new Color4(Vec); for (int i = 0; i < length; ++i) { char character = text[i]; if (character == ' ') { posX += F.GetSpaceWidth(); } else if (character == '\n') { posX = X; posY += F.GetCharHeight(); } else { Rectangle charRect = F.GetCharRect(character); int width = charRect.Right - charRect.Left; int height = charRect.Bottom - charRect.Top; Draw(new Rectangle(posX, posY, posX + width, posY + height), charRect, color); posX += width + 1; } } EndBatch(); _deviceContext.OutputMerger.SetBlendState(backupBlendState, backupBlendFactor, backupMask); }