First, let's take a look at texture. An image is called a texture after it is decoded from the original format and uploaded to the GPU. (To be honest, I am not very clear about this definition. Please advise)
In order to draw texture, ry is often used to describe texture, and vertices corresponding to ry are used to describe texture. For example, to describe a rectangle, you can describe the rectangle by describing each vertex.
To draw a graph, you must first bind the texture and then pass a geometric description to OpenGL for painting. The drawing size and position are jointly determined by the geometric description and OpenGL viewport settings.
Of course, most games will make the viewport size consistent with the screen size. This means that using pixels makes it easier to draw textures at proper sizes and positions.
It is very common to draw the ry of a rectangle. It is also very common to make the same texture at different positions in different sizes, such as the bullet screen of a sky. However, the rendering efficiency is low when each shape is passed to the GPU each time.
Therefore, many identical textures can be described together and sent to the GPU. This is what the SpriteBatch class has to do.
SpriteBatch is assigned textures and coordinates for each drawing. SpriteBatch brings together a lot of graphics that are not directly submitted to the GPU. If the texture it is given is different from the original one, it will keep the original image and get the new one.
In the previous article, SpriteBatch was used, but no image was drawn. Now let's try to draw it.
First look for an image. The resolution must be the power of 2 (for example, 32*32,256*512 ).
I took part of my desktop and adjusted the resolution to 512*512.
Copy the image to the assets folder. Put all the image files in this folder.
Then modify the code
Private Texture texture;
Instantiate texture, texture = new Texture (Gdx. files. internal ("image1.jpg "));
Then let's explain why we need to put the image in the assets folder.
Gdx. files is the file module of libgdx and mainly provides the following five functions.
Read files
Write files
Copy a file
Move files
List Files And Directories
There are four methods to obtain the FileHandle of the operation file.
1. Classpath
The file is usually read-only relative to classpath.
2. Internal
The internal file path is relative to the program root directory or the android assets folder.
3. External
The external file path is relative to the root directory of the SD card.
4. Absolute
The assets folder itself is the folder for storing resources. Compared with the resource folder, its resources do not generate the ID in R, which is suitable for storing images.
Therefore, Gdx is used. files. internal ("image1.jpg") to obtain the image and then call batch. draw (texture, 20, 10); draw a graph, where 20 and 10 are coordinates and Cartesian coordinates are taken as the origin in the lower left corner.
Complete code:
Package com. cnblogs. htynkn;
Import com. badlogic. gdx. ApplicationListener;
Import com. badlogic. gdx. Gdx;
Import com. badlogic. gdx. graphics. GL10;
Import com. badlogic. gdx. graphics. Texture;
Import com. badlogic. gdx. graphics. g2d. SpriteBatch;
Public class FirstGame implements ApplicationListener {
// SpriteBatch for plotting
Private SpriteBatch batch;
// Texture
Private Texture texture;
@ Override
Public void create (){
Batch = new SpriteBatch (); // instantiate
Texture = new Texture (Gdx. files. internal ("image1.jpg "));
}
@ Override
Public void dispose (){
// TODO Auto-generated method stub
}
@ Override
Public void pause (){
// TODO Auto-generated method stub
}
@ Override
Public void render (){
Gdx. gl. glClear (GL10.GL _ COLOR_BUFFER_BIT); // clear the screen
Batch. begin ();
Batch. draw (texture, 20, 10 );
Batch. end ();
}
@ Override
Public void resize (int width, int height ){
// TODO Auto-generated method stub
}
@ Override
Public void resume (){
// TODO Auto-generated method stub
}
Effect:
We can see that the image cannot be completely displayed, and we often use a part of the image in actual operations, or set multiple image resources in one image file.
To display part of the image, you can use the TextureRegion class.
The most common method is draw (TextureRegion region, float x, float y, float width, float height)
Specify the start point and length.
Modify the Code:
Package com. cnblogs. htynkn;
Import com. badlogic. gdx. ApplicationListener;
Import com. badlogic. gdx. Gdx;
Import com. badlogic. gdx. graphics. GL10;
Import com. badlogic. gdx. graphics. Texture;
Import com. badlogic. gdx. graphics. g2d. SpriteBatch;
Import com. badlogic. gdx. graphics. g2d. TextureRegion;
Public class FirstGame implements ApplicationListener {
// SpriteBatch for plotting
Private SpriteBatch batch;
// Texture
Private Texture texture;
// Region
Private TextureRegion region;
@ Override
Public void create (){
Batch = new SpriteBatch (); // instantiate
Texture = new Texture (Gdx. files. internal ("image1.jpg "));
Region = new TextureRegion (texture, 30, 80, 200,200 );
}
@ Override
Public void dispose (){
// TODO Auto-generated method stub
}
@ Override
Public void pause (){
// TODO Auto-generated method stub
}
@ Override
Public void render (){
Gdx. gl. glClear (GL10.GL _ COLOR_BUFFER_BIT); // clear the screen
Batch. begin ();
Batch. draw (region, 0, 0 );
Batch. end ();
}
@ Override
Public void resize (int width, int height ){
// TODO Auto-generated method stub
}
@ Override
Public void resume (){
// TODO Auto-generated method stub
}
}
Effect:
You may think that TextureRegion is not powerful enough. It doesn't matter. You can also use it.Sprite.
Sprite not only includes the TextureRegion function, but also can specify the position and color.
Key code:
View sourceprint?
Sprite = new Sprite (texture, 80, 80,400,300 );
Sprite. setPosition (10, 10); // position
Sprite. setRotation (15 );
If you think about the previous examples, you can find that the Sprite function is the set above. But Sprite is more convenient. It describes everything with an object.
The complete code is as follows:
Package com. cnblogs. htynkn;
Import com. badlogic. gdx. ApplicationListener;
Import com. badlogic. gdx. Gdx;
Import com. badlogic. gdx. graphics. GL10;
Import com. badlogic. gdx. graphics. Texture;
Import com. badlogic. gdx. graphics. g2d. Sprite;
Import com. badlogic. gdx. graphics. g2d. SpriteBatch;
Public class FirstGame implements ApplicationListener {
// SpriteBatch for plotting
Private SpriteBatch batch;
// Texture
Private Texture texture;
// Genie
Private Sprite sprite;
@ Override
Public void create (){
Batch = new SpriteBatch (); // instantiate
Texture = new Texture (Gdx. files. internal ("image1.jpg "));
Sprite = new Sprite (texture, 80, 80,400,300 );
Sprite. setPosition (10, 10); // position
Sprite. setRotation (15); // rotate
}
@ Override
Public void dispose (){
// TODO Auto-generated method stub
}
@ Override
Public void pause (){
// TODO Auto-generated method stub
}
@ Override
Public void render (){
Gdx. gl. glClear (GL10.GL _ COLOR_BUFFER_BIT); // clear the screen
Batch. begin ();
Sprite. draw (batch );
Batch. end ();
}
@ Override
Public void resize (int width, int height ){
// TODO Auto-generated method stub
}
@ Override
Public void resume (){
// TODO Auto-generated method stub
}
}
Effect:
SetColor (float r, float g, float B, float)
The color expression is a number between 0 and 1.
There is basically so much content to draw. The next article is about 2D scenarios.
Conclusion:
1. The hybrid mode is enabled by default. This means that when the drawing is finished, the translucent part has been mixed. When the blending is disabled, anything on the scene will be replaced by a texture, which is suitable for drawing a large background.
Batch. disableBlending ();
BackgroundSprite. draw (batch );
Batch. enableBlending ();
2. Performance Optimization
SpriteBatch has a constructor that can specify the maximum number of buffers. If the value is too low, additional GPU calls may occur. If the value is too high, excessive memory usage will occur.
In SpriteBatch, a field is maxSpritesInBatch. You can set a high buffer number first, and then observe the value of maxSpritesInBatch to determine the appropriate buffer value.
There is also a field rendercils. When the end is called, its value indicates the number of times the in and end are sent to the GPU.
Another constructor can specify the buffer size and quantity. Reasonable settings can greatly improve performance.
Author: htynkn Source: http://www.cnblogs.com/htynkn/