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

Author Topic: Scrolling Text Issue  (Read 1690 times)

0 Members and 1 Guest are viewing this topic.

diegonolan

  • Newbie
  • *
  • Posts: 26
    • View Profile
Scrolling Text Issue
« on: February 04, 2012, 03:36:50 am »
Hello, I want text to scroll onto the screen.  Ie. display more and more characters every time step of some given string.  I also have it divided up into lines.  I got the code working up the method I am using seems to be very inefficient because I am getting very low framerates. I bet it is because I am using sf::Text::SetString a lot but I am unable to think of a better solution.  This is in SFML 2.0.

Here is the snippet of code I am using:

Code: [Select]

Time += float(Window.GetFrameTime())/1000.f;
      int charstoshow = int(Time*CHARSPERSECOND);
      for(unsigned int i=0;i<TextLines.size();++i)
      {
         // More or equal to the amount of chars in this line
         if(charstoshow >= TextLines[i].size())
         {
            TextToDisplay[i].SetString(TextLines[i]);
            Window.Draw(TextToDisplay[i]);
            charstoshow -= TextLines[i].size();
         }
         // this line will run out of characters
         else
         {
            std::stringstream ss;
            for(unsigned int j=0;j<charstoshow;++j)
            {
               ss << TextLines[i][j];
            }
            string temp;
            ss >> temp;
            TextToDisplay[i].SetString(temp);
            Window.Draw(TextToDisplay[i]);
            break;
         }
      }



Time is a timer i use to keep track of when the text should start scrolling and how far i am along.  Then i cast it to an int to find how many characters i should show.  TextLines is a stl vector holding strings for each line of text. Then text to display is a stl vector holding sf::Text for each line.  I set the strings then display them, when I run out of characters I break.  This is done each time step.  Any help on a better implementation would be appreciated.  Thanks.

diegonolan

  • Newbie
  • *
  • Posts: 26
    • View Profile
Scrolling Text Issue
« Reply #1 on: February 04, 2012, 03:52:18 am »
Well i fixed it, I think my program has a memory leak, because i restarted and that seemed to fix the low framerate problem.  But I also changed the code. I remembered that streamstream separated by spaces so I changed it to this.

Code: [Select]

Time += float(Window.GetFrameTime())/1000.f;
      int charstoshow = int(Time*CHARSPERSECOND);
      for(unsigned int i=0;i<TextLines.size();++i)
      {
         // More or equal to the amount of chars in this line
         if(charstoshow >= TextLines[i].size())
         {
            TextToDisplay[i].SetString(TextLines[i]);
            Window.Draw(TextToDisplay[i]);
            charstoshow -= TextLines[i].size();
         }
         // this line will run out of characters
         else
         {
            string str;
            for(unsigned int j=0;j<charstoshow;++j)
            {
               str += TextLines[i][j];
            }
            TextToDisplay[i].SetString(str);
            Window.Draw(TextToDisplay[i]);
            break;
         }
      }

 

anything