Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
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  
  Screen tearing with BufferStrategy  (Read 2929 times)
0 Members and 1 Guest are viewing this topic.
Offline Donald_W

Senior Newbie





« Posted 2012-04-04 01:22:33 »

I am using BufferStrategy with 2 buffers (tried with 3 as well) but I cannot rid of screen tearing in my application (no vsync?).
I draw everything on one BufferedImage and then draw it on the graphics received from BufferStrategy.

Any ideas how to solve this problem?

This is my render method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
    public void render() {
        do {
            do {
                Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
                g.setColor(Color.BLACK);
                g.fillRect(0, 0, getWidth(), getHeight());
                g.clipRect(0, 0, getWidth(), getHeight());
                g.scale(scale, scale);
                g.drawImage(screen.getImage(), 0, 0, screen.getWidth(), screen.getHeight(), null);
                g.dispose();
            } while (bufferStrategy.contentsRestored());
        } while (bufferStrategy.contentsLost());
        bufferStrategy.show();
    }
Offline ra4king

JGO Kernel


Medals: 341
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-04-04 01:40:46 »

What Operating System are you using?

Offline ReBirth
« Reply #2 - Posted 2012-04-04 01:46:02 »

Seems you do triple buffering here Cheesy

try remove the do..while pair.

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

JGO Kernel


Medals: 341
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-04-04 01:49:50 »

No the do-while pair is necessary. I also just noticed you have the show() call in the wrong place. It's supposed to be right after the inner do-while loop ends, not after the outer one.

Offline ReBirth
« Reply #4 - Posted 2012-04-04 01:54:39 »

Really? I never used that before.

Offline ra4king

JGO Kernel


Medals: 341
Projects: 2
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-04-04 03:07:39 »

Really? I never used that before.
I have been telling people to do this since the beginning of time >.>

Offline Donald_W

Senior Newbie





« Reply #6 - Posted 2012-04-04 09:30:07 »

@ra4king: I have tearing on both Windows and Linux. Whole application is in JFrame - maybe that is an issue? Should I swap to applet?
Offline ra4king

JGO Kernel


Medals: 341
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2012-04-04 09:46:23 »

You shouldn't be seeing tearing in either Vista, 7, or 8. I don't know about Linux.

Can we see the code/JAR to run it and see for ourselves?

Offline Donald_W

Senior Newbie





« Reply #8 - Posted 2012-04-04 10:14:53 »

Here is the jar: http://dl.dropbox.com/u/1146144/reckless-skirmishers.jar

Just go to the border of the pavement floor (go to the right) and move left-right repeatedly.
Game updates every 2nd frame but with 60 FPS - 60 updates I get tearing as well so I guess it is not a problem.
 
BTW: Graphics are for test purposes (taken from Catacomb Snatch).
Offline ra4king

JGO Kernel


Medals: 341
Projects: 2
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2012-04-04 10:57:25 »

No screen tearing here. It runs fine, but at 59 FPS? Something is wrong with your sleeping logic.

Are you using Windows 7?

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

Senior Newbie





« Reply #10 - Posted 2012-04-04 11:03:09 »

Linux Ubuntu/Xubuntu and Windows 7. Now I am on the machine with Linux, tested it on friend's xubuntu as well (maybe it is a xubuntu issue then).

Game loop implementation:
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  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
public class FixedLengthGameLoop implements GameLoop {
    private static final Logger LOGGER = LoggerFactory.getLogger(FixedLengthGameLoop.class);

    // Limits the update amounts before render
   private static final int MAX_UPDATES_PER_RENDER = Integer.MAX_VALUE;

    private static final double NANOS_PER_SECOND = 1000000000;

    // desired updates rate
   private static final double UPDATES_RATE = 30;

    // time between game updates
   private static final double UPDATES_INTERVAL = NANOS_PER_SECOND / UPDATES_RATE;

    // desired frame rate
   private static final double FRAMES_RATE = 60;

    // time between each render
   private static final double RENDERS_INTERVAL = NANOS_PER_SECOND / FRAMES_RATE;

    private GameContext context;

    public FixedLengthGameLoop(GameContext context) {
        LOGGER.info("Initializing default game loop");
        this.context = context;
    }

    public void run() {
        LOGGER.info("Executing main loop");

        double lastUpdateTime = System.nanoTime();
        double lastRenderTime = System.nanoTime();

        double now;
        int updatesCount;

        int frames = 0;
        int fps = (int) FRAMES_RATE;
        double lastFpsUpdateTime = System.nanoTime();

        while (context.isRunning()) {
            now = System.nanoTime();
            updatesCount = 0;

            while (now - lastUpdateTime > UPDATES_INTERVAL && updatesCount < MAX_UPDATES_PER_RENDER) {
                context.update();
                lastUpdateTime += UPDATES_INTERVAL;
                ++updatesCount;
            }

            if (now - lastRenderTime > RENDERS_INTERVAL) {
                float delta = Math.min(1.0f, (float) ((now - lastUpdateTime) / UPDATES_INTERVAL));
                context.render(delta, fps);
                lastRenderTime = now;
                ++frames;
            }

            if (now - lastFpsUpdateTime > NANOS_PER_SECOND) {
                fps = frames;
                frames = 0;
                lastFpsUpdateTime = now;
            }

            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                // consume
           }
        }
    }
}
Offline Donald_W

Senior Newbie





« Reply #11 - Posted 2012-04-04 14:45:54 »

Well on my machine even Catacomb Snatch have issues with vsync so I guess it is OS fault.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #12 - Posted 2012-04-04 16:57:55 »

I'm pretty sure vsync is ignored in windowed mode for OpenGL on Windows and Linux and it uses whatever the desktop is set to.  On most recent Linux distributions, I've found you can't even get the desktop to use vsync.
Offline ra4king

JGO Kernel


Medals: 341
Projects: 2
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2012-04-04 22:59:44 »

This is Java2D, not OpenGL.

Also, in Windows Vista and 7, you shouldn't be seeing any screen tearing due to the Desktop Window Compositor preventing any tearing.

Offline Donald_W

Senior Newbie





« Reply #14 - Posted 2012-04-05 03:19:50 »

Well it looks that I am doing something wrong. Now I/We have to figure out what is it Wink
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.

CogWheelz (16 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!