Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Problems with vertical tearing using Bufferstrategy FSEM  (Read 512 times)
0 Members and 1 Guest are viewing this topic.
Offline thebebinator

Senior Newbie


Medals: 1



« Posted 2013-05-06 06:57:41 »

Hello all, I'm new to the forum, so hello!

I'm relatively new to java gaming programming, especially when it comes to graphics stuff, so apologies if this is a noobish question.

Right now I have a simple program in Full Screen Exclusive Mode that draws a moving rectangle bouncing back and forth horizontally on the screen, but I'm getting extremely bad vertical tear along the left and right sides of the rectangle. I read in multiple places that using a bufferstrategy would automatically handle this problem as long as the capabilities reported flipping as true.When I report the buffer capabilities, it tells me flipping is true, and setting the strategy to PRIOR during creation doesn't give me any errors or anything, but tearing continues. I have posted code where I create the bufferstrategy, and then my drawing method , which is called from a simple game loop that updates the game state then draws the screen. Can you think of what might be causing this tearing issue?  Huh

EDIT: forgot to add that the main class is extended from Frame (awt). I also tried with JFrame, same problem.

I'm on a brand new gaming hackintosh (on mountain lion), so I'm thinking it may be a problem with my graphics drivers or something? I have a NVidea GTX680 so I can definitely support flipping... Maybe I'll run my program on my old iMac and see if the same problem persists while I await replies.

Thanks so much in advance for your help,
Adam

Here is my bufferstrategy creation

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
      //get graphics device
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
      GraphicsDevice gd = ge.getDefaultScreenDevice();
     
      //set parameters for fullscreen window
     this.setUndecorated(true);
      this.setIgnoreRepaint(true);
      this.setVisible(true);
      this.setBackground(Color.gray);
     
      // set the frame to full screen exclusive mode
     gd.setFullScreenWindow(this);
     
      // set up the buffer Strategy
     this.createBufferStrategy(2);
      bs = this.getBufferStrategy();


And here is my flipping part: I call drawScreen() in a while(true) loop for now. I had some FPS limiting code in but took it out to solve this issue.

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  
   public void drawScreen() 
   {
     
      do {// keep repeating rendering if the drawing buffer was lost

         do { //ensures that the contents of the drawing buffer are consistent in case the underlying surface was recreated
           
            // get graphics object to draw on
           Graphics2D g = (Graphics2D) bs.getDrawGraphics();

            //render screen: Here I just and draw the background of the frame and the moving rectangle to g,
           renderScreen(g);

            //dispose the graphics
           g.dispose();
           
         } while (bs.contentsRestored()); //repeat rendering if the drawing buffer contents restored
       
         //flip buffer
        bs.show();

         //sync display, fixes something on Unix
        Toolkit.getDefaultToolkit().sync();
         
      } while(bs.contentsLost()); // repeat rendering if the buffer contents were lost

   }
Offline SHC
« Reply #1 - Posted 2013-05-06 13:47:16 »

Try with this. I just render normally and when contents lost not restored.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public void drawScreen() 
{
     
    do { //ensures that the contents of the drawing buffer are consistent in case the underlying surface was recreated
           
        // get graphics object to draw on
       Graphics2D g = (Graphics2D) bs.getDrawGraphics();

        //render screen: Here I just and draw the background of the frame and the moving rectangle to g,
       renderScreen(g);

        //dispose the graphics
       g.dispose();
        //flip buffer
       bs.show();

        //sync display, fixes something on Unix
       Toolkit.getDefaultToolkit().sync();
         
    } while(bs.contentsLost()); // repeat rendering if the buffer contents were lost

}

Offline thebebinator

Senior Newbie


Medals: 1



« Reply #2 - Posted 2013-05-06 15:08:10 »

Thanks a lot for your reply, but I still seem to be getting tearing after making those changes.

Adam
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline thebebinator

Senior Newbie


Medals: 1



« Reply #3 - Posted 2013-05-06 16:45:04 »

After testing it on my older iMac, and a less old macbook air, the vertical tearing persists, which makes me think I'm doing something wrong.

Here is how I draw the background and moving rectangle

1  
2  
3  
4  
5  
6  
7  
   //paint background
  g.setColor(Color.gray);
   g.fillRect(this.getBounds().x, this.getBounds().y, this.getBounds().width, this.getBounds().height);

        //paint rectangle
  g.setColor(Color.BLUE);
   g.fillRect( (int)Math.round(position.x-size), (int)Math.round(position.y-size), size*2, size*2);


Also not sure if it matters, but I am using a dell 27 inch high res monitor. I've tried switching the display size to a smaller resolution, but the tearing gets more noticeable.
Offline Fsig

Senior Newbie





« Reply #4 - Posted 2013-05-09 05:42:49 »

Hi there.

I also encounted this error when I first started playing around with the Bufferstrategy.

For me the problem was not with the buffer or rendering methods at all but with how I handled my input.

For example I had a KeyListener on my canvas which I would use to update map movement etc...
To fix it, in the KeyListener I just set a boolean value of up,down,left,right equals true then in my update method I would check if up,down,left,right equals true and if it did I would process my movement.

By doing this you update all your map,units etc before rendering so there is no tearing.
Offline thebebinator

Senior Newbie


Medals: 1



« Reply #5 - Posted 2013-05-10 19:41:52 »

Thanks so much for your reply. In attempting to correct this issue, I've actually turned off pretty much everything, it's just a simple rectangle bouncing back and forth across the screen with a set speed, and position. My loop is just updating the position, then drawing the screen, with nothing else at all really. So I'm not accessing any input or anything. Any idea what else might be causing it?

Thanks so much for the help

Adam


Hi there.

I also encounted this error when I first started playing around with the Bufferstrategy.

For me the problem was not with the buffer or rendering methods at all but with how I handled my input.

For example I had a KeyListener on my canvas which I would use to update map movement etc...
To fix it, in the KeyListener I just set a boolean value of up,down,left,right equals true then in my update method I would check if up,down,left,right equals true and if it did I would process my movement.

By doing this you update all your map,units etc before rendering so there is no tearing.
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.

BurntPizza (22 views)
2014-09-19 03:14:18

Dwinin (35 views)
2014-09-12 09:08:26

Norakomi (64 views)
2014-09-10 13:57:51

TehJavaDev (90 views)
2014-09-10 06:39:09

Tekkerue (44 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (48 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (37 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!