From 7850123988966d394dcf1c1b02338ff89b3a8120 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 5 Dec 2006 06:51:41 +0000 Subject: [PATCH] Factored some duplicated code into a shared method that can be used to easily and efficiently dirty old and new bounds when our bounds change. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@88 ed5b42cb-e716-0410-a449-f6a68f950b19 --- .../com/threerings/media/AbstractMedia.java | 37 ++++++++++++++++++- .../threerings/media/animation/Animation.java | 19 ++-------- .../com/threerings/media/sprite/Sprite.java | 19 ++-------- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/java/com/threerings/media/AbstractMedia.java b/src/java/com/threerings/media/AbstractMedia.java index da4b7902..c4ebb563 100644 --- a/src/java/com/threerings/media/AbstractMedia.java +++ b/src/java/com/threerings/media/AbstractMedia.java @@ -271,10 +271,43 @@ public abstract class AbstractMedia _firstTick = tickStamp; } + /** + * If this media's size or location are changing, it should create a new + * rectangle from its old boudns (new Rectangle(_bounds)), then effect the + * bounds changes and then call this method with the old bounds and this + * method will either merge the new bounds with the old to create a single + * dirty rectangle or dirty them separately depending on which is more + * appropriate. It will also behave properly if this media is not currently + * managed (not being rendered) by NOOPing. + * + * Do not pass {@link #_bounds} to this method. The rectangle + * passed in will be modified and then passed on to the region manager + * which will modify it further. + */ + protected void invalidateAfterChange (Rectangle obounds) + { + // if we're not added we need not dirty + if (_mgr == null) { + return; + } + + // if our new bounds intersect our old bounds, grow a single dirty + // rectangle to incorporate them both + if (_bounds.intersects(obounds)) { + obounds.add(_bounds); + } else { + // otherwise invalidate our new bounds separately + _mgr.getRegionManager().invalidateRegion(_bounds); + } + + // finally invalidate the original/merged bounds + _mgr.getRegionManager().addDirtyRegion(obounds); + } + /** * Called by the media manager after the media is removed from service. - * Derived classes may override this method, but should be sure to - * call super.shutdown(). + * Derived classes may override this method, but should be sure to call + * super.shutdown(). */ protected void shutdown () { diff --git a/src/java/com/threerings/media/animation/Animation.java b/src/java/com/threerings/media/animation/Animation.java index d0be45d5..f193423d 100644 --- a/src/java/com/threerings/media/animation/Animation.java +++ b/src/java/com/threerings/media/animation/Animation.java @@ -69,25 +69,14 @@ public abstract class Animation extends AbstractMedia return; // no-op } - // start with our current bounds - Rectangle dirty = new Rectangle(_bounds); + // make a note of our current bounds + Rectangle obounds = new Rectangle(_bounds); // move ourselves super.setLocation(x, y); - // grow the dirty rectangle to incorporate our new bounds and pass - // the dirty region to our region manager - if (_mgr != null) { - // if our new bounds intersect our old bounds, grow a single - // dirty rectangle to incorporate them both - if (_bounds.intersects(dirty)) { - dirty.add(_bounds); - } else { - // otherwise invalidate our new bounds separately - _mgr.getRegionManager().invalidateRegion(_bounds); - } - _mgr.getRegionManager().addDirtyRegion(dirty); - } + // this method will invalidate our old and new bounds efficiently + invalidateAfterChange(obounds); } // documentation inherited diff --git a/src/java/com/threerings/media/sprite/Sprite.java b/src/java/com/threerings/media/sprite/Sprite.java index 9abf1c6a..c026b7a2 100644 --- a/src/java/com/threerings/media/sprite/Sprite.java +++ b/src/java/com/threerings/media/sprite/Sprite.java @@ -154,8 +154,8 @@ public abstract class Sprite extends AbstractMedia return; // no-op } - // start with our current bounds - Rectangle dirty = new Rectangle(_bounds); + // make a note of our current bounds + Rectangle obounds = new Rectangle(_bounds); // move ourselves _ox = x; @@ -165,19 +165,8 @@ public abstract class Sprite extends AbstractMedia // of our current bounds updateRenderOrigin(); - // grow the dirty rectangle to incorporate our new bounds and pass - // the dirty region to our region manager - if (_mgr != null) { - // if our new bounds intersect our old bounds, grow a single - // dirty rectangle to incorporate them both - if (_bounds.intersects(dirty)) { - dirty.add(_bounds); - } else { - // otherwise invalidate our new bounds separately - _mgr.getRegionManager().invalidateRegion(_bounds); - } - _mgr.getRegionManager().addDirtyRegion(dirty); - } + // this method will invalidate our old and new bounds efficiently + invalidateAfterChange(obounds); } // documentation inherited