fast scroller question

anyone have made any efficient smooth background scroller?
i have tried three method to make a 1440 x 1216 background that could scrolled
constructed with many 32x32 tiles

first, the easiest one, i make a big image 1440 x 1216 large, put all tiles in initialize
then scroll it with g.drawImage(image, -x, -y, null);
takes 40% cpu usage with 46,850K memory consumptions

second (i used now) i create a 640 x 480 image (as large as the resolution)
and each frame, i scan and put the tiles into the images
imageGraphics.drawImage(tiles[pos], x, y, null);
then i put the image into screen g.drawImage(image, 0, 0, null);
takes 70% cpu usage with 34,680K memory consumptions

and the last, i create a 640 x 480 image and then fill it with
grass tile (the basic tile) in initialize and put the image
g.drawImage(image, 0, 0, null);
then scan through all the tiles whereas the tile different from the grass i put it with
g.drawImage(differentTiles, x, y, null);
not implemented yet

the first takes too large memory
(every changing the background it takes about 10megs memory consumptions increment)
the second takes too many cpu time, make the fps drops.
(cos with 640 x 480 large cause the tiles drawn 640/32 = 20 for horizontal scanning and
480/20 = 15 for vertical and the result 300 tiles must be drawn every frame)
the last one is the best one, but i haven’t got the point :frowning:
only drawing grass tiles takes 40% cpu usage with 37,932K memory consumptions
all method i observe in windows xp with windowed mode,
using volatile image (passive rendering), and
the result same as using buffer strategy (active rendering)
and the image is png files using BufferedImage

so what’s the best way to do this??

thanxx in advance ;D

Go to http://java.dnsalias.com and download the GAGE2D package. You’ll find an example scroller in there. Also, Planetation by ZParticle does high speed scrolling and was the inspiration for GAGE. Here’ s the URL for that:

http://www.scottshaver2000.com/template/template.php?page=planetation_main

Your second idea isn’t as bad as it sounds. You can make it faster if you scroll your offscreen image with copyArea() and then only draw the tiles at the edges. In worst case that’s only 35 tiles /frame.

[quote]Your second idea isn’t as bad as it sounds. You can make it faster if you scroll your offscreen image with copyArea() and then only draw the tiles at the edges. In worst case that’s only 35 tiles /frame.
[/quote]
thats assuming you never scroll faster than cellWidth/celHeight in a single frame.
Otherwise, its gonna be more.

is the original poster using hardware accelerated images?
cos if he is, there is something wrong with his code.

on any decent pc, using hardware accelerated images and back buffer, you can draw atleast 10000 images @ 30fps.

yup i have take a look at gage2d works
and i think mine is using similar technic like yours
but with different implementation :stuck_out_tongue:
i use only one map for the background
but i’ll take a look closer, cos i haven’t optimize my code

umm how to use copyArea()? any simple code plz?

i don’t know is my images are accelerated or not
how to know that?
i use BufferedImage for all images and also my background back buffer
and use VolatileImage for offscreen image
what do you think should i use for back buffer? is it right using BufferedImage or should i change it to VolatileImage, what’s the difference?
and one thing i can’t use BufferStrategy, so i use offscreen buffer
my images is png files (24 bit color) and i create the image like this :


      public static final Image getBufferedImage(Image image) {
            BufferedImage bufferedImage = gc.createCompatibleImage(
                  image.getWidth(null), image.getHeight(null), Transparency.OPAQUE);
            Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
            g.drawImage(image, 0, 0, null, null);
            g.dispose();

            image = null;

            return bufferedImage;
      }

your image creation looks fine, images created that way are elligable for acceleration.

Ideally you should use BufferStrategy for managing your back buffer, Why can’t you use it?

The alternative to BufferStrategy is to use a VolatileImage as the back buffer.

There are however other complications.
If you are performing pixel operations that arn’t hardware accelerated (alpha compositing, affine transforming etc etc) it is infact quicker to keep everything in system memory. (the images, and the back buffer!)

ups i have solved my problem
i’m draw the map into back buffer and then
draw it to screen graphics
g.drawImage(backBuffer,0,0,null);
i forget that the screen graphics is back buffer too
then i directly draw the map into the screen
g.drawImage(tile[i+(j*horiz)], m, n, null);
and it’s fast now :slight_smile: