Sign in to follow this  

using strings in DrawText

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this