SFML 2.0 restrict screen scrolling

Oct 14, 2013 at 10:51pm
I have screen scrolling up and running but it keeps going after the background finishes. how do you fix that?
Oct 14, 2013 at 11:26pm
Er... don't scroll passed the edge of the background. :P

SFML doesn't scroll on its own. You have to tell it where to scroll to. So just don't tell it to scroll somewhere that's "offscreen"

Check the bounds of the map, check the size of the visible screen, and create a boundary that has min and max scroll values. Then, before you give any scroll position to SFML, simply make sure the scroll is within that boundary.
Oct 14, 2013 at 11:39pm
closed account (3qX21hU5)
If your not looking for a repeating background you can do like disch said and stop the scrolling boundary to the edge of your background. Otherwise you can make your background repeating quite easily. You can make it only repeat for a certain distance or forever.

Here is something I grabbed from one of my games that might help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	// Setup scrolling background

	// Gets the texture you wish to use for the background. Depending on how you manage your textures
	// you might have to get it from your texture manager or you can just load the texture up to you.
	sf::Texture& texture = textures.get(Textures::SpaceBackground);

	// Set up how far you want the scrolling to continue.
	// Here is how I set it up for my game 
	// worldBounds(0.0f, 0.0f, worldView.getSize().x, 5000.0f)
	// It scrolling for 5000 pixels then ends but will continue scrolling unless you handle it.
	// Which is as easy as changing the scrolling speed to 0 when it ends.
	sf::IntRect textureRect(worldBounds);

	// Tell SFML that you want it to repeat the texture over and over.
	texture.setRepeated(true);

	// Setup everything else you need.
	std::unique_ptr<SpriteNode> backgroundSprite(new SpriteNode(texture, textureRect));
	backgroundSprite->setPosition(worldBounds.left, worldBounds.top);
	sceneLayers[Background]->attachChild(std::move(backgroundSprite));
Last edited on Oct 14, 2013 at 11:47pm
Oct 22, 2013 at 4:41am
can i have an implementation in code Disch because i don't exactly know hot to do that.
Oct 22, 2013 at 5:37am
@Cronnoc:

To scroll in SFML, you have to give SFML a Vector2f containing the coordinates you want to scroll to. All you have to do is keep the coordinates within a certain range:

1
2
3
4
5
6
7
8
9
10
sf::Vector2f myscroll;

// change myscroll to be whatever you want here

if(myscroll.x < minimum_x)  myscroll.x = minimum_x;
if(myscroll.x > maximum_x)  myscroll.x = maximum_x;
if(myscroll.y < minimum_y)  myscroll.y = minimum_y;
if(myscroll.y > maximum_y)  myscroll.y = maximum_y;

// give myscroll to SFML here 
Topic archived. No new replies allowed.