我正在尝试弄清楚如何在SlimDX中在屏幕上绘制文本。
最初的研究并不令人鼓舞。我找到的唯一一个具体的例子是:
http://www.aaronblog.us/?p=36
我试图将它移植到我的代码中,但它被证明是极其困难的,4小时后我开始怀疑这是不是正确的方法。
从本质上讲,要做像在屏幕上写文本这样简单的事情似乎相当困难。Aaron的方法似乎是目前唯一实用的方法。没有其他的比较点。
其他人有什么可以提供的建议吗?
另外,我怀疑我现在已经可以为字母创建单独的图像,并编写了一个例程将字符串转换为一系列的精灵。不过,这样做看起来确实有点疯狂。
发布于 2012-09-04 17:03:14
渲染文本确实有点复杂。呈现一般文本的唯一方法是使用Direct2D / DirectWrite。而且这只在DirectX10中受支持。所以你必须创建一个DirectX 10设备、DirectWrite和Direct2D工厂。然后,您可以创建可由DirectX 10和11设备使用的共享纹理。
此纹理将包含渲染后的文本。Aaron使用此纹理将其与全屏混合。因此,您可以使用DirectWrite来绘制完整的字符串。从本质上讲,这就像绘制一个纹理四边形。
另一种方法是,正如你已经提到的,绘制一个精灵工作表,并将字符串分割成几个精灵。我假设,后一种方法更快一点,但我还没有测试过它。我在我的Sprite & Text engine中使用了这个方法。请参见方法AssertDevice和CreateCharTable
发布于 2012-09-06 17:38:35
有一个名为http://fw1.codeplex.com/的很好的DirectWrite包装器
它是用c++编写的,但是为它制作一个混合模式的包装器非常简单(这就是我为我的c#项目所做的)。
下面是一个简单的例子:
.h文件
#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文件
#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));
}https://stackoverflow.com/questions/12254201
复制相似问题