Problems with vertical tearing using Bufferstrategy FSEM

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? ???

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


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

	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

	}

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


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

}

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

Adam

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



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

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.

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