BufferedStrategy double buffering

I thought I understood how it works in BufferedStrategy, but how wrong was I.

Basically I’m passing the strategy from the main class to the engineManager, which handles drawing. Everything is fine and it draws the actual Images except that it flickers horribly.

This is my main and how I form the buffered image


 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice device = env.getDefaultScreenDevice();
        
        gameFrame game = new gameFrame();
        
        screenManager gameScreen = new screenManager(device, game);

        
        
          engineManager engine = new engineManager(game);
        engine.start(); 
        
        

        
        

        
        
        
        KeyListener keyInput = new inputManager();
        MouseListener mouseListener = new inputManager();
        MouseMotionListener mouseTracker = new inputManager();

        
        
            game.createBufferStrategy(2);
            game.addKeyListener(keyInput);
            game.addMouseMotionListener(mouseTracker);
            game.addMouseListener(mouseListener);
            game.setIgnoreRepaint(true); 

Then at the engine manager I do this


      
      Graphics2D g;
      
      public engineManager(Frame f){
            bufferStrategy = f.getBufferStrategy();
        g = (Graphics2D)bufferStrategy.getDrawGraphics();
        
            
      }
      
      
      public void render(Graphics2D g){
      

            Rectangle2D.Double rect = new Rectangle2D.Double(352F, 252F, 48F, 48F);
            
            g.setColor(Color.white);
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);


            
                  AffineTransform t = new AffineTransform();
            //t.rotate(java.lang.Math.PI/180*x);
            t.rotate(inputManager.rotateAngle, 376, 276);
            g.setTransform(t);
            g.clearRect(0,0,800,600);
            g.fill(rect);
            
            bufferStrategy.show();
            
            
      }


Nothing else here is important except the BufferStrategy.

How am I suppesed to work the Double Buffering? And what about the chance of getting memory leakage without g.dispose();

If I use g.dispose(); There won’t be any prettay pictures on mah screen.

The problem is that your render() method uses one of the operations which require reading from the back-buffer (which is in vram): antialiasing.

So here’s what happens: we detect that you’re reading from the back-buffer on each frame, and since reading from vram (thta’s where the back-buffer is) is a very slow operation, we punt the back-buffer to system memory. At this point we can’t do hardware buffer flip, and have to use a copy (blit) to put the back-buffer to the screen instead, that’s why you can see the flashing.

Currently there are two ways to fix this:

  • don’t use Java2D operations which require reading from the destination (such as antialiasing, compositing)
  • run your application with the following parameter:
    java -Dsun.java2d.ddforcevram=true YourApp
    This property turns off our punting mechanism and forces all accelerated images to stay in vram. You’ll see a confirmation message.

The real solution, of course, would be to accelerate all those operations through hardware so we wouldn’t need to use our sofrware routines, which require reading from the destination. We’re working on this solution for one of the next releases.

Hope this helps.

Also, you might want to take a look at this paper:

http://java.sun.com/products/java-media/2D/perf_graphics.html

where we describe this kind of issues.

This doesn’t help at all…The flickering is still horrbile although I use the vram mode, I didn’t see a confirmation message though, but it ran fine with the command, except the flickering.

I tried taking off the antialiasing, and it reduced the flickering a lot.

Flickering seems to be indirectly porpotial to the speed of the thread. If I run it with thread.sleep = 500 then it can be hardly seen. But with speed of 0-100 its god awful.

Something is significantly wrong. It seems to be that I have no db at all.

ok… the problem disappeared when I stopped using clearRect(int,int,int,int)

I wonder how on earth am I’m supposed to handle the screen now. I’m using getClip(rect) but this doesn’t clear the background, in fact there is no use for it.

Does anyone have any ideas how to progress from this on? How should I architecture my structure so that I can avoid clearRect.

Any kind of clip seems to cause the flickering. Does anyone know what causes this?

I think I see the problem.

Where do you flip frames? I don’t see any code for stepping through
the buffer chain here.

Unelss Im missing something all you are doing is repeatingly drawing to the on screen image. This will flicker because all of your drawing is exposed.

You can loo kat the manin loop of Scroller to see what you should be doing
(The main is in a file called Test.java.)

Oh, shoot! I forgot. I was doing the universal animation loop from the black art of java game programming. This is quite new to me since I have only programmed games with very little animations, such as turn based rpgs, but anyhow, thaks a lot. How can I get this scroller demo exactly?

Thanks!

[quote]I think I see the problem.

Where do you flip frames? I don’t see any code for stepping through
the buffer chain here.

Unelss Im missing something all you are doing is repeatingly drawing to the on screen image. This will flicker because all of your drawing is exposed.

You can loo kat the manin loop of Scroller to see what you should be doing
(The main is in a file called Test.java.)
[/quote]
I got it working last night, but I didn’t quite grasp the idea of the flipping thingie until I looked at scroller code at the code forum.

Thank you sir.

Oh good. I’m glad it helped ;D

It’s one of the most commonly asked questions around here in general so don’t feel bad about it taking a bit to sink in. We used to have a link to Mike’s article on FullScreen that discussed this (and to my Scroller program) but since we won’t have that back til we have a full site again, I figured the appropriate excerpt might be useful. :slight_smile: