Jump to content
  • Advertisement
Sign in to follow this  

[C++, GDI] TextOut advice needed

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

As you know TextOut() requires a string-type parameter. But what is a general way to output numeric values (int, float, double)? Let's say I need to output the value of cos(45) or pow(3,2). They won't be accepted coz they're not strings. I tried _itoa() but isn't there a faster solution?

Share this post

Link to post
Share on other sites
An easy way is using sprintf() to combine the various(sp?) types into
an output string, then display that string. ie:

char buf[256];

sprintf (buf, "Player %s HP %i MP %i", strPlayer.c_str(), hp, mp);

// Have TextOut() display buf

Its somewhat ugly because of the character buffer though. Nontheless,
it is standard C (itoa() is not standard, although atoi() is)

itoa() is not defined by ANSI

sprintf() is a portable alternative.

Share this post

Link to post
Share on other sites
A type-safe and much more elegant alternative to sprintf() would be to use string streams from the Standard C++ Library:

#include <sstream>
#include <string>

int HP = 100;

std::wstringstream wss;

wss << HP;

std::wstring message = L"Player's HP is " + wss.str();

TextOut(hDC, 0, 0, message.c_str(), message.length());

You can use std::stringstream and std::string if your project is not Unicode-enabled.

Hope this helps.

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!