The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Printing integer with TextOut()
Discuss Printing integer with TextOut() in the C Programming forum on Dev Shed. Printing integer with TextOut() C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 24th, 2010, 01:00 AM
|
|
Registered User
|
|
Join Date: May 2010
Location: Finland
Posts: 4
Time spent in forums: 1 h 43 m 42 sec
Reputation Power: 0
|
|
|
Printing integer with TextOut()
Hi everyone,
I'm programming a Tetris clone with Visual C++ and my problem is printing score with TextOut() function. I've created a generic template class for string conversion and trying to use stringstream to help with conversion. Here's the code I've come up with:
Code:
template <class T>
std::string to_string(T t, std::ios_base & (*f)(std::ios_base&))
{
std::ostringstream outputStream;
outputStream << f << t;
return outputStream.str();
}
LPCTSTR lpString = "Score:";
LPCTSTR stBuffer;
int iScore = 0;
std::stringstream oss;
stBuffer << to_string <int> (iScore, std::dec);
stBuffer = (LPCTSTR)oss.str();
TextOut(hdc, 175, 350, stBuffer, strlen(stBuffer));
Above technique works fine in the console, but when trying to make it work on Windows application, the code won't even compile. Any ideas to help with the current code? Or maybe completely different approach to print out the score?
|

May 24th, 2010, 01:15 AM
|
 |
Lord of Dorkness
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
Exactly how did you try to move this to a GUI environment? The code you show is, quite frankly, worthless.
A console app is, generally speaking, a series of steps from here to there. A GUI app is more likely to be driven by events. It isn't a tough transition, but its different.
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.
|

May 24th, 2010, 01:27 AM
|
|
Registered User
|
|
Join Date: May 2010
Location: Finland
Posts: 4
Time spent in forums: 1 h 43 m 42 sec
Reputation Power: 0
|
|
I do text drawing in the TheWindowProc CALLBACK function when receiving message WM_PAINT. My WM_PAINT event handling is as follows.
Code:
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd,&ps);
BitBlt(hdc, 0, 0, TILESIZE*MAPWIDTH+TILESIZE*GREY, TILESIZE*MAPHEIGHT, bmoMap, 0, 0, SRCCOPY);
LPCTSTR lpString = "Score:";
LPCTSTR stBuffer;
int iScore = 0;
std::stringstream oss;
stBuffer << to_string <int> (iScore, std::dec);
stBuffer = (LPCTSTR)oss.str();
TextOut(hdc, 175, 300, lpString, 6);
TextOut(hdc, 175, 350, stBuffer, strlen(stBuffer));
EndPaint(hwnd, &ps);
return 0;
} break;
First TextOut which prints out the string "Score:" works fine, so the actual problem is somehow to convert int to LPSTRING.
|

May 24th, 2010, 01:59 AM
|
|
Registered User
|
|
Join Date: May 2010
Location: Finland
Posts: 4
Time spent in forums: 1 h 43 m 42 sec
Reputation Power: 0
|
|
Ok, I was able to figure it out. The solution was as simple as just use sprintf in the following way...
Code:
sprintf(stBuffer, "%i", iScore);
...and then just print stBuffer with TextOut.
|

May 24th, 2010, 10:44 AM
|
 |
Contributed User
|
|
|
|
http://msdn.microsoft.com/en-us/lib...28VS.85%29.aspx
Quote:
LPCSTR
Pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters. For more information, see Character Sets Used By Fonts.
This type is declared in WinNT.h as follows:
Code:
typedef __nullterminated CONST CHAR *LPCSTR;
LPCTSTR
An LPCWSTR if UNICODE is defined, an LPCSTR otherwise.
This type is declared in WinNT.h as follows:
Code:
#ifdef UNICODE
typedef LPCWSTR LPCTSTR;
#else
typedef LPCSTR LPCTSTR;
#endif
|
> sprintf(stBuffer, "%i", iScore);
1. If you didn't allocate any space for this (it's just a raw pointer of some sort), you've just trashed some memory.
Sooner or later, this will crash - mostly close to the release deadline (or in a demo) for various unexplained reasons.
2. You've declared it as the "T" variant of the type, which means everything changes automatically if you enable UNICODE.
But that only works if you've used all the other UNICODE wrappers for all the functions which use said variables.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|