Let the vsync limit our frame rate instead of trying to do it by hand.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3934 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2006-03-09 23:54:26 +00:00
parent 13c0830908
commit 3f5aed3555
+9 -33
View File
@@ -56,22 +56,11 @@ import com.threerings.jme.camera.CameraHandler;
/** /**
* Defines a basic application framework providing integration with the * Defines a basic application framework providing integration with the
* <a href="../presents/package.html">Presents</a> networking system and * <a href="../presents/package.html">Presents</a> networking system and
* targeting a fixed framerate. * targeting the framerate of the display.
*/ */
public class JmeApp public class JmeApp
implements RunQueue implements RunQueue
{ {
/**
* Configures the target frame rate in frames per second.
*/
public void setTargetFrameRate (long framesPerSecond)
{
if (framesPerSecond <= 0) {
throw new IllegalArgumentException("FPS must be > 0.");
}
_targetFrameTicks = _timer.getResolution() / framesPerSecond;
}
/** /**
* Returns a context implementation that provides access to all the * Returns a context implementation that provides access to all the
* necessary bits. * necessary bits.
@@ -104,9 +93,6 @@ public class JmeApp
// create an appropriate timer // create an appropriate timer
_timer = Timer.getTimer(_api); _timer = Timer.getTimer(_api);
// default to 60 fps
setTargetFrameRate(60);
// initialize our main camera controls and user input handling // initialize our main camera controls and user input handling
initInput(); initInput();
@@ -257,7 +243,6 @@ public class JmeApp
{ {
// create the main display system // create the main display system
_display = createDisplay(); _display = createDisplay();
_display.setVSyncEnabled(true);
// create a camera // create a camera
int width = _display.getWidth(), height = _display.getHeight(); int width = _display.getWidth(), height = _display.getHeight();
@@ -388,23 +373,15 @@ public class JmeApp
*/ */
protected void processEvents (long frameStart) protected void processEvents (long frameStart)
{ {
// now process events or sleep until the next frame (assume zero Runnable r;
// frame duration to start to ensure that we always process at int events = 0;
// least one event per frame) while ((r = (Runnable)_evqueue.getNonBlocking()) != null) {
long frameDuration = 0L; r.run();
while (frameDuration < _targetFrameTicks) { // only process at most two events per frame
Runnable r = (Runnable)_evqueue.getNonBlocking(); if (++events > 1) {
if (r == null) { break;
try {
Thread.sleep(1);
} catch (InterruptedException ie) {
Log.warning("Ticker interrupted: " + ie);
}
} else {
r.run();
} }
frameDuration = _timer.getTime() - frameStart; }
}
} }
/** /**
@@ -538,7 +515,6 @@ public class JmeApp
protected InputHandler _input; protected InputHandler _input;
protected BRootNode _rnode; protected BRootNode _rnode;
protected long _targetFrameTicks;
protected boolean _finished; protected boolean _finished;
protected int _failures; protected int _failures;