I’m trying to make a repeating background. I guess you are supposed to use texture.setWrap… or something but I can’t seem to find good documentation on how it works. If someone could point me to some documentation on how to repeat a texture and render it, or give me some pseudo code, I would be grateful.
Use GL_REPEAT instead of GL_CLAMP_TO_EDGE when creating the texture. (or the LibGDX equivalents)
setTextureWrap(TextureWrap.GL_REPEAT) on the texture. Or specify the wrap when using TexturePacker2. Or use TiledDrawable.
Do I have to do anything different in the render method?
Texture background;
background = new Texture("data/background.png");
background.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
render(){
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
world.update();
batch.begin();
batch.draw(background,0,0);
batch.end();
}
Is this right? It doesn’t seem like I’m telling it to rendering the texture more than once.
You need to give SpriteBatch UVs using:
draw(Texture texture, float x, float y, float width, float height, float u, float v, float u2, float v2)
The UVs are from 0 to 1, where 0 is the top (v) / left (u) and 1 is the bottom (v) / right (u). You can use values outside this range and it will repeat. Eg, use 0,0,2,1 and it will repeat twice horizontally and once vertically.
You can also use a TextureRegion to specify the UVs. Also see the TextureRegion scroll method.
Here is an example of using Texture.Repeat:
... create ...
... create texture
tex.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
... render ...
//repeat N times horizontally and vertically
int tileCount = 3;
spriteBatch.draw(tex, 0, 0,
tex.getWidth() * tileCount,
tex.getHeight() * tileCount,
0, tileCount,
tileCount, 0);
However, this means you can’t render your background in the same batch as the rest of your sprites. It also means you can’t include the background texture in your texture atlas. The texture needs to be power-of-two sized (unless you are targeting NPOT-supported devices).
Ultimately it might be easier just to render the sprite multiple times in your batch (e.g. nested for loop). This allows you to include the background in the same batch as the rest of your sprites, and overcome the power-of-two issue if needed.
Anyways… The performance difference is pretty much negligible, so choose whichever is easiest for you.
You could include the region in an atlas if you only need to repeat in one direction, you just need to set the max size of the atlas page the region is on to your POT image size.
To render a region repeatedly without using the repeat wrap mode, see TiledDrawable.
Is there a way that I can repeat the tile infinitely? Draw them as needed? Or do i just need to set the “N” to be a large number that my player won’t reach?
My game is basically going to the right as far as you can before you die
Thank you for all your help.
Incase anyone else ever has this trouble this is what i ended up doing… I don’t know if it is the best way to do it or not, but it works.
bgSprite1 = new Sprite(background, 0, 0,width, height);
bgSprite2 = new Sprite(bgSprite1);
render(){
if(stage.getCamera().position.x -width/2> bgSprite2.getX()){
bgSprite1.setPosition(bgSprite2.getX(),0);
bgSprite2.setPosition(bgSprite1.getX()+width, 0);
}
bgSprite1.draw(batch);
bgSprite2.draw(batch);
}