2013-04-09 55 views
0

我想提出一個遊戲在DirectX,C++用的不同部分在課堂之中。目前我正在做一個字體類,但是當我去繪製一個字符串時,它不顯示,我不知道爲什麼。很感謝任何形式的幫助。DirectX字體沒有繪圖?

font.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; 
}; 

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(); 
} 

回答

1

看起來你指定一個零字體重量。嘗試像這樣

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

字體描述符中顯然有一些錯誤的字段。一個是羅傑羅蘭提到的重量。另一個是最後一個,FaceName(字體名稱)。你試圖將一個指針指向一個char,這會導致不好的結果。如果您的項目配置爲使用Unicode(默認在Visual Studio中大多數項目類型),該面名成員將WCHARs數組,所以你應該使用wstring的。另一件事是,你應該檢查D3DXCreateFontIndirect的返回值(以及任何其他D3D函數和方法返回一個HRESULT):

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; 
} 
+0

您好,感謝您的答案,但我使用的是多字節字符集因此這不起作用。用我目前的版本,我檢查了HRESULT,並沒有問題。 – Antony 2013-04-10 10:58:02

+1

@OnlyAntony因此,使用** **字符串和mbscpy_s代替wcscpy_s。 – user1610015 2013-04-10 12:03:13

+0

對不起,這不起作用。我已經包括,然後使用:_mbscpy_s(desc.FaceName,LF_FACESIZE,name.c_str());我曾嘗試desc.Filename和名稱難熬&之前,也移除了名的c._str()。它顯示的錯誤是:錯誤C2664: 'errno_t _mbscpy_s(無符號字符*,爲size_t,const的無符號字符*)':無法從 'CHAR [32]' 到 '無符號字符*' – Antony 2013-04-10 14:54:23