Results 1 to 19 of 19
Like Tree1Likes
  • 1 Post By gimbal2

Thread: FPS dropping to exactly half when running game through .jar file

  1. #1
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default FPS dropping to exactly half when running game through .jar file

    Hello,

    My game runs fine and at exactly 60fps if I run it through NetBeans, though it runs at exactly 30fps if I build a .jar file and run it. I have limited my gameloop fps to 60 by creating a boolean check to see if it is currently repainting. I had to do this because i had objects being moved in the middle of repainting and they wouldn't be painted properly. Double buffering is turned on and I am thinking that it might have something to do with that and my loop waiting for the repainting to be finished. I don't know why it's different whether i run it through netbeans or a .jar though. Any help would be appreciated :)
    Thanks

    Java Code:
    dm = new DisplayMode(640,480,32,60);
    Java Code:
    setDoubleBuffered(true);
    Java Code:
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            // Rendering objects code 
            isPainting = false;
        }
    Java Code:
    if (gameScreen.getIsPainting() == false){
                            updateInput();
                            checkCollisions();
                            updateAnimation();
                            animate();
                            updateMovement();
                            updateTimers();
                            render();
                        }
    Java Code:
        public void render(){
            gameScreen.setIsPainting(true);
            gameScreen.repaint();
        }

  2. #2
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: FPS dropping to exactly half when running game through .jar file

    I still can't figure this out....I have cut out all the rendering code and turned off double buffering, but it still does it. Why would the render method run 60 times per second in netbeans, but only 30 times per second when compiled to a .jar?

    Java Code:
    @Override
        public void paintComponent(Graphics g){
            isPainting = false;
        }
    Java Code:
        public void gameLoop(){
            while (true){
                if (gameScreen.getIsPainting() == false){
                    render();
                }
            }
        }
        
        public void render(){
            gameScreen.setIsPainting(true);
            gameScreen.repaint();
        }

  3. #3
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: FPS dropping to exactly half when running game through .jar file

    So i've discovered about the setIgnoreRepaint() method and have set that to true to no avail. Maybe I should be using a canvas instead of swing?
    I have read about whether to use canvas or swing for games, and the answers are mixed...Some say canvas is quicker and better to use for games, others say it's old and swing should be used......Could anyone shed some light on this subject?

  4. #4
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: FPS dropping to exactly half when running game through .jar file

    Ok here is another test I have done. I have added a counter to paintcomponent which adds 1 every time it gets called, then after 1 second it will display how many times it was called.
    If I run this exact code through Netbeans, it does exactly what I need....
    - It takes 15ms from the time repaint() gets called, till paintComponent has finished.
    - paintComponent takes 8ms of that 15ms time.
    - Paintcomponent will be called 60 times per second.

    If I compile this exact code in to a jar file, the FPS drops by half
    - It takes 30ms from the time repaint() gets called, till paintComponent has finished.
    - paintComponent takes 15ms of that 30ms time.
    - Paintcomponent will be called 30times per second.

    I am using Fullscreen exclusive mode.
    Double buffering on/of has no effect.
    Changing resolution has no effect.
    I am going to give up on using swings paintComponent very soon and learn to use canvas if I can't solve this.

    Java Code:
        public void gameLoop(){
            double startMS;
            double currentMS;
            while (true){
                startMS = System.currentTimeMillis();
                currentMS = System.currentTimeMillis() - startMS ;
                while (currentMS < 1000){ // Display FPS after 1 second has passed
                    if (gameScreen.getIsPainting() == false){
                        render();
                    }
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(GameEngine.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    currentMS = System.currentTimeMillis() - startMS ;
                }
                if (SHOW_FPS == true){
                    SwingUtilities.invokeLater(new Runnable(){
                        @Override
                        public void run(){
                            gameScreen.getTfFPS().setText(String.valueOf(GameScreen.paintingAmount));
                            gameScreen.revalidate();
                            GameScreen.paintingAmount = 0; // Reset painting counter to get amount of times painting every second
                        }
                    });
                }
                
            }
        }
    
        public void render(){
            gameScreen.setIsPainting(true);
            gameScreen.repaint();
        }
    Java Code:
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            paintingAmount++;
            isPainting = false;
        }

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: FPS dropping to exactly half when running game through .jar file

    Quote Originally Posted by Teedo View Post
    I am going to give up on using swings paintComponent very soon and learn to use canvas if I can't solve this.
    You should give up on using anything Java2D related and in stead use something like LibGDX for any kind of reliable performance. But when I do go Java2D, I indeed ignore Swing entirely and just use a Canvas + BufferedStrategy indeed.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: FPS dropping to exactly half when running game through .jar file

    I was thinking of using a library but I have the Oracle Java Standard Edition 6 Programmer Certified Professional Exam (1Z0-851 formerly CX-310-065) exam coming up soon and would like to stick with plain java to make the exam as easy as possible. I will learn to implement a canvas and see how I go :)

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: FPS dropping to exactly half when running game through .jar file

    Can you show all of your painting code (i.e. everything that uses a graphics context)?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: FPS dropping to exactly half when running game through .jar file

    The latest posted code is my whole painting code. All it does is call super and set a boolean and int. I will post my JFrame code where I set the display mode and fullscreen exclusive mode when I get home from work :-)

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: FPS dropping to exactly half when running game through .jar file

    So in other words you are not actually painting anything (using the graphics context or drawing an image). You are just noticing the methods being invoked run slower when run from a jar file than from an IDE?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: FPS dropping to exactly half when running game through .jar file

    That's exactly right, it does it whether I am painting anything or not.

  11. #11
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: FPS dropping to exactly half when running game through .jar file

    I have a done a bit more testing and it only happens when in fullscreen exclusive mode, It runs at about 320fps when running in a window. I also tried turning VSync off (it was on application-controlled, which means fullscreen exclusive mode uses VSync right?) in the Nvidia control panel on my computer and it fixed it too by running at 320fps. So the problem seems to be that it knows my monitors refresh rate is 60 in my Netbeans IDE, but for some reason it thinks it is 30 when running from a jar file. A bad fix for it could be to tell everyone to turn VSync off when they play....but

    Here is the cut down version of where I set the display mode.

    Java Code:
        private void init(){
            frame.setUndecorated(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
            gameScreen = new GameScreen(this);
            gameEngine.init();
            input = new Input();
            input.initInput();
            frame.add(input);
            frame.add(gameScreen);
            frame.setVisible(true);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
            DisplayMode dm = new DisplayMode(640,480,32,60);
            gd.setFullScreenWindow(frame);
            gd.setDisplayMode(dm);
        }
    Last edited by Teedo; 08-26-2013 at 11:45 AM.

  12. #12
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: FPS dropping to exactly half when running game through .jar file

    I have tried running the jar on a friends computer and it was the same running at 30fps, But it ran at 160-180fps on his surface pro tablet. I have read that it could be a problem with NVIDIA graphics cards. We do both have NVIDIA cards and the surface pro has an intel card. I have found another thread with the same kind of problem but their fix of setExtendedState(java.awt.Frame.MAXIMIZED_BOTH) did not work. weird mac+fullscreen excl. framerate issue - Java-Gaming.org

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: FPS dropping to exactly half when running game through .jar file

    Note that the link is about Java on a Mac, which adds the risk that it is actually about Apple Java, not Sun/Oracle Java. The two are just completely different, and the Mac implementation of the Oracle JDK actually has chunks of the Apple Java code ported into it so it too will have different performance metrics and operations.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: FPS dropping to exactly half when running game through .jar file

    Ah okay, so I guess that link is irrelevant :)

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: FPS dropping to exactly half when running game through .jar file

    like so many links on the net. It -is- a relevant site though, you may want to consider posing the question there as I'm 95% sure someone on that site knows exactly what's going on. Not me, or I'd tell you right now :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: FPS dropping to exactly half when running game through .jar file


  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: FPS dropping to exactly half when running game through .jar file

    Funny how the first advice you get is to use LibGDX ;) That wasn't me of course.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: FPS dropping to exactly half when running game through .jar file

    Yeah I will definitely be using it after this game is finished :)

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: FPS dropping to exactly half when running game through .jar file

    I can personally testify that your experience will be that of relief - after having kicked around different technologies myself for years (Java2D using a Canvas, LWJGL, Slick2D), I tried LibGDX and was immediately charmed by the clean, easy to understand API.

    Note that I have historical experience with programming OpenGL using C/C++, that helps a lot for me to understand what goes on under the hood. That's the one thing that makes game development using Java slightly more difficult once you leave the relative safety of Java2D; you may lack a whole bunch of prerequisite knowledge coming from native programming. LibGDX does a good job hiding it though, so the experience should be a lot smoother than when you would use LWJGL (which is a thin wrapper around several native libraries including OpenGL).
    Teedo likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. JCombobox not dropping down after using JTextField
    By The original stinger in forum AWT / Swing
    Replies: 4
    Last Post: 05-30-2013, 05:13 PM
  2. Running a game tournament
    By Hazza in forum New To Java
    Replies: 6
    Last Post: 04-27-2013, 09:25 PM
  3. Game running far too fast on friend's computer
    By ruben381 in forum New To Java
    Replies: 2
    Last Post: 06-11-2012, 08:51 PM
  4. dropping a URL: how to get page title?
    By zacchia in forum AWT / Swing
    Replies: 3
    Last Post: 06-20-2011, 04:45 AM
  5. Running jar file.
    By yogi1410 in forum Advanced Java
    Replies: 1
    Last Post: 01-11-2010, 11:59 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
  •