【问题标题】:Rendering text with DX11使用 DX11 渲染文本
【发布时间】:2012-09-03 21:28:03
【问题描述】:

我想弄清楚如何在 SlimDX 中在屏幕上绘制文本。

最初的研究并不令人鼓舞。我发现的唯一具体例子是:

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

我正在尝试将其移植到我的代码中,但事实证明这非常困难,4 小时后我开始怀疑这是否是正确的方法。

从本质上讲,像在屏幕上写文本这样简单的事情似乎很困难。 Aaron 的方法似乎是目前唯一可行的方法。没有其他比较点。

还有其他人可以提供什么建议吗?

附:我怀疑我现在可以为字母创建单独的图像,并编写一个例程将字符串转换为一系列精灵。不过,这样做似乎有点疯狂。

【问题讨论】:

    标签: c# text directx slimdx directx-11


    【解决方案1】:

    渲染文本确实有点复杂。呈现一般文本的唯一方法是使用 Direct2D / DirectWrite。这仅在 DirectX10 中受支持。所以你必须创建一个 DirectX 10 设备、DirectWrite 和 Direct2D 工厂。然后,您可以创建可供 DirectX 10 和 11 设备使用的共享纹理。

    此纹理将包含渲染后的文本。 Aaron 使用此纹理将其与全屏融合。因此,您可以使用 DirectWrite 绘制完整的字符串。本质上,这就像绘制一个带纹理的四边形。

    另一种方法是,正如您已经提到的,绘制一个精灵表并将字符串分成几个精灵。我认为,后一种方式要快一些,但我还没有测试过。我在Sprite & Text engine 中使用了这种方法。见方法AssertDeviceCreateCharTable

    【讨论】:

    • 谢谢尼科!看起来你在这方面领先于游戏。一旦我将 Aaron 的功能移植到我的框架中,我将更新我的自定义精灵类。 8D
    【解决方案2】:

    有一个不错的 DirectWrite 包装器,名为 http://fw1.codeplex.com/

    它在 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));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多