Skip to content

Instantly share code, notes, and snippets.

@piotr-j
Forked from xoppa/PolygonShapeTest.java
Created June 17, 2016 11:44
Show Gist options
  • Save piotr-j/953b6e3ed4db89e66cafd0ef6101f580 to your computer and use it in GitHub Desktop.
Save piotr-j/953b6e3ed4db89e66cafd0ef6101f580 to your computer and use it in GitHub Desktop.
Shows how to render primitive shapes using Batch, without using ShapeRenderer. Requires at least libGDX 1.6.4 or latest nightly.
package com.badlogic.gdx.tests.g2d;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g3d.utils.MeshBuilder;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.tests.utils.GdxTest;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
public class PolygonShapeTest extends GdxTest {
PolygonShapeDrawer drawer;
PolygonSpriteBatch batch;
Stage stage;
Skin skin;
Actor ellipse;
TextureRegion white;
@Override
public void create () {
drawer = new PolygonShapeDrawer();
batch = new PolygonSpriteBatch();
stage = new Stage(new ScreenViewport(), batch);
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
white = skin.getAtlas().findRegion("white");
white.setRegion(white.getRegionX()+1, white.getRegionY()+1, 1, 1);
TextButton button = new TextButton("Behind the shape!", skin);
button.setPosition(80, 120);
stage.addActor(button);
ellipse = new Actor() {
@Override
public void draw (Batch batch, float parentAlpha) {
drawer.setTextureRegion(white);
float w = getWidth(), h = getHeight();
drawer.setColor(getColor());
drawer.ellipse(w, h, w * 0.5f, h * 0.5f, 20, getX() + w * 0.5f, getY() + h * 0.5f, 0, 0, 0, -1);
drawer.draw((PolygonSpriteBatch)batch);
}
};
ellipse.setSize(100, 100);
ellipse.setPosition(100, 100);
ellipse.setColor(Color.YELLOW);
stage.addActor(ellipse);
button = new TextButton("In front of the shape!", skin);
button.setPosition(80, 160);
stage.addActor(button);
Gdx.input.setInputProcessor(stage);
}
@Override
public void resize (int width, int height) {
stage.getViewport().update(width, height, true);
}
@Override
public void render () {
stage.act();
stage.draw();
}
public class PolygonShapeDrawer extends MeshBuilder {
private Texture texture;
public PolygonShapeDrawer () {
super();
super.begin(
new VertexAttributes(new VertexAttribute(Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE), VertexAttribute
.ColorPacked(), VertexAttribute.TexCoords(0)), GL20.GL_TRIANGLES);
}
@Override
public Mesh end () {
throw new GdxRuntimeException("Not supported!");
}
@Override
public Mesh end (Mesh mesh) {
throw new GdxRuntimeException("Not supported!");
}
public void setTextureRegion (TextureRegion region) {
if (getNumIndices() > 0)
throw new GdxRuntimeException("Cannot change the TextureRegion in while creating a shape, call draw first.");
texture = region.getTexture();
setUVRange(region);
}
public void draw (PolygonSpriteBatch batch) {
if (texture == null)
throw new GdxRuntimeException("No texture specified, call setTextureRegion before creating the shape");
batch.draw(texture, getVertices(), 0, getNumVertices() * getFloatsPerVertex(), getIndices(), 0, getNumIndices());
clear();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment