Texture(Texture)
An image is called a texture after it is decoded from the original format and uploaded to the GPU. OpenGL requires that the texture height and width must both be 2 N-power. Only texture images that meet this condition are valid. Once the pixel value is obtained, we can pass the data to OpenGL to generate a texture map.
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 it by four vertices of the rectangle.
To draw a drawing, first bind the texture, and then pass a geometric description (such as the four vertices of the rectangle) 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, which means that using pixels makes it easier for the texture to be drawn in a proper size and position.
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 libgdx spritebatch class has to do.
Spritebatch is assigned textures and coordinates for drawing each image. It aggregates a lot of images without directly committing them 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. The resolution of the image to be drawn must be the power of 2 (for example, 32x32,128x128,256x512,512 X)
For example, in this example, the resolution is 256x256 pixels:
Drawing
Draw a graph, the program starts (create) -- read the image -- texture (texture) Binding -- draw initialization (sprite. begin) -- render -- Sprite. end) --> destroy (dispose) and other steps.
Mainactivity
public class MainActivity extends AndroidApplication {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initialize(new MyGame(), false);}}
Mygame
Public class mygame implements applicationlistener {private spritebatch batch; // batch draw private texture; // texture @ overridepublic void create () {batch = new spritebatch (); texture = new texture (GDX. files. internal ("image03.png"); // read the image from the asset file} @ overridepublic void resize (INT arg0, int arg1) {}@ overridepublic void render () {GDX. GL. glclear (gl10.gl _ color_buffer_bit); // clear the screen GDX. GL. glclearcolor (0.5f, 0.5f, 0.5f, 1); // set the screen background to gray batch. begin (); batch. draw (texture, 10, 10); batch. end () ;}@ overridepublic void pause () {}@ overridepublic void resume () {}@ overridepublic void dispose () {texture. dispose (); batch. dispose ();}}
In the mygamecode field, the image03.png image must be saved in the assets folder, which is determined by the libgdx file module design.
GDX. Files is the file module of libgdx and mainly provides the following five functions:
- Read files
- Write files
- Copy a file
- Move files
There are five methods to obtain the filehandle of the operation file:
1) classpath. The file is generally read-only relative to classpath. Note: classpath is incompatible with some Android functions, such as audio. newsound (filehandle) and audio. newmusic (filehandle)
2) Internal: the internal file path is relative to the root directory of the desktop program or the assets folder of Android. The assets folder itself is the folder for storing resources. Compared with the res folder, its resources do not generate the ID in R, which is suitable for storing images.
3) External: the external file path is relative to the desktop program home directory, or the android SD card root directory.
4) absolute is an absolute directory of the file system. When using a cross-platform system, you must pay attention to the corresponding absolute path.
5) Local: local directory. for Android, It is the files directory inside the application, and for desktop, It is the root directory.
Therefore, images obtained using GDX. Files. Internal ("image1.jpg") must be saved in the assets folder.
After reading the image, call batch. Draw (texture, 10, 10) to draw the image, and (10, 10) to draw the coordinates. It is based on the Cartesian coordinates, that is, the coordinate origin in the lower left corner.
Running result:
The preceding example shows:
First, apply for texture in create () and read the image image03.png from the internal assets folder.
Texture = new texture (GDX. Files. Internal ("image03.png "));
Then, render the image in render ().
Batch. Draw (texture, 10, 10); // draw Region
Finally, destroy the resource in dispose.
Texture. Dispose ();
Batch. Dispose ();
Reference recommendations:
Android game development framework libgdx graphic rendering