# Passing variables through DrawText or DrawTextA

## Recommended Posts

I'm a DX9 newbie (hmm... this late in the game I should be learning dx10 huh...?) and I'm trying to use DrawText (or DrawTextA ) to write a variable to the screen, but it's erroring out with "error C2664: 'ID3DXFont::DrawTextA' : cannot convert parameter 2 from 'float' to 'LPCSTR'" That line looks like this: dxfont->DrawTextA(NULL, posx, 6, &textbox, DT_CENTER | DT_VCENTER, D3DCOLOR_ARGB(255,255,255,255)); "posx" is a static float. I'm evidently missing a step, can anybody point me in the right direction? Does DrawText not allow me to do that? Or should I be looking for a way to convert the float to a LPCSTR before I call DrawTextA? Thanks for any and all help Bigfoot

##### Share on other sites
you cant pass a float directly to DrawText.
you have to use a char buffer.

char buffer[32] = {0};
sprintf( buffer, "Pos: %f", posx );

dxfont->DrawTextA(NULL, buffer, 6, &textbox, DT_CENTER | DT_VCENTER, D3DCOLOR_ARGB(255,255,255,255));

And you don't need to start at DX10, DX9 will still be used for many more years to come, so don't worry.

##### Share on other sites
I'd strongly recommend taking a step back and leaving D3D for a few months. All the SDK samples and documentation assumes you have a good grasp of C++ and a reasonable grasp of Win32 - converting a float, int, whatever to a string (Well, char*) is fairly basic C/C++, and if you throw yourself in at the deep end like this you're just going to end up getting very confused.
I'd recommend getting a good grasp of text based C/C++ before moving on with D3D. I don't mean to put you off, but D3D is pretty advanced, and you're just going to cause yourself problems if you try to tackle it head on.

##### Share on other sites
LPCSTR is a typedef for "const char*", which in the Windows API (which is designed for C) is used to represent strings. Since you're surely using C++, you should be using std::string (or std::wstring for Unicode) for representing strings.

Now obviously a "float" is not a string, which means you need to convert it to one. Molle85 has shown one way to do this, but sprintf is a C Standard Library function and therefore operates on arrays of chars (and we don't like doing this in C++, for a lot of reasons). Instead you can use std::stringstream or the ever-popular boost::lexical_cast. They're easy and flexible.

// using stringstreamsstd::ostringstream ss ("Posx: ");ss << posx;dxfont->DrawTextA(NULL, ss.str().c_str() ... );// using boost::lexical_caststring posString = "Pos: " + boost::lexical_cast<std::string, float>(posx);dxfont->DrawTextA(NULL, posString.c_str() .. );

If these concepts are over your head, I suggest taking more time to learn C++ before getting into D3D. Because things will only get worse.

##### Share on other sites
@Molle & MJP:
Thanks, I'll give those a shot and see what works best for my project.

@Evil Steve & MJP:
Thanks also, I can see that I'm putting the "cart before the horse" and need to go finish my C++ training -- DirectX is just so much fun though! :) Thanks for the constructive criticism... that's why this is a great forum.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628349
• Total Posts
2982210

• 10
• 9
• 24
• 11
• 9