Advertisement Jump to content
Sign in to follow this  
Dirty Dave

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.

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
Share on other sites
the coordinates can be negative as well, and i don't understand your approach either.
Someone help me please, and sorry for my bad english.

Share this post

Link to post
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.


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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!