public static void RenderSprite(Sprite sprite, int leftPixel, int topPixel, int pixelWidth, int pixelHeight, ImageData imageData)
{
if (sprite.Texture == null || sprite.BlendOperation != BlendOperation.Regular)
{
// for now throw an exception, later we may want to handle pure color rendering and stuff like that
throw new NotImplementedException();
}
ImageData spriteTextureImageData = ImageData.FromTexture2D(sprite.Texture);
#if FRB_MDX
ColorOperation colorOperation = GraphicalEnumerations.TranslateTextureOperationToColorOperation(sprite.ColorOperation);
#else
ColorOperation colorOperation = sprite.ColorOperation;
#endif
spriteTextureImageData.ApplyColorOperation(colorOperation, sprite.Red, sprite.Green, sprite.Blue, sprite.Alpha);
int rightBound = System.Math.Min(imageData.Width, leftPixel + pixelWidth);
int bottomBound = System.Math.Min(imageData.Height, topPixel + pixelHeight);
int actualWidth = rightBound - leftPixel;
int actualHeight = bottomBound - topPixel;
for (int destinationX = leftPixel; destinationX < rightBound; destinationX++)
{
for (int destinationY = topPixel; destinationY < bottomBound; destinationY++)
{
int sourcePixelX = spriteTextureImageData.Width * (destinationX - leftPixel) / pixelWidth;
int sourcePixelY = spriteTextureImageData.Height * (destinationY - topPixel) / pixelHeight;
Color sourcePixel = spriteTextureImageData.GetPixelColor(sourcePixelX, sourcePixelY);
if (sourcePixel.A != 255)
{
Color destinationPixel = imageData.GetPixelColor(destinationX, destinationY);
#if FRB_MDX
sourcePixel = Color.FromArgb(
System.Math.Max(sourcePixel.A, destinationPixel.A),
(byte)(destinationPixel.R * (255 - sourcePixel.A) / 255.0f + sourcePixel.R * (sourcePixel.A) / 255.0f),
(byte)(destinationPixel.G * (255 - sourcePixel.A) / 255.0f + sourcePixel.G * (sourcePixel.A) / 255.0f),
(byte)(destinationPixel.B * (255 - sourcePixel.A) / 255.0f + sourcePixel.B * (sourcePixel.A) / 255.0f));
// This is probably not accurate, but will work currently. Eventually we may want to look at how blending is actually performed
#else
sourcePixel.R = (byte)(destinationPixel.R * (255 - sourcePixel.A) / 255.0f + sourcePixel.R * (sourcePixel.A) / 255.0f);
sourcePixel.G = (byte)(destinationPixel.G * (255 - sourcePixel.A) / 255.0f + sourcePixel.G * (sourcePixel.A) / 255.0f);
sourcePixel.B = (byte)(destinationPixel.B * (255 - sourcePixel.A) / 255.0f + sourcePixel.B * (sourcePixel.A) / 255.0f);
// This is probably not accurate, but will work currently. Eventually we may want to look at how blending is actually performed
sourcePixel.A = System.Math.Max(sourcePixel.A, destinationPixel.A);
#endif
}
imageData.SetPixel(destinationX, destinationY, sourcePixel);
}
}
}