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 java.util.Comparator; 20 21 import com.badlogic.gdx.Gdx; 22 import com.badlogic.gdx.graphics.GL20; 23 import com.badlogic.gdx.graphics.Texture; 24 import com.badlogic.gdx.graphics.g2d.Sprite; 25 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 26 import com.badlogic.gdx.graphics.g2d.TextureRegion; 27 import com.badlogic.gdx.math.MathUtils; 28 import com.badlogic.gdx.tests.utils.GdxTest; 29 import com.badlogic.gdx.utils.Array; 30 31 /** Demonstrates how to do simple z-sorting of sprites 32 * @author mzechner */ 33 public class SortedSpriteTest extends GdxTest { 34 /** Sprite based class that adds a z-coordinate for depth sorting. Note that allt he constructors were auto-generated in Eclipse 35 * (alt + shift + s, c). 36 * @author mzechner */ 37 public class MySprite extends Sprite { 38 public float z; 39 MySprite()40 public MySprite () { 41 super(); 42 } 43 MySprite(Sprite sprite)44 public MySprite (Sprite sprite) { 45 super(sprite); 46 } 47 MySprite(Texture texture, int srcX, int srcY, int srcWidth, int srcHeight)48 public MySprite (Texture texture, int srcX, int srcY, int srcWidth, int srcHeight) { 49 super(texture, srcX, srcY, srcWidth, srcHeight); 50 } 51 MySprite(Texture texture, int srcWidth, int srcHeight)52 public MySprite (Texture texture, int srcWidth, int srcHeight) { 53 super(texture, srcWidth, srcHeight); 54 } 55 MySprite(Texture texture)56 public MySprite (Texture texture) { 57 super(texture); 58 } 59 MySprite(TextureRegion region, int srcX, int srcY, int srcWidth, int srcHeight)60 public MySprite (TextureRegion region, int srcX, int srcY, int srcWidth, int srcHeight) { 61 super(region, srcX, srcY, srcWidth, srcHeight); 62 } 63 MySprite(TextureRegion region)64 public MySprite (TextureRegion region) { 65 super(region); 66 } 67 } 68 69 /** Comparator used for sorting, sorts in ascending order (biggset z to smallest z). 70 * @author mzechner */ 71 public class MySpriteComparator implements Comparator<MySprite> { 72 @Override compare(MySprite sprite1, MySprite sprite2)73 public int compare (MySprite sprite1, MySprite sprite2) { 74 return (sprite2.z - sprite1.z) > 0 ? 1 : -1; 75 } 76 } 77 78 /** spritebatch used for rendering **/ 79 SpriteBatch batch; 80 /** the texture used by the sprites **/ 81 Texture texture; 82 /** array of sprites **/ 83 Array<MySprite> sprites = new Array<MySprite>(); 84 /** a comparator, we keep it around so the GC shuts up **/ 85 MySpriteComparator comparator = new MySpriteComparator(); 86 87 @Override create()88 public void create () { 89 // create the SpriteBatch 90 batch = new SpriteBatch(); 91 92 // load a texture, usually you dispose of this 93 // eventually. 94 texture = new Texture("data/badlogicsmall.jpg"); 95 96 // create 100 sprites, tinted red, from dark to light. 97 // red color component is also used as z-value so we 98 // can see that the sorting works. 99 for (int i = 0; i < 100; i++) { 100 // create the sprite and set a random position 101 MySprite sprite = new MySprite(texture); 102 sprite.setPosition(MathUtils.random() * Gdx.graphics.getWidth(), MathUtils.random() * Gdx.graphics.getHeight()); 103 104 // create a random z coordinate in the range 0-1 105 sprite.z = MathUtils.random(); 106 107 // set the tinting color to the z coordinate as well 108 // for visual inspection 109 sprite.setColor(sprite.z, 0, 0, 1); 110 111 // add the sprite to the array 112 sprites.add(sprite); 113 } 114 } 115 116 @Override render()117 public void render () { 118 Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1); 119 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 120 121 // sort the sprites (not necessary if we know 122 // the are already sorted). 123 sprites.sort(comparator); 124 125 // draw the sprites 126 batch.begin(); 127 for (MySprite sprite : sprites) { 128 sprite.draw(batch); 129 } 130 batch.end(); 131 } 132 133 @Override dispose()134 public void dispose () { 135 batch.dispose(); 136 texture.dispose(); 137 } 138 } 139