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

Started by
6 comments, last by Roger911 11 years, 9 months ago
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;
}
}
Advertisement
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

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. [font=courier new,courier,monospace]clear()[/font] does not clear the contents of the [font=courier new,courier,monospace]stringstream[/font]. It clears the error control state.

@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 reset the contents of the [font=courier new,courier,monospace]stringstream[/font] 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 [font=courier new,courier,monospace]boost::lexical_cast<>()[/font]...).
[size=2][ 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 ]

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'


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 [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])
[size=2][ 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 ]

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.

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: [font=courier new,courier,monospace]std::stringstream[/font] should be used directly.
[size=2][ 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 ]

[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.

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'
[/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 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);

}
}

This topic is closed to new replies.

Advertisement