Basic questions on Buffer Strategy, please help.

A few questions about using buffer strategy. The answers are probably really obvious if you’re used to it, but I’m only learning so give me a chance.

For example:


// Render loop
while (!done) {
    Graphics g = strategy.getDrawGraphics();    
    // Draw to graphics  
  ...    strategy.show();
}

  1. What exactly is happening here? Is the screen being drawn to constantly, as fast as the computer can handle, as opposed to
    {What I’ve been doing since I’ve started using Java)

public void run(){
  while (true){  
    updateProgram();
    repaint();
    try{ 
     Thread.sleep(100);
    }catch(InterruptedException e){}
  }
}

  1. Where do you place the updateProgram() method when using buffer strategy, in the render loorp?
  2. And when will done ever be false in the rendering loop? Is the rendering loop supposed to be implemented in a seperate thread?
  3. One more thing, I take it that the drawing is being done direct to the JFrame as opposed to using JPanels?

Hope someone can help me out, I want to start putting some games in full screen mode and getting smoother animation.
Thanks.

New Code: I think this block may help your confusion

public void run(){
while (true){
updateProgram();
Graphics g = strategy.getDrawGraphics();
// Draw to graphics
// you have code already in your paint method so take it out
// and create another method that takes a Graphics g
// something like
paintToBuffer(g);
strategy.show();
try{
Thread.sleep(100);
}catch(InterruptedException e){}
}
}

I haven’t actually used a Bufferstrategy yet, but from what I’ve read, this is probably what you want to do… for a simple render loop.

Later.

Is using the BufferStrategy for double buffering any differnet from drowing to an ‘offScreen’ volatile image, them painting it? ie the pre 1.4 way of doublebuffering?

M

Yes because BufferStrategy can use the Vsync of your monitor to prevent the tearing effect.

[quote]Yes because BufferStrategy can use the Vsync of your monitor to prevent the tearing effect.
[/quote]
When operating in fullscreen exclusive mode.

So BufferStrategy is VSync locked automatically? I looked through the entire JavaDoc and couldn’t find anything about VSync, so I implemented my own. :-/

[quote]So BufferStrategy is VSync locked automatically? I looked through the entire JavaDoc and couldn’t find anything about VSync, so I implemented my own. :-/
[/quote]
how’d u do it yourself? native I’m guessing?

Nice one.
Thanks hymatron.

If you have downloaded the SDK (not J2RE) you can actually look inside the buffer classes if you want. The buffer are implemented using voilatile (sp?) images and are blitted to the screen (windowed mode) or flipped (fullscreen mode).

A pre 1.4 way of doing double buffering would be using a BufferedImage. Note that Swing components should be double buffered by default (check by calling isDoubleBuffered). I don’t know if implementing it yourself would be faster since I haven’t looked up how it’s implemented.

  • Johan

Ummm…well, I didn’t say I implemented it properly :slight_smile:

I actually just used a timer and made sure to only render after the vsync refresh. The entire code was in place and all I needed was a timer with 1 ms resolution. So I say I implemented it, but really it wasn’t fully since I didn’t have an appropriate timer.

BTW, I know about the GAGETimer. I just haven’t gotten around to doing animation yet so I haven’t bothered with it.