Ejemplo n.º 1
0
        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;
            }
        }