Skip to content

Commit

Permalink
Replaced old rotation system with new cleaner one
Browse files Browse the repository at this point in the history
  • Loading branch information
MrScautHD committed Sep 15, 2024
1 parent 5ed8013 commit 61a47bf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,70 +260,27 @@ public void DrawEmptyRectangle(RectangleF rectangle, float thickness, Vector2? o
public void DrawFilledRectangle(RectangleF rectangle, Vector2? origin = null, float rotation = 0.0F, Color? color = null) {
Vector2 finalOrigin = origin ?? Vector2.Zero;
Color finalColor = color ?? Color.White;
float finalRotation = float.DegreesToRadians(rotation);

float sin = 0;
float cos = 0;
float nOriginX = -finalOrigin.X;
float nOriginY = -finalOrigin.Y;

if (rotation != 0.0F) {
float radiansRot = float.DegreesToRadians(rotation);
sin = MathF.Sin(radiansRot);
cos = MathF.Cos(radiansRot);
}
Matrix4x4 transform = Matrix4x4.CreateRotationZ(finalRotation, new Vector3(rectangle.Position, 0));

PrimitiveVertex2D topLeft = new PrimitiveVertex2D() {
Position = rotation == 0.0F
? new Vector2(
rectangle.X - finalOrigin.X,
rectangle.Y - finalOrigin.Y)
: new Vector2(
rectangle.X + nOriginX * cos - nOriginY * sin,
rectangle.Y + nOriginX * sin + nOriginY * cos),
Position = Vector2.Transform(new Vector2(rectangle.X, rectangle.Y) - finalOrigin, transform),
Color = finalColor.ToRgbaFloat().ToVector4()
};

float x = QuadVertexTemplate[(int) VertexTemplateType.TopRight].X;
float w = rectangle.Width * x;

PrimitiveVertex2D topRight = new PrimitiveVertex2D() {
Position = rotation == 0.0F
? new Vector2(
rectangle.X - finalOrigin.X + w,
rectangle.Y - finalOrigin.Y)
: new Vector2(
rectangle.X + (nOriginX + w) * cos - nOriginY * sin,
rectangle.Y + (nOriginX + w) * sin + nOriginY * cos),
Position = Vector2.Transform(new Vector2(rectangle.X + rectangle.Width, rectangle.Y) - finalOrigin, transform),
Color = finalColor.ToRgbaFloat().ToVector4()
};

float y = QuadVertexTemplate[(int) VertexTemplateType.BottomLeft].Y;
float h = rectangle.Height * y;

PrimitiveVertex2D bottomLeft = new PrimitiveVertex2D() {
Position = rotation == 0.0F
? new Vector2(
rectangle.X - finalOrigin.X,
rectangle.Y - finalOrigin.Y + h)
: new Vector2(
rectangle.X + nOriginX * cos - (nOriginY + h) * sin,
rectangle.Y + nOriginX * sin + (nOriginY + h) * cos),
Position = Vector2.Transform(new Vector2(rectangle.X, rectangle.Y + rectangle.Height) - finalOrigin, transform),
Color = finalColor.ToRgbaFloat().ToVector4()
};

x = QuadVertexTemplate[(int) VertexTemplateType.BottomRight].X;
y = QuadVertexTemplate[(int) VertexTemplateType.BottomRight].Y;
w = rectangle.Width * x;
h = rectangle.Height * y;

PrimitiveVertex2D bottomRight = new PrimitiveVertex2D() {
Position = rotation == 0.0F
? new Vector2(
rectangle.X - finalOrigin.X + w,
rectangle.Y - finalOrigin.Y + h)
: new Vector2(
rectangle.X + (nOriginX + w) * cos - (nOriginY + h) * sin,
rectangle.Y + (nOriginX + w) * sin + (nOriginY + h) * cos),
Position = Vector2.Transform(new Vector2(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height) - finalOrigin, transform),
Color = finalColor.ToRgbaFloat().ToVector4()
};

Expand Down
59 changes: 8 additions & 51 deletions src/Bliss/CSharp/Graphics/Rendering/Batches/Sprites/SpriteBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,10 @@ public void DrawText(Font font, string text, Vector2 position, int size, float c
/// <param name="flip">Specifies how the texture should be flipped horizontally or vertically.</param>
public void DrawTexture(Texture2D texture, SamplerType samplerType, Vector2 position, Rectangle? sourceRect = null, Vector2? scale = null, Vector2? origin = null, float rotation = 0.0F, Color? color = null, SpriteFlip flip = SpriteFlip.None) {
Rectangle finalSource = sourceRect ?? new Rectangle(0, 0, (int) texture.Width, (int) texture.Height);
Color finalColor = color ?? Color.White;
Vector2 finalScale = scale ?? new Vector2(1.0F, 1.0F);
Vector2 finalOrigin = origin ?? new Vector2(0.0F, 0.0F);
float finalRotation = float.DegreesToRadians(rotation);
Color finalColor = color ?? Color.White;

Vector2 spriteScale = new Vector2(finalSource.Width, finalSource.Height) * finalScale;
Vector2 spriteOrigin = finalOrigin * finalScale;
Expand All @@ -298,79 +299,35 @@ public void DrawTexture(Texture2D texture, SamplerType samplerType, Vector2 posi

bool flipX = flip == SpriteFlip.Horizontal || flip == SpriteFlip.Both;
bool flipY = flip == SpriteFlip.Vertical || flip == SpriteFlip.Both;

float sin = 0;
float cos = 0;
float nOriginX = -spriteOrigin.X;
float nOriginY = -spriteOrigin.Y;

if (rotation != 0.0F) {
float radiansRot = float.DegreesToRadians(rotation);
sin = MathF.Sin(radiansRot);
cos = MathF.Cos(radiansRot);
}

Matrix4x4 transform = Matrix4x4.CreateRotationZ(finalRotation, new Vector3(position, 0));

SpriteVertex2D topLeft = new SpriteVertex2D() {
Position = rotation == 0.0F
? new Vector2(
position.X - spriteOrigin.X,
position.Y - spriteOrigin.Y)
: new Vector2(
position.X + nOriginX * cos - nOriginY * sin,
position.Y + nOriginX * sin + nOriginY * cos),
Position = Vector2.Transform(new Vector2(position.X, position.Y) - spriteOrigin, transform),
TexCoords = new Vector2(
flipX ? (finalSource.X + finalSource.Width) * texelWidth : finalSource.X * texelWidth,
flipY ? (finalSource.Y + finalSource.Height) * texelHeight : finalSource.Y * texelHeight),
Color = finalColor.ToRgbaFloat().ToVector4()
};

float x = VertexTemplate[(int) VertexTemplateType.TopRight].X;
float w = spriteScale.X * x;

SpriteVertex2D topRight = new SpriteVertex2D() {
Position = rotation == 0.0F
? new Vector2(
(position.X - spriteOrigin.X) + w,
position.Y - spriteOrigin.Y)
: new Vector2(
position.X + (nOriginX + w) * cos - nOriginY * sin,
position.Y + (nOriginX + w) * sin + nOriginY * cos),
Position = Vector2.Transform(new Vector2(position.X + spriteScale.X, position.Y) - spriteOrigin, transform),
TexCoords = new Vector2(
flipX ? finalSource.X * texelWidth : (finalSource.X + finalSource.Width) * texelWidth,
flipY ? (finalSource.Y + finalSource.Height) * texelHeight : finalSource.Y * texelHeight),
Color = finalColor.ToRgbaFloat().ToVector4()
};

float y = VertexTemplate[(int) VertexTemplateType.BottomLeft].Y;
float h = spriteScale.Y * y;

SpriteVertex2D bottomLeft = new SpriteVertex2D() {
Position = rotation == 0.0F
? new Vector2(
position.X - spriteOrigin.X,
position.Y - spriteOrigin.Y + h)
: new Vector2(
position.X + nOriginX * cos - (nOriginY + h) * sin,
position.Y + nOriginX * sin + (nOriginY + h) * cos),
Position = Vector2.Transform(new Vector2(position.X, position.Y + spriteScale.Y) - spriteOrigin, transform),
TexCoords = new Vector2(
flipX ? (finalSource.X + finalSource.Width) * texelWidth : finalSource.X * texelWidth,
flipY ? finalSource.Y * texelHeight : (finalSource.Y + finalSource.Height) * texelHeight),
Color = finalColor.ToRgbaFloat().ToVector4()
};

x = VertexTemplate[(int) VertexTemplateType.BottomRight].X;
y = VertexTemplate[(int) VertexTemplateType.BottomRight].Y;
w = spriteScale.X * x;
h = spriteScale.Y * y;

SpriteVertex2D bottomRight = new SpriteVertex2D() {
Position = rotation == 0.0F
? new Vector2(
position.X - spriteOrigin.X + w,
position.Y - spriteOrigin.Y + h)
: new Vector2(
position.X + (nOriginX + w) * cos - (nOriginY + h) * sin,
position.Y + (nOriginX + w) * sin + (nOriginY + h) * cos),
Position = Vector2.Transform(new Vector2(position.X + spriteScale.X, position.Y + spriteScale.Y) - spriteOrigin, transform),
TexCoords = new Vector2(
flipX ? finalSource.X * texelWidth : (finalSource.X + finalSource.Width) * texelWidth,
flipY ? finalSource.Y * texelHeight : (finalSource.Y + finalSource.Height) * texelHeight),
Expand Down

0 comments on commit 61a47bf

Please sign in to comment.