C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 24th, 2010, 01:00 AM
echelon echelon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2010
Location: Finland
Posts: 4 echelon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old May 24th, 2010, 01:15 AM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Lord of Dorkness
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,515 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 59 m 31 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
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.
Comments on this post
echelon disagrees!
__________________
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.

Reply With Quote
  #3  
Old May 24th, 2010, 01:27 AM
echelon echelon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2010
Location: Finland
Posts: 4 echelon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old May 24th, 2010, 01:59 AM
echelon echelon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2010
Location: Finland
Posts: 4 echelon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old May 24th, 2010, 10:44 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,909 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 2 h 4 m 37 sec
Reputation Power: 1774
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.
Comments on this post
echelon agrees: Thanks!
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Printing integer with TextOut()

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap