# DirectX 11, support for Font drawing

I have recently started to work with DirectX 11 and 12, I am using Microsoft Visual Studio Community Edition. I am under a 64-bit machine. I am currently rendering a frame with background and a window using Windows C++ API and DirectX 11.1.

I was thinking about drawing text, but I thought about using custom Fonts, so I will have original Font file, not some graphic file e.g. *bmp or some other format. I have read about: SpriteBatch[1] and SpriteFont[2], which can be used via DirectX Tool Kit (https://github.com/microsoft/DirectXTK).

I tried:

std::unique_ptr< SpriteFont> font(new SpriteFont(device, L"text.spritefont"));
std::unique_ptr<SpriteBatch> spriteBatch(new SpriteBatch(context));
spriteBatch->Begin();
font->DrawString(spriteBatch.get(), L"Hello, ", XMFLOAT2(200,200));
spriteBatch->End();


But, I am running into some issues with the Begin() function, that seems to have mandatory parameters. I did try: Begin(SpriteSortMode:: SpriteSortMode_Deferred, nullptr, nullptr, nullptr); But, I keep experiencing the same errors in Visual Studio regarding overloading and params.

I know that DirectX 9 had D3DXFont, https://drunkenhyena.com/cgi-bin/view_cpp_article.pl?chapter=3;article=17 , I also found this: http://download.nvidia.com/developer/GPU_Gems/CD_Image/Natural_Effects/Water_Simulation/d3dfont.cpp

I also found the DXUT for Direct3D 11, https://github.com/walbourn/directx-sdk-samples/tree/master/DXUT -- but seems this is no longer active.

EDIT #1: With DirectX 9, I can do the following:

 HRESULT hr=D3DXCreateFont(
device,           //D3D Device
22,               //Font height
0,                //Font width
FW_NORMAL,        //Font Weight
1,                //MipLevels
false,            //Italic
DEFAULT_CHARSET,  //CharSet
OUT_DEFAULT_PRECIS, //OutputPrecision
ANTIALIASED_QUALITY, //Quality
DEFAULT_PITCH|FF_DONTCARE,//PitchAndFamily
"Arial",          //pFacename,
&font);           //ppFont


But, unfortunately this does not exist in DirectX 10, 11, or 12.

Questions:

1. Is using these new recommendations (SpriteBatch, SpriteFont & DirectXDTK) by Microsoft, in any way, linking the stuff I build with XNA?
2. Is it possible to use Font file, custom one? Or, do I have to create graphic font? (What is more recommended generally speaking)?

A book recommended writing your own Font drawer class, it uses:

• ID3D11InputLayout
• ID3D11Buffer
• ID3D11SamplerState
• D311_MAPPED_SUBRESOURCE

EDIT #2: WHY DO I HAVE TO CAST, Microsoft::WRL::ComPtr

• What is your question? – Tyyppi_77 Jul 7 at 13:40
• @Tyyppi_77, please see my update, thanks a lot. – John Smith Jul 7 at 13:47
• Please ask one question per post. If you have a separate question that's still not solved after you get answers to your first question, you can ask it in a second post. – DMGregory Jul 7 at 13:55
• Your edit about Microsoft::WRL::ComPtr looks like something to ask on StackOverflow. You'll tend to get better answers if you focus this question just on font rendering. – DMGregory Jul 7 at 20:28

For DirectX Tool Kit, you should walk through the first few lessons of the tutorials which covers the basic of SpriteBatch and SpriteFont.

DirectX Tool Kit is a C++ language library. The class designs are inspired by the XNA Game Studio class design, but they are very different things. The intent is if you are familiar with SpriteBatch from XNA Game Studio in C#, it should be easier to get started with the C++ SpriteBatch in DirectX Tool Kit.

D3DXCreateFont was a D3DX utility library function. While there was in fact a Direct3D 10 version of in D3DX10, the D3DX11 library was very much trimmed down to just the texture loading. In any case, D3DX9, D3DX10, and D3DX11 are deprecated and only available in the end-of-life legacy DirectX SDK. See Microsoft Docs

The DXUT for Direct3D 11 framework includes a very basic sprite-based bitmap font system because D3DX11 didn't have one, but it's not a great implementation. It was done in a rush to fill a need for samples, but SpriteBatch / SpriteFont is a better choice.

You can also make use of Direct2D/DirectWrite through device interop with Direct3D 11, and this is the recommended solution if you need support for large fonts or complex font layouts. For basic "Hello, World" use you should start with SpriteFont.

• Hello Chuck, thanks for your reply. I am trying to figure out what's best here, I read about direct write, but wasn't so sure. Bitmapped Fonts seem very limited e.g. if I want to modify the color of in-game text. What else can you share, p.s. I read on of your answer to some similar question. But looking forward to hearing more. – John Smith Jul 7 at 23:44
• Bitmap fonts can be color-modified just like any other texture. The only real limitation with them is scaling. If you want a mix of resolutions with approximately the same looking font, you need to capture a number of point sizes. – Chuck Walbourn Jul 8 at 3:45
• Is there a way to scale it? (A better way, than making copies of different sizes). For example, SFML has so you can load a arial.ttf directly and draw text. But, I don't want to use SFML. – John Smith Jul 8 at 5:54
• TTF are 'vector' fonts, not bitmap fonts. If you want a vector font, use DirectWrite. – Chuck Walbourn Jul 8 at 8:46
• If you need vector-font scaling using True-Type Fonts, sure. It's just a bit complicated to use the API since it requires you render to a Direct2D surface, then use that surface as a Direct3D texture to get the rendered image into your scene. It works, but it's overkill for simple cases which is why there is SpriteFont. See Microsoft Docs. – Chuck Walbourn Jul 8 at 9:11