【问题标题】:DirectX Font not drawing?DirectX字体不绘图?
【发布时间】:2013-04-09 13:31:36
【问题描述】:

我正在使用 DirectX、C++ 制作游戏,其中不同部分位于类中。目前我正在做一个字体类,但是当我去绘制一个字符串时,它没有显示出来,我不知道为什么。非常感谢任何帮助。

字体.h

class d2Font
{
public:
    d2Font(void);
    ~d2Font(void);

    void Create(string name, int size, LPDIRECT3DDEVICE9 device);
    void Draw(string text, int x, int y, int width, int height, DWORD format = DT_LEFT, D3DCOLOR colour = D3DCOLOR_XRGB(255, 255, 255));

private:
    LPD3DXFONT font;
};

字体.cpp

d2Font::d2Font(void)
{
font = NULL;
}

d2Font::~d2Font(void)
{
if(font)
    font->Release();
}

void d2Font::Create(string name, int size, LPDIRECT3DDEVICE9 device)
{
LPD3DXFONT tempFont = NULL;
D3DXFONT_DESC desc = {
    size,
    0,
    0,
    0,
    false,
    DEFAULT_CHARSET,
    OUT_TT_PRECIS,
    CLIP_DEFAULT_PRECIS,
    DEFAULT_PITCH,
    (char)name.c_str()
};
D3DXCreateFontIndirect(device, &desc, &tempFont);

font = tempFont;
}

void d2Font::Draw(string text, int x, int y, int width, int height, DWORD format, D3DCOLOR colour)
{
RECT rect = {x, y, width, height};
//SetRect(&rect, x, y, width, height);

font->DrawText(NULL, text.c_str(), -1, &rect, format, colour);
}

编辑: 这是main.cpp中的代码

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
gameHinstance = hInstance;

gameMain = new d2Main();
testFont = new d2Font();

if(!gameMain->NewWindow(hInstance, hWnd, "Test Game", 800, 400, nCmdShow, false))
{
    MessageBox(NULL, "Error! Unable to create window!", "D2EX", MB_OK | MB_ICONASTERISK);
    return 0;
}

gameMain->GameRunning = true;

testFont->Create("Arial", 12, gameMain->dx->d3ddev);

gameMain->GameLoop();

return 0;
}

void d2Main::GameUpdate()
{
gameMain->dx->d3ddev->BeginScene();
testFont->Draw("HelloWorld!", 10, 10, 200, 30, DT_LEFT, D3DCOLOR_XRGB(0, 255, 0));
gameMain->dx->d3ddev->EndScene();
}

【问题讨论】:

    标签: c++ text fonts directx game-engine


    【解决方案1】:

    字体描述符中显然有一些错误的字段。一个是重量,正如 Roger Rowland 所提到的。另一个是最后一个,FaceName(字体名称)。您正在尝试将指针转换为 char,这会产生不好的结果。如果您的项目配置为使用 Unicode(Visual Studio 中大多数项目类型的默认设置),则 FaceName 成员将是一个 WCHAR 数组,因此您应该使用 wstring。另一件事是您应该检查 D3DXCreateFontIndirect 的返回值(以及任何其他返回 HRESULT 的 D3D 函数和方法):

    HRESULT d2Font::Create(const wstring& name, int size, LPDIRECT3DDEVICE9 device)
    {
        D3DXFONT_DESC desc = {
            size,
            0,
            400,
            0,
            false,
            DEFAULT_CHARSET,
            OUT_TT_PRECIS,
            CLIP_DEFAULT_PRECIS,
            DEFAULT_PITCH
        };
    
        wcscpy_s(desc.FaceName, LF_FACESIZE, name.c_str());
    
        HRESULT hr = D3DXCreateFontIndirect(device, &desc, &font);
        if (FAILED(hr))
            return hr;
    
        return S_OK;
    }
    

    【讨论】:

    • 您好,感谢您的回答,但我使用的是多字节字符集,因此这不起作用。使用我当前的版本,我检查了 HRESULT,一切正常。
    • @OnlyAntony 所以使用 string 和 mbscpy_s 而不是 wcscpy_s。
    • 对不起,这不起作用。我已经包含了 然后使用: _mbscpy_s(desc.FaceName, LF_FACESIZE, name.c_str());我已经尝试了 desc.Filename & name witha & 在它之前,还从名称中删除了 c._str() 。它显示的错误是:error C2664: 'errno_t _mbscpy_s(unsigned char *,size_t,const unsigned char *)' : cannot convert parameter 1 from 'CHAR [32]' to 'unsigned char *'
    • 在这种情况下,您只需将参数转换为无符号字符:_mbscpy_s((unsigned char*)desc.FaceName, LF_FACESIZE, (unsigned char*)name.c_str());
    • 我已经尝试了上述方法,但仍然无法正常工作。所以我要重新开始,谢谢你的帮助。
    【解决方案2】:

    您似乎为字体粗细指定了零。试试这样的

    D3DXFONT_DESC desc = {
    size,
        0,
        0,
        400,
        false,
        DEFAULT_CHARSET,
        OUT_TT_PRECIS,
        CLIP_DEFAULT_PRECIS,
        DEFAULT_PITCH,
        (char)name.c_str()
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 1970-01-01
      相关资源
      最近更新 更多