\$10

### Image of the Day Submit

IOTD | Top Screenshots

## c++, SDL, sstream, and brain farts.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1Roger911  Members

Posted 24 June 2012 - 03:00 PM

Hey guys! long time lurker 1st time poster here,

Goal : Basically, what I'm trying to do is use SDL to make a window, then use SDL_ttf to print out the xy coordinates of the mouse in said window.

Issue : Instead of the current value of the stream removing then replacing the old value (aka updating), it just piles up (EG. moving mouse from left to right should change it from 1 to 2 to 3 etc. instead it shows 123).

here's the piece of code that I'm working with.

while(SDL_PollEvent(&event))
{
if(event.type == SDL_MOUSEMOTION)
{
SDL_FreeSurface(Mousecoords);
if(SDL_Flip(screen) == -1)
{
return 0;
}
int x = 0;
int y = 0;
x = event.motion.x;
y = event.motion.y;
xcoord << x;
ycoord << y;
Mousecoords = TTF_RenderText_Solid(font, xcoord.str().c_str(), textColor);
apply_surface(0,0,Mousecoords,screen);
if(SDL_Flip(screen) == -1)
{
return 0;
}
}

### #2Zoomulator  Members

Posted 24 June 2012 - 06:25 PM

That's just how stringstream works. You use the << operator to fill it up, use the string some where and then you gotta clear it.

Simply call
xcoord.clear();
Edit: This is wrong, view next post
http://en.cppreferen...ic_stringstream

Edited by Zoomulator, 24 June 2012 - 06:43 PM.

### #3Cornstalks  Members

Posted 24 June 2012 - 06:34 PM

That's just how stringstream works. You use the << operator to fill it up, use the string some where and then you gotta clear it.

Simply call

xcoord.clear();
http://en.cppreferen...ic_stringstream

Close, but no cigar. clear() does not clear the contents of the stringstream. It clears the error control state.

@Roger911: there are two ways to do this. stringstreams will keep storing things in them unless you: reset the stringstream or extract the values out. You can reset the contents of the stringstream by doing:
xcoord.str(""); // "" will set it to an empty string


Or you can extract out what you've put into it
xcoord << x; // put your data into it
Mousecoords = TTF_RenderText_Solid(font, xcoord.str().c_str(), textColor); // use your data
xcoord >> x; // extract the data back out of it so it doesn't stay buffered up


I'd go with the first one if I was you (actually, I'd be using something like boost::lexical_cast<>()...).

Edited by Cornstalks, 24 June 2012 - 06:37 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #4Zoomulator  Members

Posted 24 June 2012 - 06:38 PM

Close, but no cigar. clear() does not clear the contents of the stringstream. It clears the error control state.

Oops.. poor observation from my part. I usually never reuse string streams that way.

I usually only keep it in the scope where it's directly used and let it crumple by the wrath of the }
I doubt there's much to gain from storing a stringstream away from its 'working area'

Edited by Zoomulator, 24 June 2012 - 06:41 PM.

### #5Cornstalks  Members

Posted 24 June 2012 - 06:51 PM

I usually only keep it in the scope where it's directly used and let it crumple by the wrath of the }

Me too, actually. I'm not sure why I didn't suggest that, because I would actually probably do that before using the str(""); method (and I don't think I'd ever use the extraction method) (though in reality I'd probably still use something like boost::lexical_cast<>())
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #6Zoomulator  Members

Posted 25 June 2012 - 04:59 AM

in reality I'd probably still use something like boost::lexical_cast<>()

I do too, as long as I already use boost for something else in the project. I try to avoid using boost solely for smaller things.

If you gotta boost though: you gotta boost.

I'm fairly sure lexical_cast uses string stream internally anyway. Just makes it a pretty package to slap on to a single line.

Since xcoord is an int, it doesn't matter much.. but if it were a float you might want to use string stream directly to lessen the precision a bit.

### #7Cornstalks  Members

Posted 25 June 2012 - 01:34 PM

I'm fairly sure lexical_cast uses string stream internally anyway.

It used to (though I guess technically it still does in some cases; but it's got specialized cases now). But it's faster now.

Since xcoord is an int, it doesn't matter much.. but if it were a float you might want to use string stream directly to lessen the precision a bit.

Yes, if you want to control precision or formatting you are quite correct: std::stringstream should be used directly.

Edited by Cornstalks, 25 June 2012 - 01:35 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #8Roger911  Members

Posted 25 June 2012 - 08:50 PM

Close, but no cigar. clear() does not clear the contents of the stringstream. It clears the error control state.

Oops.. poor observation from my part. I usually never reuse string streams that way.

I usually only keep it in the scope where it's directly used and let it crumple by the wrath of the }
I doubt there's much to gain from storing a stringstream away from its 'working area'

Yeah, this is actually what I ended up doing. Learning to use new libs while tired makes for a frustrating piece of code.

Also, I found out that you can't use multiple variables of text with the TTF_RenderText function, so I ended up dropping xcoord and ycoord, and replaced them with something like
void widgets::getmousecoords()
{
if(event.type == SDL_MOUSEMOTION)
{
x = event.motion.x;
y = event.motion.y;
std::stringstream mousecoords;
mousecoords << x << " " << y;
Mousecoords = TTF_RenderText_Solid(font8, mousecoords.str().c_str(), textColor);

}
}

Edited by Roger911, 25 June 2012 - 08:53 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.