1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.graphics.Color; 21 import com.badlogic.gdx.graphics.GL20; 22 import com.badlogic.gdx.graphics.OrthographicCamera; 23 import com.badlogic.gdx.graphics.Texture; 24 import com.badlogic.gdx.graphics.g2d.PolygonRegion; 25 import com.badlogic.gdx.graphics.g2d.PolygonRegionLoader; 26 import com.badlogic.gdx.graphics.g2d.PolygonSprite; 27 import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch; 28 import com.badlogic.gdx.graphics.g2d.TextureRegion; 29 import com.badlogic.gdx.graphics.glutils.ShapeRenderer; 30 import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; 31 import com.badlogic.gdx.math.MathUtils; 32 import com.badlogic.gdx.math.Rectangle; 33 import com.badlogic.gdx.tests.utils.GdxTest; 34 import com.badlogic.gdx.utils.Array; 35 36 public class PolygonSpriteTest extends GdxTest { 37 38 PolygonSpriteBatch batch; 39 ShapeRenderer renderer; 40 41 Texture texture; 42 OrthographicCamera camera; 43 PolygonRegion region; 44 Rectangle bounds; 45 46 Array<PolygonSprite> sprites = new Array<PolygonSprite>(); 47 48 @Override create()49 public void create () { 50 texture = new Texture(Gdx.files.internal("data/tree.png")); 51 52 PolygonRegionLoader loader = new PolygonRegionLoader(); 53 region = loader.load(new TextureRegion(texture), Gdx.files.internal("data/tree.psh")); 54 55 renderer = new ShapeRenderer(); 56 57 camera = new OrthographicCamera(480, 320); 58 camera.position.x = 240; 59 camera.position.y = 160; 60 camera.update(); 61 62 batch = new PolygonSpriteBatch(); 63 64 for (int i = 0; i < 50; i++) { 65 PolygonSprite sprite = new PolygonSprite(region); 66 sprite.setPosition(MathUtils.random(-30, 440), MathUtils.random(-30, 290)); 67 sprite.setColor(MathUtils.random(), MathUtils.random(), MathUtils.random(), 1.0f); 68 sprite.setScale(MathUtils.random(0.5f, 1.5f), MathUtils.random(0.5f, 1.5f)); 69 sprites.add(sprite); 70 } 71 } 72 73 @Override render()74 public void render () { 75 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 76 Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1.0f); 77 78 camera.update(); 79 batch.setProjectionMatrix(camera.combined); 80 81 batch.begin(); 82 83 for (int i = 0; i < sprites.size; i++) { 84 PolygonSprite sprite = sprites.get(i); 85 sprite.rotate(45 * Gdx.graphics.getDeltaTime()); 86 sprite.translateX(10 * Gdx.graphics.getDeltaTime()); 87 88 if (sprite.getX() > 450) sprite.setX(-50); 89 90 sprite.draw(batch); 91 } 92 batch.end(); 93 94 // Some debug rendering, bounding box & origin of one sprite 95 renderer.setProjectionMatrix(camera.combined); 96 renderer.setColor(Color.GREEN); 97 renderer.begin(ShapeType.Line); 98 99 PolygonSprite sprite = sprites.get(49); 100 101 bounds = sprite.getBoundingRectangle(); 102 renderer.rect(bounds.x, bounds.y, bounds.width, bounds.height); 103 104 renderer.end(); 105 106 renderer.begin(ShapeType.Filled); 107 108 renderer.circle(sprite.getX() + sprite.getOriginX(), sprite.getY() + sprite.getOriginY(), 4); 109 110 renderer.end(); 111 } 112 113 @Override dispose()114 public void dispose () { 115 texture.dispose(); 116 batch.dispose(); 117 } 118 } 119