Sign in to follow this  
Roger911

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

Recommended Posts

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.

[code]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;
}
}[/code]

Share this post


Link to post
Share on other sites
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
[CODE]xcoord.clear();[/CODE]
Edit: This is wrong, view next post
[url="http://en.cppreference.com/w/cpp/io/basic_stringstream"]http://en.cppreferen...ic_stringstream[/url] Edited by Zoomulator

Share this post


Link to post
Share on other sites
[quote name='Zoomulator' timestamp='1340583939' post='4952490']
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
[CODE]xcoord.clear();[/CODE]
[url="http://en.cppreference.com/w/cpp/io/basic_stringstream"]http://en.cppreferen...ic_stringstream[/url]
[/quote]
Close, but no cigar. [font=courier new,courier,monospace]clear()[/font] does not clear the contents of the [font=courier new,courier,monospace]stringstream[/font]. It clears the [url="http://www.cplusplus.com/reference/iostream/ios/clear/"]error control state[/url].

@Roger911: there are two ways to do this. [font=courier new,courier,monospace]stringstream[/font]s will keep storing things in them unless you: reset the [font=courier new,courier,monospace]stringstream[/font] or extract the values out. You can [url="http://www.cplusplus.com/reference/iostream/stringstream/str/"]reset the contents of the [font=courier new,courier,monospace]stringstream[/font] by doing[/url]:
[code]
xcoord.str(""); // "" will set it to an empty string
[/code]

Or you can extract out what you've put into it
[code]
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
[/code]

I'd go with the first one if I was you (actually, I'd be using something like [font=courier new,courier,monospace]boost::lexical_cast<>()[/font]...). Edited by Cornstalks

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1340584491' post='4952492']
Close, but no cigar. clear() does not clear the contents of the stringstream. It clears the error control state.
[/quote]
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 [size=5]}[/size]
I doubt there's much to gain from storing a stringstream away from its 'working area' Edited by Zoomulator

Share this post


Link to post
Share on other sites
[quote name='Zoomulator' timestamp='1340584738' post='4952493']
I usually only keep it in the scope where it's directly used and let it crumple by the wrath of the [size=5]}[/size]
[/quote]
Me too, actually. I'm not sure why I didn't suggest that, because I would actually probably do that before using the [font=courier new,courier,monospace]str("");[/font] method (and I don't think I'd ever use the extraction method) (though in reality I'd probably still use something like [font=courier new,courier,monospace]boost::lexical_cast<>()[/font])

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1340585487' post='4952499']
in reality I'd probably still use something like boost::lexical_cast<>()
[/quote]
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.

Share this post


Link to post
Share on other sites
[quote name='Zoomulator' timestamp='1340621945' post='4952614']
I'm fairly sure lexical_cast uses string stream internally anyway.
[/quote]
[url="http://accu.org/index.php/journals/1375"]It used to[/url] (though I guess technically it still does in some cases; but it's got specialized cases now). [url="http://www.boost.org/doc/libs/1_49_0/doc/html/boost_lexical_cast/performance.html"]But it's faster now[/url].

[quote name='Zoomulator' timestamp='1340621945' post='4952614']
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.
[/quote]
Yes, if you want to control precision or formatting you are quite correct: [font=courier new,courier,monospace]std::stringstream[/font] should be used directly. Edited by Cornstalks

Share this post


Link to post
Share on other sites
[quote name='Zoomulator' timestamp='1340584738' post='4952493']
[quote name='Cornstalks' timestamp='1340584491' post='4952492']
Close, but no cigar. clear() does not clear the contents of the stringstream. It clears the error control state.
[/quote]
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 [size=5]}[/size]
I doubt there's much to gain from storing a stringstream away from its 'working area'
[/quote]

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 [code]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);

}
}[/code] Edited by Roger911

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this