Question

Possible Duplicate:
How do I convert a double into a string in C++?
Convert double to string C++?

Total C++ and Win32 noob here playing around in Visual Studio 2012 land, so bear with me as I sometimes bite off more than I can chew :)

I have a Win32 app that's simply set up a window using a peekmessage loop for real-time updating. The app runs okay and the Window shows fine. I'd like to print text to the Window title bar displaying the frames per second from a clock / timer class I was provided with.

I've yet to learn GDI / GDI+ or Direct2D & DirectWrite for outputting text and variable values to the client area, so for now I need a simple method of outputting some basic statistics to the Window and I figured the easiest way is to update the window title at this stage. My window class has a SetTitle method that takes a std::wstring so I was wondering how best to build a string from literal text and variable values such as double? As you can likely surmise I'm also unfamiliar with strings beyond the basic std:cout for Console-based apps.

Amazon tells me my Holy 'Book of Petzold, 5th Edition' is 3 days away so any tips will be greatly appreciated and you'll make it onto my Christmas card list this year.

Thanks.

Was it helpful?

Solution

After hunkering down and diving deeper into stream input and output, I finally found a solution that seems to work well.

After including <sstream> in my project I created a std::wstringstream myString; object.

In the real-time loop of my application I then had the following code to format the string and the value coming from the Clock class, which I then passed on to GDI to print out to the window client area:

  myString.clear(); // clear status bits
  myString.str(L""); // Clear the string for reuse
  myString << "FPS " << pGameClock->GetFrameRate();

  TextOutW(GdiDeviceContext, x_pos, y_pos,
           myString.str().c_str(),
           myString.str().length());

During my learning process I took a quick detour into simple GDI text output because I initially had problems with updating my application window title property using the winapi SetWindowTextW function. It seems it doesn't like being called at a high frequency in the main loop, resulting in my system becoming unresponsive when running the program, hence the need to learn basic GDI to outptut to my window client area rather than the window title bar.

The pGameClock->GetFrameRate(); call by the way, is courtesy of an implementation of a high resolution clock and timer class designed by the talented Noel Llopis and presented in Game Programming Gems 4. (Thanks Noel).

I've also discovered that there's still plenty to learn about techniques for more performant string formatting & building techniques, especially when applied to real-time requirements in games but I suspect I'll be better served heading over to http://gamedev.stackexchange.com (which I only recently discovered) for further questions and help?

Books that helped this C++ noob better understand input and output streams:

  • Programming Principles and Practice Using C++, Bjarne Stroustrup
  • C++ for Everyone, 2nd Edition, by Cay Horstmann

OTHER TIPS

Both for efficiency and for convenience a dedicated StringWriter class is preferable.

E.g., off the cuff,

template< class Type >
static wstring stringFrom( Type const& o )
{
    wostringstream stream;

    stream << o;
    return stream.str();
}

class StringWriter
{
    wstring s_;

    static wstring const& fastStringFrom( wstring const& s )
    { return s; }

    static wchar_t const* fastStringFrom( wchar_t const* s )
    { return s; }

    template< class Type >
    static wstring fastStringFrom( Type const& o )
    {
        return stringFrom( o );
    }

public:
    template< class Type >
    StringWriter& operator<<( Type const& o )
    {
        s_ += fastStringFrom( o );
        return *this;
    }

    operator wstring const& () const { return s_; }
    operator wchar_t const* () const { return s_.c_str(); }
};

Then use like

typedef StringWriter S;
foo( S() << L"pi = " << 3.14 );

Disclaimer: code not touched by compiler's hands.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top