libgdx游戏引擎开发笔记(二)主要类的详解(图像绘制)

下面我们来看看libgdx游戏引擎中一些常用的类

1.Gdx类

   Gdx是Libgdx类库运行的核心所在,不单运行Graphics、Input、Files、Audio、AndroidApplication等Libgdx关键部分,所必需的实例会在Libgdx初始化时注入Gdx中对应的graphics、input、files、audio、app等静态变量里面,就连Libgdx对OpenGL接口(或OpenGLES,视Libgdx运行平台而定,以下统称OpenGL)的GL10、GL11、GL20、GLCommon等封装类也会在Graphics实例化时分别注入到gl10、gl11、gl20、gl这四个同样位于Gdx的静态变量当中(在Graphics中也会继续保留它们的引用,因此无论你执行Gdx.graphics.getGL10还是Gdx.gl10,其实都在调用同一个静态变量),总之一句话,要想运行Libgdx必须的使用Gdx!


2.图形绘制

  2.1.Texture类:

定义:图片从原始格式解码并上传到GPU就被称为纹理,其实质可理解为保存在显存中的Image。

  Libgdx的纹理可以直接从指定文件路径加载,也可以通过它提供的Pixmap类凭空创建(它的Texture(int width, int height, Format format)构造内部直接调用了Pixmap,不是必须在外部生成Pixmap后注入)。这里说一下前者:

1
Gdx.files. internal ( "button.jpg" )


  Gdx.files,它是libgdx的文件模块,主要提供以下5大功能:读取文件,写入文件、复制文件、移动文件、列出文件和目录。其中获取文件有许多种办法:

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

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

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

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

  2.2.SpriteBatch类:

  定义:SpriteBatch用于绘制二维矩形参考纹理(区域)。SpriteBatch类可用于批量绘图命令和优化处理的GPU。

  Libgdx所提供的纹理渲染器,本质上是OpenGL的简易封装体,每次调用SpriteBatch类都必须以begin函数开头,以end函数结尾,中间调用draw()进行绘制。SpriteBatch可以把许多相同纹理一起描述并一起送入GPU,同时赋予纹理和坐标以便每个图形的绘制。


  2.3TextureRegion类

定义定义了一个矩形区域的纹理。使用的坐标系统与x轴指向右,Y轴向下在左上角的起源。

  将多个图片资源集合在一个图片文件夹中,而要显示图片的一部分就要使用该类,TextureRegion一般都是截取texture,然后定义截取起点(x,y)随后再定义宽高(width,height),如果宽高是正数,那么就是沿x,y正方向截取,如果是负就是沿x,y负方向截取,方向只和宽高的正负有关。


1
textureRegion = new TextureRegion(texture, 0 , 0 , 48 , 48 );

1
textureRegion = new TextureRegion(texture, 48 , 48 ,- 48 ,- 48 );


  2.4.Sprite类

  定义:持有的几何形状,颜色,和纹理信息使用加载绘制2D精灵。一个精灵有定位和给定的宽度和高度尺寸。的位置是相对于通过加载指定的坐标系的原点。begin()和各自的矩阵。一个精灵总是矩形和它的位置(x,y)是位于左下角的矩形。一个精灵也有一个起点,旋转和缩放的执行(即,产地不通过旋转和缩放修改),起点是相对于左下角的精灵,它的位置。

  它是TextureRegion的加强版,还能指定位置,颜色,旋转等等。

鉴于以上的总结,我们来看一个显示图片的例子(部分裁剪)


1
2
3
4
5
6
7
8
9
10
11
12
package com.zhf.android_libgdx;
import com.badlogic.gdx.backends.android.AndroidApplication;
import android.os.Bundle;
public class MainActivity extends AndroidApplication {
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
//在此启动游戏,FirstGame实现了ApplicationListener的类
initialize( new FirstGame(), false );   //specifying the configuration for the GLSurfaceView.
//第二个参数如果设为true,则在opengl 2.0可用的情况下会使用opengl 2.0。
}
}

FirstGame类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.zhf.android_libgdx;
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
private SpriteBatch batch;
//纹理
private Texture texture;
//区域
private TextureRegion region;
@Override
public void create() {
batch = new SpriteBatch(); //实例化
texture= new Texture(Gdx.files. internal ( "image1.jpg" ));
region= new TextureRegion(texture, 30 , 80 , 200 , 200 );
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
region.dispose();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); //清屏
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
}
}

效果图:

使用Sprite类:

声明

1
2
//Sprite不光包含TextureRegion的功能,还可以指定位置和颜色
privateSprite sprite;

实例化

1
2
3
4
5
//精灵类
sprite=newSprite(texture, 80 , 80 , 400 , 300 );
sprite.setPosition( 10 , 10 ); //位置
sprite.setRotation( 20 ); //旋转
sprite.setColor( 0 .5f, 0 .0f, 0 .0f, 0 .5f); //为图形着色死奥用

调用

1
sprite.draw(batch); //精灵类绘画


效果图:


  对了,这里还有很重要的一点哦!


  Android上画图可以使用OpenGL ES,分两个不兼容的版本:OpenGL ES 1.x,和 OpenGL ES 2.0。
它们之间的一个重要区别就是:OpenGL ES 1.x的图片大小必须是2的整数次幂,而OpenGL ES 2.0则无此要求。

而libgdx在早期使用OpenGL es1.x 的版本所以只支持2的整数次幂的图像,并一直沿用至今,新版本(0.9.8)中允许开发者使用OpenGL ES 2.0,这样使用OpenGL ES 2.0就可以不用管图片分辨率的问题了。android系统对opengl es的支持情况:OpenGL ES 1.0------------>andoid 1.0-2.1(api 7)

OpenGL ES 2.0------------>android 2.2(api 8)以上


  恩,暂时就这么多,下次继续总结吧!

参考博客:http://www.apkbus.com/Android-159-1.html

本文出自 “狂奔的蜗牛” 博客,http://smallwoniu.blog.51cto.com/3911954/1255140


你可能感兴趣的:(android,libgdx)