Limiting framerate in SimpleCanvasImpl

You can try turning vsync on, then the framerate should be limited to the refresh rate of your monitor.

display.setVSyncEnabled(true);

ah, this one is useful for me as well, thanks!:slight_smile: I was once or twice wondering about do I really need that much FPS and CPU usage, now this I should add to my project…

thanks - the line I added finally was:



DisplaySystem.getDisplaySystem().setVSyncEnabled(true);



Is there a way to show the current fps in the applet? Since, my application still is using all the CPU - so, I would like to confirm that the line I added makes a difference to the fps rendered.

you should be able to get the fps from the renderer at some point during your render method. You could look at the source for SimpleGame (I think) and see an example of that.

You could use the Text class and print 1.0/tpf to it in your update method.

Thanks,

I just added a System.out.println to my update method - even though this isn't very good style, it gives a quick overview over the framerate.


   public void simpleUpdate() {
      System.out.println(1/getTimePerFrame());
   }



The output of the console was:


59.999996
124.99999
142.85713
149.99998
153.84615
161.29031
122.448975
127.27271
131.14754
134.32835
128.20512
132.53012
134.83145
135.41666
135.92232
136.36363
139.13043
141.66667
139.53487
134.75177
133.33333
134.61537
132.53012
133.72092
134.83145
135.86955
119.81566
121.621605
120.689644



assuming that a LCD display uses a refresh rate between 60 and 75 Hz, this just seems, that limiting the fps that way just doesn't work... :(

Here are my classes for setting up the jme panel, maybe I got something wrong.... I tried to insert the vsync in both classes for testing purposes....

public class 3DPanel extends JPanel {

   private static final long serialVersionUID = 1L;
   Canvas comp = null;
   JMECanvasImplementor impl;

   3DPanel(Dimension dimension) {
      setLayout(new BorderLayout());
      System.out.println("dimension - width: " + dimension.width
            + "  height: " + dimension.height);
      comp = DisplaySystem.getDisplaySystem("lwjgl").createCanvas(
            dimension.width, dimension.height);
      DisplaySystem.getDisplaySystem().setVSyncEnabled(true);
      // add a listener... if window is resized, we can do something about it.
      comp.addComponentListener(new ComponentAdapter() {
         public void componentResized(ComponentEvent ce) {
            doResize();
         }
      });

      // MAKE SURE YOU REPAINT SOMEHOW OR YOU WON'T SEE THE UPDATES...
        new Thread() {
             { setDaemon(true); }
             public void run() {
                 while (true) {
                     comp.repaint();
                     yield();
                 }
             }
         }.start();

      KeyInput.setProvider(KeyInput.INPUT_AWT);
      AWTMouseInput.setup(comp, false);

      impl = new SceneImpl(dimension.width, dimension.height);
      JMECanvas jmeCanvas = ((JMECanvas) comp);
      jmeCanvas.setImplementor(impl);
      jmeCanvas.setUpdateInput(true);
      
      comp.setBounds(0, 0, dimension.width, dimension.height);
      add(comp);

   }

   protected void doResize() {
         impl.resizeCanvas(comp.getWidth(), comp.getHeight());
   }
}



class SceneImpl extends SimpleCanvasImpl {
   private static final Logger logger = Logger
         .getLogger(TestColladaLoading.class.getName());

   SceneImpl(int width, int height) {
      super(width, height);
                DisplaySystem.getDisplaySystem().setVSyncEnabled(true);
   }

   public void simpleSetup() {
      

         getCamera().setAxes(new Vector3f(-1, 0, 0), new Vector3f(0, 0, 1),
               new Vector3f(0, 1, 0));
         getCamera().setLocation(new Vector3f(0, -100, 20));
      }
   
   public void simpleUpdate() {
      System.out.println(1/getTimePerFrame());
   }
}



I really appreciate any help on this....
Any other ideas on how to limit the framerate?

so far, as usual the solution quite simple…

get JME 2 und use the canvas.setTargetRate() method… done…

hmm, there is something strange happening so far…

the more I limit the framerate, the less cpu is obviously used… but, my whole application slows down the same way…

when I set fps=1 my whole application is slow as hell. which is odd, because my cpu is almost doing nothing…

what's happening here?!?