Skip to content

Commit

Permalink
Improve retrieval of empty sprites by preventing linear lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
steffen-wilke committed Dec 15, 2021
1 parent 634ba28 commit bd81b76
Showing 1 changed file with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
public final class Spritesheet implements Comparable<Spritesheet> {
private static final Logger log = Logger.getLogger(Spritesheet.class.getName());

private final List<Integer> emptySprites = new CopyOnWriteArrayList<>();

private final BufferedImage image;
private final String name;
private final ImageFormat imageFormat;

private BufferedImage[] sprites;
private boolean[] emptySprites;
private int columns;
private int rows;
private int spriteHeight;
Expand Down Expand Up @@ -52,14 +51,15 @@ public Spritesheet(
this.imageFormat = ImageFormat.get(FileUtilities.getExtension(path));

this.updateRowsAndCols();
this.emptySprites = new boolean[this.getTotalNumberOfSprites()];
this.sprites = new BufferedImage[this.getTotalNumberOfSprites()];

Resources.spritesheets().add(this.name, this);

Resources.images()
.addClearedListener(
() -> {
this.emptySprites.clear();
this.emptySprites = new boolean[this.getTotalNumberOfSprites()];
this.sprites = new BufferedImage[this.getTotalNumberOfSprites()];
});
}
Expand Down Expand Up @@ -130,7 +130,7 @@ public BufferedImage getSprite(final int index) {
}

public BufferedImage getSprite(final int index, final int margin, final int spacing) {
if (this.emptySprites.contains(index) || this.sprites.length == 0) {
if (index < 0 || index >= this.sprites.length || this.emptySprites[index] || this.sprites.length == 0) {
return null;
}

Expand All @@ -148,7 +148,7 @@ public BufferedImage getSprite(final int index, final int margin, final int spac
final BufferedImage sprite =
this.getImage().getSubimage(position.x, position.y, this.spriteWidth, this.spriteHeight);
if (Imaging.isEmpty(sprite)) {
emptySprites.add(index);
emptySprites[index] = true;
return null;
}

Expand Down

0 comments on commit bd81b76

Please sign in to comment.