Results 1 to 6 of 6
  1. #1
    Acidic is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Force Graphics Sync (?)

    I'm sure this has been asked before, but I couldn't find any straightforwards information about the subject.

    I'm currently working on making a 2D game using Java2D and I'm using a simple 60fps main-timer and a paint() method with a custom double buffer, and I noticed that at certain points, especially when the computer is under load they can slow down and/or get out of sync for a short while and the results mess up the game flow.

    Is there any way to prevent this by forcing graphics sync similar to a commercial game where lack of horsepower will simply lower the "fps" instead of slowing down the game?

    I apologize that I couldn't formulate the question better, I'm rather new to making games. (even simple ones)
    Other suggestions on how to prevent this would obviously be very welcome.

    Thanks in advance. :)

    Also posted on: JavaProgrammingForums.com
    Last edited by Acidic; 04-14-2011 at 02:51 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

  3. #3
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    It would be better if you would not use passive rendering, aka calling repaint() as I believe you're doing here. You should do all game logic and drawing in the same thread.

  4. #4
    Acidic is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by ra4king View Post
    It would be better if you would not use passive rendering, aka calling repaint() as I believe you're doing here. You should do all game logic and drawing in the same thread.

    Sorry if this question is somewhat general, but how would that be done?

  5. #5
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    First, you should be drawing in a java.awt.Canvas. There you can use BufferStrategy:

    Java Code:
    //init game
    canvas.createBufferStrategy(2);
    BufferStrategy strategy = canvas.getBufferStrategy();
    
    //in game loop
    do{
        do{
            Graphics2D g2 = (Graphics2D)strategy.getDrawGraphics();
            //drawing code
            g2.dispose();
        }while(strategy.contentsRestored());
        
        strategy.show();
    }while(strategy.contentsLost());
    EDIT: Make sure to disable automatic repainting on your canvas and JFrame/Applet when doing this by calling "setIgnoreRepaint(true)" on your components.
    Last edited by ra4king; 04-14-2011 at 03:50 AM.

  6. #6
    Acidic is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by ra4king View Post
    First, you should be drawing in a java.awt.Canvas. There you can use BufferStrategy:

    Java Code:
    //init game
    canvas.createBufferStrategy(2);
    BufferStrategy strategy = canvas.getBufferStrategy();
    
    //in game loop
    do{
        do{
            Graphics2D g2 = (Graphics2D)strategy.getDrawGraphics();
            //drawing code
            g2.dispose();
        }while(strategy.contentsRestored());
        
        strategy.show();
    }while(strategy.contentsLost());
    EDIT: Make sure to disable automatic repainting on your canvas and JFrame/Applet when doing this by calling "setIgnoreRepaint(true)" on your components.
    I've spent the last few days gathering information about the subject and your message was indeed the right direction.

    Thanks a lot.

Similar Threads

  1. file sync from FTP to FTP
    By kgk04 in forum Advanced Java
    Replies: 0
    Last Post: 12-18-2010, 10:15 AM
  2. How to force applet to use libraries?
    By Arvutis6ber in forum Java Applets
    Replies: 1
    Last Post: 09-20-2010, 02:24 PM
  3. thered sync
    By miko5054 in forum Threads and Synchronization
    Replies: 2
    Last Post: 05-25-2010, 10:51 AM
  4. Howto force to use an external jar
    By riccardo_b in forum Eclipse
    Replies: 0
    Last Post: 12-11-2008, 10:08 AM
  5. Force Caps in JTable...
    By markw8500 in forum Advanced Java
    Replies: 0
    Last Post: 07-09-2008, 02:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •