Public Group

# Infinite repeating background

This topic is 4991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi Community! I want to program an infinite repeating background. The background has to be linked with the players coordinates, and it uses just one tile. Could anyone explain me how to implement this the best way?

##### Share on other sites
I forgot: the background has to scroll in x and in y direction.

##### Share on other sites
Assuming the positions are always positive, then tile.x=player.x%tile.width, ditto for y.

##### Share on other sites
the coordinates can be negative as well, and i don't understand your approach either.

##### Share on other sites
I'm not going to write an code to test this but:

for(int y = -(pos.y%tile.height); y < screen.height; ++y) for(int x = -(pos.x%tile.width); x < screen.width; ++x)  tile.draw(screen, x, y);

should probably work (again, with positive numbers). x%y divides x by y and returns the remainder.

Example:
0%3=0
1%3=1
2%3=2
3%3=0
4%3=1
5%3=2
6%3=0
7%3=1
8%3=2
9%3=0

As you can see this is useful because it can be used to easilly make numbers 'wrap around'.

The remainders for negative numbers are also negative, and increase in the opposite direction. You can fix it with (x<0?y+(x+1)%y-1:x%y), which is fugly.

And last, floating point numbers don't have a modulus operator. You can use fmod for that.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015654
×