2
votes

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.

1
Welcome to Stack Overflow. As it stands, your question may get voted for deletion, because you are asking for an external resource. Instead, what you need to do is to shre your code and whats wrong with it. Then people can assist you.Rohit Gupta
Thank you. Have a question, though. Where do I put the code? I have a part already done but I'm missing something to make it work. Do I post it as an answer to this question or something else? Thanks again.SPACoD
Just edit your post and add it at the end. Prefix each code line with 4 spaces and it will format it.Rohit Gupta
Thanks for the help, I've done the editing.SPACoD
Now that the question is looking better, I have up voted it.Rohit Gupta

1 Answers

2
votes

I'm totally new to SharpDx, so take my suggestion with a grain of salt, but I'm using a SharpDX.Direct2D1.DeviceContext to render text in my application. My understanding is that it's the Direct2D that makes text relatively easy to render. (in 2d at least) There are a lot of examples/details at https://github.com/spazzarama/Direct3D-Rendering-Cookbook (which ties into the SharpDx book by the same name)

context2D.DrawText("myTextHere", _textFormat, new RectangleF(0, 0, 100, 100), _gradeLines[ii].ForegroundBrush);