1

I am making a grid-based game that resizes its grid as the window size changes. I also may apply color filters for lighting effects in the future. I am concerned about the performance of this code, which draws to the screen an image in one of the grid squares.

public void drawSquares(Graphics g){
        ListIterator<Viewport> iterator = vp.listIterator();
        while(iterator.hasNext()){
            Viewport v = (Viewport)iterator.next();
            BufferedImage img = v.getSqView().getImage();
            Rectangle b = v.getPixRect();

            g.drawImage(img, b.x, b.y, b.width, b.height, v.getSqView().getBackground(), null);
        }
        return;
    }

What this code does is get the image (stored in img) and get the pixel rectangle it needs to fit in (stored in b), then draw it in the space alloted via drawImage.

drawImage says that it scales images on the fly - which means that all images are being rescaled every frame. But the window is only resized rarely, so this must waste lots of processor time doing the same thing over and over again.

Now I saw this and decided that I would just update all the images upon resizing once, then store the result and be able to draw normally.

Like this:

public void resizeImage(int width, int height){
         BufferedImage resized = new BufferedImage(width, height, img.getType());
        Graphics2D g = resized.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(img, 0, 0, width, height, 0, 0, img.getWidth(), img.getHeight(), null);
        g.dispose();
        img = resized;
    }

This doesn't work - I think it has something to do with img = resized. I just lose all the images with this code.

I have a few questions then.

  1. What is the performance cost of repeatedly scaling with drawImage? Is it any different even if the window has not been resized in between frames?
  2. How should I get the second code snippet to work? What is going wrong?
  3. If I apply a lighting filter to a tile, will that eat up tons of processor time as well if I run it each frame? (Think 225 or so small images on a 800x800 or so display)
  4. What is best practice for applying lighting filters? I am planning on overlaying on the whole map a pitch black filter, then exposing the areas around light sources.

Thanks for any help with this!

Eric Thoma
  • 5,905
  • 6
  • 30
  • 40
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jan 19 '13 at 06:29
  • Rather the performing scaling and effects in real time, where possible, cache the result in a temporary `BufferedImage`. The basic idea would be to maintain a chain of effects that would need to be applied when the image needs to change, so you can add/remove and re-apply them as needed. This also allows you to decide when and how the effects can be applied. – MadProgrammer Jan 19 '13 at 08:47

2 Answers2

2

Resize the frame of this Grid to get a subjective feel for the latency. Use the approach shown here to measure the latency. Verify your findings in a profiler.

There's no reason you shouldn't be able to resize the elements of a List<Image> as you propose, but add() the resized instances to a new list as they are created.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

What is the performance cost of repeatedly scaling with drawImage? Is it any different even if the window has not been resized in between frames?

You should always measure, but there is definitely a performance cost here, even if the window is not resized, because as the Javadoc says, there is no caching behind this drawImage method. The cost also depends on the frame rate.

How should I get the second code snippet to work? What is going wrong?

The second code snippet should be OK, I think the problem is somewhere else. Try reproducing the problem in a "small but complete" program, and post another question if you still see the problem.

If I apply a lighting filter to a tile, will that eat up tons of processor time as well if I run it each frame? (Think 225 or so small images on a 800x800 or so display)

You should always measure :)

What is best practice for applying lighting filters? I am planning on overlaying on the whole map a pitch black filter, then exposing the areas around light sources.

You can use an AlphaComposite for this.

lbalazscs
  • 17,474
  • 7
  • 42
  • 50