Display text with DX11

I am trying to figure out how to draw text on the screen in SlimDX.

Initial studies are not encouraging. The only concrete example I found was the following:

http://www.aaronblog.us/?p=36

I am trying to port this to my code, but it is very difficult, and I start to wonder after 4 hours if this is the right way.

Essentially, it’s quite difficult to do something as simple as writing text on the screen. Aaron's method seems to be the only one that is practical at the moment. There are no other comparison points.

Does anyone else have any advice that they could offer?

PS I suspect that I could create separate images for letters, and encoded a procedure for converting a string into a series of sprites. It seems a little crazy to do it though.

+4
source share
2 answers

Rendering text is a bit complicated. The only way to make general text is to use Direct2D / DirectWrite. And this is only supported in DirectX10. Therefore, you need to create a DirectX 10 device, DirectWrite, and Direct2D Factory. You can then create a general texture that can be used by both DirectX 10 and 11 devices.

This texture will contain the text after rendering. Aaron uses this texture to blend it with a full screen. Therefore, you can use DirectWrite to draw full lines. In essence, this is similar to drawing a textured square.

Another way to do this, as you already mentioned, is to draw a sprite sheet and break the lines into several sprites. I assume the latter method is a bit faster, but I have not tested it yet. I used this method in Sprite and Text engine . See AssertDevice and CreateCharTable Methods

+4
source

There's a nice DirectWrite cover called http://fw1.codeplex.com/

This is in C ++, but it’s very simple to make a mixed wrapper for it (this is what I did for my C # projects).

Here is a simple example:

.h file

 #pragma once #include "Lib/FW1FontWrapper.h" using namespace System::Runtime::InteropServices; public ref class DX11FontWrapper { public: DX11FontWrapper(SlimDX::Direct3D11::Device^ device); void Draw(System::String^ str,float size,int x,int y,int color); private: SlimDX::Direct3D11::Device^ device; IFW1FontWrapper* pFontWrapper; }; 

.cpp file

 #include "StdAfx.h" #include "DX11FontWrapper.h" DX11FontWrapper::DX11FontWrapper(SlimDX::Direct3D11::Device^ device) { this->device = device; IFW1Factory *pFW1Factory; FW1CreateFactory(FW1_VERSION, &pFW1Factory); ID3D11Device* dev = (ID3D11Device*)device->ComPointer.ToPointer(); IFW1FontWrapper* pw; pFW1Factory->CreateFontWrapper(dev, L"Arial", &pw); pFW1Factory->Release(); this->pFontWrapper = pw; } void DX11FontWrapper::Draw(System::String^ str,float size,int x,int y, int color) { ID3D11DeviceContext* pImmediateContext = (ID3D11DeviceContext*)this->device->ImmediateContext->ComPointer.ToPointer(); void* txt = (void*)Marshal::StringToHGlobalUni(str); pFontWrapper->DrawString(pImmediateContext, (WCHAR*)txt, size, x, y, color, 0); Marshal::FreeHGlobal(System::IntPtr(txt)); } 
+3
source

Source: https://habr.com/ru/post/1432157/


All Articles