The difference between texture and Sprite

Source: Internet
Author: User

Texture: A texture saved in the GPU buffer.
Textureregion: Defines a texture for a rectangular area. The main function is to playable the rectangular area that needs to be used from a piece of texture.
Sprite:holds the geometry, color, and texture information for drawing 2D. This class is inherited from Textureregion, is the extension of the function of textureregion, compared with the increase of rotation, animation and other effects.
Spritebatch:a SpriteBatch is used to draw 2D rectangles, reference A texture (region). Mainly dealing with the texture of painting, painting before the optimization and so on.

From the source can be known:
Sprites can be constructed using textureregion, and textureregion can be constructed using texture. So spritebatch at the bottom of the final operation or texture. That's what their relationship is all about.

On the code to compare their differences:
Environment: My simulator resolution is 240x400, the image used in the example is 512x512 size. Note that the texture picture must be a power size of 2 n times.

Test picture size 512x512

Painting with textures texture:

1 Private Texture texture;//define a texture
 2     Private spritebatch spritebatch;//define a texture painter
 3     private Textureregion textureregion;//Define a texture area
 4  
 5 public     Mogullistener () {
 6        
 7     }
 8     @ Override
 9 public     void Create () {         texture =new Texture (Gdx.files.internal ("wallpaper.jpg"));         spritebatch =new SpriteBatch ();     @Override public     void render () {         Gdx.gl.glClear (gl10.gl_color_ Buffer_bit);         Spritebatch.begin ();         Spritebatch.draw (texture, 0,0);//depending on the screen size, you can only draw a picture from the lower-left corner with a size of 240x400.         spritebatch.end ();
+     }

Since the texture cannot set the coordinates and size of the visible area, the picture is as follows, and some of it is not drawn:

Use texture area drawing: You can define the relative coordinates and size of the area in the texture. The bottom right part of the picture is drawn below.

1 @Override
 2 public     void Create () {
 3         texture =new Texture (Gdx.files.internal ("wallpaper.jpg"));
 4         textureregion=new textureregion (texture,272,112,240,400);
 5         SpriteBatch =new spritebatch ();
 6     }
 7     @Override
 8 public     void Render () {
 9         Gdx.gl.glClear (gl10.gl_color_buffer_ BIT); 
Ten         Spritebatch.begin ();         Spritebatch.draw (textureregion, 0,0);         spritebatch.end ();
+     }

Effect Diagram:

Use Sprite drawing. Because the sprite inherits from Textureregion, the sprite has all textureregion properties. The sprite also implements some of its own properties, such as rotational animation.

1 private Texture Texture;
 2     private SpriteBatch spritebatch;
 3     private textureregion textureregion;
 4     private Sprite sprite; 
 5 public     Mogullistener () {
 6        
 7     }
 8     @Override
 9 public     void Create () {
10         Texture =new Texture (Gdx.files.internal ("wallpaper.jpg"));
One         sprite=new sprite (texture, 292, 20,220, 380);//Draw the bottom right corner of the picture         in Sprite.setposition (20, 20);//from screen coordinates 20, 20 start painting         sprite.setrotation (45);//Rotate 45 degrees         spritebatch =new spritebatch ();
A. @Override-public     void render () {         Gdx.gl.glClear (gl10.gl_color_ Buffer_bit);         Spritebatch.begin ();         Sprite.draw (spritebatch);         spritebatch.end ();
*     

Effect Diagram:

Lazy here, not the status bar and the application of the label removed, affect the effect of the display.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.