Nez.Portable/Graphics/Batcher/Batcher.cs
/// <summary>
/// Subtexture alternative to the old SpriteBatch pushSprite
/// </summary>
Subtexture alternative to the old SpriteBatch pushSprite
Subtexture alternative to the old SpriteBatch pushSprite
[MethodImpl(MethodImplOptions.AggressiveInlining)]
        void pushSprite(
            Subtexture subtexture,
            float destinationX,
            float destinationY,
            float destinationW,
            float destinationH,
            Color color,
            Vector2 origin,
            float rotation,
            float depth,
            byte effects,
            float skewTopX,
            float skewBottomX,
            float skewLeftY,
            float skewRightY)
        {
            if (_numSprites >= MAX_SPRITES)
                flushBatch();
            var originX = (origin.X / subtexture.uvs.width) / subtexture.texture2D.Width;
            var originY = (origin.Y / subtexture.uvs.height) / subtexture.texture2D.Height;
            destinationW *= subtexture.sourceRect.Width;
            destinationH *= subtexture.sourceRect.Height;
            float rotationMatrix1X;
            float rotationMatrix1Y;
            float rotationMatrix2X;
            float rotationMatrix2Y;
            if (!Mathf.withinEpsilon(rotation, 0))
            {
                var sin = Mathf.sin(rotation);
                var cos = Mathf.cos(rotation);
                rotationMatrix1X = cos;
                rotationMatrix1Y = sin;
                rotationMatrix2X = -sin;
                rotationMatrix2Y = cos;
            }
            else
            {
                rotationMatrix1X = 1.0f;
                rotationMatrix1Y = 0.0f;
                rotationMatrix2X = 0.0f;
                rotationMatrix2Y = 1.0f;
            }
            if (effects != 0)
            {
                skewTopX *= -1;
                skewBottomX *= -1;
                skewLeftY *= -1;
                skewRightY *= -1;
            }
            var cornerX = (_cornerOffsetX[0] - originX) * destinationW + skewTopX;
            var cornerY = (_cornerOffsetY[0] - originY) * destinationH - skewLeftY;
            _vertexInfo[_numSprites].position0.X =
                ((rotationMatrix2X * cornerY) + (rotationMatrix1X * cornerX) + destinationX);
            _vertexInfo[_numSprites].position0.Y =
                ((rotationMatrix2Y * cornerY) + (rotationMatrix1Y * cornerX) + destinationY);
            cornerX = (_cornerOffsetX[1] - originX) * destinationW + skewTopX;
            cornerY = (_cornerOffsetY[1] - originY) * destinationH - skewRightY;
            _vertexInfo[_numSprites].position1.X =
                ((rotationMatrix2X * cornerY) + (rotationMatrix1X * cornerX) + destinationX);
            _vertexInfo[_numSprites].position1.Y =
                ((rotationMatrix2Y * cornerY) + (rotationMatrix1Y * cornerX) + destinationY);
            cornerX = (_cornerOffsetX[2] - originX) * destinationW + skewBottomX;
            cornerY = (_cornerOffsetY[2] - originY) * destinationH - skewLeftY;
            _vertexInfo[_numSprites].position2.X =
                ((rotationMatrix2X * cornerY) + (rotationMatrix1X * cornerX) + destinationX);
            _vertexInfo[_numSprites].position2.Y =
                ((rotationMatrix2Y * cornerY) + (rotationMatrix1Y * cornerX) + destinationY);
            cornerX = (_cornerOffsetX[3] - originX) * destinationW + skewBottomX;
            cornerY = (_cornerOffsetY[3] - originY) * destinationH - skewRightY;
            _vertexInfo[_numSprites].position3.X =
                ((rotationMatrix2X * cornerY) + (rotationMatrix1X * cornerX) + destinationX);
            _vertexInfo[_numSprites].position3.Y =
                ((rotationMatrix2Y * cornerY) + (rotationMatrix1Y * cornerX) + destinationY);
            _vertexInfo[_numSprites].textureCoordinate0.X =
                (_cornerOffsetX[0 ^ effects] * subtexture.uvs.width) + subtexture.uvs.x;
            _vertexInfo[_numSprites].textureCoordinate0.Y =
                (_cornerOffsetY[0 ^ effects] * subtexture.uvs.height) + subtexture.uvs.y;
            _vertexInfo[_numSprites].textureCoordinate1.X =
                (_cornerOffsetX[1 ^ effects] * subtexture.uvs.width) + subtexture.uvs.x;
            _vertexInfo[_numSprites].textureCoordinate1.Y =
                (_cornerOffsetY[1 ^ effects] * subtexture.uvs.height) + subtexture.uvs.y;
            _vertexInfo[_numSprites].textureCoordinate2.X =
                (_cornerOffsetX[2 ^ effects] * subtexture.uvs.width) + subtexture.uvs.x;
            _vertexInfo[_numSprites].textureCoordinate2.Y =
                (_cornerOffsetY[2 ^ effects] * subtexture.uvs.height) + subtexture.uvs.y;
            _vertexInfo[_numSprites].textureCoordinate3.X =
                (_cornerOffsetX[3 ^ effects] * subtexture.uvs.width) + subtexture.uvs.x;
            _vertexInfo[_numSprites].textureCoordinate3.Y =
                (_cornerOffsetY[3 ^ effects] * subtexture.uvs.height) + subtexture.uvs.y;
            _vertexInfo[_numSprites].position0.Z = depth;
            _vertexInfo[_numSprites].position1.Z = depth;
            _vertexInfo[_numSprites].position2.Z = depth;
            _vertexInfo[_numSprites].position3.Z = depth;
            _vertexInfo[_numSprites].color0 = color;
            _vertexInfo[_numSprites].color1 = color;
            _vertexInfo[_numSprites].color2 = color;
            _vertexInfo[_numSprites].color3 = color;
            if (_disableBatching)
            {
                _vertexBuffer.SetData(
                    0,
                    _vertexInfo,
                    0,
                    1,
                    VertexPositionColorTexture4.realStride,
                    SetDataOptions.None);
                drawPrimitives(subtexture, 0, 1);
            }
            else
            {
                _textureInfo[_numSprites] = subtexture;
                _numSprites += 1;
            }
        }
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ] void pushSprite ( Subtexture subtexture , float destinationX , float destinationY , float destinationW , float destinationH , Color color , Vector2 origin , float rotation , float depth , byte effects , float skewTopX , float skewBottomX , float skewLeftY , float skewRightY ) { if ( _numSprites >= MAX_SPRITES ) flushBatch ( ) ; var originX = ( origin . X / subtexture . uvs . width ) / subtexture . texture2D . Width ; var originY = ( origin . Y / subtexture . uvs . height ) / subtexture . texture2D . Height ; destinationW *= subtexture . sourceRect . Width ; destinationH *= subtexture . sourceRect . Height ; float rotationMatrix1X ; float rotationMatrix1Y ; float rotationMatrix2X ; float rotationMatrix2Y ; if ( ! Mathf . withinEpsilon ( rotation , 0 ) ) { var sin = Mathf . sin ( rotation ) ; var cos = Mathf . cos ( rotation ) ; rotationMatrix1X = cos ; rotationMatrix1Y = sin ; rotationMatrix2X = - sin ; rotationMatrix2Y = cos ; } else { rotationMatrix1X = 1.0f ; rotationMatrix1Y = 0.0f ; rotationMatrix2X = 0.0f ; rotationMatrix2Y = 1.0f ; } if ( effects != 0 ) { skewTopX *= - 1 ; skewBottomX *= - 1 ; skewLeftY *= - 1 ; skewRightY *= - 1 ; } var cornerX = ( _cornerOffsetX [ 0 ] - originX ) * destinationW + skewTopX ; var cornerY = ( _cornerOffsetY [ 0 ] - originY ) * destinationH - skewLeftY ; _vertexInfo [ _numSprites ] . position0 . X = ( ( rotationMatrix2X * cornerY ) + ( rotationMatrix1X * cornerX ) + destinationX ) ; _vertexInfo [ _numSprites ] . position0 . Y = ( ( rotationMatrix2Y * cornerY ) + ( rotationMatrix1Y * cornerX ) + destinationY ) ; cornerX = ( _cornerOffsetX [ 1 ] - originX ) * destinationW + skewTopX ; cornerY = ( _cornerOffsetY [ 1 ] - originY ) * destinationH - skewRightY ; _vertexInfo [ _numSprites ] . position1 . X = ( ( rotationMatrix2X * cornerY ) + ( rotationMatrix1X * cornerX ) + destinationX ) ; _vertexInfo [ _numSprites ] . position1 . Y = ( ( rotationMatrix2Y * cornerY ) + ( rotationMatrix1Y * cornerX ) + destinationY ) ; cornerX = ( _cornerOffsetX [ 2 ] - originX ) * destinationW + skewBottomX ; cornerY = ( _cornerOffsetY [ 2 ] - originY ) * destinationH - skewLeftY ; _vertexInfo [ _numSprites ] . position2 . X = ( ( rotationMatrix2X * cornerY ) + ( rotationMatrix1X * cornerX ) + destinationX ) ; _vertexInfo [ _numSprites ] . position2 . Y = ( ( rotationMatrix2Y * cornerY ) + ( rotationMatrix1Y * cornerX ) + destinationY ) ; cornerX = ( _cornerOffsetX [ 3 ] - originX ) * destinationW + skewBottomX ; cornerY = ( _cornerOffsetY [ 3 ] - originY ) * destinationH - skewRightY ; _vertexInfo [ _numSprites ] . position3 . X = ( ( rotationMatrix2X * cornerY ) + ( rotationMatrix1X * cornerX ) + destinationX ) ; _vertexInfo [ _numSprites ] . position3 . Y = ( ( rotationMatrix2Y * cornerY ) + ( rotationMatrix1Y * cornerX ) + destinationY ) ; _vertexInfo [ _numSprites ] . textureCoordinate0 . X = ( _cornerOffsetX [ 0 ^ effects ] * subtexture . uvs . width ) + subtexture . uvs . x ; _vertexInfo [ _numSprites ] . textureCoordinate0 . Y = ( _cornerOffsetY [ 0 ^ effects ] * subtexture . uvs . height ) + subtexture . uvs . y ; _vertexInfo [ _numSprites ] . textureCoordinate1 . X = ( _cornerOffsetX [ 1 ^ effects ] * subtexture . uvs . width ) + subtexture . uvs . x ; _vertexInfo [ _numSprites ] . textureCoordinate1 . Y = ( _cornerOffsetY [ 1 ^ effects ] * subtexture . uvs . height ) + subtexture . uvs . y ; _vertexInfo [ _numSprites ] . textureCoordinate2 . X = ( _cornerOffsetX [ 2 ^ effects ] * subtexture . uvs . width ) + subtexture . uvs . x ; _vertexInfo [ _numSprites ] . textureCoordinate2 . Y = ( _cornerOffsetY [ 2 ^ effects ] * subtexture . uvs . height ) + subtexture . uvs . y ; _vertexInfo [ _numSprites ] . textureCoordinate3 . X = ( _cornerOffsetX [ 3 ^ effects ] * subtexture . uvs . width ) + subtexture . uvs . x ; _vertexInfo [ _numSprites ] . textureCoordinate3 . Y = ( _cornerOffsetY [ 3 ^ effects ] * subtexture . uvs . height ) + subtexture . uvs . y ; _vertexInfo [ _numSprites ] . position0 . Z = depth ; _vertexInfo [ _numSprites ] . position1 . Z = depth ; _vertexInfo [ _numSprites ] . position2 . Z = depth ; _vertexInfo [ _numSprites ] . position3 . Z = depth ; _vertexInfo [ _numSprites ] . color0 = color ; _vertexInfo [ _numSprites ] . color1 = color ; _vertexInfo [ _numSprites ] . color2 = color ; _vertexInfo [ _numSprites ] . color3 = color ; if ( _disableBatching ) { _vertexBuffer . SetData ( 0 , _vertexInfo , 0 , 1 , VertexPositionColorTexture4 . realStride , SetDataOptions . None ) ; drawPrimitives ( subtexture , 0 , 1 ) ; } else { _textureInfo [ _numSprites ] = subtexture ; _numSprites += 1 ; } }
Subtexture alternative to the old SpriteBatch pushSprite
Subtexture alternative to the old SpriteBatch pushSprite
// out of space, flush // Source/Destination/Origin Calculations. destinationW/H is the scale value so we multiply by the size of the texture region // Rotation Calculations // flip our skew values if we have a flipped sprite // calculate vertices // top-left // top-right // bottom-left // bottom-right
subtexture
Subtexture
destinationX
float
destinationY
float
destinationW
float
destinationH
float
color
Color
origin
Vector2
rotation
float
depth
float
effects
byte
skewTopX
float
skewBottomX
float
skewLeftY
float
skewRightY
float