Use an array to mark dirty tiles. Added double-buffering to the

SceneViewPanel.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@194 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2001-08-08 03:19:39 +00:00
parent 6b423cd53a
commit 49764d7b79
4 changed files with 90 additions and 44 deletions
@@ -1,5 +1,5 @@
// //
// $Id: AnimationManager.java,v 1.9 2001/08/08 00:10:50 shaper Exp $ // $Id: AnimationManager.java,v 1.10 2001/08/08 03:19:39 shaper Exp $
package com.threerings.miso.sprite; package com.threerings.miso.sprite;
@@ -113,6 +113,7 @@ public class AnimationManager implements Interval, PerformanceObserver
ArrayList rects = _spritemgr.getDirtyRects(); ArrayList rects = _spritemgr.getDirtyRects();
if (rects.size() > 0) { if (rects.size() > 0) {
// pass the dirty-rects on to the scene view // pass the dirty-rects on to the scene view
_view.invalidateRects(rects); _view.invalidateRects(rects);
@@ -160,7 +161,7 @@ public class AnimationManager implements Interval, PerformanceObserver
protected Runnable _ticker; protected Runnable _ticker;
/** The desired number of refresh operations per second. */ /** The desired number of refresh operations per second. */
protected static final int FRAME_RATE = 40; protected static final int FRAME_RATE = 70;
/** The milliseconds to sleep to obtain desired frame rate. */ /** The milliseconds to sleep to obtain desired frame rate. */
protected static final long REFRESH_INTERVAL = 1000 / FRAME_RATE; protected static final long REFRESH_INTERVAL = 1000 / FRAME_RATE;
@@ -1,5 +1,5 @@
// //
// $Id: IsoSceneView.java,v 1.28 2001/08/08 00:10:50 shaper Exp $ // $Id: IsoSceneView.java,v 1.29 2001/08/08 03:19:38 shaper Exp $
package com.threerings.miso.scene; package com.threerings.miso.scene;
@@ -39,9 +39,13 @@ public class IsoSceneView implements EditableSceneView
// get the font used to render tile coordinates // get the font used to render tile coordinates
_font = new Font("Arial", Font.PLAIN, 7); _font = new Font("Arial", Font.PLAIN, 7);
// create the lists of dirty tiles and rectangles // create the array used to mark dirty tiles
_dirty = new ArrayList(); _dirty = new boolean[Scene.TILE_WIDTH][Scene.TILE_HEIGHT];
// create the list of dirty rectangles
_dirtyRects = new ArrayList(); _dirtyRects = new ArrayList();
clearDirtyRegions();
} }
/** /**
@@ -61,7 +65,7 @@ public class IsoSceneView implements EditableSceneView
Shape oldclip = gfx.getClip(); Shape oldclip = gfx.getClip();
gfx.setClip(0, 0, _model.bounds.width, _model.bounds.height); gfx.setClip(0, 0, _model.bounds.width, _model.bounds.height);
if (_dirty.size() == 0) { if (_numDirty == 0) {
// render the full scene // render the full scene
renderScene(gfx); renderScene(gfx);
@@ -70,8 +74,9 @@ public class IsoSceneView implements EditableSceneView
renderSceneInvalid(gfx); renderSceneInvalid(gfx);
// draw frames of dirty tiles and rectangles // draw frames of dirty tiles and rectangles
drawDirtyRegions(gfx); //drawDirtyRegions(gfx);
// clear out the dirty tiles and rectangles
clearDirtyRegions(); clearDirtyRegions();
} }
@@ -87,23 +92,31 @@ public class IsoSceneView implements EditableSceneView
protected void clearDirtyRegions () protected void clearDirtyRegions ()
{ {
_dirty.clear();
_dirtyRects.clear(); _dirtyRects.clear();
_numDirty = 0;
for (int xx = 0; xx < Scene.TILE_WIDTH; xx++) {
for (int yy = 0; yy < Scene.TILE_HEIGHT; yy++) {
_dirty[xx][yy] = false;
}
}
} }
protected void drawDirtyRegions (Graphics2D gfx) protected void drawDirtyRegions (Graphics2D gfx)
{ {
// draw the dirty tiles // draw the dirty tiles
gfx.setColor(Color.cyan); gfx.setColor(Color.cyan);
int size = _dirty.size(); for (int xx = 0; xx < Scene.TILE_WIDTH; xx++) {
for (int ii = 0; ii < size; ii++) { for (int yy = 0; yy < Scene.TILE_HEIGHT; yy++) {
int dinfo[] = (int[])_dirty.get(ii); if (_dirty[xx][yy]) {
gfx.draw(getTilePolygon(dinfo[0], dinfo[1])); gfx.draw(getTilePolygon(xx, yy));
}
}
} }
// draw the dirty rectangles // draw the dirty rectangles
gfx.setColor(Color.red); gfx.setColor(Color.red);
size = _dirtyRects.size(); int size = _dirtyRects.size();
for (int ii = 0; ii < size; ii++) { for (int ii = 0; ii < size; ii++) {
Rectangle rect = (Rectangle)_dirtyRects.get(ii); Rectangle rect = (Rectangle)_dirtyRects.get(ii);
gfx.draw(rect); gfx.draw(rect);
@@ -117,34 +130,39 @@ public class IsoSceneView implements EditableSceneView
*/ */
protected void renderSceneInvalid (Graphics2D gfx) protected void renderSceneInvalid (Graphics2D gfx)
{ {
int size = _dirty.size(); int numDrawn = 0;
for (int ii = 0; ii < size; ii++) {
// retrieve the next dirty tile coordinates for (int xx = 0; xx < Scene.TILE_WIDTH; xx++) {
int[] dinfo = (int[])_dirty.get(ii); for (int yy = 0; yy < Scene.TILE_HEIGHT; yy++) {
int tx = dinfo[0], ty = dinfo[1];
// get the tile's screen position // skip this tile if it's not marked dirty
Polygon poly = getTilePolygon(tx, ty); if (!_dirty[xx][yy]) continue;
// draw all layers at this tile position // get the tile's screen position
for (int kk = 0; kk < Scene.NUM_LAYERS; kk++) { Polygon poly = getTilePolygon(xx, yy);
// get the tile at these coordinates and layer // draw all layers at this tile position
Tile tile = _scene.tiles[tx][ty][kk]; for (int kk = 0; kk < Scene.NUM_LAYERS; kk++) {
if (tile == null) continue;
// offset the image y-position by the tile-specific height // get the tile at these coordinates and layer
int ypos = poly.ypoints[0] - _model.tilehhei - Tile tile = _scene.tiles[xx][yy][kk];
(tile.height - _model.tilehei); if (tile == null) continue;
// draw the tile image // offset the image y-position by the tile-specific height
gfx.drawImage(tile.img, poly.xpoints[0], ypos, null); int ypos = poly.ypoints[0] - _model.tilehhei -
} (tile.height - _model.tilehei);
// draw all sprites residing in the current tile // draw the tile image
_spritemgr.renderSprites(gfx, poly); gfx.drawImage(tile.img, poly.xpoints[0], ypos, null);
} }
// draw all sprites residing in the current tile
_spritemgr.renderSprites(gfx, poly);
// bail early if we know we've drawn all dirty tiles
if (++numDrawn == _numDirty) break;
}
}
} }
/** /**
@@ -376,7 +394,8 @@ public class IsoSceneView implements EditableSceneView
if (y < (screenY + _model.tilehhei)) { if (y < (screenY + _model.tilehhei)) {
ty--; ty--;
for (int ii = 0; ii < numh; ii++) { for (int ii = 0; ii < numh; ii++) {
_dirty.add(new int[] { tx++, ty-- }); if (!_dirty[tx][ty]) _numDirty++;
_dirty[tx++][ty--] = true;
} }
} }
@@ -415,7 +434,8 @@ public class IsoSceneView implements EditableSceneView
// add all tiles in the row to the dirty set // add all tiles in the row to the dirty set
for (int jj = 0; jj < length; jj++) { for (int jj = 0; jj < length; jj++) {
_dirty.add(new int[] { tx++, ty-- }); if (!_dirty[tx][ty]) _numDirty++;
_dirty[tx++][ty--] = true;
} }
// step along the x- or y-axis appropriately // step along the x- or y-axis appropriately
@@ -489,8 +509,11 @@ public class IsoSceneView implements EditableSceneView
/** The font to draw tile coordinates. */ /** The font to draw tile coordinates. */
protected Font _font; protected Font _font;
/** The dirty tile row segments that need to be re-painted. */ /** The dirty tiles that need to be re-painted. */
protected ArrayList _dirty; protected boolean _dirty[][];
/** The number of dirty tiles. */
protected int _numDirty;
/** The dirty rectangles that need to be re-painted. */ /** The dirty rectangles that need to be re-painted. */
protected ArrayList _dirtyRects; protected ArrayList _dirtyRects;
@@ -1,5 +1,5 @@
// //
// $Id: SceneViewPanel.java,v 1.3 2001/08/08 00:10:50 shaper Exp $ // $Id: SceneViewPanel.java,v 1.4 2001/08/08 03:19:38 shaper Exp $
package com.threerings.miso.scene; package com.threerings.miso.scene;
@@ -54,10 +54,26 @@ public class SceneViewPanel extends JPanel
/** /**
* Render the panel and the scene view to the given graphics object. * Render the panel and the scene view to the given graphics object.
*/ */
public void render (Graphics g)
{
if (_offimg == null) {
createOffscreen();
}
_view.paint(_offg);
g.drawImage(_offimg, 0, 0, null);
}
public void paintComponent (Graphics g) public void paintComponent (Graphics g)
{ {
// super.paintComponent(g); render(g);
_view.paint(g); }
protected void createOffscreen ()
{
Rectangle bounds = getBounds();
_offimg = createImage(bounds.width, bounds.height);
_offg = _offimg.getGraphics();
} }
/** /**
@@ -84,6 +100,12 @@ public class SceneViewPanel extends JPanel
/** Origin vertical offset in pixels. */ /** Origin vertical offset in pixels. */
protected static final int VOFFSET = -(5 * THEIGHT); protected static final int VOFFSET = -(5 * THEIGHT);
/** The offscreen image used for double-buffering. */
protected Image _offimg;
/** The graphics context for the offscreen image. */
protected Graphics _offg;
/** The scene data model. */ /** The scene data model. */
protected IsoSceneModel _smodel; protected IsoSceneModel _smodel;
@@ -1,5 +1,5 @@
// //
// $Id: ViewerSceneViewPanel.java,v 1.3 2001/08/07 18:29:18 shaper Exp $ // $Id: ViewerSceneViewPanel.java,v 1.4 2001/08/08 03:19:39 shaper Exp $
package com.threerings.miso.viewer; package com.threerings.miso.viewer;
@@ -40,7 +40,7 @@ public class ViewerSceneViewPanel extends SceneViewPanel
// load up the initial scene // load up the initial scene
prepareStartingScene(); prepareStartingScene();
((EditableSceneView)_view).setShowCoordinates(true); //((EditableSceneView)_view).setShowCoordinates(true);
PerformanceMonitor.register(this, "paint", 1000); PerformanceMonitor.register(this, "paint", 1000);
} }
@@ -100,7 +100,7 @@ public class ViewerSceneViewPanel extends SceneViewPanel
} }
// hackily highlight the tile that was clicked on for happy testing // hackily highlight the tile that was clicked on for happy testing
((EditableSceneView)_view).setHighlightedTile(x, y); // ((EditableSceneView)_view).setHighlightedTile(x, y);
} }
public void mouseClicked (MouseEvent e) { } public void mouseClicked (MouseEvent e) { }