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.
[1] https://github.com/microsoft/DirectXTK/wiki/SpriteBatch
[2] https://github.com/microsoft/DirectXTK/wiki/SpriteFont
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:
- Is using these new recommendations (SpriteBatch, SpriteFont & DirectXDTK) by Microsoft, in any way, linking the stuff I build with XNA?
- 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:
- ID3D11VertexShader
- ID3D11PixelShader
- ID3D11InputLayout
- ID3D11Buffer
- ID3D11ShaderResourceView
- ID3DX11CreateShaderResourceViewFromFile
- ID3D11SamplerState
- D311_MAPPED_SUBRESOURCE
- D311_MAP_WRITE_DISCARD
EDIT #2: WHY DO I HAVE TO CAST, Microsoft::WRL::ComPtr