Rendering Text, need help [solved]

Started by
5 comments, last by nothingalike 12 years, 9 months ago
This is probably really simple, so hopefully i can get some help.

I'm trying to add a score board to my pong clone. I have all the movement input and collision detection setup, but i can't get the score to act right. I put the draw score function which is



score = r_Score.GetScore();
//Generate the message surface
show_score = TTF_RenderText_Solid( font, score.c_str(), textColor );

apply_surface(500, 20, show_score, screen);

SDL_FreeSurface(show_score);



but when the screen updates it doesn't just keep updating the score at 500,20 it just repeats it along the X-axis.

Any help would be great. Thanks.
Advertisement
What do you mean by "it just repeats it along the X-axis"?

This is probably really simple, so hopefully i can get some help.

I'm trying to add a score board to my pong clone. I have all the movement input and collision detection setup, but i can't get the score to act right. I put the draw score function which is



score = r_Score.GetScore();
//Generate the message surface
show_score = TTF_RenderText_Solid( font, score.c_str(), textColor );

apply_surface(500, 20, show_score, screen);

SDL_FreeSurface(show_score);



but when the screen updates it doesn't just keep updating the score at 500,20 it just repeats it along the X-axis.

Any help would be great. Thanks.


have you checked the content of the score variable you get with r_Score.GetScore() ?
if your rendered text "repeats" along the x axis its quite likely that you are appending a string rather than adding to a integer:

for example, string score1 = "3";
score1+"2" is not "5" , its "32";

Ofcourse i'm guessing a bit wildly here as i can't see the rest of your code. (The r_Score class is probably where the error is, or possibly in the apply_surface function, allthough i'm guessing thats taken from lazy foo so it should be good)

If my guess is correct you should store the score as an integer and use a stringstream object to convert it to a string when its time to render it.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
[font=arial, verdana, tahoma, sans-serif][size=2]I just realized im also a noobie at forums, sorry.

the repeat along the X-axis looks like thing, assuming one score is 0. [/font]

00000000000000000000000000000000000000000

and here is the code for GetScore.() and Add()



void Score::Add()
{
m_Score += 1;
}

std::string Score::GetScore()
{
m_SS << m_Score;
return m_SS.str();
}



Hopefully that helps, now im going to go read the how-to on operating a forum lol
I think you may be right about my Score class so i posted it. I've never really dealt with stringstream so i think i maybe doing it wrong.






class Score
{
public:
Score(int x, int y);
virtual ~Score();
void Add();
std::string GetScore();
void draw();

protected:
int m_Score, xSet, ySet;
std::stringstream m_SS;
};
Score::Score(int x, int y)
{
m_Score = 0;
xSet = x;
ySet = y;
}

Score::~Score(){}

void Score::Add()
{
m_Score += 1;
}

std::string Score::GetScore()
{
m_SS << m_Score;
return m_SS.str();
}
void Score::draw()
{
std::string score = GetScore();
//Generate the message surface
SDL_Surface* show_score = TTF_RenderText_Solid( font, score.c_str(), textColor );

apply_surface(xSet, ySet, show_score, screen);

SDL_FreeSurface(show_score);
}[/quote]
Since you are using the same stringstream every time in GetScore, you are just appending the score onto the end of the stringstream after each frame. One way to fix this would be to clear the stringstream before using it each time, so changing GetScore to something like


std::string Score::GetScore()
{
m_SS.str("");
m_SS << m_Score;
return m_SS.str();
}
Worked like a charm. Thanks.

Ok I'm guessing thats not really a prime way of constructing that class. Do you have any advice on restructuring it?

This topic is closed to new replies.

Advertisement