Skip to main content
GameDev.net gamedev.net
🔒 Locked

using strings in DrawText

Started by pteri498 Mar 26, 2006 at 11:44 PM 4 replies 1.4k views
Original Post
pteri498
pteri498
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?
__ODIN__
__ODIN__
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
circlesoft
circlesoft
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()
pteri498
pteri498
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?
Evil Steve
Evil Steve
If you're using unicode, you should be using std::wstring. Alternatively, the function that circlesoft mentioned (MultiByteToWideChar()) will do the job
__ODIN__
__ODIN__
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.