I was looking around the internet to try to find code that allows me to draw text from a regular font (not a sprite font) and only found snippets which didn't help me at all.
Does someone have a full source file that allows me to easily draw text using SharpDX and D3D11/DirectWrite? I want to make a transparent overlay window that displays some info (like Fraps). I found a tutorial that does that but it uses a sprite font and I want it to be customizable in terms of size and font face.
This is what I have so far:
using System;
using SharpDX;
using SharpDX.Direct3D11;
using SharpDX.Windows;
using SharpDX.Direct2D1;
using SharpDX.DirectWrite;
using SharpDX.DXGI;
namespace SharpDXRenderer
{
static class Program
{
[STAThread]
static void Main()
{
SharpDX.DirectWrite.TextFormat directWriteTextFormat;
SharpDX.DirectWrite.Factory directWriteFactory;
SharpDX.Direct2D1.SolidColorBrush directWriteFontColor;
SharpDX.Direct2D1.RenderTarget direct2DRenderTarget;
Surface d2dSurface;
RenderForm form = new RenderForm("Example");
var desc = new SwapChainDescription()
{
BufferCount = 1,//buffer count
ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),//sview
IsWindowed = true,
OutputHandle = form.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
var d2dFactory = new SharpDX.Direct2D1.Factory();
d2dSurface = backBuffer.QueryInterface<Surface>();
direct2DRenderTarget = new SharpDX.Direct2D1.RenderTarget(d2dFactory, d2dSurface, new SharpDX.Direct2D1.RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)));
directWriteFactory = new SharpDX.DirectWrite.Factory();
directWriteTextFormat = new SharpDX.DirectWrite.TextFormat(directWriteFactory, "Calibri", 12) { TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading, ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Near };
directWriteFontColor = new SharpDX.Direct2D1.SolidColorBrush(direct2DRenderTarget, Color.White);
}
}
}
I know I'm missing a SwapChain, a Device and I think a Texture, but I'm not sure if I need more things. This backBuffer is what I'm missing but I don't know what type it is.
d2dSurface = backBuffer.QueryInterface<Surface>();
Any help regarding text and basic shape drawing is appreciated.
Thanks in advance.