I’m currently working on a generic sprite surface class for use in future Java games. I’m having difficulty getting a fast scrolling function.
My approach is this: When the sprite surface is scrolled, the parts of the background which have already been drawn and are still visible are redrawn at the opposite edge of the surface, and new tiles which haven’t been drawn yet are then painted. The first part of that is accomplished like this (after calculating offsets, etc.):
Graphics bufferg = bg.getGraphics();
bufferg.drawImage(bg,
0, 0,
(int)getBounds().getWidth(), (int)getBounds().getHeight(),
offset_x, offset_y,
offset_x + (int)getBounds().getWidth(), offset_y + (int)getBounds().getHeight(),
null);
So basically, part of the image is being redrawn over itself. That worked fine on my Mac and resulted in a fairly fast scroll, but when I tested on Windows (any version, latest JVM from Sun), it went completely apeshit. After messing with it, I realized that the Windows version was reading from the same buffer it was writing to, so when I scrolled in a negative direction, the same row/column of tiles would be drawn repeatedly. The Mac apparently used two buffers by default.
The obvious solution was to copy the image and draw from the copy onto the original. I did that using a Toolkit call which I found in some forums (since you can’t just clone an Image or BufferedImage). It worked, but the demo immediately grinded down to ~8 FPS due to the extra copying.
So the question is, what do I do about it? Is there a way to get Windows to quickly copy the image buffer the way the Mac seems to?
If anyone needs any more information, do ask. Thanks in advance for any help!