public void Render() { var coords0 = grid.PixelsToGridCoordinates(viewport.X, viewport.Y); var coords1 = grid.PixelsToGridCoordinates(viewport.X + viewport.Width, viewport.Y + viewport.Height); Grid.GridRange visibleRange = grid.GetRange(coords0, coords1); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.BeginScene(); if (layerStates[Layers.Selection]) { selection.Render(); } if (layerStates[Layers.Floor]) { floors.Render(visibleRange, viewport); } if (layerStates[Layers.Guide]) { guides.Render(); } device.EndScene(); device.Present(); }
public void SetFloorAt(CellData.FloorData floor, int pixelX, int pixelY) { int[] coords = grid.PixelsToGridCoordinates(pixelX + viewport.X, pixelY + viewport.Y); if (selection.IsInsideSelection(new Point(pixelX, pixelY))) { int[] startCoords = grid.PixelsToGridCoordinates(selection.IPoint.X, selection.IPoint.Y); int[] endCoords = grid.PixelsToGridCoordinates(selection.FPoint.X, selection.FPoint.Y); Grid.GridRange range = grid.GetRange(startCoords, endCoords); foreach (Grid.GridCell cell in range) { if (cell.Data == null) { cell.Data = new CellData(); } cell.Data.Floor = floor; grid.Insert(cell.Data, cell.X, cell.Y); } selection.ClearSelection(); } else { CellData data = grid.Get(coords[0], coords[1]); if (data == null) { data = new CellData(); } data.Floor = floor; grid.Insert(data, coords[0], coords[1]); } }
public void Render(Grid.GridRange range, Rectangle viewport) { // 1. Remove previous tiles. //// foreach (var cellList in visibleCells.Values) { cellList.Clear(); } // 1. Fill //// foreach (Grid.GridCell cell in range) { if (cell.Data == null) { continue; } BlobImage image = cell.Data.Floor.Image; if (!visibleCells.ContainsKey(image)) { Bitmap bitmap = image.Image; using (Stream s = new MemoryStream()) { bitmap.Save(s, ImageFormat.Png); s.Seek(0, SeekOrigin.Begin); Texture tex = Texture.FromStream(device, s, bitmap.Width, bitmap.Height, 0, Usage.None, Format.Unknown, Pool.Managed, Filter.None, Filter.None, 0); textures.Add(image, tex); visibleCells.Add(image, new List <TexturedVertex>()); } } List <TexturedVertex> currentList = visibleCells[image]; int pixelsPerCell = 8; int x = (cell.X * pixelsPerCell) - viewport.X; int y = (cell.Y * pixelsPerCell) - viewport.Y; int w = 8; int h = 8; // Calculate the (u,v) we need to use based on the tile coordinates. float scaleW = 8.0f / image.Image.Width; float scaleH = 8.0f / image.Image.Height; float uStart = cell.X * scaleW; float vStart = cell.Y * scaleH; float uEnd = uStart + scaleW; float vEnd = vStart + scaleH; // Clockwise winding // TODO: Index these vertices! argh var v0 = new TexturedVertex(new Vector4(x, y, RenderingZOffset, 1.0f), new Vector2(uStart, vStart)); var v1 = new TexturedVertex(new Vector4(x + w, y, RenderingZOffset, 1.0f), new Vector2(uEnd, vStart)); var v2 = new TexturedVertex(new Vector4(x + w, y + h, RenderingZOffset, 1.0f), new Vector2(uEnd, vEnd)); var v3 = new TexturedVertex(new Vector4(x, y, RenderingZOffset, 1.0f), new Vector2(uStart, vStart)); var v4 = new TexturedVertex(new Vector4(x + w, y + h, RenderingZOffset, 1.0f), new Vector2(uEnd, vEnd)); var v5 = new TexturedVertex(new Vector4(x, y + h, RenderingZOffset, 1.0f), new Vector2(uStart, vEnd)); currentList.Add(v0); currentList.Add(v1); currentList.Add(v2); currentList.Add(v3); currentList.Add(v4); currentList.Add(v5); } // 2. Fill buffer. //// DataStream stream = vertexBuffer.Lock(0, 0, LockFlags.Discard); foreach (var vertexList in visibleCells) { if (vertexList.Value.Count == 0) { continue; } stream.WriteRange(vertexList.Value.ToArray()); } vertexBuffer.Unlock(); // 3. Draw. //// device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Linear); device.SetStreamSource(0, vertexBuffer, 0, TexturedVertex.Size); device.VertexDeclaration = vertexDecl; int offset = 0; foreach (var vertexList in visibleCells) { var texture = textures[vertexList.Key]; device.SetTexture(0, texture); int tilesToDraw = vertexList.Value.Count / 3; device.DrawPrimitives(PrimitiveType.TriangleList, offset, tilesToDraw); offset += tilesToDraw * 3; } }