libgdx - SpriteBatch的使用

SpriteBatch 是绘图时会经常使用的一个功能类,它位于 com.badlogic.gdx.graphics.g2d中。



控制类接口:

begin()
开始准备成批量的画图,它打开了混合和纹理。


end()
结束渲染,它关闭了混合和纹理,并且必须在begin后调用。


flush()
所有在 Pending状态的 sprites 都会被渲染,并且不需要执行end()。




绘制图形的API

对于绘制图形的API来说,最好是能看到他们运行的效果,这是最直观的方式。


draw(Texture texture, float x, float y)
画图,纹理的左上角的坐标为(x, y),图片的宽度和高度为纹理的宽度和高度。
例如:测试图片像素为 128 x 128


libgdx - SpriteBatch的使用_第1张图片




draw(Texture texture, float x, float y, float width, float height)
画图,纹理的左上角的坐标为(x, y),图片的宽度和高度为 width 和 height。
例如:测试图片像素为 128 x 128,但设置的 width = 32, height=32


libgdx - SpriteBatch的使用_第2张图片




动态的图(动画)

draw(TextureRegion region, float x, float y)


draw(TextureRegion region, float x, float y, float width, float height)



当然如果把长和宽指定了的话会按照静态的方式发生缩放的变化。







带向量的动态图

draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height,

           float scaleX, float scaleY,float rotation)

请注意最后一个参数,它使图形会带有角度,可以根据运动的向量取得其角度。

关于该函数的说明:

Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height. The rectangle is offset by originX, originY relative to the origin. Scale specifies the scaling factor by which the rectangle should be scaled around originX, originY. Rotation specifies the angle of counter clockwise rotation of the rectangle around originX, originY.






下面是代码,其中标注的 test pic_1,  test pic_2,  test pic_3,  test pic_4, 分别是对应于图片 1 ,  2  , 3 ,  4 , 的测试语句。

package com.example.libgdx_sample_spirtebatch;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;


public class SampleRender 
{
	public static int WIDTH = 320;
	public static int HEIGHT = 480;
	
    OrthographicCamera m_Cam;

	SpriteBatch batch = new SpriteBatch(5460);

	FPSLogger fps = new FPSLogger();
	ImmediateModeRenderer20 renderer = new ImmediateModeRenderer20(false, true, 0);
	
	private Animation m_MoveLeft;
	private Texture m_StaticTex = null;
	
    private float m_StateTime;

    Vector2 m_Pos = new Vector2();
    Texture m_bkg = null;
    Vector2 m_Vel = new Vector2();
    
    
    private TextureRegion m_region;
    
	public SampleRender()
	{
		this.m_Cam = new OrthographicCamera(WIDTH, HEIGHT);
		this.m_Cam.position.set(WIDTH/2, HEIGHT/2, 0);
		m_StateTime = 0;
		createAnimations();
		this.m_Vel.set(8, 40);
	}

	public void update(float deltaTime)
	{
		m_Vel.y -= 4*deltaTime;//抛物线
		m_Pos.add(m_Vel.x * deltaTime,  m_Vel.y * deltaTime);

		m_StateTime += deltaTime;
		
		if (m_Pos.y < 0)
		{
			m_StateTime = 0;
			m_Vel.set(8, 40);
			m_Pos.x=0;
			m_Pos.y=0;
		}
	}
	
	
	private void createAnimations()
	{
		m_bkg = new Texture(Gdx.files.internal("data1.png"));
		m_region = new TextureRegion(m_bkg);

		Texture TankTexture = new Texture(Gdx.files.internal("data.png"));
		m_StaticTex = new Texture(Gdx.files.internal("data.png"));

		TextureRegion[] split = new TextureRegion(TankTexture).split(32, 32)[0];
		TextureRegion[] mirror = new TextureRegion(TankTexture).split(32,32)[0];

		for (TextureRegion region : mirror)
			region.flip(true, false);
				
		
				
		m_MoveLeft = new Animation(0.1f, split[0], split[1]);
	}
	
	
	Vector3 lerpTarget = new Vector3();
	public void render (float deltaTime)
	{
		//m_Cam.position.lerp(lerpTarget.set(m_Pos.x, m_Pos.y, 0), 2f * deltaTime);
		//m_Cam.update();
		//m_StateTime += deltaTime;

		renderer.begin(m_Cam.combined, GL20.GL_LINES);
		renderer.end();

		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		//batch.setProjectionMatrix(m_Cam.combined);
		batch.begin();

		Animation anim = m_MoveLeft;
		batch.draw(m_region, 0, 0);
		// test pic_1
		//batch.draw(m_StaticTex,  m_Pos.x, m_Pos.y);   
		
		// test pic_2
		//batch.draw(m_StaticTex,  m_Pos.x, m_Pos.y,32,32);
		
		// test pic_3
		//batch.draw(anim.getKeyFrame(m_StateTime, true), m_Pos.x, m_Pos.y);
		//batch.draw(anim.getKeyFrame(m_StateTime, true), m_Pos.x, m_Pos.y,16,16);
		
		// test pic_4
		batch.draw(anim.getKeyFrame(m_StateTime, true), m_Pos.x, m_Pos.y, 0.5f, 0.5f, 32, 32, 1, 1, m_Vel.angle());
		
		batch.end();
	}

	public void dispose () 
	{
		batch.dispose();
	}
}

实例代码下载


还有一些其他的 API

draw(Texture texture, float[] spriteVertices, int offset, int count)

draw(Texture texture, float x, float y, float width, float height, float u, float v, float u2, float v2)

draw(Texture texture, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)


draw(Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)


draw(Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight)


draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, boolean clockwise)

你可能感兴趣的:(libgdx - SpriteBatch的使用)