Bufferstrategy.contentsLost proper handling?

http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=share;action=display;num=1036791657

If bufferstragey.contentsLost return true, then only way I could resolve it was to recreate a new bufferstrategy instances. See full example posted at the end of given url.

Is this safe to do in case of contentsLost event?


private void render() {
    // buffer lost, recreate
    if( bufferStrategy.contentsLost() ) {
  // if I comment createBuff.. and getBuffer methods
  // then it never recover the contentsLost state.
  mainFrame.createBufferStrategy( 2 ); // or use 3
  bufferStrategy = mainFrame.getBufferStrategy();
  return;
    }
 
    Graphics g = bufferStrategy.getDrawGraphics();
 ...continue normal renderer
}

This looks correct.

I don’t think you need to recreate the entire BufferStrategy. Just make sure the contents weren’t lost before calling the BufferStrategy’s show() method.


public void render(){
    Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
    //paint some stuff
    //   ...
    //done painting
    g.dispose();
    if(!contentsLost()){
        strategy.show();
    }
}