• Advertisement
Sign in to follow this  

Drawing Integer values with Gdiplus::DrawString()

This topic is 3481 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

Hi, ive looked everywhere and cannot figure out how you draw integer values to a window in GDI+ (or GDI). For example i need to draw the players score on the screen but to do this im going to want an integer variable for the players score and so on. but i cant find any function that will allow me to draw it to screen. I basically need a way to convert my 'real' values to 'string' values. What am i expected to do here? Thanks ~Reegan

Share this post


Link to post
Share on other sites
Advertisement
std::stringstream, or sprintf if you're using plain C.


#include <sstream>

int player_score = 42;
std::stringstream str;
str << player_score;

DrawText(..., str.str().c_str(), ...);

Share this post


Link to post
Share on other sites
Hmmm, that looks.. odd never seen that done before :P
in console app i get the address of 'str' when using

std::cout << str

Will it be the same using the DrawString function in GDI+?
lol 'sprintf' such a disgusting looking function, you can tell that one isnt modern.

Share this post


Link to post
Share on other sites
Quote:
Original post by Reegan
Hmmm, that looks.. odd never seen that done before :P
in console app i get the address of 'str' when using

std::cout << str


str is a stringstream. Why were you expecting anything sane by outputting it?

Quote:
Will it be the same using the DrawString function in GDI+?
lol 'sprintf' such a disgusting looking function, you can tell that one isnt modern.


To be exact, sprintf does have a cleaner interface than streams. However, for meager technical limitations of the C++ type system, it's preferable to use streams. In PHP or Objective Caml, sprintf is the superior alternative because it's more concise.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by Reegan
Hmmm, that looks.. odd never seen that done before :P
in console app i get the address of 'str' when using

std::cout << str


str is a stringstream. Why were you expecting anything sane by outputting it?

To connect the dots for the OP, Evil Steve's example involved more than just trying to output the value of str:
Quote:
str.str().c_str()

The first ".str( )" returns a std::string with the contents of the stringstream, which would be suitable for sending to std::cout or another stream. ".c_str( )" is called on that returned std::string, which returns a pointer to a null terminated array of elements, likely some flavor of char, which is what DrawString wants from you (I assume Evil Steve got it right, I don't have any experience with it).

So, follow the full example Evil Steve posted, and then read up on stringstreams.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement