andengine 画圆

    技术2022-05-20  31

    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(); } } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

     

    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(); } }

    .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

    接下来知道怎么做了吧


    最新回复(0)