Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. public class TutorialShaperenderer extends ApplicationAdapter {
  2.  
  3.     Vector2 centerTwo, centerOne;
  4.     FloatArray verticesTwo, verticesOne;
  5.  
  6.     Texture texture;
  7.     PolygonSprite polySprite;
  8.     PolygonSpriteBatch polyBatch;
  9.  
  10.     ShapeRenderer sr;
  11.  
  12.     @Override
  13.     public void create() {
  14.  
  15.         // 1
  16.         sr = new ShapeRenderer();
  17.  
  18.         // 2
  19.         polyBatch = new PolygonSpriteBatch();
  20.  
  21.         Pixmap pix = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
  22.         pix.setColor(1, 0, 0, 1);
  23.         pix.fill();
  24.         texture = new Texture(pix);
  25.         TextureRegion textureRegion = new TextureRegion(texture);
  26.  
  27.         // 1
  28.         centerOne = new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
  29.  
  30.         verticesOne = new FloatArray(
  31.                 new float[] { centerOne.x, centerOne.y + 50, centerOne.x + 100, centerOne.y, centerOne.x + 50,
  32.                         centerOne.y - 50, centerOne.x - 50, centerOne.y - 50, centerOne.x - 100, centerOne.y });
  33.         // 2
  34.         centerTwo = new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2 - 100);
  35.  
  36.         verticesTwo = new FloatArray(
  37.                 new float[] { centerTwo.x, centerTwo.y + 50, centerTwo.x + 100, centerTwo.y, centerTwo.x + 50,
  38.                         centerTwo.y - 50, centerTwo.x - 50, centerTwo.y - 50, centerTwo.x - 100, centerTwo.y });
  39.  
  40.         EarClippingTriangulator triangulator = new EarClippingTriangulator();
  41.         ShortArray triangleIndices = triangulator.computeTriangles(verticesTwo);
  42.  
  43.         PolygonRegion polyReg = new PolygonRegion(textureRegion, verticesTwo.toArray(), triangleIndices.toArray());
  44.  
  45.         polySprite = new PolygonSprite(polyReg);
  46.  
  47.     }
  48.  
  49.     @Override
  50.     public void render() {
  51.         Gdx.gl.glClearColor(0, 0, 0, 1);
  52.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  53.  
  54.         // 2
  55.         polyBatch.begin();
  56.         polySprite.draw(polyBatch);
  57.         polyBatch.end();
  58.  
  59.         // 1
  60.         sr.begin(ShapeType.Line);
  61.         sr.setColor(0, 1, 0, 1);
  62.         sr.polygon(verticesOne.toArray());
  63.         sr.end();
  64.     }
  65.  
  66.     @Override
  67.     public void resize(int width, int height) {
  68.         super.resize(width, height);
  69.     }
  70.  
  71.     @Override
  72.     public void dispose() {
  73.         polyBatch.dispose();
  74.         texture.dispose();
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement