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

Author Topic: Text Flicker in 1.6 [Solved]  (Read 1639 times)

0 Members and 1 Guest are viewing this topic.

Lee

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Text Flicker in 1.6 [Solved]
« on: October 29, 2012, 02:03:05 am »
Working with SFML 1.6 and when I display a block a text to the screen, it flickers everytime the window reads an event (like moving my mouse). When I say flicker, I mean ghosts of a string a piecing together appear at the end. I know it has to be the string giving me problems but I'm not sure why.

If I draw a line of text from code, there's no ghosting. If I draw my compiled string (pieced together from a string vector) I get flickers. This is even if I use string::assign and make a copy to a different variable.

This is probably a rookie problem but I'm not really sure what went wrong. I started from using tutorials and wiki code:

http://www.gamefromscratch.com/page/Game-from-Scratch-CPP-Edition-Part-2.aspx
http://www.gamefromscratch.com/page/Game-From-Scratch-CPP-Edition-Part-3.aspx
https://github.com/SFML/SFML/wiki/Source%3A-Settings-Parser

EDIT: If you need a text file to load, any block of text under 1000 characters will do:

Code: [Select]
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent varius, dolor ac viverra vulputate, purus turpis molestie augue, id iaculis mauris nulla a enim. Donec augue nibh, cursus a pharetra sit amet, elementum non leo. Phasellus leo felis, feugiat in mollis vitae, aliquam ut dui. Suspendisse suscipit est sapien, eget aliquet nisl. Vestibulum sed mauris diam. Aliquam erat volutpat.

Praesent lectus nunc, aliquet ut vulputate ac, eleifend sed velit. Morbi sed metus non leo molestie rhoncus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Ut id purus metus, non sollicitudin dolor. Mauris sapien sem, viverra ut lobortis id, dapibus eget neque. Fusce eu arcu sapien. Mauris non metus nisi. Mauris commodo massa in nibh viverra euismod vel et nulla. Cras lacinia nisl scelerisque tortor scelerisque ac euismod libero suscipit. Vestibulum quis ornare leo. Phasellus non metus vitae eros pharetra interdum bibendum a mi.

Code from the class I use to wrap the text (reads a text file, makes a vector of strings, builds a single string from the vector to output).

std::string TextParser::Wrap(std::string line)
{
    std::string buffer, word;

    std::vector<std::string> textWrap;
    std::string::iterator s_it;
    std::vector<std::string>::iterator v_it, pushtxt;
    unsigned int wrapLength = 80;

    for(s_it = line.begin(); s_it < line.end(); s_it++)
    {
        if((*s_it) != ' '  && (*s_it) != '\n')
        {
            word += (*s_it);
        }
        else if ((*s_it) == '\n')
        {
            while((*s_it) == '\n')
            {
                word.append(1, *s_it);
                s_it++;
            }
            s_it--;
            buffer += word;
            word.clear();
            textWrap.push_back(buffer);
            buffer.clear();
        }
        else
        {
            if(buffer.length() + word.length() > wrapLength)
            {
                buffer.append(1, '\n');
                textWrap.push_back(buffer);
                buffer.clear();
            }
            buffer += word;
            buffer.append(1, ' ');
            word.clear();
        }
    }

    //clear out the last bit
    textWrap.push_back(buffer);

    std::string output;

    for ( pushtxt=textWrap.begin() ; pushtxt < textWrap.end(); pushtxt++ )
    {
        output += *pushtxt;

    }

    return output;
}
 


And here's the code I use to draw the text to the screen:

void Game::GameLoop()
{
    switch(_gameState)
    {
        case Game::ShowingMenu:
        {
            ShowMenu();
            break;
        }
        case Game::ShowingSplash:
        {
            ShowSplashScreen();
            break;
        }
        case Game::Playing:
        {

                TextParser introText("text/intro.txt");
                introText.Read();

                std::string outputString;

                outputString.assign(introText.Wrap(introText.getRaw()));

                sf::Font cutText;

                //Load font from file
                if(!cutText.LoadFromFile("font/PTC55F.ttf"))
                {
                    // Error...
                }
                sf::String screenText(outputString, cutText, 18);
                screenText.SetColor(sf::Color(255,255,255));
                screenText.Move(300.f, 160.f);

                sf::Event currentEvent;
                while(_mainWindow.GetEvent(currentEvent))
                {

                    if(currentEvent.Type == sf::Event::Closed)
                    {
                        _gameState = Game::Exiting;
                    }

                    if(currentEvent.Type == sf::Event::KeyPressed)
                    {
                        if(currentEvent.Key.Code == sf::Key::Escape)
                        {
                            ShowMenu();
                        }
                    }


                _mainWindow.Clear(sf::Color(0,0,0));
                _mainWindow.Draw(screenText);

                _mainWindow.Display();



                }
                break;

        }

    }
}
 

I don't have my webserver up so I can't just upload the entire project. However, if you want to see anything else just let me know. Finally got the text wrap "working" and displaying the text does this. I have no idea where to begin to solve it.
« Last Edit: November 04, 2012, 09:26:27 pm by Lee »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text Flicker in 1.6
« Reply #1 on: October 29, 2012, 08:02:30 am »
Two things are really wrong in your code:
- you draw in your event loop (so you refresh the screen only when something happens -- a mouse move, a key press, ...)
- either there's no main loop in your code, or its not shown and in this case your init code (load font, ...) is execute every frame
Laurent Gomila - SFML developer

Lee

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Text Flicker in 1.6
« Reply #2 on: November 03, 2012, 05:27:18 am »
Sorry for taking so long to reply...

There is no main loop. What you see there is pretty much the entire program. I just simply adding on top of that tutorial I linked. I thought it was strange as I typed it up.

One thing I'm not clear on, you said:

Quote
your init code (load font, ...) is execute every frame

Are you saying it should execute every frame or that my code is executing it every frame. From my understanding the only part of the code that should be executing every frame is Clear/Draw/Display.
« Last Edit: November 03, 2012, 05:33:29 am by Lee »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text Flicker in 1.6
« Reply #3 on: November 03, 2012, 10:01:10 am »
Quote
From my understanding the only part of the code that should be executing every frame is Clear/Draw/Display
That's true, but it doesn't happen in your code: once all the events have been processed, your while loop ends and everything is finished. You should have a main loop. Just have a look at any tutorial, documentation or example.
Laurent Gomila - SFML developer

Lee

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Text Flicker in 1.6
« Reply #4 on: November 04, 2012, 09:23:05 pm »
Thanks a lot. I threw the code inside a main loop and the flicker went away. Going to try to follow the official tutorials a little closer next time.

 

anything