File Management - How to do it right

Hi guys,

I made my own Sprite class, containing an ArrayList of subimages and a String “name”.
A class called FileManager creates all sprites and loads the images for the sprites like this:


Sprite blocks = new Sprite(null, "blocks");
            blocks.frames.add(ImageIO.read(new File(imgD + "green_block.png")));
            blocks.frames.add(ImageIO.read(new File(imgD + "red_block.png")));
            blocks.frames.add(ImageIO.read(new File(imgD + "blue_block.png")));
            blocks.frames.add(ImageIO.read(new File(imgD + "red_block.png")));
            sprites.add(blocks);

I then load each image via fileManager.getSpriteByName(“name”);

But this is a lot of work since I have to load each frame seperately and name each sprite.
Is there any other, more “clever” way to do this? I didn’t come to a conclusion yet :frowning:

I mean you could use a sprite sheet.

What I’ve done before, is make a SpriteSheet class that can load like JSON or something, and in that JSON file is the metadata for a certain SpriteSheet, like the name of the sprites, the location of the sprite sheet image, size of the sheet, e.t.c. But also in that file could be an array of sprites, with their respective metadata, such as width, height, x, y.

That would mean that you would have to pack each individual sprite into a single image file, but that will also in theory make your game perform better due to less texture binding calls (one call per sheet as opposed to a call per sprite).

Also, are you saying that you are reloading each sprite from disk every frame? Because if you do, please don’t.

You could also store your sprites in separate directories and loop through the directory that corresponds to the sprite looking for images, and add them to the sprite that way.

Not that you would really want to do this for sprites, but the technique can be used else where in the future for loading other things, and it’s easier to implement for the system you already have in place.