Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to fill region with repeated texture? Now I'm using next method:

spriteBatch.begin();

final int tWidth = texture.getWidth();
final int tHeight = texture.getHeight();

for (int i = 0; i < regionWidth / tWidth; i++) {
    for (int k = 0; k < regionHeight / tHeight; k++) {
        spriteBatch.draw(texture, i*tWidth, k);
    }
}

spriteBatch.end();

It's very obvious. Maybe is there any built in method?

share|improve this question
up vote 7 down vote accepted

You want to set the texture's TextureWrap setting to Repeat. See the documentation for more information, and the texture method.

Specifically:

setWrap(Repeat, Repeat);
share|improve this answer
    
Thank you very much :) – eleven Aug 20 '12 at 17:37

Important: It took me a while to figure it out, but to be mirrored, your texture must be a power of two size. It was working on Android but not on iOS and you don't get a message - it was shown as black. So it must be 4x4 or 8x8, 16x16 .. 256x256 or 512x512 ..

You can use "SetWrap" on your texture and create a TextureRegion based on that Texture. To Create a 3x3 mirrored image (or axb size)

Texture imgTexture = new Texture(Gdx.files.internal("badlogic.jpg"));
imgTexture.setWrap(Texture.TextureWrap.MirroredRepeat, Texture.TextureWrap.MirroredRepeat);
TextureRegion imgTextureRegion = new TextureRegion(imgTexture);
imgTextureRegion.setRegion(0,0,imgTexture.getWidth()*3,imgTexture.getHeight()*3);

Will give you this: enter image description here

Below you can see the sample code that generated that picture using a Stage and Image Actor (Scene2D)

public class GameScreen implements Screen {

    MyGdxGame game;
    private Stage stage;

    public GameScreen(MyGdxGame aGame){
        stage = new Stage(new ScreenViewport());
        game = aGame;
        Texture imgTexture = new Texture(Gdx.files.internal("badlogic.jpg"));
        imgTexture.setWrap(Texture.TextureWrap.MirroredRepeat, Texture.TextureWrap.MirroredRepeat);
        TextureRegion imgTextureRegion = new TextureRegion(imgTexture);
        imgTextureRegion.setRegion(0,0,imgTexture.getWidth()*3,imgTexture.getHeight()*3);

        TextureRegionDrawable imgTextureRegionDrawable = new TextureRegionDrawable(imgTextureRegion);
        Image img = new Image();
        img.setDrawable(imgTextureRegionDrawable);
        img.setSize(imgTexture.getWidth()*3,imgTexture.getHeight()*3);
        stage.addActor(img);
    }

    @Override
    public void show() {

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act(delta);
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
        stage.getViewport().update(width, height, true);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        stage.dispose();
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.