BufferStrategy and flickering

I searched the forums and found nothing.

I did double buffering manually the old way so I decided to incorporate BufferStrategy in my game.

All I changed in my rendering loop is that I use strategy.getDrawGraphics() to get a Graphics object to draw on and then strategy.show() to flip.

However, this results in heavy flickering. It seams like it draws graphics only on every other Graphocs object.

Is this a commong problem meaning I’ve missed something fundamental?

Basically I use this approach: First I have some menus and stuff (I use Swing for buttons here) and then when playing I use no Swing stuff at all. I pass the Graphics object along to my dufferent objects to draw on (track, cars, etc).

Any idea? Is it the Swing stuff that messes everything up or is something else wrong? I can describe my rendering loop in detail if needed.

well, to start, how many buffers are you using? createBufferStrategy(1) will still flicker

[quote]well, to start, how many buffers are you using? createBufferStrategy(1) will still flicker
[/quote]
In my “adventures” with BufferStrategy and it’s ilk, I’ve read through the code for both Component implementations of BufferStrategy and at least FlipBufferStrategy won’t let you use less than 2. I’m not sure about Blit… I guess he can’t be using 1 buffer if he’s using flipping.

Maybe, if he’s using active rendering, his paint method is still being called and that could have something to do with it. Probably not.

We need more info, is what.

I found the second constructor so now things are working somewhat better. It’s still not perfect – some stuff do not get draw initially – it just behaves oddly.

before I used createBufferStrategy(2) but now I use this instead:
BufferCapabilities cap = new BufferCapabilities(new ImageCapabilities(true), new ImageCapabilities(true), BufferCapabilities.FlipContents.COPIED);
createBufferStrategy(2, cap);

anyway, have anyone of you tried to draw Swing components (eg JButtons) and used BufferedStrategy at the same time?

about passive and active rendering… I’ve set setIgnoreRepaint(true) and I’m calling my draw method from my game loop manually.

sigh,
if a method doesn’t appear to be working as expected,
don’t just try to work around it,
find out why its not working!
it’ll only cause you problems later on otherwise.

createBufferStrategy(2) should work just fine.
(java will use the optimum strategy for your current display mode.)

it sounds to me like u are assuming the contents of the buffer after a page flip is in a known state.
By default (when using createBufferStrategy(2)) the buffer contents after a page flip are Undefined.
(which would result in your ‘flicker’)

post your main class, it won’t be hard to find the problem.

umm I guess my rendering loops should be enough

first I create the BS in the constructor:


BufferCapabilities cap = new BufferCapabilities(new ImageCapabilities(true), new ImageCapabilities(true), BufferCapabilities.FlipContents.COPIED);
try {
parent.createBufferStrategy(2, cap);
}
catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
strategy = parent.getBufferStrategy();
dBuff = strategy.getDrawGraphics();

and my game loop then looks like this:


while (running) {
            calcDeltaTime();
            

            switch (gameState) {
                case RACING :
                    raceManager.performGameTick(dt);
                    render();
                    if (raceManager.raceOver()) {
                        upgradeScreen.addComponents(this);
                        gradeScreen.paintdBuff); 
                        gameState = UPGRADESCREEN;
                        setDefaultCursor();
                    }
                    break;
            }

            try {
                synchronized (timer) {
                    timer.wait();
                }
            }
            catch (InterruptedException iE) {}
        }


public void render() {
        if (!strategy.contentsLost()) {
            dBuff = strategy.getDrawGraphics();

            //panelGfx = g;

            switch (gameState) {
       
                case RACING :
                    raceManager.updateGfx(dBuff);
                    break;
            }

            try {
                strategy.show();
            }
            catch (IllegalStateException e) {
                return;
            }
            
            dBuff.dispose();

            calcFPS();
        }


(btw, how to get the tabs/indentions to display properly?)