Libgdx 圆形冷却框绘制.
MyGame.java:
package com.potato;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.graphics.VertexAttribute;
public class MyGame implements ApplicationListener {
ProgressTimer myprogress ;
ProgressTimer myprogress2 ;
AssetManager manager;
BitmapFont font;
SpriteBatch batch;
BitmapFont font2;
TextureAtlas tex2;
Texture tex1;
Texture progressBar_BG;
Texture progressBar_FT;
TextureRegion region;
float progress = 0;
Texture mytest;
boolean diagnosed = false;
private Mesh lineMesh;
@Override
public void create() {
batch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("data/font.fnt"), false);
manager = new AssetManager();
progressBar_BG = new Texture("data/ProgressBar.png");
progressBar_FT = new Texture("data/GreenBar.png");
region = new TextureRegion(progressBar_BG, 0, 0, 512, 64);
mytest=new Texture("data/ProgressBar.png");
load();
}
public void load() {
tex1 = new Texture("data/animation.png");
tex2 = new TextureAtlas(Gdx.files.internal("data/pack"));
font2 = new BitmapFont(Gdx.files.internal("data/verdana39.fnt"), false);
// 将资源加到预加载队列。
manager.load("data/animation.png", Texture.class);
manager.load("data/mytest2.png", Texture.class);
manager.load("data/pack1.png", Texture.class);
manager.load("data/pack", TextureAtlas.class);
manager.load("data/verdana39.png", Texture.class);
manager.load("data/verdana39.fnt", BitmapFont.class);
}
public void unload() {
tex1.dispose();
tex2.dispose();
font2.dispose();
// 卸载
manager.unload("data/mytest2.png");
manager.unload("data/pack1.png");
manager.unload("data/pack");
manager.unload("data/verdana39.png");
manager.unload("data/verdana39.fnt");
}
@Override
public void dispose() {
}
float MAXnum=10000f;
float CURnum=10000f;
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
boolean result = manager.update();
batch.begin();
if (manager.isLoaded("data/verdana39.png"))
batch.draw(manager.get("data/verdana39.png", Texture.class), 10,
80);
if (manager.isLoaded("data/animation.png"))
batch.draw(manager.get("data/animation.png", Texture.class), 100,
100);
if (manager.isLoaded("data/pack"))
batch.draw(
manager.get("data/pack", TextureAtlas.class).findRegion(
"particle-star"), 164, 100);
if(CURnum>0){
CURnum-=5;
}else{
CURnum=10000;
}
if (manager.isLoaded("data/animation.png")){//animation mytest2
if(myprogress==null){
myprogress = new ProgressTimer(manager, 90, 90, 32, 16, 3, "data/animation.png", ProgressTimer.DIRECTION_COUNTERCLOCKWISE);
}
if(myprogress2==null){
myprogress2=new ProgressTimer(manager, 190, 90, 32, 16, 3, "data/animation.png", ProgressTimer.DIRECTION_COUNTERCLOCKWISE);
}
}
float time=CURnum/MAXnum;
if(myprogress!=null){
myprogress.draw(CURnum/MAXnum);
}
if(myprogress2!=null){
myprogress2.draw(CURnum/MAXnum);
}
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
ProgressTimer .java:
package com.potato;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
public class ProgressTimer {
public static final int DIRECTION_CLOCKWISE = -1;
public static final int DIRECTION_COUNTERCLOCKWISE = 1;
private int MAX_VERTICE;
private int MIN_VERTICE;
private Texture texture;
private Texture texture2;
private Mesh mesh;
private int iVertice;
private float r;
private float fCenterX;
private float fCenterY;
private int iDirection;
public ProgressTimer(AssetManager manager, float centerX, float centerY, float r, int maxVertice, int minVertice, String textureName, int direction) {
fCenterX = centerX;
fCenterY = centerY;
this.r = r;
MAX_VERTICE = maxVertice - minVertice;
MIN_VERTICE = minVertice;
texture = manager.get(textureName, Texture.class);
texture2=new Texture("data/ccc.png");//mytest2.png ProgressBar
iDirection = direction;
}
public ProgressTimer(Texture mytexture, float centerX, float centerY, float r, int maxVertice, int minVertice, int direction) {
fCenterX = centerX;
fCenterY = centerY;
this.r = r;
MAX_VERTICE = maxVertice - minVertice;
MIN_VERTICE = minVertice;
texture =mytexture;
iDirection = direction;
}
public void draw(float progress) {
createMesh(progress);
Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
texture2.bind();
mesh.render(GL10.GL_TRIANGLE_FAN);
}
private void createMesh(float progress) {
iVertice = (int) (MAX_VERTICE * progress) + MIN_VERTICE;
double fTotalAngle = progress * 2 * Math.PI;
double fPerAngle = fTotalAngle / (iVertice - 2);
if(mesh != null)
mesh.dispose();
mesh = new Mesh(true, iVertice * 3, iVertice,
new VertexAttribute(Usage.Position, 3, "point"),
new VertexAttribute(Usage.TextureCoordinates, 2, "texCoords"));
float[] vertices = new float[iVertice * 5];
short[] indexs = new short[iVertice];
vertices[0] = fCenterX;
vertices[1] = fCenterY;
vertices[2] = 0;
vertices[3] = 0.5f;
vertices[4] = 0.5f;
indexs[0] = 0;
for(int i = 1; i < iVertice; i ++){
float fAngle = (float) (iDirection * fPerAngle * (i - 1));
indexs[i] = (short) i;
for(int j = 0; j < 5; j ++){
int iIndex = i * 5 + j;
switch(j)
{
case 0:
vertices[iIndex] = (float) (fCenterX + r * Math.cos(fAngle));
break;
case 1:
vertices[iIndex] = (float) (fCenterY + r * Math.sin(fAngle));
break;
case 2:
vertices[iIndex] = 0;
break;
case 3:
vertices[iIndex] = (float) ( (Math.cos(fAngle) + 1) / 2);
break;
case 4:
vertices[iIndex] = (float) ( ((- Math.sin(fAngle)) + 1) / 2);
break;
}
}
}
mesh.setVertices(vertices);
mesh.setIndices(indexs);
}
}