andengine 画圆

andengine可以画线和矩形。没有提供画圆的方法。

画圆的实现我们需要自己实现。首先,我们需要两个类。

Ellipse.java

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.shape.GLShape;
import org.anddev.andengine.entity.shape.IShape;
import org.anddev.andengine.opengl.buffer.BufferObjectManager;
import org.anddev.andengine.opengl.util.GLHelper;

public class Ellipse extends GLShape {
  private static final float LINEWIDTH_DEFAULT = 1.0f;
  private static final int SEGMENTS_DEFAULT = 50;
  private final EllipseVertexBuffer vertexBuffer;
  private int filledMode;
  private int segments;
  private float lineWidth;
  private float height;
  private float width;

  public Ellipse(float pX, float pY, float radius) {
    this(pX, pY, radius, radius);
  }

  public Ellipse(float pX, float pY, float radius, boolean filled) {
    this(pX, pY, radius, radius, filled, SEGMENTS_DEFAULT);
  }

  public Ellipse(float pX, float pY, float radius, float lineWidth, boolean filled, int segments) {
    this(pX, pY, radius, radius, lineWidth, filled, segments);
  }

  public Ellipse(float pX, float pY, float radius, int segments) {
    this(pX, pY, radius, LINEWIDTH_DEFAULT, false, segments);
  }

  public Ellipse(float pX, float pY, float width, float height) {
    this(pX, pY, width, height, LINEWIDTH_DEFAULT, false, SEGMENTS_DEFAULT);
  }

  public Ellipse(int pX, int pY, int radius, float lineWidth, int segments) {
    this(pX, pY, radius, lineWidth, false, segments);
  }

  public Ellipse(float pX, float pY, float width, float height, float lineWidth, boolean filled,
      int segments) {
    super(pX, pY);
    this.width = width;
    this.height = height;
    this.filledMode = (filled) ? GL10.GL_TRIANGLE_FAN : GL10.GL_LINE_LOOP;
    this.segments = segments;
    this.lineWidth = lineWidth;

    vertexBuffer = new EllipseVertexBuffer(segments, GL11.GL_STATIC_DRAW);
    BufferObjectManager.getActiveInstance().loadBufferObject(vertexBuffer);
    this.updateVertexBuffer();
  }

  @Override
  public float[] getSceneCenterCoordinates() {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public float getWidth() {
    return width;
  }

  @Override
  public float getHeight() {
    return height;
  }

  @Override
  public float getBaseWidth() {
    return width;
  }

  @Override
  public float getBaseHeight() {
    return height;
  }

  @Override
  public boolean collidesWith(IShape pOtherShape) {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public boolean contains(float pX, float pY) {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public float[] convertSceneToLocalCoordinates(float pX, float pY) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public float[] convertLocalToSceneCoordinates(float pX, float pY) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  protected void onUpdateVertexBuffer() {
    vertexBuffer.update(segments, getWidth(), getHeight());
  }

  @Override
  protected EllipseVertexBuffer getVertexBuffer() {
    return vertexBuffer;
  }

  @Override
  protected boolean isCulled(Camera pCamera) {
    return false;
  }

  @Override
  protected void onInitDraw(final GL10 pGL) {
    super.onInitDraw(pGL);
    GLHelper.disableTextures(pGL);
    GLHelper.disableTexCoordArray(pGL);
    // enable for nicer lines, at the expense of a limited linewidth of 1
    // pGL.glEnable(GL10.GL_LINE_SMOOTH);
    GLHelper.lineWidth(pGL, lineWidth);
  }

  @Override
  protected void drawVertices(GL10 gl, Camera pCamera) {
    gl.glDrawArrays(filledMode, 0, segments);
  }

  public float getLineWidth() {
    return lineWidth;
  }

  public void setLineWidth(float lineWidth) {
    this.lineWidth = lineWidth;
  }

  public void setHeight(float height) {
    this.height = height;
    this.updateVertexBuffer();
  }

  public void setWidth(float width) {
    this.width = width;
    this.updateVertexBuffer();
  }

}

 

EllipseVertexBuffer.java

import org.anddev.andengine.opengl.util.FastFloatBuffer;
import org.anddev.andengine.opengl.vertex.VertexBuffer;
import org.anddev.andengine.util.MathUtils;

public class EllipseVertexBuffer extends VertexBuffer {

  public EllipseVertexBuffer(int segments, int pDrawType) {
    super(segments * 2, pDrawType);
  }

  void update(int segments, float width, float height) {
    final int[] vertices = this.mBufferData;

    int count = 0;
    for (float i = 0; i < 360.0f; i += (360.0f / segments)) {
      vertices[count++] = Float
          .floatToRawIntBits((float) (Math.cos(MathUtils.degToRad(i)) * width));
      vertices[count++] = Float
          .floatToRawIntBits((float) (Math.sin(MathUtils.degToRad(i)) * height));
    }

    final FastFloatBuffer buffer = this.getFloatBuffer();
    buffer.position(0);
    buffer.put(vertices);
    buffer.position(0);

    super.setHardwareBufferNeedsUpdate();
  }
}

接下来知道怎么做了吧

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值