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

Author Topic: Make old text dissappear and new appear in sf::Text  (Read 2374 times)

0 Members and 1 Guest are viewing this topic.

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Make old text dissappear and new appear in sf::Text
« on: August 15, 2014, 10:47:35 pm »
So, I'm trying to display value that is constantly changing with sf::Text, but instead of changed value I get something like this (attachment).
Here is my code:
std::string ballSpeedString;
    std::stringstream convert;
    font.loadFromFile("LCD_Solid.ttf");
    ballSpeedText.setFont(font);


ballSpeedText.setString("");
    convert << (int)moveSpeed; //moveSpeed of the ball
    ballSpeedString = convert.str();
    ballSpeedText.setString(ballSpeedString);
 

« Last Edit: August 15, 2014, 11:00:22 pm by paupav »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Make old text dissappear and new appear in sf::Text
« Reply #1 on: August 15, 2014, 10:53:57 pm »
If you never clear your stringstream, text will accumulate in it. To clear a stringstream, call convert.str("").

Better: use a new stringstream instance for each conversion. Even better: put conversion code in a dedicated function. Even better if you compiler allows it: use std::to_string.

PS: you forgot the attachment.
Laurent Gomila - SFML developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Make old text dissappear and new appear in sf::Text
« Reply #2 on: August 16, 2014, 02:25:42 am »
Also remember to clear() (and redraw) your screen between each display of the text. I don't know if you already do this (not enough code shown) just wanted to mention it. :)

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Re: Make old text dissappear and new appear in sf::Text
« Reply #3 on: August 16, 2014, 02:49:40 am »
Also remember to clear() (and redraw) your screen between each display of the text. I don't know if you already do this (not enough code shown) just wanted to mention it. :)
How bad is your opinion about me? :P

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: Make old text dissappear and new appear in sf::Text
« Reply #4 on: August 16, 2014, 10:04:49 pm »
How bad is your opinion about me? :P
What is that supposed to mean? You don't know basics he was just noting something important.
But if he knew the ball was actually moving, he could observe there is no trail of ball movement  8)

EDIT::
This operator<< is appending data
http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/
Code: [Select]
std::stringstream convert1(std::string("Content");
std::stringstream convert2(std::string("");
//Doing following operation
convert1 << (int)moveSpeed; //moveSpeed of the ball
convert2 << (int)moveSpeed; //moveSpeed of the ball
//Lead to convert1 and conver2 look like
conver1.str == "Content1";
conver2.str == "1";
You can use .str() instead of operator<<
Code: [Select]
std::stringStream S;
s.str() = std::string((int)moveSpeed); //moveSpeed of the ball
Another suggestion, use std::to_string
Code: [Select]
int i = 999888;
std::string str = std::to_string(i);//str would be "999888"
« Last Edit: August 17, 2014, 03:02:57 am by BaneTrapper »
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Re: Make old text dissappear and new appear in sf::Text
« Reply #5 on: August 17, 2014, 05:20:45 am »
Try all.