LibGDX Game Engine-2-Image Rendering (Texture)

Source: Internet
Author: User
Tags libgdx


LibGDX Game Engine-2-Image Rendering

This series of blog posts are from Sina's blog-xiaotudou. I have added my own understanding and modifications to this article!


Method Type


1. Use Texture as the image container and draw the image on SpriteBatch.


2. Use TextureRegion to intercept texture and draw it on SpriteBatch


3. Use the Sprite genie to set textureRegion, pass in SpriteBatch, and draw the image (this type is used in this example)


------------------------------------------------------------------------------
Sample Code:


Package com. qsuron;


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;
Import com. badlogic. gdx. graphics. g2d. TextureRegion;


Public class MyDemo implements ApplicationListener {
Private SpriteBatch batch;
Private Texture texture;
Private TextureRegion region;
Private Sprite sprite;

@ Override
Public void create (){
Batch = new SpriteBatch ();
Texture = new Texture (Gdx. files. internal ("data/open.png "));
Region = new TextureRegion (texture, 420,120,310,360); // capture 420,120*310 from (360)
Sprite = new Sprite ();
Sprite. setRegion (region );
Sprite. setOrigin (0, 0); // center of rotation
Sprite. setRotation (20); // Rotation Angle
Sprite. flip (true, false); // flip horizontally
Sprite. setSize (200,300); // draw size
Sprite. setPosition (100,100); // draw position
}


@ Override
Public void dispose (){
Batch. dispose ();
Texture. dispose ();
}


@ Override
Public void render (){
Gdx. gl. glClearColor (1, 1, 1, 1 );
Gdx. gl. glClear (GL10.GL _ COLOR_BUFFER_BIT );
Batch. begin ();
Sprite. draw (batch );
Batch. end ();
}


@ Override
Public void resize (int width, int height ){
}


@ Override
Public void pause (){
}


@ Override
Public void resume (){
}

}












Texture class (image container)


------------------------------------------------------------------------------


API definition: an image is called a texture after it is decoded from the original format and uploaded to the GPU. I am not very clear here, please answer.


------------------------------------------------------------------------------


Function purpose: it is to load the container for the obtained destination image. In fact, you can directly regard Texture as an image.


------------------------------------------------------------------------------
Gdx. files


It is the file module of libgdx and mainly provides the following five functions: Reading files, writing files,
Copy, move, and list files and directories. There are many methods to obtain files:


1. Classpath: The file is generally read-only relative to the classpath path.
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, and its resources are
The ID in R is not generated, which is suitable for storing images.


Therefore, you can use Gdx. files. internal ("data/Potato.jpg") to retrieve images,
Then call batch. draw (texture, x, y, height, width) to draw the image. Here (x, y) is the starting coordinate of the drawing,
(Height, width) The size of the drawing. libgdx uses the Cartesian coordinate system and uses the lower left corner as the origin.
The draw direction is from bottom up, from left to right.


For the convenience of understanding, I would like to give you an example here. For example, if you have a bowl of porridge, you need to use the spoon from the pot,
This spoon is equivalent to Texture. They are all containers used to get things, and they are the intermediary.


Usage:
----------------------------------------------------------------
Private Texture texture;
Texture = new Texture (Gdx. files. internal ("data/libgdx.png "));










SpriteBatch class (paint brush)


------------------------------------------------------------------------------


API definition: SpriteBatch is used to draw two-dimensional rectangular reference textures (regions ).
The SpriteBatch class can be used for batch drawing commands and optimized processing GPUs.


-----------------------------------------------------------------------------


Function purpose: SpriteBatch can describe multiple identical textures and send them together to the GPU,
At the same time, the texture and coordinates are assigned for drawing each image. In fact, my understanding is that SpriteBatch is a paint brush,
Images cannot be painted without a paint brush.


------------------------------------------------------------------------------


Usage: first declare the class name, instantiate, and draw in three steps, you must first call the begin () method,
Then call the draw () method, and end () is finished after painting. This order is fixed and should be memorized.


------------------------------------------------------------------------------
Usage:


Private SpriteBatch batch;


Batch = new SpriteBatch ();


Public void render (){
Batch. begin ();
Sprite. draw (batch );
Batch. end ();
}


-----------------------------------------------------------------

Get screen width
Gdx. graphics. getWidth ();
Gdx. graphics. getHeight ();






TextureRegion class

Screenshot


New TextureRegion (texture, 100,100 );


Take (100,100) as the starting point, cut texture to the left 48, and up 48










Reprinted please indicate the source: blog.csdn.net/qsuron Xiaoshu blog (qsuron)





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.