void DrawAtGeocode(Geocode tlGeo, Geocode brGeo, IMapRenderer renderer, Geocode geocode, int pixelLevelZoom, int adjustX, int adjustY, IMapDrawable drawable) { if (!GeocodeBoxContains(tlGeo, brGeo, geocode) || drawable == null) { return; } Point p = GeocodeToScreen(geocode, pixelLevelZoom, adjustX, adjustY); renderer.Draw(drawable, new Rectangle(p.X - drawable.Width / 2, p.Y - drawable.Height / 2, drawable.Width, drawable.Height), new Rectangle(0, 0, drawable.Width, drawable.Height)); }
public void Draw(Camera2D camera, SpriteBatch spriteBatch) { _mapRenderer.Draw(camera.GetViewMatrix()); spriteBatch.Begin(transformMatrix: camera.GetViewMatrix(), samplerState: SamplerState.PointClamp); if (SceneManager.Instance.DebugMode) { DrawTileColliders(spriteBatch); } spriteBatch.End(); }
public override void Draw(SpriteBatch spriteBatch) { var transformMatrix = CameraTracker.TransformationMatrix; spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: transformMatrix, sortMode: SpriteSortMode.BackToFront, depthStencilState: DepthStencilState.Default); _mapRenderer.Draw(transformMatrix); foreach (var toRender in RenderList) { if (CameraTracker.Contains(toRender.Area)) { toRender.Draw(spriteBatch); } } PathRenderer.Draw(spriteBatch); spriteBatch.End(); GuiManager.Draw(spriteBatch); }
void DrawAtGeocode(Geocode tlGeo, Geocode brGeo, IMapRenderer renderer, Geocode geocode, int pixelLevelZoom, int adjustX, int adjustY, IMapDrawable drawable) { if (!GeocodeBoxContains(tlGeo, brGeo, geocode) || drawable == null) return; Point p = GeocodeToScreen(geocode, pixelLevelZoom, adjustX, adjustY); renderer.Draw(drawable, new Rectangle(p.X - drawable.Width / 2, p.Y - drawable.Height / 2, drawable.Width, drawable.Height), new Rectangle(0, 0, drawable.Width, drawable.Height)); }
public int DrawMap(IMapRenderer renderer, int x, int y, int width, int height, WaitCallback callback, object state) { int unavailable = 0; // approximate the the top left tile (it may be off by 1), but the loop // below will kept it from being drawn int midX = x + width / 2 - myCenterOffset.X; int midY = y + height / 2 - myCenterOffset.Y; int xTiles = (midX - x) / 256 + 1; int yTiles = (midY - y) / 256 + 1; Key currentXKey = new Key(myCenterTile.X - xTiles, myCenterTile.Y - yTiles, myZoom); int xStart = midX - xTiles * 256; int yStart = midY - yTiles * 256; Rectangle rect = new Rectangle(x, y, width, height); int tickCount = Environment.TickCount; for (int currentX = xStart; currentX < x + width; currentX += 256, currentXKey.X++) { Key key = currentXKey; for (int currentY = yStart; currentY < y + height; currentY += 256, key.Y++) { IMapDrawable tile = null; // find the intersect region of the tile that we are drawing Rectangle tileRect = new Rectangle(currentX, currentY, 256, 256); tileRect.Intersect(rect); Rectangle sourceRect = new Rectangle(tileRect.X - currentX, tileRect.Y - currentY, tileRect.Width, tileRect.Height); // dont draw off the map tiles if (!key.IsValid) { // dont draw gray rect if we're drawing transparent if (!HasAlpha) renderer.FillRectangle(BackColor, tileRect); continue; } // first try to get the tile from the tileData TileData tileData = GetTile(key, renderer, callback, state); if (tileData != null) { tile = tileData.Bitmap; tileData.LastUsed = tickCount; } if (tile == null) { // tile not available, so try to generate a tile from child tiles unavailable++; Key childKey = new Key(key.X * 2, key.Y * 2, key.Zoom + 1); Key tl = childKey; Key tr = new Key(childKey.X + 1, childKey.Y, childKey.Zoom); Key br = new Key(childKey.X + 1, childKey.Y + 1, childKey.Zoom); Key bl = new Key(childKey.X, childKey.Y + 1, childKey.Zoom); TileData tld; TileData trd; TileData bld; TileData brd; // see if the children are available // we also need to null check, because they could be loading if (TileCache.TryGetValue(tl, out tld) && TileCache.TryGetValue(tr, out trd) && TileCache.TryGetValue(br, out brd) && TileCache.TryGetValue(bl, out bld) && tld != null && trd != null && bld != null & brd != null && tld.Bitmap != null && trd.Bitmap != null && bld.Bitmap != null && brd.Bitmap != null) { // children are available, so mark them as recently used tld.LastUsed = trd.LastUsed = bld.LastUsed = brd.LastUsed = tickCount; // calculate the destination rects of each child tile Rectangle tlr = new Rectangle(currentX, currentY, 128, 128); Rectangle trr = new Rectangle(currentX + 128, currentY, 128, 128); Rectangle blr = new Rectangle(currentX, currentY + 128, 128, 128); Rectangle brr = new Rectangle(currentX + 128, currentY + 128, 128, 128); tlr.Intersect(rect); trr.Intersect(rect); blr.Intersect(rect); brr.Intersect(rect); // calculate the source rect of each child tile Rectangle tlsr = new Rectangle(tlr.X - currentX, tlr.Y - currentY, tlr.Width * 2, tlr.Height * 2); Rectangle trsr = new Rectangle(trr.X - currentX - 128, trr.Y - currentY, trr.Width * 2, trr.Height * 2); Rectangle blsr = new Rectangle(blr.X - currentX, blr.Y - currentY - 128, blr.Width * 2, blr.Height * 2); Rectangle brsr = new Rectangle(brr.X - currentX - 128, brr.Y - currentY - 128, brr.Width * 2, brr.Height * 2); // don't attempt to draw tiles that we don't need to if (tlsr.Width > 0 && tlsr.Height > 0) renderer.Draw(tld.Bitmap, tlr, tlsr); if (trsr.Width > 0 && trsr.Height > 0) renderer.Draw(trd.Bitmap, trr, trsr); if (blsr.Width > 0 && blsr.Height > 0) renderer.Draw(bld.Bitmap, blr, blsr); if (brsr.Width > 0 && brsr.Height > 0) renderer.Draw(brd.Bitmap, brr, brsr); continue; } else { // can't generate from children, so try generating one of the parents Key parent = key; Rectangle parentRect = sourceRect; TileData parentData = null; while (parent.Zoom >= 0 && parentData == null) { parentRect.Width /= 2; parentRect.Height /= 2; parentRect.X /= 2; parentRect.Y /= 2; if (parent.X % 2 == 1) parentRect.X += 128; if (parent.Y % 2 == 1) parentRect.Y += 128; parent.X /= 2; parent.Y /= 2; parent.Zoom--; TileCache.TryGetValue(parent, out parentData); } if (parentData != null && parentData.Bitmap != null) { // mark this tile as used recently parentData.LastUsed = tickCount; if (tileRect.Width > 0 && tileRect.Height > 0) renderer.Draw(parentData.Bitmap, tileRect, parentRect); continue; } else { // tile is being downloaded, and we have no parent or child images we can use to draw a temp // image. let's try to use a refresh bitmap. // tile is not available, and this is a transparent draw, // so dont draw at all if (HasAlpha) continue; if ((tile = RefreshBitmap) == null) { renderer.FillRectangle(BackColor, tileRect); continue; } } } } if (tile != null && tileRect.Width > 0 && tileRect.Height > 0) renderer.Draw(tile, tileRect, sourceRect); } } int pixelLevelZoom = myZoom + 8; int centerXReference = myCenterTile.X << 8; int centerYReference = myCenterTile.Y << 8; Geocode tlGeo = PointToGeocode(new Point(Math.Max(centerXReference + myCenterOffset.X - width / 2, 0), Math.Max(centerYReference + myCenterOffset.Y - height / 2, 0)), pixelLevelZoom); Geocode brGeo = PointToGeocode(new Point(Math.Min(centerXReference + myCenterOffset.X + width / 2, 1 << pixelLevelZoom), Math.Min(centerYReference + myCenterOffset.Y + height / 2, 1 << pixelLevelZoom)), pixelLevelZoom); int adjustX = midX - centerXReference; int adjustY = midY - centerYReference; foreach(Route route in myRoutes) { List<Point> points = new List<Point>(); Geocode lastOffscreenPoint = Geocode.Null; for (int i = 0; i < route.PolyLine.Length; i++) { Geocode geocode = route.PolyLine[i]; if (myLevelToZoom[route.Levels[i]] > myZoom) continue; // check if we're drawing off the screen if (!GeocodeBoxContains(tlGeo, brGeo, geocode)) { // if we're drawing from on screen to off screen, draw it, but note that // we are now off screen if (lastOffscreenPoint == Geocode.Null) points.Add(GeocodeToScreen(geocode, pixelLevelZoom, adjustX, adjustY)); lastOffscreenPoint = geocode; continue; } // draw in from off the screen if necessary if (lastOffscreenPoint != Geocode.Null) points.Add(GeocodeToScreen(lastOffscreenPoint, pixelLevelZoom, adjustX, adjustY)); // note that we are now in screen space lastOffscreenPoint = Geocode.Null; points.Add(GeocodeToScreen(geocode, pixelLevelZoom, adjustX, adjustY)); } if (points.Count > 1) renderer.DrawLines(route.LineWidth, Color.Cyan, points.ToArray()); } foreach (IMapOverlay overlay in Overlays) { DrawAtGeocode(tlGeo, brGeo, renderer, overlay.Geocode, pixelLevelZoom, adjustX + overlay.Offset.X, adjustY + overlay.Offset.Y, overlay.Drawable); } return unavailable; }
public int DrawMap(IMapRenderer renderer, int x, int y, int width, int height, WaitCallback callback, object state) { int unavailable = 0; // approximate the the top left tile (it may be off by 1), but the loop // below will kept it from being drawn int midX = x + width / 2 - myCenterOffset.X; int midY = y + height / 2 - myCenterOffset.Y; int xTiles = (midX - x) / 256 + 1; int yTiles = (midY - y) / 256 + 1; Key currentXKey = new Key(myCenterTile.X - xTiles, myCenterTile.Y - yTiles, myZoom); int xStart = midX - xTiles * 256; int yStart = midY - yTiles * 256; Rectangle rect = new Rectangle(x, y, width, height); int tickCount = Environment.TickCount; for (int currentX = xStart; currentX < x + width; currentX += 256, currentXKey.X++) { Key key = currentXKey; for (int currentY = yStart; currentY < y + height; currentY += 256, key.Y++) { IMapDrawable tile = null; // find the intersect region of the tile that we are drawing Rectangle tileRect = new Rectangle(currentX, currentY, 256, 256); tileRect.Intersect(rect); Rectangle sourceRect = new Rectangle(tileRect.X - currentX, tileRect.Y - currentY, tileRect.Width, tileRect.Height); // dont draw off the map tiles if (!key.IsValid) { // dont draw gray rect if we're drawing transparent if (!HasAlpha) { renderer.FillRectangle(BackColor, tileRect); } continue; } // first try to get the tile from the tileData TileData tileData = GetTile(key, renderer, callback, state); if (tileData != null) { tile = tileData.Bitmap; tileData.LastUsed = tickCount; } if (tile == null) { // tile not available, so try to generate a tile from child tiles unavailable++; Key childKey = new Key(key.X * 2, key.Y * 2, key.Zoom + 1); Key tl = childKey; Key tr = new Key(childKey.X + 1, childKey.Y, childKey.Zoom); Key br = new Key(childKey.X + 1, childKey.Y + 1, childKey.Zoom); Key bl = new Key(childKey.X, childKey.Y + 1, childKey.Zoom); TileData tld; TileData trd; TileData bld; TileData brd; // see if the children are available // we also need to null check, because they could be loading if (TileCache.TryGetValue(tl, out tld) && TileCache.TryGetValue(tr, out trd) && TileCache.TryGetValue(br, out brd) && TileCache.TryGetValue(bl, out bld) && tld != null && trd != null && bld != null & brd != null && tld.Bitmap != null && trd.Bitmap != null && bld.Bitmap != null && brd.Bitmap != null) { // children are available, so mark them as recently used tld.LastUsed = trd.LastUsed = bld.LastUsed = brd.LastUsed = tickCount; // calculate the destination rects of each child tile Rectangle tlr = new Rectangle(currentX, currentY, 128, 128); Rectangle trr = new Rectangle(currentX + 128, currentY, 128, 128); Rectangle blr = new Rectangle(currentX, currentY + 128, 128, 128); Rectangle brr = new Rectangle(currentX + 128, currentY + 128, 128, 128); tlr.Intersect(rect); trr.Intersect(rect); blr.Intersect(rect); brr.Intersect(rect); // calculate the source rect of each child tile Rectangle tlsr = new Rectangle(tlr.X - currentX, tlr.Y - currentY, tlr.Width * 2, tlr.Height * 2); Rectangle trsr = new Rectangle(trr.X - currentX - 128, trr.Y - currentY, trr.Width * 2, trr.Height * 2); Rectangle blsr = new Rectangle(blr.X - currentX, blr.Y - currentY - 128, blr.Width * 2, blr.Height * 2); Rectangle brsr = new Rectangle(brr.X - currentX - 128, brr.Y - currentY - 128, brr.Width * 2, brr.Height * 2); // don't attempt to draw tiles that we don't need to if (tlsr.Width > 0 && tlsr.Height > 0) { renderer.Draw(tld.Bitmap, tlr, tlsr); } if (trsr.Width > 0 && trsr.Height > 0) { renderer.Draw(trd.Bitmap, trr, trsr); } if (blsr.Width > 0 && blsr.Height > 0) { renderer.Draw(bld.Bitmap, blr, blsr); } if (brsr.Width > 0 && brsr.Height > 0) { renderer.Draw(brd.Bitmap, brr, brsr); } continue; } else { // can't generate from children, so try generating one of the parents Key parent = key; Rectangle parentRect = sourceRect; TileData parentData = null; while (parent.Zoom >= 0 && parentData == null) { parentRect.Width /= 2; parentRect.Height /= 2; parentRect.X /= 2; parentRect.Y /= 2; if (parent.X % 2 == 1) { parentRect.X += 128; } if (parent.Y % 2 == 1) { parentRect.Y += 128; } parent.X /= 2; parent.Y /= 2; parent.Zoom--; TileCache.TryGetValue(parent, out parentData); } if (parentData != null && parentData.Bitmap != null) { // mark this tile as used recently parentData.LastUsed = tickCount; if (tileRect.Width > 0 && tileRect.Height > 0) { renderer.Draw(parentData.Bitmap, tileRect, parentRect); } continue; } else { // tile is being downloaded, and we have no parent or child images we can use to draw a temp // image. let's try to use a refresh bitmap. // tile is not available, and this is a transparent draw, // so dont draw at all if (HasAlpha) { continue; } if ((tile = RefreshBitmap) == null) { renderer.FillRectangle(BackColor, tileRect); continue; } } } } if (tile != null && tileRect.Width > 0 && tileRect.Height > 0) { renderer.Draw(tile, tileRect, sourceRect); } } } int pixelLevelZoom = myZoom + 8; int centerXReference = myCenterTile.X << 8; int centerYReference = myCenterTile.Y << 8; Geocode tlGeo = PointToGeocode(new Point(Math.Max(centerXReference + myCenterOffset.X - width / 2, 0), Math.Max(centerYReference + myCenterOffset.Y - height / 2, 0)), pixelLevelZoom); Geocode brGeo = PointToGeocode(new Point(Math.Min(centerXReference + myCenterOffset.X + width / 2, 1 << pixelLevelZoom), Math.Min(centerYReference + myCenterOffset.Y + height / 2, 1 << pixelLevelZoom)), pixelLevelZoom); int adjustX = midX - centerXReference; int adjustY = midY - centerYReference; foreach (Route route in myRoutes) { List <Point> points = new List <Point>(); Geocode lastOffscreenPoint = Geocode.Null; for (int i = 0; i < route.PolyLine.Length; i++) { Geocode geocode = route.PolyLine[i]; if (myLevelToZoom[route.Levels[i]] > myZoom) { continue; } // check if we're drawing off the screen if (!GeocodeBoxContains(tlGeo, brGeo, geocode)) { // if we're drawing from on screen to off screen, draw it, but note that // we are now off screen if (lastOffscreenPoint == Geocode.Null) { points.Add(GeocodeToScreen(geocode, pixelLevelZoom, adjustX, adjustY)); } lastOffscreenPoint = geocode; continue; } // draw in from off the screen if necessary if (lastOffscreenPoint != Geocode.Null) { points.Add(GeocodeToScreen(lastOffscreenPoint, pixelLevelZoom, adjustX, adjustY)); } // note that we are now in screen space lastOffscreenPoint = Geocode.Null; points.Add(GeocodeToScreen(geocode, pixelLevelZoom, adjustX, adjustY)); } if (points.Count > 1) { renderer.DrawLines(route.LineWidth, Color.Cyan, points.ToArray()); } } foreach (IMapOverlay overlay in Overlays) { DrawAtGeocode(tlGeo, brGeo, renderer, overlay.Geocode, pixelLevelZoom, adjustX + overlay.Offset.X, adjustY + overlay.Offset.Y, overlay.Drawable); } return(unavailable); }
private void RerenderMap() { mapRenderer.Draw(map, ref texture); }