Initial work on making use of full-screen exclusive mode and additional
rendering optimizations available in JDK 1.4. Use a BufferStrategy, and changed AnimatedPanel to extend Canvas rather than JPanel. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@847 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
//
|
||||
// $Id: SpritePanel.java,v 1.8 2001/12/17 03:33:40 mdb Exp $
|
||||
// $Id: SpritePanel.java,v 1.9 2002/01/08 22:16:58 shaper Exp $
|
||||
|
||||
package com.threerings.cast.builder;
|
||||
|
||||
import java.awt.*;
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.border.BevelBorder;
|
||||
|
||||
import com.threerings.media.sprite.*;
|
||||
|
||||
@@ -33,9 +31,6 @@ public class SpritePanel
|
||||
_spritemgr = new SpriteManager();
|
||||
_animmgr = new AnimationManager(_spritemgr, this);
|
||||
|
||||
// create a visually pleasing border
|
||||
setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
|
||||
|
||||
// listen to the builder model so that we can update the
|
||||
// sprite when a new component is selected
|
||||
_model.addListener(this);
|
||||
|
||||
@@ -1,25 +1,28 @@
|
||||
//
|
||||
// $Id: AnimatedPanel.java,v 1.5 2002/01/07 23:05:39 shaper Exp $
|
||||
// $Id: AnimatedPanel.java,v 1.6 2002/01/08 22:16:58 shaper Exp $
|
||||
|
||||
package com.threerings.media.sprite;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.AWTException;
|
||||
import java.awt.BufferCapabilities;
|
||||
import java.awt.Canvas;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Image;
|
||||
import java.awt.ImageCapabilities;
|
||||
import java.awt.Rectangle;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import java.awt.image.BufferStrategy;
|
||||
|
||||
import com.threerings.media.Log;
|
||||
|
||||
/**
|
||||
* The animated panel provides a useful extensible implementation of a
|
||||
* Swing {@link JPanel} that implements the {@link AnimatedView}
|
||||
* interface. Sub-classes should override {@link #render} to draw
|
||||
* their panel-specific contents, and may choose to override {@link
|
||||
* #invalidateRects} to optimize their internal rendering.
|
||||
* {@link Canvas} that implements the {@link AnimatedView} interface.
|
||||
* Sub-classes should override {@link #render} to draw their
|
||||
* panel-specific contents, and may choose to override {@link
|
||||
* #invalidateRects} and {@link #invalidateRect} to optimize their
|
||||
* internal rendering.
|
||||
*/
|
||||
public class AnimatedPanel extends JPanel implements AnimatedView
|
||||
public class AnimatedPanel extends Canvas implements AnimatedView
|
||||
{
|
||||
/**
|
||||
* Constructs an animated panel.
|
||||
@@ -27,26 +30,19 @@ public class AnimatedPanel extends JPanel implements AnimatedView
|
||||
public AnimatedPanel ()
|
||||
{
|
||||
// set our attributes for optimal display performance
|
||||
setDoubleBuffered(false);
|
||||
setOpaque(true);
|
||||
// setIgnoreRepaint(true);
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void paintComponent (Graphics g)
|
||||
public void paint (Graphics g)
|
||||
{
|
||||
// create the offscreens if they don't yet exist
|
||||
if (_offimg == null && !createOffscreen()) {
|
||||
return;
|
||||
}
|
||||
update(g);
|
||||
}
|
||||
|
||||
// give sub-classes a chance to do their thing
|
||||
render(_offg);
|
||||
|
||||
// Rectangle bounds = getBounds();
|
||||
// Log.info("paintComponent [bounds=" + bounds + "].");
|
||||
|
||||
// draw the offscreen to the screen
|
||||
g.drawImage(_offimg, 0, 0, null);
|
||||
// documentation inherited
|
||||
public void update (Graphics g)
|
||||
{
|
||||
paintImmediately();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,26 +55,6 @@ public class AnimatedPanel extends JPanel implements AnimatedView
|
||||
// nothing for now
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the offscreen image and graphics context to which we draw
|
||||
* the scene for double-buffering purposes. Returns whether the
|
||||
* offscreen image was successfully created.
|
||||
*/
|
||||
protected boolean createOffscreen ()
|
||||
{
|
||||
Dimension d = getSize();
|
||||
try {
|
||||
_offimg = createImage(d.width, d.height);
|
||||
_offg = _offimg.getGraphics();
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.warning("Failed to create offscreen [e=" + e + "].");
|
||||
Log.logStackTrace(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void invalidateRects (DirtyRectList rects)
|
||||
{
|
||||
@@ -91,47 +67,54 @@ public class AnimatedPanel extends JPanel implements AnimatedView
|
||||
// nothing for now
|
||||
}
|
||||
|
||||
/**
|
||||
* Paints this panel immediately. Since we know that we are always
|
||||
* opaque and not dependent on Swing's double-buffering, we bypass the
|
||||
* antics that <code>JComponent.paintImmediately()</code> performs in
|
||||
* the interest of better performance.
|
||||
*/
|
||||
// documentation inherited
|
||||
public void paintImmediately ()
|
||||
{
|
||||
if (!isValid()) {
|
||||
// don't paint anything until we've been fully laid out
|
||||
// Log.warning("Attempted to paint invalid panel.");
|
||||
if (!isValid() || !isShowing()) {
|
||||
Log.warning("Attempt to paint unprepared panel " +
|
||||
"[valid=" + isValid() +
|
||||
", showing=" + isShowing() + "].");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_strategy == null) {
|
||||
// create and obtain a reference to the buffer strategy
|
||||
createBufferStrategy(BUFFER_COUNT);
|
||||
_strategy = getBufferStrategy();
|
||||
Log.info("Created buffer strategy [strategy=" + _strategy + "].");
|
||||
}
|
||||
|
||||
// render the panel
|
||||
Graphics g = null;
|
||||
|
||||
try {
|
||||
Graphics pcg = getGraphics();
|
||||
// apparently getGraphics() can fail if we are removed from
|
||||
// the UI between the time that we queued up the code that
|
||||
// calls this method and the time that it's called
|
||||
if (pcg != null) {
|
||||
g = pcg.create();
|
||||
pcg.dispose();
|
||||
paintComponent(g);
|
||||
g = _strategy.getDrawGraphics();
|
||||
render(g);
|
||||
} finally {
|
||||
if (g != null) {
|
||||
g.dispose();
|
||||
}
|
||||
}
|
||||
_strategy.show();
|
||||
}
|
||||
|
||||
} catch (NullPointerException e) {
|
||||
e.printStackTrace();
|
||||
public void createBufferStrategy (int numBuffers)
|
||||
{
|
||||
// for now, always use un-accelerated blitting. page-flipping
|
||||
// seems to result in artifacts in certain conditions, and the
|
||||
// buffer strategy's volatile images are irretrievably lost when
|
||||
// the panel is hidden.
|
||||
BufferCapabilities bufferCaps = new BufferCapabilities(
|
||||
new ImageCapabilities(false), new ImageCapabilities(false), null);
|
||||
try {
|
||||
createBufferStrategy(numBuffers, bufferCaps);
|
||||
} catch (AWTException e) {
|
||||
throw new InternalError("Could not create a buffer strategy");
|
||||
}
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public JComponent getComponent ()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
/** The number of buffers to use when rendering. */
|
||||
protected static final int BUFFER_COUNT = 2;
|
||||
|
||||
/** The offscreen image used for double-buffering. */
|
||||
protected Image _offimg;
|
||||
|
||||
/** The graphics context for the offscreen image. */
|
||||
protected Graphics _offg;
|
||||
/** The buffer strategy used for optimal animation rendering. */
|
||||
protected BufferStrategy _strategy;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: AnimatedView.java,v 1.5 2001/12/15 04:20:26 mdb Exp $
|
||||
// $Id: AnimatedView.java,v 1.6 2002/01/08 22:16:58 shaper Exp $
|
||||
|
||||
package com.threerings.media.sprite;
|
||||
|
||||
@@ -37,11 +37,4 @@ public interface AnimatedView
|
||||
* paint.
|
||||
*/
|
||||
public void paintImmediately ();
|
||||
|
||||
/**
|
||||
* Return the component associated with the view so that the
|
||||
* animation manager can restrict its animation to when the
|
||||
* component is actually visible.
|
||||
*/
|
||||
public JComponent getComponent ();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: IsoSceneView.java,v 1.81 2001/12/18 08:38:33 mdb Exp $
|
||||
// $Id: IsoSceneView.java,v 1.82 2002/01/08 22:16:59 shaper Exp $
|
||||
|
||||
package com.threerings.miso.scene;
|
||||
|
||||
@@ -143,9 +143,7 @@ public class IsoSceneView implements SceneView
|
||||
*/
|
||||
protected void invalidate ()
|
||||
{
|
||||
DirtyRectList rects = new DirtyRectList();
|
||||
rects.add(_model.bounds);
|
||||
invalidateRects(rects);
|
||||
invalidateRect(_model.bounds);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
//
|
||||
// $Id: SceneView.java,v 1.20 2001/12/15 04:20:55 mdb Exp $
|
||||
// $Id: SceneView.java,v 1.21 2002/01/08 22:16:59 shaper Exp $
|
||||
|
||||
package com.threerings.miso.scene;
|
||||
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
import java.util.List;
|
||||
|
||||
import com.threerings.media.sprite.DirtyRectList;
|
||||
@@ -25,6 +26,14 @@ public interface SceneView
|
||||
*/
|
||||
public void invalidateRects (DirtyRectList rects);
|
||||
|
||||
/**
|
||||
* Invalidate a rectangle in screen pixel coordinates in the scene
|
||||
* view for later repainting.
|
||||
*
|
||||
* @param rect the {@link java.awt.Rectangle} object.
|
||||
*/
|
||||
public void invalidateRect (Rectangle rect);
|
||||
|
||||
/**
|
||||
* Renders the scene to the given graphics context.
|
||||
*
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
//
|
||||
// $Id: SceneViewPanel.java,v 1.21 2001/11/29 20:33:16 mdb Exp $
|
||||
// $Id: SceneViewPanel.java,v 1.22 2002/01/08 22:16:59 shaper Exp $
|
||||
|
||||
package com.threerings.miso.scene;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
import javax.swing.*;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Rectangle;
|
||||
|
||||
import com.samskivert.util.Config;
|
||||
|
||||
import com.threerings.media.sprite.*;
|
||||
import com.threerings.media.sprite.AnimatedPanel;
|
||||
import com.threerings.media.sprite.DirtyRectList;
|
||||
import com.threerings.media.sprite.SpriteManager;
|
||||
import com.threerings.miso.util.MisoUtil;
|
||||
|
||||
/**
|
||||
@@ -82,6 +84,12 @@ public class SceneViewPanel extends AnimatedPanel
|
||||
_view.invalidateRects(rects);
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void invalidateRect (Rectangle rect)
|
||||
{
|
||||
_view.invalidateRect(rect);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the desired size for the panel based on the requested
|
||||
* and calculated bounds of the scene view.
|
||||
|
||||
Reference in New Issue
Block a user