user5464066
user5464066

Reputation:

TextureRegions setRegion method libgdx java

I am very confused as to what TextureRegions setRegion(int x, int y, int width, int height) method does because there is no documentation on it in the API can someone please explain exactly what these parameters do. This is my first time using TextureRegions and I am not trying to use it for animation and that is the only thing I can find online that has to do with them, please and thanks.

Upvotes: 2

Views: 1704

Answers (3)

DoubleDouble
DoubleDouble

Reputation: 1493

For LibGdx, the first step to checking out a method is to check the API

setRegion()

setRegion
public void setRegion(int x,
                  int y,
                  int width,
                  int height)
Parameters:
width - The width of the texture region. May be negative to flip the sprite when drawn.
height - The height of the texture region. May be negative to flip the sprite when drawn.

If you need more information, I find that the easiest way to figure out what a method does exactly is to take a look at the source code, which is freely available on GitHub.

/** @param width The width of the texture region. May be negative to flip the sprite when drawn.
 * @param height The height of the texture region. May be negative to flip the sprite when drawn. */
public void setRegion (int x, int y, int width, int height) {
    float invTexWidth = 1f / texture.getWidth();
    float invTexHeight = 1f / texture.getHeight();
    setRegion(x * invTexWidth, y * invTexHeight, (x + width) * invTexWidth, (y + height) * invTexHeight);
    regionWidth = Math.abs(width);
    regionHeight = Math.abs(height);
}

If you are wondering how this ties in overall, broad usage information can be found here.

The TextureRegion class (source) describes a rectangle inside a texture and is useful for drawing only a portion of the texture.

private TextureRegion region;
...
texture = new Texture(Gdx.files.internal("image.png"));
region = new TextureRegion(texture, 20, 20, 50, 50);
...
batch.begin();
batch.draw(region, 10, 10);
batch.end();

Here the 20, 20, 50, 50 describes the portion of the texture, which is then drawn at 10,10. The same can be achieved by passing the Texture and other parameters to SpriteBatch, but TextureRegion makes it convenient to have a single object that describes both.

SpriteBatch has many methods for drawing a texture region:

//Draws the region using the width and height of the region.
draw(TextureRegion region, float x, float y)
//Draws the region, stretched to the width and height.
draw(TextureRegion region, float x, float y, float width, float height) 
//Draws the region, stretched to the width and height, and scaled and rotated around an origin.
draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)   

For why this is useful - Having all the Images on one Texture saves on memory, and helps optimize rendering speed - because different textures don't have to be loaded, bound, and swapped around as often. This actually helps a lot on mobile platforms.

Upvotes: 1

Netero
Netero

Reputation: 3819

I usually use TextureRegion to get a region from a Texture image for example

region = new TextureRegion(texture, 50, 30, 60, 40);

enter image description here

also i use it as a result of splitting a Spritesheet for drawing an animation

region = TextureRegion.split(texture, 32, 32);

enter image description here

Hope this was helpful !

Upvotes: 2

Xoppa
Xoppa

Reputation: 8123

Have a look at this wiki page. A texture is a 2D image in memory of the graphics card (VRAM) which can be used to render to the screen. Because switching textures in VRAM is a relative costly operation, multiple images are typically packed in one texture. For example like this: enter image description here

This is called a "texture atlas" and allows to render multiple images in one batch without having to switch the texture in VRAM in between.

To identify each individual image inside the texture you need to specify the region of that image. This can be done using the TextureRegion class. You can do this by specifying the upper left location of the image in texels and the width and height of the image in texels. So, for example, in the above image the tree starts at location x: 0, y: 0 and has a width of 140 and a height of 160 texels.

You typically never have to specify the region using the setRegion method yourself. Instead you get the region from the TextureAtlas which in turn reads the size from the .atlas file the TexturePacker generated.

Upvotes: 5

Related Questions