This discussion is archived
7 Replies Latest reply: Nov 13, 2009 2:53 AM by gimbal2 RSS

How to boost Frame Per Second?

843853 Newbie
Currently Being Moderated
Ok I see all these games that get a good 30-60 fps. I have a game I created that is 2d with some .png images. I need to boost my fps from 16 to 30+ How would I go about this? This is how I have it set <Main Applet> -> <swing.timer(1ms)> -> Repaint() is that the only way to get good fps?
  • 1. Re: How to boost Frame Per Second?
    796262 Newbie
    Currently Being Moderated
    Dana_Garcia wrote:
    Ok I see all these games that get a good 30-60 fps. I have a game I created that is 2d with some .png images. I need to boost my fps from 16 to 30+ How would I go about this? This is how I have it set <Main Applet> -> <swing.timer(1ms)> -> Repaint() is that the only way to get good fps?
    Not sure how much it would help you without knowing what you're doing, but you might try putting any non-drawing game-logic into a Thread instead of using a swing Timer? That way you aren't sharing the EDT between game logic and any painting you're doing? On a similar note, make sure that you aren't doing any game logic inside your paintComponent method.
  • 2. Re: How to boost Frame Per Second?
    843853 Newbie
    Currently Being Moderated
    Ok this is what i'm doing, I have a total of 14 images I have to paint onto the panel. I have a class for logic. This class takes care of changing the positions of the images and then throws the repaint method in the applet class. The repaint method paints the images at the x,y coords that were just managed by the logic class. However I seem to only be able to get about 16 fps. I am using double buffering would that have anything to do with it? Also another thing is that I have no idea how to use the update void. When is update even throw?
  • 3. Re: How to boost Frame Per Second?
    843853 Newbie
    Currently Being Moderated
    Just wanting to let you guys know I found a great tutorial! It helped me boost from 16fps to 90fps! I'm so freaking happy. Anyway here is the LINK I was reading it and right there in the text it reads "Threads are best for animation" and boom the light bulb went off. I did some editing on my code to change from swing.timer to Thread and compiled and ran and in the right corner was the most beautiful thing! it read "FPS: 90"! anyway thanks for the advice good luck to you all hope the link helps.
  • 4. Re: How to boost Frame Per Second?
    gimbal2 Guru
    Currently Being Moderated
    and when you check the CPU usage, is it at 100% I wonder?
  • 5. Re: How to boost Frame Per Second?
    843853 Newbie
    Currently Being Moderated
    I actually made a game with several hundred fps, I had to slow it down though - it was ridiculous.

    When you press the left key, the character would move 1 pixel per frame - and it looked like it was teleporting.

    Try something like this (Note: wait is just a method I made that waits the provided amount of milliseconds):
    long lastTime, nextTime;
     public void capFrameRate(int time) {
      if(lastTime == 0) {
       wait(1000/time);
      }
      else {
       nextTime = System.currentTimeMillis()-lastTime;
       if(nextTime < 1000/time) {
        wait(1000/time);
       }
      }
      lastTime = System.currentTimeMillis();
     }
    It may also be because you are using png images, png usually are larger in size than jpeg (and gif, I think).

    Note: it is always a good idea to load the images prior to gameplay or it will halt until the image loads.

    EX: If you made a game where if you get hit by a laser, you'd blow up - the game would halt while loading the explosion for the first time.

    Edited by: b1nary on Nov 10, 2009 8:41 AM
  • 6. Re: How to boost Frame Per Second?
    843853 Newbie
    Currently Being Moderated
    You should not use a refresh time of 1ms ever for drawing. 17ms is the closest you can get to 60fps (assuming integer ms only). You will NOT see any difference between the theoretical 1000fps for 1ms refresh rate and 60fps for a 17ms refresh rate.
    Just consider 1ms compared to 17ms. You are right in thinking that a faster refresh time will make your game faster, but the fact here is that you are doing 17 times as many calculations as you need to be doing. 17 times! No wonder your game is running slow, you are doing all this extra drawing that is totally unnecessary.

    Also as said briefly above, never do any logic in the EDT/paintComponent. There are very generally, 3 classes of threads in a simple game. Initialisers, which start your program/other threads, the EDT, which does all your drawing + gui stuff, and what are called worker threads. This is where you should be doing all your processing, so as to not interfere with the EDT and hinder the frame rate.

    I hope that helps.

    Ben
  • 7. Re: How to boost Frame Per Second?
    gimbal2 Guru
    Currently Being Moderated
    performance problems in Java2D are generally caused by mismanagement of resources. For example:

    - if you run in an environment that does software rendering and you use PNG images with transparency, that will greatly degrade performance.

    - if you manually modify an Image using getRaster(), the image will never again be "managed", meaning Java2D will have to upload the image from system memory to video memory every single time it is blitted.

    - certain operations you can do using Graphics2D are very slow in a software rendering environment, while some of them are actually very slow in a hardware rendering environment.


    It all boils down to you need to know how Java2D and Swing ticks to be able to get performance out of it. There are some resources on the net that can help you gain the understanding, for example this one:

    http://gpwiki.org/index.php/Java:Tutorials:VolatileImage