Results 1 to 19 of 19
- 08-23-2013, 05:40 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
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(); }
- 08-24-2013, 04:14 PM #2
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
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(); }
- 08-25-2013, 10:19 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
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?
- 08-25-2013, 04:47 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
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; }
- 08-25-2013, 07:10 PM #5
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: FPS dropping to exactly half when running game through .jar file
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-26-2013, 12:22 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
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 :)
- 08-26-2013, 01:18 AM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-26-2013, 03:24 AM #8
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
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 :-)
- 08-26-2013, 04:13 AM #9
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-26-2013, 04:51 AM #10
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
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.
- 08-26-2013, 11:18 AM #11
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
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.
- 08-26-2013, 11:50 AM #12
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
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
- 08-26-2013, 11:57 AM #13
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 08-26-2013, 12:05 PM #14
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
Re: FPS dropping to exactly half when running game through .jar file
Ah okay, so I guess that link is irrelevant :)
- 08-26-2013, 12:44 PM #15
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 08-26-2013, 01:39 PM #16
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
Re: FPS dropping to exactly half when running game through .jar file
Thanks gimbal, I have now cross-posted :)
FPS is 60 when running through Netbeans IDE, But drops to 30 when running jar - Java-Gaming.org
- 08-27-2013, 10:36 AM #17
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 08-28-2013, 12:52 PM #18
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
Re: FPS dropping to exactly half when running game through .jar file
Yeah I will definitely be using it after this game is finished :)
- 08-28-2013, 02:03 PM #19
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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)."Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
Similar Threads
-
JCombobox not dropping down after using JTextField
By The original stinger in forum AWT / SwingReplies: 4Last Post: 05-30-2013, 05:13 PM -
Running a game tournament
By Hazza in forum New To JavaReplies: 6Last Post: 04-27-2013, 09:25 PM -
Game running far too fast on friend's computer
By ruben381 in forum New To JavaReplies: 2Last Post: 06-11-2012, 08:51 PM -
dropping a URL: how to get page title?
By zacchia in forum AWT / SwingReplies: 3Last Post: 06-20-2011, 04:45 AM -
Running jar file.
By yogi1410 in forum Advanced JavaReplies: 1Last Post: 01-11-2010, 11:59 AM
Bookmarks