From c4591a92ef1f76e6edbce7fa181df5f76b3d0d1b Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 20 Nov 2002 02:18:49 +0000 Subject: [PATCH] System.currentTimeMillis() begone! We use our own pluggable timer system now (which uses System.currentTimeMillis() at the moment but eventually won't). git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1968 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/media/FrameManager.java | 37 +++++++++++++------ src/java/com/threerings/media/MediaPanel.java | 6 +-- .../media/animation/BobbleAnimation.java | 21 +++++++---- .../media/animation/RainAnimation.java | 31 +++++++++------- .../media/util/PerformanceMonitor.java | 27 ++++++++++++-- 5 files changed, 84 insertions(+), 38 deletions(-) diff --git a/src/java/com/threerings/media/FrameManager.java b/src/java/com/threerings/media/FrameManager.java index 5b759a5eb..65fa0ec79 100644 --- a/src/java/com/threerings/media/FrameManager.java +++ b/src/java/com/threerings/media/FrameManager.java @@ -1,5 +1,5 @@ // -// $Id: FrameManager.java,v 1.20 2002/11/16 00:15:57 mdb Exp $ +// $Id: FrameManager.java,v 1.21 2002/11/20 02:18:49 mdb Exp $ package com.threerings.media; @@ -34,6 +34,7 @@ import com.samskivert.util.IntervalManager; import com.samskivert.util.ObserverList; import com.samskivert.util.StringUtil; +import com.threerings.media.timer.MediaTimer; import com.threerings.media.util.PerformanceMonitor; import com.threerings.media.util.PerformanceObserver; @@ -101,10 +102,11 @@ public class FrameManager * * @see GraphicsDevice#setFullScreenWindow */ - public FrameManager (Frame frame) + public FrameManager (Frame frame, MediaTimer timer) { _frame = frame; _frame.setIgnoreRepaint(true); + _timer = timer; // set up our custom repaint manager _remgr = new FrameRepaintManager(_frame); @@ -149,14 +151,24 @@ public class FrameManager _participants.remove(participant); } + /** + * Returns a millisecond granularity time stamp using the {@link + * MediaTimer} with which this frame manager was configured. + * Note: this should only be called from the AWT thread. + */ + public long getTimeStamp () + { + return _timer.getElapsedMillis(); + } + /** * Starts up the per-frame tick */ public void start () { - if (_timer == null) { - _timer = new Timer(true); - _timer.scheduleAtFixedRate(_callTick, new Date(), _millisPerFrame); + if (_ticker == null) { + _ticker = new Timer(true); + _ticker.scheduleAtFixedRate(_callTick, new Date(), _millisPerFrame); } } @@ -165,9 +177,9 @@ public class FrameManager */ public synchronized void stop () { - if (_timer != null) { - _timer.cancel(); - _timer = null; + if (_ticker != null) { + _ticker.cancel(); + _ticker = null; } } @@ -177,7 +189,7 @@ public class FrameManager */ public synchronized boolean isRunning () { - return (_timer != null); + return (_ticker != null); } /** @@ -527,6 +539,9 @@ public class FrameManager /** The frame into which we do our rendering. */ protected Frame _frame; + /** Used to obtain timing measurements. */ + protected MediaTimer _timer; + /** Our custom repaint manager. */ protected FrameRepaintManager _remgr; @@ -541,7 +556,7 @@ public class FrameManager protected long _millisPerFrame = 14; /** The timer that dispatches our frame ticks. */ - protected Timer _timer; + protected Timer _ticker; /** Used to detect when we need to drop frames. */ protected boolean _ticking; @@ -564,7 +579,7 @@ public class FrameManager protected TimerTask _callTick = new TimerTask () { public void run () { if (EventQueue.isDispatchThread()) { - tick(System.currentTimeMillis()); + tick(_timer.getElapsedMillis()); } else if (!isTicking()) { EventQueue.invokeLater(this); } else { diff --git a/src/java/com/threerings/media/MediaPanel.java b/src/java/com/threerings/media/MediaPanel.java index 988b88881..de9f9289d 100644 --- a/src/java/com/threerings/media/MediaPanel.java +++ b/src/java/com/threerings/media/MediaPanel.java @@ -1,5 +1,5 @@ // -// $Id: MediaPanel.java,v 1.21 2002/10/29 20:33:51 shaper Exp $ +// $Id: MediaPanel.java,v 1.22 2002/11/20 02:18:49 mdb Exp $ package com.threerings.media; @@ -93,12 +93,12 @@ public class MediaPanel extends JComponent if (_paused = paused) { // make a note of our pause time - _pauseTime = System.currentTimeMillis(); + _pauseTime = _framemgr.getTimeStamp(); } else { // let the animation and sprite managers know that we just // warped into the future - long delta = System.currentTimeMillis() - _pauseTime; + long delta = _framemgr.getTimeStamp() - _pauseTime; _animmgr.fastForward(delta); _spritemgr.fastForward(delta); diff --git a/src/java/com/threerings/media/animation/BobbleAnimation.java b/src/java/com/threerings/media/animation/BobbleAnimation.java index a407c59cc..cd8b3a986 100644 --- a/src/java/com/threerings/media/animation/BobbleAnimation.java +++ b/src/java/com/threerings/media/animation/BobbleAnimation.java @@ -1,5 +1,5 @@ // -// $Id: BobbleAnimation.java,v 1.4 2002/04/25 16:23:30 mdb Exp $ +// $Id: BobbleAnimation.java,v 1.5 2002/11/20 02:18:49 mdb Exp $ package com.threerings.media.animation; @@ -26,10 +26,10 @@ public class BobbleAnimation extends Animation * @param sy the starting y-position. * @param rx the horizontal bobble range. * @param ry the vertical bobble range. - * @param length the time to animate in milliseconds. + * @param duration the time to animate in milliseconds. */ public BobbleAnimation ( - Image image, int sx, int sy, int rx, int ry, int length) + Image image, int sx, int sy, int rx, int ry, int duration) { super(new Rectangle(sx - rx, sy - ry, sx + image.getWidth(null) + rx, sy + image.getHeight(null) + ry)); @@ -42,13 +42,18 @@ public class BobbleAnimation extends Animation _ry = ry; // calculate animation ending time - _end = System.currentTimeMillis() + length; + _duration = duration; } // documentation inherited - public void tick (long timestamp) + public void tick (long tickStamp) { - _finished = (timestamp >= _end); + // grab our ending time on our first tick + if (_end == 0L) { + _end = tickStamp + _duration; + } + + _finished = (tickStamp >= _end); invalidate(); // calculate the latest position @@ -95,6 +100,6 @@ public class BobbleAnimation extends Animation /** The image to animate. */ protected Image _image; - /** The ending animation time. */ - protected long _end; + /** Animation ending timing information. */ + protected long _duration, _end; } diff --git a/src/java/com/threerings/media/animation/RainAnimation.java b/src/java/com/threerings/media/animation/RainAnimation.java index ad7698834..a2f33b5b6 100644 --- a/src/java/com/threerings/media/animation/RainAnimation.java +++ b/src/java/com/threerings/media/animation/RainAnimation.java @@ -1,5 +1,5 @@ // -// $Id: RainAnimation.java,v 1.4 2002/04/25 16:23:30 mdb Exp $ +// $Id: RainAnimation.java,v 1.5 2002/11/20 02:18:49 mdb Exp $ package com.threerings.media.animation; @@ -19,32 +19,32 @@ public class RainAnimation extends Animation * of raindrops and their dimensions. * * @param bounds the bounding rectangle for the animation. - * @param delay the number of seconds the animation should last. + * @param duration the number of seconds the animation should last. */ - public RainAnimation (Rectangle bounds, long delay) + public RainAnimation (Rectangle bounds, long duration) { super(bounds); - init(delay, DEFAULT_COUNT, DEFAULT_WIDTH, DEFAULT_HEIGHT); + init(duration, DEFAULT_COUNT, DEFAULT_WIDTH, DEFAULT_HEIGHT); } /** * Constructs a rain animation. * * @param bounds the bounding rectangle for the animation. - * @param delay the number of seconds the animation should last. + * @param duration the number of seconds the animation should last. * @param count the number of raindrops to render in each frame. * @param wid the width of a raindrop's bounding rectangle. * @param hei the height of a raindrop's bounding rectangle. */ public RainAnimation ( - Rectangle bounds, long delay, int count, int wid, int hei) + Rectangle bounds, long duration, int count, int wid, int hei) { super(bounds); - init(delay, count, wid, hei); + init(duration, count, wid, hei); } - protected void init (long delay, int count, int wid, int hei) + protected void init (long duration, int count, int wid, int hei) { // save things off _count = count; @@ -55,13 +55,18 @@ public class RainAnimation extends Animation _drops = new int[count]; // calculate ending time - _end = System.currentTimeMillis() + delay; + _duration = duration; } // documentation inherited - public void tick (long timestamp) + public void tick (long tickStamp) { - _finished = (timestamp >= _end); + // grab our ending time on our first tick + if (_end == 0L) { + _end = tickStamp + _duration; + } + + _finished = (tickStamp >= _end); // calculate the latest raindrop locations for (int ii = 0; ii < _count; ii++) { @@ -106,6 +111,6 @@ public class RainAnimation extends Animation /** The raindrop locations. */ protected int[] _drops; - /** The ending animation time. */ - protected long _end; + /** Animation ending timing information. */ + protected long _duration, _end; } diff --git a/src/java/com/threerings/media/util/PerformanceMonitor.java b/src/java/com/threerings/media/util/PerformanceMonitor.java index 6db76149c..3db8354ae 100644 --- a/src/java/com/threerings/media/util/PerformanceMonitor.java +++ b/src/java/com/threerings/media/util/PerformanceMonitor.java @@ -1,11 +1,13 @@ // -// $Id: PerformanceMonitor.java,v 1.4 2002/04/23 01:16:28 mdb Exp $ +// $Id: PerformanceMonitor.java,v 1.5 2002/11/20 02:18:49 mdb Exp $ package com.threerings.media.util; import java.util.HashMap; import com.threerings.media.Log; +import com.threerings.media.timer.MediaTimer; +import com.threerings.media.timer.SystemMediaTimer; /** * Provides a simple mechanism for monitoring the number of times an @@ -112,8 +114,27 @@ public class PerformanceMonitor action.tick(); } + /** + * Used to configure the performance monitor with a particular {@link + * MediaTimer} implementation. By default it uses a pure-Java + * implementation which isn't extremely accurate across platforms. + */ + public static void setMediaTimer (MediaTimer timer) + { + _timer = timer; + } + + /** Used by the performance actions. */ + protected synchronized static long getTimeStamp () + { + return _timer.getElapsedMillis(); + } + /** The observers monitoring some set of actions. */ protected static HashMap _observers = new HashMap(); + + /** Used to obtain high resolution time stamps. */ + protected static MediaTimer _timer = new SystemMediaTimer(); } /** @@ -127,14 +148,14 @@ class PerformanceAction _obs = obs; _name = name; _delta = delta; - _lastDelta = System.currentTimeMillis(); + _lastDelta = PerformanceMonitor.getTimeStamp(); } public void tick () { _numTicks++; - long now = System.currentTimeMillis(); + long now = PerformanceMonitor.getTimeStamp(); long passed = now - _lastDelta; if (passed >= _delta) { // update the last checkpoint time