public override TileData GetTile(TiledMapSession.Key key, IMapRenderer renderer, System.Threading.WaitCallback callback, object state) { // make sure there's something to blend if (mySessions.Count == 0) return null; // see if we need to update this tile at all int currentBlend = 0; if (mySessionBlend.TryGetValue(key, out currentBlend) && currentBlend == myTotalBlend) return TileCache[key]; // see how many tiles we can blend int canBlend = 0; for (int i = 0; i < mySessions.Count; i++) { if (!mySessionEnabled[i]) continue; TileData data = mySessions[i].GetTile(key, renderer, callback, state); if (data != null && data.Bitmap != null) canBlend++; } // if there's nothing new to blend, don't if (canBlend == currentBlend) return null; mySessionBlend[key] = canBlend; // get the target bitmap ready if (RefreshBitmap == null) throw new InvalidOperationException("You must provide a RefreshBitmap"); StandardBitmap refreshBitmap = RefreshBitmap as StandardBitmap; int width = refreshBitmap.Width; int height = refreshBitmap.Height; StandardBitmap bitmap = new StandardBitmap(new Bitmap(width, height)); using (Graphics graphics = Graphics.FromImage(bitmap.Bitmap)) { refreshBitmap.Draw(graphics, new Rectangle(0, 0, width, height), new Rectangle(0, 0, width, height)); } // draw the bitmaps using (Graphics graphics = Graphics.FromImage(bitmap.Bitmap)) { for (int i = 0; i < mySessions.Count; i++) { TiledMapSession session = mySessions[i]; if (!mySessionEnabled[i]) continue; TileData tile = mySessions[i].GetTile(key, renderer, callback, state); if (tile == null) continue; IGraphicsDrawable tileBitmap = tile.Bitmap as IGraphicsDrawable; tileBitmap.Draw(graphics, new Rectangle(0, 0, 256, 256), new Rectangle(0, 0, 256, 256)); if (ClearBlendedTiles && canBlend == myTotalBlend) { tileBitmap.Dispose(); mySessions[i].TileCache.Remove(key); } } } TileData ret; if (!TileCache.TryGetValue(key, out ret)) { ret = new TileData(); TileCache.Add(key, ret); } else if (ret.Bitmap != null) { ret.Bitmap.Dispose(); ret.Bitmap = null; } // TODO: optimize to do a lock/read load MemoryStream mem = new MemoryStream(); bitmap.Bitmap.Save(mem, System.Drawing.Imaging.ImageFormat.Bmp); mem.Seek(0, SeekOrigin.Begin); ret.Bitmap = myRenderer.LoadBitmap(mem, false); return ret; }
public override TileData GetTile(TiledMapSession.Key key, IMapRenderer renderer, System.Threading.WaitCallback callback, object state) { // make sure there's something to blend if (mySessions.Count == 0) { return(null); } // see if we need to update this tile at all int currentBlend = 0; if (mySessionBlend.TryGetValue(key, out currentBlend) && currentBlend == myTotalBlend) { return(TileCache[key]); } // see how many tiles we can blend int canBlend = 0; for (int i = 0; i < mySessions.Count; i++) { if (!mySessionEnabled[i]) { continue; } TileData data = mySessions[i].GetTile(key, renderer, callback, state); if (data != null && data.Bitmap != null) { canBlend++; } } // if there's nothing new to blend, don't if (canBlend == currentBlend) { return(null); } mySessionBlend[key] = canBlend; // get the target bitmap ready if (RefreshBitmap == null) { throw new InvalidOperationException("You must provide a RefreshBitmap"); } StandardBitmap refreshBitmap = RefreshBitmap as StandardBitmap; int width = refreshBitmap.Width; int height = refreshBitmap.Height; StandardBitmap bitmap = new StandardBitmap(new Bitmap(width, height)); using (Graphics graphics = Graphics.FromImage(bitmap.Bitmap)) { refreshBitmap.Draw(graphics, new Rectangle(0, 0, width, height), new Rectangle(0, 0, width, height)); } // draw the bitmaps using (Graphics graphics = Graphics.FromImage(bitmap.Bitmap)) { for (int i = 0; i < mySessions.Count; i++) { TiledMapSession session = mySessions[i]; if (!mySessionEnabled[i]) { continue; } TileData tile = mySessions[i].GetTile(key, renderer, callback, state); if (tile == null) { continue; } IGraphicsDrawable tileBitmap = tile.Bitmap as IGraphicsDrawable; tileBitmap.Draw(graphics, new Rectangle(0, 0, 256, 256), new Rectangle(0, 0, 256, 256)); if (ClearBlendedTiles && canBlend == myTotalBlend) { tileBitmap.Dispose(); mySessions[i].TileCache.Remove(key); } } } TileData ret; if (!TileCache.TryGetValue(key, out ret)) { ret = new TileData(); TileCache.Add(key, ret); } else if (ret.Bitmap != null) { ret.Bitmap.Dispose(); ret.Bitmap = null; } // TODO: optimize to do a lock/read load MemoryStream mem = new MemoryStream(); bitmap.Bitmap.Save(mem, System.Drawing.Imaging.ImageFormat.Bmp); mem.Seek(0, SeekOrigin.Begin); ret.Bitmap = myRenderer.LoadBitmap(mem, false); return(ret); }