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/

  • \$\begingroup\$ :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. \$\endgroup\$ – George Jun 12 '13 at 16:30
  • \$\begingroup\$ :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? \$\endgroup\$ – George Jun 12 '13 at 16:33
  • \$\begingroup\$ 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. \$\endgroup\$ – petervaz Jun 12 '13 at 19:01
  • \$\begingroup\$ 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. \$\endgroup\$ – mobo Jun 12 '13 at 23:02
  • \$\begingroup\$ @mobo:Ok , thanks!If you happen to know any program in Linux (gimp maybe?)?BEcause I can't find sth. \$\endgroup\$ – 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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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