How to have BufferedImages take up less RAM

A BufferedImage is basically a thin wrapper over an array, usually an int[]. Doesn’t really gain you anything, except the need to write a software renderer (which can be fun mind you, and faster than the software Java2D loops in some cases)

It wouldn’t probably affect the ram, but it would reduce the use of BI. Usually I use this method for small games in pure java which is very efficient. Now, he has 1000+ image objects, I don’t see how he can reduce the ram being used. Unless he creates a way to draw compressed images, which would be interesting

It seems that it has more to do with OpenGL textures based on the fourth bullet point of general comments here: https://community.oracle.com/docs/DOC-983015

I am using the JVM argument -Dsun.java2d.opengl=true. Does this mean that this affects me?

You’re brave! ;D That’s a very old document. There’s definitely code in the JDK source for GL to support non-power-of-two textures, it’s even mentioned in that document. I assume it’s working?! :-\

Never mind about the post I just deleted. The content was irrelevant.

It has been a while, but I had another question: How large should the maximum size of my spritesheets be if any limit should be imposed? (i.e. 2048 x 2048, 4096 x 4096, etc.)

These days 4096x4096 is generally supported in hardware but I notice that performance seems to drop off a cliff with Intel integrated GPUs at this size.

Cas :slight_smile:

Since you say that performance ‘drops off a cliff’ with Intel integrated GPUs at 4096 x 4096, would 2048 x 2048 be better?

Er, yes :slight_smile: Also my information is maybe a few years out of date - try it and see.

Cas :slight_smile:

Sounds good! I’ll see how it goes.

have you tried storing buffered images temporarily?
because a spritesheet is ONE image but it will be cropped into multiple buffered images right?

if thats the case try using something similar to this method to load a image.


public static Image getSprite(int id) {
    BufferedImage tempSprite; //isn't stored constantly?

    try {
        tempSprite = ImageIO.read(new File("spriteFolder/" + id + ".png);
        
    }
    catch(IOException e) {

    }

   return tempSprite;

}

i’ve used this way of loading images with the ‘drawImage’ method and it increased my performance alot. (still not sure if its the BEST way though)


drawImage(getSprite(2), 0, 0, 50, 50, null); 

FWIW.

There is such a wide range of screen resolutions in use, it’s not practical to have appropriate-size
images for everything. I use a process which starts with an oversize image, and dynamically resizes
down to an appropriate size using smooth scaling. I could (but don’t) also discard the original oversize
image. Two things are important to make this work; the oversize images shouldn’t have hard edges so
the scaled versions will look decent, and the UI code needs to use an container class instead of raw
images to hide all the machinery.