# How do I use TextureRegion?

I can't understand how to use texture region.

I have a png file which has images (sprite sheet) and I must "extract" every image I want.

From here it just says TextureRegion(texture, 20, 20, 50, 50);

Where 20, 20, 50, 50 describes the portion of the texture,

How can I find which portion has the image I want to extract?

Should I open the png file with Gimp for example? And then I am search for some coordinates? And if yes, how?

The params describes the upper left location of the region and the width/height. So 20,20,50,50 describes a region starting at 20,20 and then has a width and height of 50. However, extracting regions manually can be a pain. Texture packer allows you to put multiple images together into a single texture. Then using texture atlas you can retrieve those regions by name.

Packer and Atlas Info: https://code.google.com/p/libgdx/wiki/TexturePacker#Running_TexturePacker2

A GUI Packer: https://code.google.com/p/libgdx-texturepacker-gui/

• :So, if I have already a sprite sheet with images ,I must create a pack of it with the GUI packer?Or I have to find/create every image and then make a pack? I am asking also ,because in the internet I am finding sprites sheets. – George Jun 12 '13 at 16:30
• :As far as I understand with the GUI packer above you pack images to one atlas,ok.But if I have already a sprite sheet ,what I should do? – George Jun 12 '13 at 16:33
• You can use the sheet but you have to figure the size and position of every image on it. If that is too hard you can probably save time by opening in an editor cutting the images separately and repacking. – petervaz Jun 12 '13 at 19:01
• The packer will place the entire sheet into one region if it is a single file. If you want to use the atlas in a prebuilt spritesheet you first have to split it up. Just Google how to split up spritesheet for details. Its really easy to do in Photoshop or Fireworks. – mobo Jun 12 '13 at 23:02
• @mobo:Ok , thanks!If you happen to know any program in Linux (gimp maybe?)?BEcause I can't find sth. – George Jun 13 '13 at 8:39

As you said, you need to know the position and size of every image inside the sheet, in pixels, this is usually easy if you have all images the same size, it is just multiplication matter.

If you are using several images with different sizes, you will need an archive that maps each image position and size. This file is usually generated by the program you used to pack the images into a single one. There is actually a program available with Libgdx called TexturePacker.
The TexturePacker will take a full folder of images and generate one or more sheets and a .pack file mapping each image and position with its name.

Having that it is just matter of instancing a TextureAtlas and using findRegion("imageName") to recover the TextureRegion.

• :Thanks for the answer.I chose the other because it was first.:) . – George Jun 13 '13 at 8:39