Sign in to follow this  
Bigfoot Nick

Passing variables through DrawText or DrawTextA

Recommended Posts

Bigfoot Nick    122
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 this post


Link to post
Share on other sites
Molle85    172
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 this post


Link to post
Share on other sites
Evil Steve    2017
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 this post


Link to post
Share on other sites
MJP    19786
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 stringstreams
std::ostringstream ss ("Posx: ");
ss << posx;
dxfont->DrawTextA(NULL, ss.str().c_str() ... );

// using boost::lexical_cast
string 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 this post


Link to post
Share on other sites
Bigfoot Nick    122
@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.

Share this post


Link to post
Share on other sites

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