Image creation/scaling questions

I recently started making simple games using Java2D, and have come up with a couple of questions about creating/handling Images. There’s a lot of good advice about this topic in these forums, but it seems to be spread out and not in one place (and a lot of it is relatively old - what’s accelerated and what’s not seems to change from one JRE release to the next).

  1. If your game only ever runs in a window (no FSEM), and uses a Canvas to render on, is there any difference between:

GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(w, h)

and

canvas.getGraphicsConfiguration().createCompatibleImage(w, h)

I’d naively imagine these values would be the same. And are things any different in FSEM?

  1. In games with lots of sprites, I find myself loading a single image that I use as a “sprite sheet” containing all frames of animation for a sprite(s). The actual sprite instances themselves will contain an (x,y) coordinate into the sprite sheet for each frame, and to render they call one of the Graphics.drawImage() overloads that take a subrectangle into the image to draw. The question is: how does this affect performance, if at all? Is it really worth it to break a “sprite sheet” apart into several smaller images, and draw the proper one at the right time? It certainly seems simpler not to break it apart if possible.

reply to 1: both are equality if your Canvas is instancied with the same GraphicsConfiguration as the LocalEnvironment. Where the GraphicsConfiguration can bring out some deeper features like double- or triple-buffering, such hardware capabilities and other.

I was loading a huge image that contained our tile art, and using the image as a whole. I found this used a lot more cpu and on my friends computer the tile art totally messed up. But when I loaded the image and sliced it into a array of 32x32 tiles everything worked well.
It might of been because the image was too large, I dono. Anyway hope that helps.

  1. what broumrboum said

  2. that depends on how large is your sprite sheet and whether hw
    acceleration is enabled or not.

If hw acceleration isn’t enabled, it doesn’t matter - there’s no performance difference
when copying the whole image vs region of the image.

If hw acceleration is enabled, it depends: some hardware has restrictions
on max. dimensions of textures (which is where the sprite sheet is cached).
If it doesn’t fit, it may be unaccelerated. Some hw doesn’t support non power of
two, or non-square textures, which means that we’d have to allocate more video
memory than necessary (for 257x257 texture we’d have to allocate a
512x512 texture).

Depending on how your application does rendering,
it may be better to have all your sprites in one image because
if it is uploaded into a texture, then when rendering one sprite after
another we will not have to change the current texture, which is typically
a plus.

So if you do decided to keep them in sprite sheets, I’d suggest to
keep them square, pow of two size, not more than 512x512 - that’d
cover most of the hardware.

Thanks,
Dmitri

Thanks Dmitri and everyone else who responded!