Ejemplo n.º 1
0
        protected override void Draw(Vector2 position, float rotation, Vector2 scale, ImageFlip flip, Color color, Vector2 scroll, Shader shader, IShaderParameters shaderParameters, Vector2 origin, float layerDepth)
        {
            if (Columns == 0 || Rows == 0 || TileSize.Area == 0)
            {
                return;
            }

            BasicShader bs = Game.Instance.BasicShader;

            // transformations
            bs.World = Microsoft.Xna.Framework.Matrix.CreateScale(scale.X, scale.Y, 1f)
                       * Microsoft.Xna.Framework.Matrix.CreateTranslation(-origin.X, -origin.Y, 0f)
                       * Microsoft.Xna.Framework.Matrix.CreateRotationZ(Math.ToRadians(rotation))
                       * Microsoft.Xna.Framework.Matrix.CreateTranslation(position.X, position.Y, 0f)
                       * Renderer.World;

            bs.View       = Renderer.View;
            bs.Projection = Renderer.Projection;

            // material
            bs.SetMaterial(color, Opacity);

            GraphicsDevice device = Game.Instance.GraphicsDevice;

            // texture
            bs.TextureEnabled = false;

            shaderParameters?.ApplyParameters(shader);

            // we need to manually update every GraphicsDevice states here
            device.BlendState        = Renderer.Batch.BlendState;
            device.SamplerStates[0]  = Renderer.Batch.SamplerState;
            device.DepthStencilState = Renderer.Batch.DepthStencilState;
            device.RasterizerState   = Renderer.Batch.RasterizerState;

            // grid
            foreach (object pass in bs)
            {
                device.Indices = _indexBuffer;
                device.SetVertexBuffer(_vertexBuffer);
                device.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, _vertexBuffer.VertexCount, 0, Columns - 1 + (Rows - 1));
            }

            // borders
            if (_useBorderColor)
            {
                bs.SetMaterial(BorderColor, Opacity);
            }

            shaderParameters?.ApplyParameters(shader);

            // we need to manually update every GraphicsDevice states here
            device.BlendState        = Renderer.Batch.BlendState;
            device.SamplerStates[0]  = Renderer.Batch.SamplerState;
            device.DepthStencilState = Renderer.Batch.DepthStencilState;
            device.RasterizerState   = Renderer.Batch.RasterizerState;

            foreach (object pass in bs)
            {
                device.Indices = _indexBuffer;
                device.SetVertexBuffer(_vertexBuffer);
                device.DrawIndexedPrimitives(PrimitiveType.LineList, _usingVerticesCount - 4, 0, _vertexBuffer.VertexCount, _usingIndicesCount - 8, 4);
            }

            bs.ResetParameters();
        }
Ejemplo n.º 2
0
        private void RenderHollowItems(GraphicsDevice graphicsDevice, ref Matrix world, ref Matrix view, ref Matrix projection)
        {
            if (_batchHollowItems.Count == 0)
            {
                return;
            }

            if (_vertexBuffer == null || _vertexBuffer.VertexCount < _hollowVerticesCount)
            {
                _vertexBuffer = new DynamicVertexBuffer(graphicsDevice, VertexPositionColorTexture.VertexDeclaration, _hollowVerticesCount, BufferUsage.WriteOnly);
            }

            if (_indexBuffer == null || _indexBuffer.IndexCount < _hollowIndicesCount)
            {
                _indexBuffer = new DynamicIndexBuffer(graphicsDevice, IndexElementSize.ThirtyTwoBits, _hollowIndicesCount, BufferUsage.WriteOnly);
            }

            int vertexId = 0,
                indexId  = 0;

            int[] indices = new int[_hollowIndicesCount];
            VertexPositionColorTexture[] vertices = new VertexPositionColorTexture[_hollowVerticesCount];

            foreach (PrimitiveBatchItem batchItem in _batchHollowItems)
            {
                System.Array.Copy(batchItem.VertexData, 0, vertices, vertexId, batchItem.VertexData.Length);

                for (int i = 0; i < batchItem.IndexData.Length; i++)
                {
                    indices[indexId + i] = vertexId + batchItem.IndexData[i];
                }

                vertexId += batchItem.VertexData.Length;
                indexId  += batchItem.IndexData.Length;
            }

            _vertexBuffer.SetData(
                0,
                vertices,
                0,
                vertices.Length,
                _vertexBuffer.VertexDeclaration.VertexStride,
                SetDataOptions.Discard
                );

            _indexBuffer.SetData(
                0,
                indices,
                0,
                indices.Length,
                SetDataOptions.Discard
                );


            BasicShader shader = Shader as BasicShader;

            shader.ResetParameters();

            // transformations
            shader.World      = world;
            shader.View       = view;
            shader.Projection = projection;

            shader.DiffuseColor = Color.White;
            shader.Alpha        = 1f;

            shader.TextureEnabled = false;

            graphicsDevice.RasterizerState   = RasterizerState.CullNone;
            graphicsDevice.DepthStencilState = DepthStencilState.None;

            foreach (object pass in shader)
            {
                graphicsDevice.Indices = _indexBuffer;
                graphicsDevice.SetVertexBuffer(_vertexBuffer);
                graphicsDevice.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, _vertexBuffer.VertexCount, 0, _hollowIndicesCount / 2);
            }

            shader.ResetParameters();

#if DEBUG
            TotalHollowDrawCalls++;
#endif
        }