Example #1
0
        private SpriteVertex[] QuadFromSprite(Sprite sprite)
        {
            SpriteVertex[] verts = new SpriteVertex[4];

            Rectangle src = sprite.Source;
            Rectangle dest = sprite.Destination;
            var width = sprite.Texture.Description.Width;
            var height = sprite.Texture.Description.Height;

            verts[0].Position = PointToNdc(dest.Left, dest.Bottom, 0f);
            verts[1].Position = PointToNdc(dest.Left, dest.Top, 0f);
            verts[2].Position = PointToNdc(dest.Right, dest.Top, 0f);
            verts[3].Position = PointToNdc(dest.Right, dest.Bottom, 0f);

            verts[0].TexCoord = new Vector2((float)src.Left / width, (float)src.Bottom / height);
            verts[1].TexCoord = new Vector2((float)src.Left / width, (float)src.Top / height);
            verts[2].TexCoord = new Vector2((float)src.Right / width, (float)src.Top / height);
            verts[3].TexCoord = new Vector2((float)src.Right / width, (float)src.Bottom / height);

            for(int i = 0; i < verts.Length; i++) {
                verts[i].Color = sprite.Color;
            }

            var tx = 0.5f * (verts[0].Position.X + verts[3].Position.X);
            var ty = 0.5f * (verts[0].Position.Y + verts[1].Position.Y);
            var origin = new Vector2(tx, ty);
            var translate = new Vector2(0f, 0f);
            var transform = Matrix.AffineTransformation2D(1f, 0f, translate);

            for(int i = 0; i < verts.Length; ++i) {
                Vector3 position = verts[i].Position;
                position = Vector3.TransformCoordinate(position, transform);
                verts[i].Position = position;
            }

            return verts;
        }
Example #2
0
        public void DrawSprite(Sprite sprite)
        {
            if (!_SpriteCacheByResource.ContainsKey(sprite.Texture))
                _SpriteCacheByResource[sprite.Texture] = new List<Sprite>();

            _SpriteCacheByResource[sprite.Texture].Add(sprite);
        }