public ThreadedVertexBuffer(ThreadedGraphicsContext device, IVertexBuffer <Vertex> vertexBuffer)
 {
     this.device = device;
     bind        = vertexBuffer.Bind;
     setData1    = tuple => { var t = (Tuple <Vertex[], int>)tuple; vertexBuffer.SetData(t.Item1, t.Item2); device.ReturnVertices(t.Item1); };
     setData2    = tuple => { var t = (Tuple <IntPtr, int, int>)tuple; vertexBuffer.SetData(t.Item1, t.Item2, t.Item3); return(null); };
     dispose     = vertexBuffer.Dispose;
 }
Example #2
0
        void UpdateMap()
        {
            var nv       = 0;
            var vertices = new Vertex[rowStride * map.Bounds.Height];

            foreach (var cell in map.Cells)
            {
                GenerateTileVertices(vertices, nv, cell);
                nv += 4;
            }

            vertexBuffer.SetData(vertices, nv);
        }
Example #3
0
        public TerrainRenderer(World world, WorldRenderer wr)
        {
            this.world = world;
            this.map = world.Map;

            var tileSize = new Size( Game.CellSize, Game.CellSize );
            var tileMapping = new Cache<TileReference<ushort,byte>, Sprite>(
                x => Game.modData.SheetBuilder.Add(world.TileSet.GetBytes(x), tileSize));

            var vertices = new Vertex[4 * map.Bounds.Height * map.Bounds.Width];

            terrainSheet = tileMapping[map.MapTiles.Value[map.Bounds.Left, map.Bounds.Top]].sheet;

            int nv = 0;

            var terrainPalette = wr.Palette("terrain").Index;

            for( int j = map.Bounds.Top; j < map.Bounds.Bottom; j++ )
                for( int i = map.Bounds.Left; i < map.Bounds.Right; i++ )
                {
                    var tile = tileMapping[map.MapTiles.Value[i, j]];
                    // TODO: move GetPaletteIndex out of the inner loop.
                    Util.FastCreateQuad(vertices, Game.CellSize * new float2(i, j), tile, terrainPalette, nv, tile.size);
                    nv += 4;

                    if (tileMapping[map.MapTiles.Value[i, j]].sheet != terrainSheet)
                        throw new InvalidOperationException("Terrain sprites span multiple sheets");
                }

            vertexBuffer = Game.Renderer.Device.CreateVertexBuffer( vertices.Length );
            vertexBuffer.SetData( vertices, nv );
        }
Example #4
0
        public void Draw(Viewport viewport)
        {
            var cells = restrictToBounds ? viewport.VisibleCellsInsideBounds : viewport.AllVisibleCells;

            // Only draw the rows that are visible.
            var firstRow = cells.CandidateMapCoords.TopLeft.V.Clamp(0, map.MapSize.Y);
            var lastRow  = (cells.CandidateMapCoords.BottomRight.V + 1).Clamp(firstRow, map.MapSize.Y);

            Game.Renderer.Flush();

            // Flush any visible changes to the GPU
            for (var row = firstRow; row <= lastRow; row++)
            {
                if (!dirtyRows.Remove(row))
                {
                    continue;
                }

                var rowOffset = rowStride * row;
                vertexBuffer.SetData(vertices, rowOffset, rowOffset, rowStride);
            }

            Game.Renderer.WorldSpriteRenderer.DrawVertexBuffer(
                vertexBuffer, rowStride * firstRow, rowStride * (lastRow - firstRow),
                PrimitiveType.TriangleList, Sheet, BlendMode);

            Game.Renderer.Flush();
        }
		public void RefreshBuffer()
		{
			if (vertexBuffer != null)
				vertexBuffer.Dispose();
			vertexBuffer = Game.Renderer.CreateVertexBuffer(totalVertexCount);
			vertexBuffer.SetData(vertices.SelectMany(v => v).ToArray(), totalVertexCount);
			cachedVertexCount = totalVertexCount;
		}
Example #6
0
        public TerrainRenderer(World world, WorldRenderer wr)
        {
            this.world = world;
            this.map = world.Map;

            var terrainPalette = wr.Palette("terrain").Index;
            var vertices = new Vertex[4 * map.Bounds.Height * map.Bounds.Width];
            int nv = 0;

            for (var j = map.Bounds.Top; j < map.Bounds.Bottom; j++)
                for (var i = map.Bounds.Left; i < map.Bounds.Right; i++)
                {
                    var tile = wr.Theater.TileSprite(map.MapTiles.Value[i, j]);
                    Util.FastCreateQuad(vertices, Game.CellSize * new float2(i, j), tile, terrainPalette, nv, tile.size);
                    nv += 4;
                }

            vertexBuffer = Game.Renderer.Device.CreateVertexBuffer(vertices.Length);
            vertexBuffer.SetData(vertices, nv);
        }
Example #7
0
        public TerrainRenderer(World world, WorldRenderer wr)
        {
            this.world = world;
            this.map   = world.Map;

            var terrainPalette = wr.Palette("terrain").Index;
            var vertices       = new Vertex[4 * map.Bounds.Height * map.Bounds.Width];
            var nv             = 0;

            foreach (var cell in map.Cells)
            {
                var tile = wr.Theater.TileSprite(map.MapTiles.Value[cell]);
                var pos  = wr.ScreenPosition(map.CenterOfCell(cell)) - 0.5f * tile.size;
                Util.FastCreateQuad(vertices, pos, tile, terrainPalette, nv, tile.size);
                nv += 4;
            }

            vertexBuffer = Game.Renderer.Device.CreateVertexBuffer(vertices.Length);
            vertexBuffer.SetData(vertices, nv);
        }
Example #8
0
		public TerrainRenderer(World world, WorldRenderer wr)
		{
			this.world = world;
			this.map = world.Map;

			var terrainPalette = wr.Palette("terrain").Index;
			var vertices = new Vertex[4 * map.Bounds.Height * map.Bounds.Width];
			var nv = 0;

			foreach (var cell in map.Cells)
			{
				var tile = wr.Theater.TileSprite(map.MapTiles.Value[cell]);
				var pos = wr.ScreenPosition(map.CenterOfCell(cell)) - 0.5f * tile.size;
				Util.FastCreateQuad(vertices, pos, tile, terrainPalette, nv, tile.size);
				nv += 4;
			}

			vertexBuffer = Game.Renderer.Device.CreateVertexBuffer(vertices.Length);
			vertexBuffer.SetData(vertices, nv);
		}
Example #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="viewport"></param>
        public void Draw(GameViewPort viewport)
        {
            var cells = restrictToBounds ? viewport.VisibleCellsInsideBounds : viewport.AllVisibleCells;

            //only draw the rows that are visible
            //只绘制可见的行
            var firstRow = cells.CandidateMapCoords.TopLeft.V.Clamp(0, map.MapSize.Y);
            var lastRow  = (cells.CandidateMapCoords.BottomRight.V + 1).Clamp(firstRow, map.MapSize.Y);

            WarGame.Renderer.Flush();

            //Flush any visible changes to the GPU
            for (var row = firstRow; row <= lastRow; row++)
            {
                if (!dirtyRows.Remove(row))
                {
                    continue;
                }

                var rowOffset = rowStride * row;

                unsafe
                {
                    //The compiler / language spec won't let us calculate a pointer to an offset inside a generic array T[],
                    //and so we are forced to calculate the start-of-row pointer here to pass in to SetData.
                    fixed(Vertex *vPtr = &vertices[0])
                    {
                        vertexBuffer.SetData((IntPtr)(vPtr + rowOffset), rowOffset, rowStride);
                    }
                }
            }
            WarGame.Renderer.WorldSpriteRenderer.DrawVertexBuffer(vertexBuffer,
                                                                  rowStride * firstRow,
                                                                  rowStride * (lastRow - firstRow),
                                                                  PrimitiveType.TriangleList,
                                                                  Sheet,
                                                                  BlendMode);

            WarGame.Renderer.Flush();
        }
Example #10
0
        public TerrainRenderer(World world, WorldRenderer wr)
        {
            this.world = world;
            this.map   = world.Map;

            var terrainPalette = wr.Palette("terrain").Index;
            var vertices       = new Vertex[4 * map.Bounds.Height * map.Bounds.Width];
            int nv             = 0;

            for (var j = map.Bounds.Top; j < map.Bounds.Bottom; j++)
            {
                for (var i = map.Bounds.Left; i < map.Bounds.Right; i++)
                {
                    var tile = wr.Theater.TileSprite(map.MapTiles.Value[i, j]);
                    Util.FastCreateQuad(vertices, Game.CellSize * new float2(i, j), tile, terrainPalette, nv, tile.size);
                    nv += 4;
                }
            }

            vertexBuffer = Game.Renderer.Device.CreateVertexBuffer(vertices.Length);
            vertexBuffer.SetData(vertices, nv);
        }
Example #11
0
        public TerrainRenderer(World world, WorldRenderer wr)
        {
            this.world = world;
            this.map = world.Map;

            Size tileSize = new Size( Game.CellSize, Game.CellSize );
            var tileMapping = new Cache<TileReference<ushort,byte>, Sprite>(
                x => Game.modData.SheetBuilder.Add(world.TileSet.GetBytes(x), tileSize));

            Vertex[] vertices = new Vertex[4 * map.Bounds.Height * map.Bounds.Width];
            ushort[] indices = new ushort[6 * map.Bounds.Height * map.Bounds.Width];

            terrainSheet = tileMapping[map.MapTiles[map.Bounds.Left, map.Bounds.Top]].sheet;

            int nv = 0;
            int ni = 0;

            for( int j = map.Bounds.Top; j < map.Bounds.Bottom; j++ )
                for( int i = map.Bounds.Left; i < map.Bounds.Right; i++ )
                {
                    Sprite tile = tileMapping[map.MapTiles[i, j]];
                    // TODO: The zero below should explicitly refer to the terrain palette, but this code is called
                    // before the palettes are created. Therefore assumes that "terrain" is the first palette to be defined
                    Util.FastCreateQuad(vertices, indices, Game.CellSize * new float2(i, j), tile, 0, nv, ni, tile.size);
                    nv += 4;
                    ni += 6;

                    if (tileMapping[map.MapTiles[i, j]].sheet != terrainSheet)
                        throw new InvalidOperationException("Terrain sprites span multiple sheets");
                }

            vertexBuffer = Game.Renderer.Device.CreateVertexBuffer( vertices.Length );
            vertexBuffer.SetData( vertices, nv );

            indexBuffer = Game.Renderer.Device.CreateIndexBuffer( indices.Length );
            indexBuffer.SetData( indices, ni );
        }
Example #12
0
        public TerrainRenderer(World world, WorldRenderer wr)
        {
            this.world = world;
            this.map   = world.Map;

            var tileSize    = new Size(Game.CellSize, Game.CellSize);
            var tileMapping = new Cache <TileReference <ushort, byte>, Sprite>(
                x => Game.modData.SheetBuilder.Add(world.TileSet.GetBytes(x), tileSize));

            var vertices = new Vertex[4 * map.Bounds.Height * map.Bounds.Width];

            terrainSheet = tileMapping[map.MapTiles.Value[map.Bounds.Left, map.Bounds.Top]].sheet;

            int nv = 0;

            var terrainPalette = wr.Palette("terrain").Index;

            for (int j = map.Bounds.Top; j < map.Bounds.Bottom; j++)
            {
                for (int i = map.Bounds.Left; i < map.Bounds.Right; i++)
                {
                    var tile = tileMapping[map.MapTiles.Value[i, j]];
                    // TODO: move GetPaletteIndex out of the inner loop.
                    Util.FastCreateQuad(vertices, Game.CellSize * new float2(i, j), tile, terrainPalette, nv, tile.size);
                    nv += 4;

                    if (tileMapping[map.MapTiles.Value[i, j]].sheet != terrainSheet)
                    {
                        throw new InvalidOperationException("Terrain sprites span multiple sheets. Try increasing Game.Settings.Graphics.SheetSize.");
                    }
                }
            }

            vertexBuffer = Game.Renderer.Device.CreateVertexBuffer(vertices.Length);
            vertexBuffer.SetData(vertices, nv);
        }
Example #13
0
        public TerrainRenderer(World world, Renderer renderer, WorldRenderer wr)
        {
            this.renderer = renderer;
            this.map = world.Map;

            Size tileSize = new Size( Game.CellSize, Game.CellSize );

            var tileMapping = new Cache<TileReference, Sprite>(
                x => SheetBuilder.SharedInstance.Add(world.TileSet.GetBytes(x), tileSize));

            Vertex[] vertices = new Vertex[4 * map.Height * map.Width];
            ushort[] indices = new ushort[6 * map.Height * map.Width];

            int nv = 0;
            int ni = 0;
            for( int j = map.YOffset ; j < map.YOffset + map.Height ; j++ )
                for( int i = map.XOffset ; i < map.XOffset + map.Width; i++ )
                {
                    Sprite tile = tileMapping[map.MapTiles[i, j]];
                    // TODO: The zero below should explicitly refer to the terrain palette, but this code is called
                    // before the palettes are created
                    Util.FastCreateQuad(vertices, indices, Game.CellSize * new float2(i, j), tile, 0, nv, ni, tile.size);
                    nv += 4;
                    ni += 6;
                }

            terrainSheet = tileMapping[map.MapTiles[map.XOffset, map.YOffset]].sheet;

            vertexBuffer = renderer.Device.CreateVertexBuffer( vertices.Length );
            vertexBuffer.SetData( vertices );

            indexBuffer = renderer.Device.CreateIndexBuffer( indices.Length );
            indexBuffer.SetData( indices );

            overlayRenderer = new SmudgeRenderer( renderer, map );
        }
Example #14
0
 public void DrawBatch(Vertex[] vertices, int numVertices, PrimitiveType type)
 {
     tempBuffer.SetData(vertices, numVertices);
     DrawBatch(tempBuffer, 0, numVertices, type);
 }
Example #15
0
 public void RefreshBuffer()
 {
     vertexBuffer = Game.Renderer.Device.CreateVertexBuffer(totalVertexCount);
     vertexBuffer.SetData(vertices.SelectMany(v => v).ToArray(), totalVertexCount);
     cachedVertexCount = totalVertexCount;
 }