LibGDX游戏引擎-2-图片绘制(Texture)

作为一个游戏引擎,最基本的东西就是绘制各种元素了,其中最重要的无非就是图片了,下面简单介绍一下libGDX引擎在绘制图片方面的方法:

最简单的方法有:

  • 1 使用Texture作为图片容器,在SpriteBatch上绘制
  • 2 使用TextureRegion截取texture,在SpriteBatch上绘制
  • 3 使用Sprite精灵,设置textureRegion,传入SpriteBatch,进行绘制(例子采用本种类)
    首先介绍一下 几个相关的类:

 

一丶Texture 类 (纹理,图片容器)

其实就是承装获取到的目的图片的容器。其实你就直接把Texture当成图片。

1丶引入Gdx.files:

要把图片文件变成数据输入到程序,需要引入Gdx.files,它是libgdx的文件模块,主要提供以下5大功能:读取文件,写入文件、复制文件、移动文件、列出文件和目录。

1.Classpath: 路径相对于classpath,文件通常为只读。

2.Internal: 内部文件路径相对于程序根目录或者android 的assets文件夹。

3.External: 外部文件路径是相对于SD卡根目录

4.Absolute:assets文件夹本身就是存储资源的文件夹,而且相比resource文件夹,它其中的资源不会生成R中的ID,用来放图片很是合适。

2丶绘制流程

所以用Gdx.files.internal(“data/Potato.jpg”) 获取图片,然后调用batch.draw(texture,x,y,height,width) 绘制图形,这里的(x,y)是绘图的起点坐标,(height,width)绘制图形的大小,libgdx使用的是笛卡尔坐标系,以左下角为原点。绘制方向是由下向上,由左到右。

3丶例子使用方法:

private Texture texture;
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
batch.draw(texture,x,y,height,width);
  # 二丶SpriteBatch类 精灵组 在绘制之前首先要调用begin方法,去开启想对应的绘制状态,当绘制完了,需要调用end方法,来真正的绘制指定的内容。SpriteBatch操作的所有的绘制命令都是在屏幕坐标系统上的,屏幕坐标系统是一个右手坐标系,x轴向右,y轴向上,原点在屏幕左下角,如果我们想做的话,也可以提供模型视口矩阵和投影矩阵给他。 ## 1丶使用方法:
    private SpriteBatch batch;
    batch = new SpriteBatch();

public void render() {
    batch.begin();
    sprite.draw(batch);
    batch.end();
}

    //获取屏幕高宽度
    Gdx.graphics.getWidth();
    Gdx.graphics.getHeight();

2丶注意事项

SpriteBatch实现了disposed接口 , 所以不使用的时候,别忘了销毁资源。在Libgdx中凡是实现了Disposable接口的,在销毁的时候都要调用dispose方法, 这一点不要忘了。

 

 

三丶TextureRegion类 用于截取图片

具体的不做介绍了,这个一般懂使用即可!

//以(100,100)为起点,向左48,向上48截取texture
new TextureRegion(texture,100,100,-48,48);
    # 四丶例子源代码:
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);//自 (420,120) 截取 310*360
        sprite = new Sprite();
        sprite.setRegion(region);
        sprite.setOrigin(0,0);//旋转中心
        sprite.setRotation(20);//旋转角度
        sprite.flip(true, false);//水平翻转
        sprite.setSize(200,300);//绘制大小
        sprite.setPosition(100,100);//绘制位置
    }

    @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() {
    }
}