SpriteBatch class explanation

Source: Internet
Author: User

SpriteBatch class explanation

In all of the previous examples, we used SpriteBatch to deal with drawing, so here's a detailed description of SpriteBatch.

Drawing

Images are usually stored in a certain format, we only speak PNG type, when a PNG file is read into the GPU (graphics processing device), we call it texture (texture). To draw a picture (a texture) on the screen, we need to set the geometry to correspond to the image (texture), for example, we can set a rectangle, and then the four corners of the picture correspond to the four corners of the rectangle. If this rectangle is only part of the picture, we call it texture region (texture area), which is useful for extracting a subset of images from a sprite.

When actually drawing, the texture needs to be loaded (BIND) First, and then you need to tell OpenGL a geometry that the size and position of the texture needs to be handled according to the geometry of the texture and viewport settings. Many 2D games set viewport to match the pixels of the screen, meaning that the geometry is in pixels, which makes it easy to handle the size and position of the image on the screen.

In practice, it is very common to draw a rectangular image, and it is common to draw the same rectangle or part of the image repeatedly. Therefore, it is inefficient to send the rectangle information to the GPU every time it needs to be drawn, instead, we can sort out all the rectangles that describe the same texture and send it to the GPU at once, which can greatly improve the efficiency of the GPU. The SpriteBatch class is used to do this packing action.

We send each texture and its coordinates that need to be drawn to the object of the SpriteBatch class, which collects all the geometry information, and if the latest texture is not the same as the last one, it will be updated to the latest texture information and then sent uniformly to the GPU for drawing. Then collect the next texture information you want to draw again.

Changing a small number of rectangles each time will cause SpriteBatch to not be able to store more geometry in large amounts, and in code it is not recommended to read the image files frequently, and it is common practice to package a large number of graphics footage into one image file, and then draw only a few of them at a time. To make the most of the ability to utilize SpriteBatch. Please refer to Textruepacker for packaging images.

SpriteBatch

Use the SpriteBatch code in your app as follows

public class Game implements Applicationlistener {

private SpriteBatch batch;

public void Create () {

Batch = new SpriteBatch ();

}

public void render () {

Gdx.gl.glClear (Gl10.gl_color_buffer_bit); This cryptic line clears the screen.

Batch.begin ();

Drawing goes here!

Batch.end ();

}

public void Resize (int width, int height) {}

public void Pause () {}

public void Resume () {}

public void Dispose () {}

}

All SpriteBatch drawing functions must be placed between the begin and end functions, and no other drawing methods other than SpriteBatch can be placed between begin and end.

Texture

The texture class is used to read an image file, load it into the GPU memory, the image file should be placed in the assets directory, the size of each image must be a power of 2, such as 16x16, 16x256, the maximum is not recommended more than 1024x1024

Private Texture Texture;

...

Texture = new Texture (Gdx.files.internal ("image.png"));

...

Batch.begin ();

Batch.draw (texture, 10, 10);

Batch.end ();

The above code is to read the Image.png file and draw it in the position (10,10), its width, height and the actual size of the graph is equal. SpriteBatch draw supports the following calling methods for drawing

| ' Draw (Texture Texture, float x, float y) ' |

The simplest way to call, according to the actual width of the texture of the (x, Y) position of the drawing

| ' Draw (Texture Texture, float x, float y, int srcx, int srcy, int srcwidth, int srcheight) ' |

Draw only part

| ' Draw (Texture Texture, float x, float y, float width, float height, int srcx, int srcy, int srcwidth, int srcheight, Boole An flipx, Boolean flipy) ' |

Only part is drawn, but the part is scaled to the specified size and supports rollover

| ' Draw (Texture Texture, float x, float y, float originx, float originy, float width, float height, float scaleX, float scal EY, float rotation,int srcx, int srcy, int srcwidth, int srcheight,boolean flipx, boolean flipy) ' |

Draws only a portion and zooms to the specified width, height, optional flip, scale, and rotation

| ' Draw (Texture Texture, float x, float y, float width, float height, float u, float V, float U2, float v2) ' |

The function is to draw a portion and zoom to the specified width and height, but the method uses the texture coordinates (0~1) instead of the pixel units

| ' Draw (Texture Texture, float[] spritevertices, int offset, int length) ' |

The lower-level processing method is used to draw arbitrary quads, not just rectangles.

Textureregion

The Textureregion class represents a rectangle in an image (texture) that is often used to draw a portion of a large shape.

Private Textureregion region;

...

Texture = new Texture (Gdx.files.internal ("image.png"));

region = new Textureregion (texture, 20, 20, 50, 50);

...

Batch.begin ();

Batch.draw (Region, 10, 10);

Batch.end ();

Here 20,20,50,50 describes a part of the image that is drawn to the (10,10) position. The same effect can be achieved using the texture class, but using Textureregion is more convenient and intuitive.

SpriteBatch also offers a variety of support for textureregion drawing

| ' Draw (textureregion region, float x, float y) ' |

Draw at (x, y) position with width and height of area

| ' Draw (textureregion region, float x, float y, float width, float height) ' |

Draws the area and stretches to the specified width and height

| ' Draw (textureregion region, float x, float y, float originx, float originy, float width, float height, float scaleX, float ScaleY, float rotation) ' |

Draws the area and stretches to the specified width and height, proportional and rotated by origin coordinates

Sprite

The Sprite class is based on textureregion and adds geometry and color properties.

Private Sprite Sprite;

...

Texture = new Texture (Gdx.files.internal ("image.png"));

Sprite = new Sprite (texture, 20, 20, 50, 50);

Sprite.setposition (10, 10);

Sprite.setrotation (45);

...

Batch.begin ();

Sprite.draw (Batch);

Batch.end ();

This code reads the image.png into the video memory, then takes out one of the areas (20,20,50,50), rotates it 45 degrees, draws it to the (10,10) position, which can be achieved with both texture and textureregion, but using a sprite can be more intuitive and convenient. In addition, the spite stores the geometry of the texture so that the geometry is recalculated only when needed, so that using a sprite is more efficient if the bar, rotation, and other properties do not change in each frame.

Note that using sprites will mix the model (position, rotation) and view (texture itself) information, so if you stick to the MVC separation design pattern, then using a sprite is less convenient, and you should use texture or textureregion.

Another note is that the sprite constructor does not have parameters associated with positional information, and the call Sprite (Texture,int, Int,int,int) does not process any location information. You must process the location information by calling SetPosition (Float,float), otherwise the sprite will be drawn to the (0,0) position.

Tint (shaded)

When you draw a texture (texture), you can give it an extra color (tinting)

Private Texture Texture;

Private Textureregion region;

Private Sprite Sprite;

...

Texture = new Texture (Gdx.files.internal ("image.png"));

region = new Textureregion (texture, 20, 20, 50, 50);

Sprite = new Sprite (texture, 20, 20, 50, 50);

Sprite.setposition (100, 10);

Sprite.setcolor (0, 0, 1, 1);

...

Batch.begin ();

Batch.setcolor (1, 0, 0, 1);

Batch.draw (texture, 10, 10);

Batch.setcolor (0, 1, 0, 1);

Batch.draw (Region, 50, 10);

Sprite.draw (Batch);

Batch.end ();

This example shows how to color texture,textureregion and sprites, which are described in terms of Rgba, four numbers between 0 and 1 if not enabled blending,a values are ignored.

Blending

Blending is enabled by default, meaning that when an image is drawn, the transparent part of the image is directly coincident with the texture already on the screen, showing the image already on the screen.

If you turn off blending, the original image on the screen will be replaced by the new texture, which will be more efficient when drawing the background, and in practice, it is possible to close blending, unless you confirm that you need this effect.

In the following example, turning off blending when you start drawing can greatly improve performance

Gdx.gl.glClear (Gl10.gl_color_buffer_bit); This cryptic line clears the screen.

Batch.begin ();

Batch.disableblending ();

Backgroundsprite.draw (Batch);

Batch.enableblending ();

Other drawing here.

Batch.end ();

Note: Before drawing begins, it is best to do a clear screen Gdx.gl.glClear (gl10.gl_color_buffer_bit); If you do not, repeatedly drawing an image with an Alpha attribute will cause hundreds of images to overlap, and the final image becomes less clear. In addition, some GPUs tend to be drawn on a clean screen, which gives better performance.

Viewport

SpriteBatch manages its own projection and transformation matrices, and when a spritebatch is created, it uses the current app/game settings to create a orthographic projection, using the y-axis up coordinate system (the origin in the lower-left corner), and when the begin is called, SpriteBatch will build viewport.

Performance optimization

SpriteBatch provides a constructor that sets the maximum number of sprites to cache before sending to the GPU, which, if too low, causes additional calls to the GPU, which, if too high, wastes video memory.

The spritebatch has a common int object, Maxspritesinbatch, which records the maximum number of sprites (spikes) that the spritebatch sends to the GPU throughout its life cycle, first setting a larger spritebatch, Then, by checking the maxspritesinbatch to make adjustments, you can get more reasonable settings. This value can be set to zero at any time to reset.

The sprintbatch has a common int object rendercalls, which records the number of render calls sent to the GPU in the previous begin-to-end cycle after the end call. The value is only present when the image is loaded (bind) or when the spritebatch is not filled. If the SpriteBatch is the right size, and the rendercalls is large (over 15~20), there is too much image loading.

SpriteBatch has another constructor that accepts the size and number of buffers as parameters, which is a closer to the underlying usage and can use VBO instead of the traditional Va. A list of buffers is preserved, and each render will use the next buffer for that list. When the Maxspriteinbatch is low and the renderclalls is large, the performance can be greatly improved by using this method.

SpriteBatch class explanation

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.