SFML Scrolling Background

Jul 15, 2010 at 1:08pm
I'm making a simple game with SFML (C++) where your cursor turns into a reticule and you just shoot stuff on a scrolling background.

I'm having trouble with the scrolling background (which I am using 2 sprites from the same image and moving one sprite to the end of the window as it goes off the window). When it moves on to the next sprite there is a flickering bar and I'm wondering what i've done wrong to cause this.

http://pastebin.com/BDw2DVyX - code
http://img46.imageshack.us/img46/1400/screenshotwn.jpg - problem

(I'm aware the code isn't very optimized at all without any classes or functions and having everything crammed into main but this will have to do for now).

Any suggestions / criticisms will be good :D

Damn, originally posted in lounge section and had to repost /palm
Jul 15, 2010 at 1:42pm
Look for an off-by-one error.
Jul 15, 2010 at 3:07pm
I don't think I can find one but if I change the line
Background[1].SetPosition((Background[0].GetPosition().x + Background[0].GetSize().x) - 1, 0);
and add a - 1,

The flickering line definitely becomes smaller but it is still noticeable as it scrolls.
Last edited on Jul 15, 2010 at 3:08pm
Jul 15, 2010 at 3:13pm
Are you converting floating-point numbers to integers before you have the pixels drawn?
Jul 15, 2010 at 4:13pm
Background[1].SetPosition(int(Background[0].GetPosition().x + Background[0].GetSize().x), 0);

Didn't seem to do anything after changing it to an int - or do I have to do something more?

EDIT: The line isn't a constant line like the picture - its a flickering line
Last edited on Jul 15, 2010 at 4:18pm
Jul 15, 2010 at 4:29pm
I've had this problem (the flickering line) using DirectX (ID3DXSprite) when I didn't control conversion from floating-point to integer myself, because of floating-point imprecision.

Just to be sure this is not your problem, you could make sure Background[0]'s position is also being cast to an integer, because if just one of the sprites is flickering, the line will show up.
Jul 16, 2010 at 6:26am
In the console I print their positions when the transition happens and these are some values -
1364        -2           CHECK 0
1362        -4           CHECK 1

CHECK 0 having the 1st value being background[0]'s position
CHECK 1 having the 1st value being background[1]'s position
The screen I test it on is 1366 x 768, so the values do add up fine.

Here is the code for easy reference:
1
2
3
4
5
6
7
8
9
10
11
12
            if ((Background[0].GetPosition().x + Background[0].GetSize().x) < 0) //out of screen
            {
                Background[0].SetPosition(int(Background[1].GetPosition().x + Background[1].GetSize().x), 0);
                Background[1].SetPosition(int(Background[0].GetPosition().x - Background[0].GetSize().x), 0);
                std::cout << Background[0].GetPosition().x << "\t" << Background[1].GetPosition().x << "\t CHECK 0" << std::endl;
            }
            if ((Background[1].GetPosition().x + Background[1].GetSize().x < 0)) //out of screen
            {
                Background[1].SetPosition(int(Background[0].GetPosition().x + Background[0].GetSize().x), 0);
                Background[0].SetPosition(int(Background[1].GetPosition().x - Background[1].GetSize().x), 0);
                std::cout << Background[1].GetPosition().x << "\t" << Background[0].GetPosition().x << "\t CHECK 1" << std::endl;
            }


When I set the sprite up, I do resize the image to fit my screen -
1
2
3
4
5
    for (int i = 0; i < 2; ++i)
    {
        Background[i].SetImage(BackgroundImage);
        Background[i].Resize(WindowWidth, WindowHeight);
    }


Funny thing is, when I change the part where the sprites move, the line doesn't flicker anymore (refer back to problem image but imagine that it is now a constant black line).

1
2
3
4
//            Background[0].Move(-500 * ElapsedTime, 0);
//            Background[1].Move(-500 * ElapsedTime, 0); //scrolling background
            Background[0].Move(-8, 0);
            Background[1].Move(-8, 0);


If I change it from the frame-dependent movement formula from the first 2 lines to the last 2 lines, there is no more flickering. ElapsedTime is App.GetFrameTime().
Topic archived. No new replies allowed.