• 12
• 11
• 9
• 10
• 13

using strings in DrawText

This topic is 4377 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm trying to plug std::string into the LPD3DXFONT::DrawText function. I can't seem to find a proper method of inputting my string into the DrawText function. Here's my code:
g_font->DrawText(g_sprite, sFPS.c_str(), -1, &rText, DT_LEFT|DT_NOCLIP, 0xFF000000)


That produces the following error: error C2664: 'ID3DXFont::DrawTextW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR' Now I think this may have something to do with the unicode nature of my coding. I can't just turn off unicode in my project properties because I've built all my code around unicode compatibility, and to turn that off invalidates a lot of my code. Does anyone know a good way to turn my string into something more LPCWSTR compatible?

Share on other sites
The compiler assumes this is a unicode build, and typedefs DrawText to DrawTextW. Try using DrawTextA instead of DrawText.

g_font->DrawTextA(g_sprite, sFPS.c_str(), -1, &rText, DT_LEFT|DT_NOCLIP, 0xFF000000)

Allan

Share on other sites
If at some time you actually want to convert to and from actual wide-character (aka unicode) strings, you have these APIs available:

- MultiByteToWideChar()
- WideCharToMultiByte()

Share on other sites
But then I lose support for inputting unicode characters, right? Is there any conversion so I can put this string into a unicode function? Maybe something that will convert the string to a wchar_t?

Share on other sites
If you're using unicode, you should be using std::wstring. Alternatively, the function that circlesoft mentioned (MultiByteToWideChar()) will do the job

Share on other sites
This snippet should work.

There's a couple of optimizations available (drop the copy-constructor by passing the wstring by reference), and whether you want to create a hard limit for the number of characters is a personal choice.

std::wstring		ConvertAtoW(const std::string& str){	if (str.empty()) return L"";	const unsigned short	UNICODE_C = 1024;	unsigned short	UnicodeA[UNICODE_C ];	size_t	StrLen = str.size();	assert(StrLen < UNICODE_C); 	memset(UnicodeA, 0, sizeof(UnicodeA));	MultiByteToWideChar(CP_UTF8, 0,str.c_str(), StrLen, UnicodeA, UNICODE_C);	std::wstring	Uni = std::wstring(UnicodeA);	return Uni;}

Allan