From 7bf599336398a963f283e6493f64a7df0c787c2d Mon Sep 17 00:00:00 2001 From: Egidijus Lileika Date: Tue, 15 Aug 2023 13:44:52 +0300 Subject: [PATCH] Added ability to render rotated sprite --- Pixie/Renderer.cs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Pixie/Renderer.cs b/Pixie/Renderer.cs index e4be210..d552a14 100644 --- a/Pixie/Renderer.cs +++ b/Pixie/Renderer.cs @@ -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)));