Repeating infinite-like background

Hello guys,

I need your help again. Imagine you’re looking onto a road like a bird and you’re flying it in eastern direction. I want the background to be always white stripes (the road middle marks) to move with. I already got a crappy solution ( I won’t show it yet, because I want to see what you think) but as I said this is crap.

Or this way: How would you draw an infinite road from the birds-eye’s view?

Thanks a lot.

cheers Kronos

Tile it. Have one texture for a section of the road and just draw new instances of it as you love down the road. You can add overlays to it to add unique things such as cracks or holes in the road. But I think tiling is probably the most efficient and probably the easiest way.

If the road is a single image and it doesn’t change, you can look up for parallax scrolling (not sure if that’s the correct name for it)

Parallax is images scrolling in opposite directions, giving a side scroller ‘depth’. The concept probably could probably be the same though.

Thanks for the answers. Right now I have two ArrayLists. The First contains my street marks as rectangles. The second is the “garbage collector” for the rectangles, which aren’t seen anymore.
I add them this way:

			
if (player.x % 800 > last) {

float width = 160;
float height = 15;
for (int i = 0; i < 10; i++) {
streetMarks.add(new Entity(streetMarkcounter * width + streetMarkcounter * 60, Window.getHeight() / 2, width, height));
streetMarkcounter++;
}

last = (int) (player.x % 800);
}

So every 800 pixels there should come these marks. But sometimes, that doesn’t work. I also use this way to add new enemies, every 800 pixels. But something is wrong.

Does anyone know whats up there? thanks

This is the code I use in Renoria to render parallax, “infinite” repeating backgrounds:


private void renderBackground(RenderSpace g, int absolutex, int absolutey) {
		int hs = getHorizontalSpacing(), vs = getVerticalSpacing();
		if (this.horizontalRenderMode == RenderMode.STRETCH) {
			if (this.verticalRenderMode == RenderMode.STRETCH) {
				g.drawSprite(background, absolutex, absolutey, width, height, null);
			} else if (this.verticalRenderMode == RenderMode.TILE) {
				for (int i = ((absolutey) % vs) - vs; i < Core.view_RenderY + vs + Core.GameHeight; i += vs) {
					g.drawSprite(background, absolutex, i);
				}
			} else if (verticalRenderMode == RenderMode.NONE) {
				g.drawSprite(background, absolutex, absolutey, width, vs);
			} else {
				throw new Error();
			}
		} else if (this.verticalRenderMode == RenderMode.STRETCH) {
			if (this.horizontalRenderMode == RenderMode.TILE) {
				for (int i = ((absolutex) % hs) - hs; i < Core.view_RenderX + hs + Core.GameWidth; i += hs) {
					g.drawSprite(background, i, absolutey);
				}
			} else if (horizontalRenderMode == RenderMode.NONE) {
				g.drawSprite(background, absolutex, absolutey, hs, height);
			} else {
				throw new Error();
			}
		} else if (this.horizontalRenderMode == RenderMode.TILE) {
			if (this.verticalRenderMode == RenderMode.TILE) {
				for (int i = ((absolutex) % hs) - hs; i < Core.view_RenderX + hs + Core.GameWidth; i += hs) {
					for (int j = ((absolutey) % vs) - vs; j < Core.view_RenderY + vs + Core.GameHeight; j += vs) {
						g.drawSprite(background, i, j);
					}
				}
			} else if (this.verticalRenderMode == RenderMode.NONE) {
				for (int i = ((absolutex) % hs) - hs; i < Core.view_RenderX + Core.GameWidth + hs; i += hs) {
					g.drawSprite(background, i, absolutey);
				}
			} else if (this.verticalRenderMode == RenderMode.STRETCH) {
				for (int i = ((absolutex) % hs) - hs; i < Core.view_RenderX + hs + Core.GameWidth; i += hs) {
					g.drawSprite(background, i, absolutey, hs, height);
				}
			} else {
				throw new Error();
			}
		} else if (this.verticalRenderMode == RenderMode.TILE) {
			if (this.horizontalRenderMode == RenderMode.NONE) {
				for (int i = ((absolutey) % vs) - vs; i < Core.view_RenderY + vs + Core.GameHeight; i += vs) {
					g.drawSprite(background, absolutex, i);
				}
			}
		} else if (horizontalRenderMode == RenderMode.NONE && verticalRenderMode == RenderMode.NONE) {
			g.drawSprite(background, absolutex, absolutey, null);
		} else {
			throw new Error();
		}
	}

In particular:


if (this.verticalRenderMode == RenderMode.TILE) {
				for (int i = ((absolutex) % hs) - hs; i < Core.view_RenderX + hs + Core.GameWidth; i += hs) {
					for (int j = ((absolutey) % vs) - vs; j < Core.view_RenderY + vs + Core.GameHeight; j += vs) {
						g.drawSprite(background, i, j);
					}
				}
			}