Libgdx is definitely a pretty good game engine. I have learned a lot recently. However, I have always been confused that the image size loaded by libgdx must be at the power of 2.
If you want to use a common image in libgdx, you need to pull the image by yourself using ps or something. It doesn't matter if you have one or two images, but it is a little annoying if the number is large.
In addition, textureregion is required to cut the enlarged image, which is really troublesome. After carefully reading the tool package of libgdx, we found that there was a solution.
The specific class is imagepacker, which is included in the com. badlogic. GDX. Tools. imagepacker package. It can merge multiple images into one. You can also obtain image resources by using the file name of the original file.
Here are several images:
Put them in a folder (this folder has nothing else ). Reference gdx-tool.jar.
CodeAs follows:
Settings settings =NewSettings ();
Settings. Alias =True;
Texturepacker. Process (settings, "d :\\ IMG", "d :\\ IMG \ output ");
You can find two files in the Output Folder, one of which is a merged image.
Another name is pack, which records the location, size, and name of the source file.
Click to view the pack File Content
Copy images and pack files to the project directory. The file name of the pack file can be changed at will. If you need to change the name of the merged image, you need to change the content in the pack file.
The specific class used is textureatlas.
Code:
Package Com. cnblogs. htynkn. game;
Import Com. badlogic. GDX. applicationlistener;
Import Com. badlogic. GDX. GDX;
Import Com. badlogic. GDX. Graphics. gl10;
Import Com. badlogic. GDX. Graphics. g2d. textureatlas;
Import Com. badlogic. GDX. Scenes. scene2d. stage;
Import Com. badlogic. GDX. Scenes. scene2d. UI. image;
Public Class Texturepackerdemo Implements Applicationlistener {
Stage stage;
@ Override
Public Void Create (){
Stage = New Stage (GDX. Graphics. getwidth (), GDX. Graphics. getheight (), False );
Textureatlas Atlas = New Textureatlas (GDX. Files. Internal ("IMGs/Pack ")); // Retrieve all images based on the pack File
Image image1 = New Image (ATLAS. findregion ("14 ")); // Obtain the image named 14 and create an image object
Image1.scalex = image1.scaley = 0.2f;
Image1.x = image1.y = 0;
Image image2 = New Image (ATLAS. findregion ("xpic2766 "));
Image2.x = image2.y = 40;
Image2.scalex = image2.scaley = 0.5f;
Stage. addactor (image1 );
Stage. addactor (image2 );
}
@ Override
Public Void Dispose (){
Stage. Dispose ();
}
@ Override
Public Void Pause (){
// Todo auto-generated method stub
}
@ Override
Public Void Render (){
GDX. gl. glclear (gl10.gl _ color_buffer_bit );
Stage. Act (GDX. Graphics. getdeltatime ());
Stage. Draw ();
}
@ Override
Public Void Resize ( Int Width, Int Height ){
// Todo auto-generated method stub
}
@ Override
Public Void Resume (){
// Todo auto-generated method stub
}
}
Effect:
Conclusion:
1. The biggest advantage of using texturepacker is convenience. I think it is transparent to developers to load images.
2. Using texturepacker improves efficiency. Merging images is undoubtedly a good choice compared to loading an image.
3. When using texturepacker, be sure not to merge the images used in one scenario blindly. In other scenarios or temporarily unavailable images, merge them into other scenarios. I personally think the size of an image should not exceed 1024*1024.
4. The texturepacker tool is not easy to use. I think it is very troublesome to create a Java project and enter the path each time. Enthusiastic netizens made a GUI version.
: Http://www.ctdisk.com/file/4279795
Project address: http://code.google.com/p/libgdx-texturepacker-gui/