Created
June 12, 2019 22:02
-
-
Save sherjilozair/8f709e5d16face045bb7e62d141984c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void PushSprite(Rectangle dest, Rectangle src, Vector2 origin, double rotation, bool flipH = false, bool flipV = false, float depth = 0f) | |
{ | |
//if (depth <= 0.0f) | |
{ | |
//Console.WriteLine($"Depth: {depth}"); | |
} | |
float cos = (float) Math.Cos(rotation); | |
float sin = (float) Math.Sin(rotation); | |
//if (Game.Instance.Debug) | |
//Console.WriteLine($"cos/sin: {cos} {sin}"); | |
float texLeft = (float)src.Left / Texture.Width; | |
float texRight = (float)src.Right / Texture.Width; | |
float texTop = (float)src.Top / Texture.Height; | |
float texBottom = (float)src.Bottom / Texture.Height; | |
//float originCorrectionX = origin.X * dest.Width; | |
//float originCorrectionY = origin.Y * dest.Height; | |
float cornerX = -origin.X * dest.Width; | |
float cornerY = -origin.Y * dest.Height; | |
float xOffset = (-sin * cornerY) + (cos * cornerX);// + originCorrectionX; | |
float yOffset = (cos * cornerY) + (sin * cornerX);// + originCorrectionY; | |
Vertex topLeft = new Vertex { | |
x = dest.X + xOffset, | |
y = dest.Y + yOffset, | |
z = depth, | |
u = flipH ? texRight : texLeft, | |
v = flipV ? texBottom : texTop }; | |
cornerX = (1.0f - origin.X) * dest.Width; | |
cornerY = -origin.Y * dest.Height; | |
xOffset = (-sin * cornerY) + (cos * cornerX);// + originCorrectionX; | |
yOffset = (cos * cornerY) + (sin * cornerX);// + originCorrectionY; | |
Vertex topRight = new Vertex { | |
x = dest.X + xOffset, | |
y = dest.Y + yOffset, | |
z = depth, | |
u = flipH ? texLeft : texRight, | |
v = flipV ? texBottom : texTop }; | |
cornerX = (1.0f - origin.X) * dest.Width; | |
cornerY = (1.0f - origin.Y) * dest.Height; | |
xOffset = (-sin * cornerY) + (cos * cornerX);// + originCorrectionX; | |
yOffset = (cos * cornerY) + (sin * cornerX);// + originCorrectionY; | |
Vertex bottomRight = new Vertex { | |
x = dest.X + xOffset, | |
y = dest.Y + yOffset, | |
z = depth, | |
u = flipH ? texLeft : texRight, | |
v = flipV ? texTop : texBottom }; | |
cornerX = (-origin.X) * dest.Width; | |
cornerY = (1.0f - origin.Y) * dest.Height; | |
xOffset = (-sin * cornerY) + (cos * cornerX);// + originCorrectionX; | |
yOffset = (cos * cornerY) + (sin * cornerX);// + originCorrectionY; | |
Vertex bottomLeft = new Vertex { | |
x = dest.X + xOffset, | |
y = dest.Y + yOffset, | |
z = depth, | |
u = flipH ? texRight : texLeft, | |
v = flipV ? texTop : texBottom }; | |
Vertex[] newVertices = new Vertex[]{topLeft, topRight, bottomRight, bottomLeft}; | |
newVertices.CopyTo(Vertices, VerticesIndex); | |
ushort[] NewIndices = new ushort[] { | |
(ushort)(VerticesIndex + 0), (ushort)(VerticesIndex + 1), (ushort)(VerticesIndex + 2), | |
(ushort)(VerticesIndex + 2), (ushort)(VerticesIndex + 3), (ushort)(VerticesIndex + 0), | |
}; | |
NewIndices.CopyTo(Indices, IndicesIndex); | |
VerticesIndex += newVertices.Length; | |
IndicesIndex += NewIndices.Length; | |
NumSprites += 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment