本章内容概述了如何绘制图像的使用和libgdx如何简化并通过sprite batch类优化了任务。
Drawing images(绘制图像)
一张从它最初的格式(eg,PNG)被解码然后上传到图形处理器的图片被叫做纹理。绘制纹理,几何图形和纹理是通过指定每个几何图形的顶点来描述和应用实施的,这个顶点是与几何图形的纹理相一致的。。例如几何图形是一个矩形,然后应用纹理以便每个矩形的角落和纹理的角落相一致
在实际绘图中,首先纹理是有边界的,那么几何图形用OpenGL来绘制。由2者确认纹理在屏幕上位置尺寸和位置,一个是几何图形 另一个是如何配置OpenGL viewport。许多2D游戏配置viewport来匹配屏幕分辨率。意思是几何图形由像素指定,在屏幕适当的尺寸和位置中使它容易绘制纹理。
绘制纹理映射到矩形这是很常见的。多次绘制同一个纹理或者纹理的不同部位也是很常见的。每次发送一个矩形到图形处理器来绘制这将是没有效率的 。对于被声明的纹理可以同时发送许多矩形到图形处理器。这就是SpriteBatch类做的事
SpriteBatch为每个矩形的绘制给出一个纹理坐标。它把没有提交给图形处理器的几何图形聚集到一起。如果和上一个纹理不同,那么它绑定上一个纹理,一起提交把几何图形聚集到一起绘制,然后开始为新的纹理来聚集几何图形。每隔几个矩形改变纹理是为了防止SpriteBatch批处理较多的几何图形来绘制。绑定纹理是一个有点昂贵的操作。由于这些原因。常见在大图像里存储许多小图像,在最大化几何图形的批处理或避免纹理改变之中的较大图像区域绘制。请参阅TexturePacker 更多信息
SpriteBatch
使用SpriteBatch类如下:
publicclassGameimplementsApplicationListener{ privateSpriteBatch batch; publicvoid create () { batch =newSpriteBatch(); } publicvoid render () { Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);// 这句话用来清空屏幕 batch.begin(); // 绘制从这开始 batch.end(); } publicvoid resize (int width,int height){} publicvoid pause (){} publicvoid resume (){} publicvoid dispose (){} }
所有SpriteBatch类的绘制需要调用 begin 和end方法。非SpriteBatch绘制不能发生begin 和 end。
Texture
Texture类解码一个图像文件并载入它到图形处理器的内存中。图像文件将被放置在一个角色assets的文件夹中。图像尺寸必须是2个N次方(如16X16,64X256等)
privateTexture texture; ... texture =newTexture(Gdx.files.internal("image.png")); ... batch.begin(); batch.draw(texture,10,10); batch.end();
Method signature Description draw(Texture texture, float x, float y) Draws the texture using the texture's width and height draw(Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight) Draws a portion of the texture. draw(Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY) Draws a portion of a texture, stretched to thewidth and height, and optionally flipped. 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) This monster method draws a portion of a texture, stretched to thewidth and height, scaled and rotated around an origin, and optionally flipped. draw(Texture texture, float x, float y, float width, float height, float u, float v, float u2, float v2) This draws a portion of a texture, stretched to thewidth and height. This is a somewhat advanced method as it uses texture coordinates from 0-1 rather than pixel coordinates. draw(Texture texture, float[] spriteVertices, int offset, int length) This is an advanced method for passing in the raw geometry, texture coordinates, and color information.
privateTextureRegion region; ... texture=newTexture(Gdx.files.internal("image.png")); region =newTextureRegion(texture,20,20,50,50); ... batch.begin(); batch.draw(region,10,10); batch.end();
上方声明一个 20,,20,50,50的纹理部分,然后再10,10坐标处绘制。同样能通过纹理和SpriteBatch的其他参数来实现,但TextureRegion使它有一个单例用来方便声明。
SpriteBatch关于绘制纹理区域有许多方法。
Method signature Description draw(TextureRegion region, float x, float y) Draws the region using the width and height of the region. draw(TextureRegion region, float x, float y, float width, float height) Draws the region, stretched to thewidth and height. draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation) Draws the region, stretched to thewidth and height, and scaled and rotated around an origin.
privateSprite sprite; ... texture =newTexture(Gdx.files.internal("image.png")); sprite =newSprite(texture,20,20,50,50); sprite.setPosition(10,10); sprite.setRotation(45); ... batch.begin(); sprite.draw(batch); batch.end();
privateTexture texture; privateTextureRegion region; privateSprite sprite; ... texture =newTexture(Gdx.files.internal("image.png")); region =newTextureRegion(texture,20,20,50,50); sprite =newSprite(texture,20,20,50,50); sprite.setPosition(100,10); sprite.setColor(0,0,1,1); ... batch.begin(); batch.setColor(1,0,0,1); batch.draw(texture,10,10); batch.setColor(0,1,0,1); batch.draw(region,50,10); sprite.draw(batch); batch.end();
上方显示怎样绘制纹理,纹理区域和sprite以及着色。颜色值用区域为1-0的RGBA来描述。如果blending(混合)被禁用Alpha则可以忽略。
Blending
Blending默认是打开的。意思是当一个纹理绘制时,纹理的半透明部分被已经在屏幕上位置的像素合并。当Blending关闭时,已经在屏幕位置上的任何一切由纹理取代。除非blending是必要的,为了更高效一般建议关闭它。例如当一个巨大的背景图片绘制超出整个屏幕时,
首先禁用blend将获得性能提升。
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);// This cryptic line clears the screen清空屏幕. batch.begin(); batch.disableBlending(); backgroundSprite.draw(batch); batch.enableBlending(); // Other drawing here在这里绘制其他. batch.end();