Jump to content
  • Advertisement
Sign in to follow this  
Dirty Dave

Infinite repeating background

This topic is 4837 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
Advertisement
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.

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.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!