简体   繁体   中英

libgdx - Simple repeating texture (setWrap())

so I got a very simple spike texture which I would like to repeat multiple times on the bottom of the screen so it always fills the whole size of the screen, no matter the width. Of course I already now about the setWrap()-function but I just can't get the result I'm looking for.

This is the texture

And I'd like to get something like this

The be more specific, I have the width of the screen given and calculate the height of the texture displayed as a percentage of screen height. Now I'd like to place the texture at 0, 0 and repeat it until it reaches the right side of the screen (of course it might be cut off by the screen a little but that's not a problem I just don't want the texture to get stretched)

I "already" got this code so far

Texture spikesTex;

spikesTex = new Texture("spike.png");
spikesTex.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);

Then I already tried different parameters for

batch.draw(spikesTex, ...);

but none of them really worked out.

I tried in this way to complete your requirement. May be it will help you.

public class MAIN extends ApplicationAdapter {
    SpriteBatch batch;
    Texture img;
    TextureRegion textureRegion;
    float drawingWidth,drawingHeight;

    @Override
    public void create () {
        batch = new SpriteBatch();
        img = new Texture("spike.png");

        img.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.ClampToEdge);
        int width=Gdx.graphics.getWidth();

        textureRegion=new TextureRegion(img,0,0,width,img.getHeight());

        drawingWidth=width;
        drawingHeight=Gdx.graphics.getHeight()*.2f; 
   }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(textureRegion,0,0,drawingWidth,drawingHeight);
        batch.end();
    }

    @Override
    public void dispose () {
        batch.dispose();
        img.dispose();
    }
}

It is not necessary to create additional TextureRegion, you can draw the repeated texture itself, giving the desired width and height params:

    img = new Texture("spike.png");
    img.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
    ...
    int imgPosX = 0;// x position where to draw
    int imgPosY = 0;// y position where to draw 
    batch.draw(img, imgPosX, imgPosY, 0, 0, drawingWidth, drawingHeight);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM