Skip to content

Commit

Permalink
Added ability to render rotated sprite
Browse files Browse the repository at this point in the history
  • Loading branch information
aegis-dev committed Aug 15, 2023
1 parent 8ca9383 commit 7bf5993
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Pixie/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,45 @@ public void Sprite(in IReadOnlySprite? sprite, long x, long y, bool flip, IReadO
}
}

public void Sprite(in IReadOnlySprite? sprite, long x, long y, float angle)
{
if (sprite is null)
{
throw new ArgumentNullException(nameof(sprite));
}

float angleRadians = MathF.PI * angle / 180.0f;

float cosTheta = MathF.Cos(angleRadians);
float sinTheta = MathF.Sin(angleRadians);

uint spriteWidth = sprite.Width;
uint spriteHeight = sprite.Height;

long newWidth = (long)(Math.Abs(cosTheta) * spriteWidth + Math.Abs(sinTheta) * spriteHeight);
long newHeight = (long)(Math.Abs(cosTheta) * spriteHeight + Math.Abs(sinTheta) * spriteWidth);

long widthOffset = (newWidth - spriteWidth) / 2;
long heightOffset = (newHeight - spriteHeight) / 2;

for (long spriteX = 0; spriteX < newWidth; ++spriteX)
{
for (long spriteY = 0; spriteY < newHeight; ++spriteY)
{
long sampleX = (long)((spriteX - newWidth / 2) * cosTheta + (spriteY - newHeight / 2) * sinTheta + spriteWidth / 2);
long sampleY = (long)((-spriteX + newWidth / 2) * sinTheta + (spriteY - newHeight / 2) * cosTheta + spriteHeight / 2);

PixieColor color = sprite.GetColorAt((uint)sampleX, (uint)sampleY);
if (color == PixieColor.None)
{
continue;
}

Point(x - widthOffset + spriteX, y - heightOffset + spriteY, color);
}
}
}

public void Text(string text, long x, long y, PixieColor color, Brightness brightness)
{
Text(text, x, y, (byte)((byte)color + ((byte)brightness * (byte)PixieColor.Count)));
Expand Down

0 comments on commit 7bf5993

Please sign in to comment.