SFML community forums

Help => Graphics => Topic started by: Dannehood on April 08, 2012, 06:42:46 pm

Title: Help needed: Display variable(int) in sf::string
Post by: Dannehood on April 08, 2012, 06:42:46 pm
I am making a game in c++ sfml 1.6 and I have encountered a problem.

What I am trying to achieve

Add a life indicator in the top right corner by drawing a string that has the value of the life variable.

What I have done so far

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    int life = 3; // decides how much health you got left (the life variable)
   
    std::ostringstream oss;
    oss << life;
   
    sf::String life_string;
    life_string.SetText(oss.str());
   
    // in the game loop
   
    App.Draw(life_string);

    // in the game loop if my character is dead

    life--;
    oss << life;
    life_string.SetText(oss.str());
}

The problem is that everytime I die a new value gets printed but the old one stays so first it's 3 then its 32 and then its 321 etc

Here is a picture with 0 deaths(http://imageshack.us/photo/my-images/190/nodeaths.png/)
Here is a pricture with 2 deaths(http://imageshack.us/photo/my-images/27/twodeaths.png/)

so how do I remove the onld value so that there is only 1 value showing
Title: Re: Help needed: Display variable(int) in sf::string
Post by: Laurent on April 08, 2012, 06:49:05 pm
To clear an ostringstream:
oss.str("");
Title: Re: Help needed: Display variable(int) in sf::string
Post by: Dannehood on April 08, 2012, 07:12:11 pm
Yes it works wonderfully thanks a LOT!