Search code examples
javaframe-rate

How do prevent my FPS dropping when loading spritesheet in java?


Hey guys i have been java game programming fr a while and i hava had a problem lately. When i load a relatively small sprite sheet in java my FPS goes from 500 to 250 and makes my game pretty laggy. If anyone knew a way to make a java game less laggy and get better FPS, that would be great!

    BufferedImage spriteSheet = ImageIO.read(new File(spriteSheetLocation));
    sprites = new BufferedImage[rows * columns];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            sprites[(i * columns) + j] = spriteSheet.getSubimage(i * width,
                    j * height, width, height);
        }
    }

Solution

  • Normally when developing games, you cache all your sprites (or almost) so you only load them once at the beginning.

    Back in the days when I was doing game development, I was using cache editor (it is easy to create your own, but there is probably plenty on web if you want to save time) then you store each of your sprites in a file as bytes (serialized objects). Then this file is compressed by the cache editor and each players would download it once and store somewhere on the computer (they have no idea it is done, it is handled by the game).

    Then, when players had to load a new sprites it was already cached on the computer and was very faster.