2013-04-09 3 views
0

저는 DirectX, C++에서 게임을 만들고 있는데, 그 중 여러 부분이 클래스에 포함되어 있습니다. 현재 글꼴 클래스를하고 있지만 문자열을 그릴 때 표시되지 않으며 이유가 없습니다. 어떤 도움이라도 대단히 감사합니다. font.hDirectX 글꼴이 그리기가 아닙니까?

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

편집

: 여기 은 제로에 대한 지정과 같은

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

것 같습니다 MAIN.CPP의 코드입니다 글꼴 체중. 이 같은 것을 시도하십시오

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

글꼴 설명자에 분명히 잘못된 필드가 있습니다. 하나는 Roger Rowland가 언급 한 무게입니다. 다른 하나는 FaceName (글꼴 이름)입니다. char에 대한 포인터를 캐스팅하려고합니다. 결과가 좋지 않습니다. 프로젝트가 유니 코드 (Visual Studio에서 대부분의 프로젝트 유형에 대한 기본값)를 사용하도록 구성되어 있으면 FaceName 멤버가 WCHAR의 배열이되므로 wstring을 사용해야합니다. 다른 점은 (는 HRESULT를 반환뿐만 아니라 다른 D3D 함수 및 메서드) D3DXCreateFontIndirect의 반환 값을 확인해야한다는 것입니다 :

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

+1

@OnlyAntony 따라서 wcscpy_s 대신 ** string ** 및 mbscpy_s를 사용하십시오. – user1610015

+0

죄송합니다. 현재 작동하지 않습니다. 나는 을 포함하고 다음을 사용합니다 : _mbscpy_s (desc.FaceName, LF_FACESIZE, name.c_str()); 나는 desc.Filename & name witha &를 시도하고 이름에서 c._str()을 제거했다. 오류 C2664 : 'errno_t _mbscpy_s (unsigned char *, size_t, const unsigned char *)': 매개 변수 1을 'CHAR [32]'에서 'unsigned char *'로 변환 할 수 없음 – Antony