From: nathan.sweet Date: Wed, 17 Nov 2010 06:28:53 +0000 (+0000) Subject: [added] SpriteSheetPacker, SpriteSheet, and SpriteSheetTest. X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=79f44387b484ab52f769736dde2b10e63376aaf4;p=mikumikustudio%2Flibgdx-mikumikustudio.git [added] SpriteSheetPacker, SpriteSheet, and SpriteSheetTest. --- diff --git a/extensions/image-packer/src/com/badlogic/gdx/imagepacker/SpriteSheetPacker.java b/extensions/image-packer/src/com/badlogic/gdx/imagepacker/SpriteSheetPacker.java new file mode 100644 index 000000000..28b1cd13e --- /dev/null +++ b/extensions/image-packer/src/com/badlogic/gdx/imagepacker/SpriteSheetPacker.java @@ -0,0 +1,480 @@ + +package com.badlogic.gdx.imagepacker; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.image.BufferedImage; +import java.awt.image.WritableRaster; +import java.io.File; +import java.io.FileWriter; +import java.io.FilenameFilter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.imageio.ImageIO; + +import com.badlogic.gdx.utils.MathUtils; + +public class SpriteSheetPacker { + static Pattern numberedImagePattern = Pattern.compile(".*?(\\d+)"); + + private ArrayList images = new ArrayList(); + FileWriter writer; + final File inputDir; + private int uncompressedSize, compressedSize; + final Direction direction; + int xPadding, yPadding; + private final Filter filter; + + // User configurable settings: + private int alphaThreshold = 11; + private boolean pot = true; + private int padding = 0; + private boolean debug = false; + + public SpriteSheetPacker (File inputDir, Filter filter, Direction direction, File outputDir, File packFile) throws IOException { + this.inputDir = inputDir; + this.filter = filter; + this.direction = direction; + + ArrayList files = getFiles(inputDir, filter, direction); + if (files == null) return; + + for (File file : files) { + if (file.isDirectory()) continue; + Image image = squeeze(file); + if (image != null) images.add(image); + } + if (images.isEmpty()) return; + + System.out.println(inputDir); + if (filter != rgba8888) System.out.println("Format: " + filter.name); + if (direction != null) System.out.println("Direction: " + direction); + for (Image image : images) + System.out.println("Packing... " + image.file.getName()); + + Collections.sort(images, new Comparator() { + public int compare (Image image1, Image image2) { + return image1.getWidth() * image1.getHeight() - image2.getWidth() * image2.getHeight(); + } + }); + + xPadding = images.size() > 1 && direction != Direction.x && direction != Direction.xy ? padding : 0; + yPadding = images.size() > 1 && direction != Direction.y && direction != Direction.xy ? padding : 0; + + outputDir.mkdirs(); + String prefix = inputDir.getParentFile().getName(); + + writer = new FileWriter(packFile, true); + try { + while (!images.isEmpty()) + writePage(prefix, outputDir); + if (writer != null) { + System.out.println("Pixels eliminated: " + (1 - compressedSize / (float)uncompressedSize) * 100 + "%"); + System.out.println(); + } + } finally { + writer.close(); + } + } + + private void writePage (String prefix, File outputDir) throws IOException { + int imageNumber = 1; + File outputFile = new File(outputDir, prefix + imageNumber + ".png"); + while (outputFile.exists()) + outputFile = new File(outputDir, prefix + ++imageNumber + ".png"); + + writer.write("\n" + prefix + imageNumber + ".png\n"); + writer.write(direction + "\n"); + + // Try reasonably hard to find the smallest size that is also the smallest POT. + Comparator bestComparator = null; + Comparator secondBestComparator = imageComparators.get(0); + int maxWidth = 1024, maxHeight = 1024; + int bestWidth = 99999, bestHeight = 99999; + int secondBestWidth = 99999, secondBestHeight = 99999; + int bestUsedPixels = 0; + int width = 64, height = 64; + int grownPixels = 0, grownPixels2 = 0; + int i = 0, ii = 0; + while (true) { + if (width > maxWidth && height > maxHeight) break; + for (Comparator comparator : imageComparators) { + Collections.sort(images, comparator); + + int usedPixels = insert(null, new ArrayList(images), width, height); + if (usedPixels > bestUsedPixels) { + secondBestComparator = comparator; + secondBestWidth = width; + secondBestHeight = height; + } + if (usedPixels == -1) { + if (width * height < bestWidth * bestHeight) { + bestComparator = comparator; + bestWidth = width; + bestHeight = height; + } + } + } + if (bestComparator != null) break; + if (pot) { + if (i % 3 == 0) { + width *= 2; + i++; + } else if (i % 3 == 1) { + width /= 2; + height *= 2; + i++; + } else { + width *= 2; + i++; + } + } else { + if (i % 3 == 0) { + width++; + grownPixels++; + if (width == MathUtils.nextPowerOfTwo(width)) { + width -= grownPixels; + grownPixels = 0; + i++; + } + } else if (i % 3 == 1) { + height++; + grownPixels++; + if (height == MathUtils.nextPowerOfTwo(height)) { + height -= grownPixels; + grownPixels = 0; + i++; + } + } else { + if (width == MathUtils.nextPowerOfTwo(width) && height == MathUtils.nextPowerOfTwo(height)) ii++; + if (ii % 2 == 1) + width++; + else + height++; + i++; + } + } + } + if (bestComparator != null) { + Collections.sort(images, bestComparator); + } else { + Collections.sort(images, secondBestComparator); + bestWidth = secondBestWidth; + bestHeight = secondBestHeight; + } + width = bestWidth; + height = bestHeight; + + if (pot) { + width = MathUtils.nextPowerOfTwo(width); + height = MathUtils.nextPowerOfTwo(height); + } + + BufferedImage canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + insert(canvas, images, bestWidth, bestHeight); + System.out.println("Writing " + canvas.getWidth() + "x" + canvas.getHeight() + ": " + outputFile); + ImageIO.write(canvas, "png", outputFile); + compressedSize += canvas.getWidth() * canvas.getHeight(); + } + + private int insert (BufferedImage canvas, ArrayList images, int width, int height) throws IOException { + if (debug && canvas != null) { + Graphics g = canvas.getGraphics(); + g.setColor(Color.green); + g.drawRect(0, 0, width - 1, height - 1); + } + // Pretend image is larger so padding on right and bottom edges is ignored. + if (direction != Direction.x && direction != Direction.xy) width += xPadding; + if (direction != Direction.y && direction != Direction.xy) height += yPadding; + Node root = new Node(0, 0, width, height); + int usedPixels = 0; + for (int i = images.size() - 1; i >= 0; i--) { + Image image = images.get(i); + Node node = root.insert(image, canvas); + if (node == null) continue; + usedPixels += image.getWidth() * image.getHeight(); + images.remove(i); + if (canvas != null) { + Graphics g = canvas.getGraphics(); + g.drawImage(image, node.left, node.top, null); + if (debug) { + g.setColor(Color.magenta); + g.drawRect(node.left, node.top, image.getWidth() - 1, image.getHeight() - 1); + } + } + } + return images.isEmpty() ? -1 : usedPixels; + } + + private Image squeeze (File file) throws IOException { + BufferedImage source = ImageIO.read(file); + if (source == null) return null; + uncompressedSize += source.getWidth() * source.getHeight(); + WritableRaster alphaRaster = source.getAlphaRaster(); + if (alphaRaster == null) return new Image(file, source, 0, 0, source.getWidth(), source.getHeight()); + final byte[] a = new byte[1]; + int top = 0; + outer: + for (int y = 0; y < source.getHeight(); y++) { + for (int x = 0; x < source.getWidth(); x++) { + alphaRaster.getDataElements(x, y, a); + int alpha = a[0]; + if (alpha < 0) alpha += 256; + if (alpha > alphaThreshold) break outer; + } + top++; + } + int bottom = source.getHeight() - 1; + outer: + for (int y = source.getHeight(); --y >= top;) { + for (int x = 0; x < source.getWidth(); x++) { + alphaRaster.getDataElements(x, y, a); + int alpha = a[0]; + if (alpha < 0) alpha += 256; + if (alpha > alphaThreshold) break outer; + } + bottom--; + } + int left = 0; + outer: + for (int x = 0; x < source.getWidth(); x++) { + for (int y = top; y <= bottom; y++) { + alphaRaster.getDataElements(x, y, a); + int alpha = a[0]; + if (alpha < 0) alpha += 256; + if (alpha > alphaThreshold) break outer; + } + left++; + } + int right = source.getWidth() - 1; + outer: + for (int x = source.getWidth(); --x >= left;) { + for (int y = top; y <= bottom; y++) { + alphaRaster.getDataElements(x, y, a); + int alpha = a[0]; + if (alpha < 0) alpha += 256; + if (alpha > alphaThreshold) break outer; + } + right--; + } + int newWidth = right - left; + int newHeight = bottom - top; + if (newWidth <= 0 || newHeight <= 0) { + System.out.println("Ignoring blank input image: " + file.getAbsolutePath()); + return null; + } + return new Image(file, source, left, top, newWidth, newHeight); + } + + private class Node { + final int left, top, width, height; + Node child1, child2; + Image image; + + public Node (int left, int top, int width, int height) { + this.left = left; + this.top = top; + this.width = width; + this.height = height; + } + + public Node insert (Image image, BufferedImage canvas) throws IOException { + if (this.image != null) return null; + if (child1 != null) { + Node newNode = child1.insert(image, canvas); + if (newNode != null) return newNode; + return child2.insert(image, canvas); + } + int neededWidth = image.getWidth() + xPadding; + int neededHeight = image.getHeight() + yPadding; + if (neededWidth > width || neededHeight > height) return null; + if (neededWidth == width && neededHeight == height) { + this.image = image; + write(canvas); + return this; + } + int dw = width - neededWidth; + int dh = height - neededHeight; + if (dw > dh) { + child1 = new Node(left, top, neededWidth, height); + child2 = new Node(left + neededWidth, top, width - neededWidth, height); + } else { + child1 = new Node(left, top, width, neededHeight); + child2 = new Node(left, top + neededHeight, width, height - neededHeight); + } + return child1.insert(image, canvas); + } + + private void write (BufferedImage canvas) throws IOException { + if (canvas == null) return; + + String imageName = image.file.getAbsolutePath().substring(inputDir.getAbsolutePath().length()) + "\n"; + if (imageName.startsWith("/") || imageName.startsWith("\\")) imageName = imageName.substring(1); + int dotIndex = imageName.lastIndexOf('.'); + if (dotIndex != -1) imageName = imageName.substring(0, dotIndex); + if (imageName.endsWith("_4444")) imageName = imageName.substring(0, imageName.length() - 5); + if (imageName.endsWith("_565")) imageName = imageName.substring(0, imageName.length() - 4); + if (imageName.endsWith("_a")) imageName = imageName.substring(0, imageName.length() - 2); + if (imageName.endsWith("_pre")) imageName = imageName.substring(0, imageName.length() - 2); + + writer.write(imageName.replace("\\", "/") + "\n"); + writer.write(left + "\n"); + writer.write(top + "\n"); + writer.write(image.getWidth() + "\n"); + writer.write(image.getHeight() + "\n"); + writer.write(image.offsetX + "\n"); + writer.write(image.offsetY + "\n"); + writer.write(image.originalWidth + "\n"); + writer.write(image.originalHeight + "\n"); + + Matcher matcher = numberedImagePattern.matcher(imageName); + if (matcher.matches()) + writer.write(Integer.parseInt(matcher.group(1)) + "\n"); + else + writer.write("0\n"); + } + } + + static private class Image extends BufferedImage { + final File file; + final int offsetX, offsetY; + final int originalWidth, originalHeight; + + public Image (File file, BufferedImage src, int left, int top, int newWidth, int newHeight) { + super(src.getColorModel(), src.getRaster().createWritableChild(left, top, newWidth, newHeight, 0, 0, null), src + .getColorModel().isAlphaPremultiplied(), null); + this.file = file; + offsetX = left; + offsetY = top; + originalWidth = src.getWidth(); + originalHeight = src.getHeight(); + } + + public String toString () { + return file.toString(); + } + } + + static private ArrayList imageComparators = new ArrayList(); + static { + imageComparators.add(new Comparator() { + public int compare (Image image1, Image image2) { + int diff = image1.getHeight() - image2.getHeight(); + if (diff != 0) return diff; + return image1.getWidth() - image2.getWidth(); + } + }); + imageComparators.add(new Comparator() { + public int compare (Image image1, Image image2) { + int diff = image1.getWidth() - image2.getWidth(); + if (diff != 0) return diff; + return image1.getHeight() - image2.getHeight(); + } + }); + imageComparators.add(new Comparator() { + public int compare (Image image1, Image image2) { + return image1.getWidth() * image1.getHeight() - image2.getWidth() * image2.getHeight(); + } + }); + } + + static private Filter rgba8888 = new Filter("RGBA8888") { + public boolean accept (File dir, String name) { + return !name.endsWith("_4444") && !name.endsWith("_565") && !name.endsWith("_a"); + } + }; + static private Filter rgba4444 = new Filter("RGBA4444") { + public boolean accept (File dir, String name) { + return name.endsWith("_4444"); + } + }; + static private Filter rgb565 = new Filter("RGB565") { + public boolean accept (File dir, String name) { + return name.endsWith("_565"); + } + }; + static private Filter alpha = new Filter("Alpha") { + public boolean accept (File dir, String name) { + return name.endsWith("_a"); + } + }; + + static abstract private class Filter implements FilenameFilter { + String name; + + public Filter (String name) { + this.name = name; + } + } + + static private enum Direction { + x, y, xy, none + } + + static private ArrayList getFiles (File inputDir, Filter filter, Direction direction) { + ArrayList files = new ArrayList(); + files.addAll(Arrays.asList(inputDir.listFiles(filter))); + for (Iterator iter = files.iterator(); iter.hasNext();) { + File file = iter.next(); + String name = file.getName(); + switch (direction) { + case none: + if (name.contains("_x") || name.contains("_y")) iter.remove(); + break; + case x: + if (!name.contains("_x") || name.contains("_xy")) iter.remove(); + break; + case y: + if (!name.contains("_y")) iter.remove(); + break; + case xy: + if (!name.contains("_xy")) iter.remove(); + break; + } + } + return files; + } + + static private void process (File inputDir, File outputDir, File packFile) throws Exception { + if (outputDir.exists()) { + String prefix = inputDir.getParentFile().getName(); + for (File file : outputDir.listFiles()) + if (file.getName().startsWith(prefix)) file.delete(); + } + + Direction[] directions = Direction.values(); + for (int i = 0; i < directions.length; i++) { + Direction direction = directions[i]; + new SpriteSheetPacker(inputDir, rgba8888, direction, outputDir, packFile); + new SpriteSheetPacker(inputDir, rgba4444, direction, outputDir, packFile); + new SpriteSheetPacker(inputDir, rgb565, direction, outputDir, packFile); + new SpriteSheetPacker(inputDir, alpha, direction, outputDir, packFile); + } + File[] files = inputDir.listFiles(); + if (files == null) return; + for (File file : files) + if (file.isDirectory()) process(file, new File(outputDir, file.getName()), packFile); + } + + static public void process (String inputDir, String outputDir) throws Exception { + File input = new File(inputDir); + if (!input.isDirectory()) { + System.out.println("Not a directory: " + input); + return; + } + File packFile = new File(outputDir, "pack"); + packFile.delete(); + process(new File(inputDir), new File(outputDir), packFile); + } + + public static void main (String[] args) throws Exception { + process("C:/Dev/libgdx/tests/gdx-tests-lwjgl/data/New folder", "c:/temp/pack-out"); + } +} diff --git a/gdx/src/com/badlogic/gdx/graphics/SpriteSheet.java b/gdx/src/com/badlogic/gdx/graphics/SpriteSheet.java new file mode 100644 index 000000000..c5163bcbe --- /dev/null +++ b/gdx/src/com/badlogic/gdx/graphics/SpriteSheet.java @@ -0,0 +1,201 @@ + +package com.badlogic.gdx.graphics; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.PriorityQueue; + +import com.badlogic.gdx.Files.FileType; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.files.FileHandle; +import com.badlogic.gdx.graphics.Texture.TextureWrap; + +import static com.badlogic.gdx.graphics.Texture.TextureWrap.*; +import static com.badlogic.gdx.graphics.Texture.TextureFilter.*; +import com.badlogic.gdx.utils.GdxRuntimeException; + +/** + * Loads images from texture atlases created by SpriteSheetPacker.
+ *
+ * A SpriteSheet must be disposed to free up the resources consumed by the backing textures. + */ +public class SpriteSheet { + private final ArrayList textures = new ArrayList(4); + private final PackedSprite[] images; + + public SpriteSheet (FileHandle packFile, FileHandle imagesDir) { + PriorityQueue sortedSprites = new PriorityQueue(16, indexComparator); + + BufferedReader reader = new BufferedReader(new InputStreamReader(packFile.read()), 64); + try { + Sprite pageImage = null; + while (true) { + String line = reader.readLine(); + if (line == null) break; + if (line.trim().length() == 0) + pageImage = null; + else if (pageImage == null) { + FileHandle file = imagesDir.child(line); + + // BOZO - Get filter from file? + + String direction = reader.readLine(); + TextureWrap wrapX = ClampToEdge; + TextureWrap wrapY = ClampToEdge; + if (direction.equals("x")) + wrapX = Repeat; + else if (direction.equals("y")) + wrapY = Repeat; + else if (direction.equals("xy")) { + wrapX = Repeat; + wrapY = Repeat; + } + + Texture texture = Gdx.graphics.newTexture(file, Linear, Linear, ClampToEdge, ClampToEdge); + textures.add(texture); + + pageImage = new Sprite(texture); + } else { + int left = Integer.parseInt(reader.readLine()); + int top = Integer.parseInt(reader.readLine()); + int width = Integer.parseInt(reader.readLine()); + int height = Integer.parseInt(reader.readLine()); + int offsetX = Integer.parseInt(reader.readLine()); + int offsetY = Integer.parseInt(reader.readLine()); + int originalWidth = Integer.parseInt(reader.readLine()); + int originalHeight = Integer.parseInt(reader.readLine()); + PackedSprite image = new PackedSprite(pageImage, left, top, width, height); + image.setPosition(offsetX, offsetY); + image.name = line; + image.offsetX = offsetX; + image.offsetY = offsetY; + image.originalWidth = originalWidth; + image.originalHeight = originalHeight; + image.index = Integer.parseInt(reader.readLine()); + if (image.index == -1) image.index = Integer.MAX_VALUE; + sortedSprites.add(image); + } + } + } catch (IOException ex) { + throw new GdxRuntimeException("Error reading pack file: " + packFile); + } finally { + try { + reader.close(); + } catch (IOException ignored) { + } + } + + int n = sortedSprites.size(); + images = new PackedSprite[n]; + for (int i = 0; i < n; i++) + images[i] = sortedSprites.poll(); + } + + /** + * Returns the first sprite found with the specified name.
+ *
+ * This method uses string comparison to find the sprite, so the result should be cached rather than calling this method every + * frame. + */ + public PackedSprite get (String name) { + for (int i = 0, n = images.length; i < n; i++) + if (images[i].name.equals(name)) return images[i]; + return null; + } + + /** + * Returns all sprites found with the specified name, ordered by smallest to largest {@link PackedSprite#getIndex() index}.
+ *
+ * This method uses string comparison to find the sprite, so the result should be cached rather than calling this method every + * frame. + */ + public List getAll (String name) { + ArrayList matched = new ArrayList(); + for (int i = 0, n = images.length; i < n; i++) + if (images[i].name.equals(name)) matched.add(images[i]); + return matched; + } + + /** + * Releases all resources associated with this PackedSprite instance. This releases all the textures backing all the sprites, + * so the sprites should no longer be used after calling dispose. + */ + public void dispose () { + for (int i = 0, n = textures.size(); i < n; i++) + textures.get(i).dispose(); + } + + static private final Comparator indexComparator = new Comparator() { + public int compare (PackedSprite image1, PackedSprite image2) { + return image1.index - image2.index; + } + }; + + /** + * A sprite that provides additional information about the packed image it represents. A PackedSprite's position is relative to + * the bottom left of the original image, before whitespace was removed for packing. + */ + static public class PackedSprite extends Sprite { + int index; + String name; + int offsetX, offsetY; + int originalWidth, originalHeight; + + PackedSprite (Sprite image, int textureLeft, int textureTop, int textureRight, int textureBottom) { + super(image, textureLeft, textureTop, textureRight, textureBottom); + } + + // BOZO - Test offset works and flip is handled. + public void setPosition (float x, float y) { + super.setPosition(x + offsetX, y + offsetY); + } + + public void setBounds (float x, float y, float width, float height) { + super.setBounds(x + offsetX, y + offsetY, width, height); + } + + /** + * The name of the original image file, with any trailing numbers or special flags removed. + */ + public String getName () { + return name; + } + + /** + * The number at the end of the original image file name, or Integer.MAX_VALUE if none.
+ *
+ * When sprites are packed, if the original file name ends with a number, it is stored as the index and is not considered as + * part of the sprite's name. This is useful for keeping animation frames in order. + * @see SpriteSheet#getAll(String) + */ + public int getIndex () { + return index; + } + + public int getOriginalWidth () { + return originalWidth; + } + + public int getOriginalHeight () { + return originalHeight; + } + + /** + * The offset from the left of the original image to the left of the packed image, after whitespace has been removed. + */ + public int getOffsetX () { + return offsetX; + } + + /** + * The offset from the bottom of the original image to the bottom of the packed image, after whitespace has been removed. + */ + public int getOffsetY () { + return offsetY; + } + } +} diff --git a/tests/gdx-tests-android/assets/data/data1.png b/tests/gdx-tests-android/assets/data/data1.png new file mode 100644 index 000000000..4392e0a1c Binary files /dev/null and b/tests/gdx-tests-android/assets/data/data1.png differ diff --git a/tests/gdx-tests-android/assets/data/pack b/tests/gdx-tests-android/assets/data/pack new file mode 100644 index 000000000..cf40babba --- /dev/null +++ b/tests/gdx-tests-android/assets/data/pack @@ -0,0 +1,43 @@ + +data1.png +none +badlogic +0 +0 +256 +256 +0 +0 +256 +256 +0 +particle-fire +256 +0 +127 +122 +0 +0 +128 +128 +0 +particle-star +383 +0 +63 +63 +0 +0 +64 +64 +0 +badlogicsmall +383 +63 +32 +32 +0 +0 +32 +32 +0 diff --git a/tests/gdx-tests-jogl/data/data1.png b/tests/gdx-tests-jogl/data/data1.png new file mode 100644 index 000000000..4392e0a1c Binary files /dev/null and b/tests/gdx-tests-jogl/data/data1.png differ diff --git a/tests/gdx-tests-jogl/data/pack b/tests/gdx-tests-jogl/data/pack new file mode 100644 index 000000000..cf40babba --- /dev/null +++ b/tests/gdx-tests-jogl/data/pack @@ -0,0 +1,43 @@ + +data1.png +none +badlogic +0 +0 +256 +256 +0 +0 +256 +256 +0 +particle-fire +256 +0 +127 +122 +0 +0 +128 +128 +0 +particle-star +383 +0 +63 +63 +0 +0 +64 +64 +0 +badlogicsmall +383 +63 +32 +32 +0 +0 +32 +32 +0 diff --git a/tests/gdx-tests-lwjgl/data/data1.png b/tests/gdx-tests-lwjgl/data/data1.png new file mode 100644 index 000000000..4392e0a1c Binary files /dev/null and b/tests/gdx-tests-lwjgl/data/data1.png differ diff --git a/tests/gdx-tests-lwjgl/data/pack b/tests/gdx-tests-lwjgl/data/pack new file mode 100644 index 000000000..cf40babba --- /dev/null +++ b/tests/gdx-tests-lwjgl/data/pack @@ -0,0 +1,43 @@ + +data1.png +none +badlogic +0 +0 +256 +256 +0 +0 +256 +256 +0 +particle-fire +256 +0 +127 +122 +0 +0 +128 +128 +0 +particle-star +383 +0 +63 +63 +0 +0 +64 +64 +0 +badlogicsmall +383 +63 +32 +32 +0 +0 +32 +32 +0 diff --git a/tests/gdx-tests-lwjgl/src/com/badlogic/gdx/tests/lwjgl/LwjglTestStarter.java b/tests/gdx-tests-lwjgl/src/com/badlogic/gdx/tests/lwjgl/LwjglTestStarter.java index a37095175..c1becefb6 100644 --- a/tests/gdx-tests-lwjgl/src/com/badlogic/gdx/tests/lwjgl/LwjglTestStarter.java +++ b/tests/gdx-tests-lwjgl/src/com/badlogic/gdx/tests/lwjgl/LwjglTestStarter.java @@ -43,12 +43,15 @@ public class LwjglTestStarter { @Override public void actionPerformed (ActionEvent e) { String testName = (String)list.getSelectedValue(); GdxTest test = GdxTests.newTest(testName); - new LwjglApplication(test,testName, 480, 320, test.needsGL20()); + new LwjglApplication(test, testName, 480, 320, test.needsGL20()); } }); add(pane, BorderLayout.CENTER); add(button, BorderLayout.SOUTH); + + // GdxTest test = GdxTests.newTest("SpriteSheetTest"); + // new LwjglApplication(test, "Test", 480, 320, test.needsGL20()); } } diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/SpriteSheetTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/SpriteSheetTest.java new file mode 100644 index 000000000..d939da9b0 --- /dev/null +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/SpriteSheetTest.java @@ -0,0 +1,56 @@ + +package com.badlogic.gdx.tests; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.GL10; +import com.badlogic.gdx.graphics.SpriteSheet; +import com.badlogic.gdx.graphics.Sprite; +import com.badlogic.gdx.graphics.SpriteBatch; +import com.badlogic.gdx.tests.utils.GdxTest; + +public class SpriteSheetTest extends GdxTest { + SpriteBatch batch; + Sprite badlogic, badlogicSmall, star; + SpriteSheet spriteSheet; + + public void create () { + batch = new SpriteBatch(); + + spriteSheet = new SpriteSheet(Gdx.files.internal("data/pack"), Gdx.files.internal("data")); + badlogic = spriteSheet.get("badlogic"); + badlogicSmall = spriteSheet.get("badlogicsmall"); + star = spriteSheet.get("particle-star"); + + badlogic.setPosition(50, 50); + badlogicSmall.setPosition(10, 10); + star.setPosition(10, 70); + + Gdx.gl.glClearColor(0, 1, 0, 1); + } + + public void render () { + Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); + batch.begin(); + badlogicSmall.draw(batch); + badlogic.draw(batch); + star.draw(batch); + batch.end(); + } + + public void resize (int width, int height) { + } + + public void pause () { + } + + public void resume () { + } + + public void dispose () { + spriteSheet.dispose(); + } + + public boolean needsGL20 () { + return false; + } +} diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/utils/GdxTests.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/utils/GdxTests.java index 77871a37d..85f7e438a 100644 --- a/tests/gdx-tests/src/com/badlogic/gdx/tests/utils/GdxTests.java +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/utils/GdxTests.java @@ -29,6 +29,7 @@ import com.badlogic.gdx.tests.Mpg123Test; import com.badlogic.gdx.tests.MultitouchTest; import com.badlogic.gdx.tests.MyFirstTriangle; import com.badlogic.gdx.tests.ObjTest; +import com.badlogic.gdx.tests.SpriteSheetTest; import com.badlogic.gdx.tests.ParticleEmitterTest; import com.badlogic.gdx.tests.PixelsPerInchTest; import com.badlogic.gdx.tests.Pong; @@ -87,6 +88,7 @@ public class GdxTests MultitouchTest.class, MyFirstTriangle.class, ObjTest.class, + SpriteSheetTest.class, ParticleEmitterTest.class, PixelsPerInchTest.class, Pong.class,