LIBGDX----Texture, textureregion, SpriteBatch

Source: Internet
Author: User
Tags libgdx

Introduction

This chapter describes some of the basic aspects of 2D graphics, including how to use Texture,textureregion and SpriteBatch.

Texture slightly. SpriteBatch LIBGDX uses SpriteBatch to complete texture mapping and displays all work on the screen that are textured-mapped quads. It makes drawing graphics on the screen extremely simple and optimized. It works in the screen coordinate system, using the absolute coordinates of the screen pixels. The lower-left corner is the origin (0,0), the x-axis to the right, and the y-axis upward.  Since SpriteBatch can process multiple draw requests at the same time, use the GPU for rendering acceleration, and serialize requests, the programmer just starts it. The following code draws a picture onto the screen: [Java]View Plaincopy
  1. Public class Texturefun implements Applicationlistener {
  2. private Texture druidtexture; //#1
  3. private SpriteBatch batch; //#2
  4. @Override
  5. public Void Create () {
  6. Druidtexture = New Texture (Gdx.files.internal ("druid.png")); //#3
  7. Batch = new SpriteBatch (); //#4
  8. }
  9. @Override
  10. public Void Render () {
  11. Batch.begin (); //#5
  12. Batch.draw (druidtexture, 100); //#6
  13. Batch.end (); //#7
  14. }
  15. //... rest of methods omitted ...//
  16. }
1. Declare a texture 2. Declares the SPRITEBATCH3 used to display textures to the screen. Create textures. Picture Druid.png need to be located in the asset folder, the picture format can be jpg,png or BMP, the image width and height must be 2 n power, such as 1,2,4,8,16,32,64,128 ... In addition, the width and height do not need to be equal to 4. Create a SPRITEBATCH5. After calling the Begin () function, SpriteBatch begins receiving request 6. A drawing request, which requests SpriteBatch to draw the texture to the position of (100,100). Note that this coordinate is the absolute screen coordinates. 7. Tell SpriteBatch that there is no other request, it can start processing the request just received.  The display results and coordinate system are as follows: You can draw multiple graphs by adding multiple draw () instructions between the Begin () and end () functions, and the Order of Draw () is the same as the call sequence, so if there are overlapping parts, later images will be overlaid on previous images. In addition to the simple Draw (), SpriteBatch has some other features, such as setting a blend color and setting the transformation matrix.  However, the transformation matrix must be set before begin (). The following code enables the SpriteBatch texture blending feature and sets the blending method. However, texture blending is enabled by default. [Java]View Plaincopy
    1. ...
    2. Batch.enableblending ();
    3. Batch.setblendfunction (Gl11.gl_src_alpha, Gl11.gl_one_minus_src_alpha);
    4. ...
Textures are created in a number of different ways, and are described in detail in the source notes. When you need to use mipmaps (multilevel textures), texture creates them as they are initialized. Mipmaps refers to the pre-calculated, the same picture of the different size of the copy, when you need to scale the picture to adapt to the quadrilateral, choose the most appropriate copy, you can improve rendering efficiency.
The draw () function has multiple overloads, and the meanings of the various parameters are detailed in the source comments. Here's one of them: [Java]View Plaincopy
  1. Public void Render () {
  2. Batch.begin ();
  3. Batch.draw (druidtexture, 100);
  4. Batch.draw (druidtexture, max, 0, +, 0, 6, 1f, 2.0f, 45f,  4, false, false);
  5. Batch.end ();
  6. }

Some of the arguments in the note are Texel space, Texel is a pixel on the guide image, to distinguish it from a pixel on the screen.

Textureregion in a game, there must be a lot of elements to be drawn, if each element is to be converted to a texture, the resource consumption of the GPU is very large, because before drawing, the GPU will be loaded into video memory and bound to OpenGL,  OpenGL then paints the required textures and switches between the different textures, and the cost of binding and switching is expensive. Texturergion solves this problem by cutting an area from a texture and letting SpriteBatch act on the area. In this way, a texture can contain multiple elements that need to be drawn, and only the part that corresponds to that element is drawn.  Textures that contain multiple drawing elements are also known as Sprite sheet. Explains how a texture is split into multiple elements. This approach avoids the expensive cost of switching between different textures.  The size of the texture must be a power of N 2, but the texture area can be defined arbitrarily. The following code creates and draws four areas of this texture, and also displays a reduced texture in the (0,0) position. [Python]View Plaincopy
  1. Public class Texturefun implements Applicationlistener {
  2. Private Texture Texture; #1
  3. private SpriteBatch batch;
  4. Private textureregion[] Regions = new textureregion[4];// #2
  5. @Override
  6. public void Create () {
  7. Texture = new Texture (Gdx.files.internal ("sprite_sheet.png"));
  8. Batch = new SpriteBatch ();
  9. regions[0] = new textureregion (texture, 0, 0, 64); #3
  10. regions[1] = new textureregion (texture, 0.5f, 0f, 1f, 0.5f); #4
  11. regions[2] = new Textureregion (texture, 0,, 64); #5
  12. regions[3] = new Textureregion (texture, 0.5f, 0.5f, 1f, 1f); #6
  13. }
  14. @Override
  15. public void render () {
  16. Batch.begin ();
  17. Batch.draw (texture, 0, 0, 64); #7
  18. for (int i = 0; i < regions.length; i++) {
  19. Batch.draw (Regions[i], * (i + 1), 100); #8
  20. }
  21. Batch.end ();
  22. }
  23. ... rest of methods ommited ...//
  24. }

If the texture area you want to split is equal in size and has no spacing, you can create textureregion in a simpler way: [Java]View Plaincopy
    1. Textureregion[][] Regions = textureregion.split (texture,. )

LIBGDX----Texture, textureregion, SpriteBatch

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.