Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  BufferedStrategy double buffering  (Read 4228 times)
0 Members and 1 Guest are viewing this topic.
Offline Captain-Goatse

Junior Member




I suck at teh 2D. XBOX IS BIG LOL!111


« Posted 2002-10-20 07:10:03 »

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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
 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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
      
      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.
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #1 - Posted 2002-10-20 07:58:19 »

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.
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #2 - Posted 2002-10-20 08:32:10 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Captain-Goatse

Junior Member




I suck at teh 2D. XBOX IS BIG LOL!111


« Reply #3 - Posted 2002-10-20 09:22:51 »

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.
Offline Captain-Goatse

Junior Member




I suck at teh 2D. XBOX IS BIG LOL!111


« Reply #4 - Posted 2002-10-20 16:54:18 »

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.
Offline Captain-Goatse

Junior Member




I suck at teh 2D. XBOX IS BIG LOL!111


« Reply #5 - Posted 2002-10-20 17:19:49 »

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

JGO Coder




Got any cats?


« Reply #6 - Posted 2002-10-22 19:32:50 »

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.)


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Captain-Goatse

Junior Member




I suck at teh 2D. XBOX IS BIG LOL!111


« Reply #7 - Posted 2002-10-23 17:38:54 »

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!
Offline Captain-Goatse

Junior Member




I suck at teh 2D. XBOX IS BIG LOL!111


« Reply #8 - Posted 2002-10-24 12:36:42 »

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.)




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.
Offline Jeff

JGO Coder




Got any cats?


« Reply #9 - Posted 2002-10-24 19:59:12 »

Oh good. I'm glad it helped  Grin

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.  Smiley

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (50 views)
2014-04-15 18:08:23

BurntPizza (46 views)
2014-04-15 03:46:01

UprightPath (62 views)
2014-04-14 17:39:50

UprightPath (44 views)
2014-04-14 17:35:47

Porlus (61 views)
2014-04-14 15:48:38

tom_mai78101 (85 views)
2014-04-10 04:04:31

BurntPizza (143 views)
2014-04-08 23:06:04

tom_mai78101 (242 views)
2014-04-05 13:34:39

trollwarrior1 (201 views)
2014-04-04 12:06:45

CJLetsGame (208 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!