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.
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 astd::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:
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
SetWindowTextWfunction. 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: