Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help needed: Display variable(int) in sf::string  (Read 2349 times)

0 Members and 1 Guest are viewing this topic.

Dannehood

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Help needed: Display variable(int) in sf::string
« 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
Here is a pricture with 2 deaths

so how do I remove the onld value so that there is only 1 value showing
« Last Edit: April 08, 2012, 06:48:23 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Help needed: Display variable(int) in sf::string
« Reply #1 on: April 08, 2012, 06:49:05 pm »
To clear an ostringstream:
oss.str("");
Laurent Gomila - SFML developer

Dannehood

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Help needed: Display variable(int) in sf::string
« Reply #2 on: April 08, 2012, 07:12:11 pm »
Yes it works wonderfully thanks a LOT!

 

anything