Example #1
0
        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);
                }
            }
        }
Example #2
0
        public void ApplyColorOperation(ColorOperation colorOperation, float red, float green, float blue, float alpha)
        {
            Color appliedColor;

#if FRB_MDX
            // passed values from MDX will be 0-255 instead of 0-1 so we simply cast into a byte (Justin 5/15/2012)
            appliedColor = Color.FromArgb(
                (byte)FlatRedBall.Math.MathFunctions.RoundToInt(alpha),
                (byte)FlatRedBall.Math.MathFunctions.RoundToInt(red),
                (byte)FlatRedBall.Math.MathFunctions.RoundToInt(green),
                (byte)FlatRedBall.Math.MathFunctions.RoundToInt(blue)
                );
#else
            // passed values from XNA will be 0-1, use the float constructor to create a color object (Justin 5/15/2012)
            appliedColor = new Color(red, green, blue, alpha);
#endif
            ApplyColorOperation(colorOperation, appliedColor);
        }
        public WorldAxesDisplay()
        {
            mXAxis = ShapeManager.AddLine();
            mXAxis.RelativePoint1 = new Point3D(0, 1000000);
            mXAxis.RelativePoint2 = new Point3D(0, -1000000);

            mYAxis = ShapeManager.AddLine();
            mYAxis.RelativePoint1 = new Point3D(1000000, 0);
            mYAxis.RelativePoint2 = new Point3D(-1000000, 0);

#if FRB_XNA
            mXAxis.Color = new Color(40, 40, 40, 255);
            mYAxis.Color = new Color(40, 40, 40, 255);
#else
            mXAxis.Color = Color.FromArgb(255, 40, 40, 40);
            mYAxis.Color = Color.FromArgb(255, 40, 40, 40);
#endif
        }
Example #4
0
        public void ApplyColorOperation(ColorOperation colorOperation, Color appliedColor)
        {
            Color baseColor;

            switch (colorOperation)
            {
            case ColorOperation.Add:
                for (int x = 0; x < Width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        baseColor = GetPixelColor(x, y);
#if FRB_MDX
                        // System.Drawing.Color doesn't have setters for A, R, G, B
                        // so we have to do it a more inefficient way in FRB MDX
                        Color combinedColor = Color.FromArgb(
                            baseColor.A,
                            (byte)System.Math.Min((baseColor.R + appliedColor.R), 255),
                            (byte)System.Math.Min((baseColor.G + appliedColor.G), 255),
                            (byte)System.Math.Min((baseColor.B + appliedColor.B), 255));
                        baseColor = combinedColor;
#elif XNA4
                        baseColor.R = (byte)(System.Math.Min((baseColor.R + appliedColor.R), 255) * baseColor.A / 255);
                        baseColor.G = (byte)(System.Math.Min((baseColor.G + appliedColor.G), 255) * baseColor.A / 255);
                        baseColor.B = (byte)(System.Math.Min((baseColor.B + appliedColor.B), 255) * baseColor.A / 255);
#else
                        baseColor.R = (byte)System.Math.Min((baseColor.R + appliedColor.R), 255);
                        baseColor.G = (byte)System.Math.Min((baseColor.G + appliedColor.G), 255);
                        baseColor.B = (byte)System.Math.Min((baseColor.B + appliedColor.B), 255);
#endif
                        SetPixel(x, y, baseColor);
                    }
                }
                break;

            case ColorOperation.Modulate:

                // Justin Johnson - May 15, 2012 - pre-multiply so we don't calculate every iteration (Justin 5/15/2012)
                float red   = appliedColor.R / 255f;
                float green = appliedColor.G / 255f;
                float blue  = appliedColor.B / 255f;

                for (int x = 0; x < Width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        baseColor = GetPixelColor(x, y);
#if FRB_MDX
                        // System.Drawing.Color doesn't have setters for A, R, G, B
                        // so we have to do it a more inefficient way in FRB MDX
                        Color combinedColor = Color.FromArgb(
                            baseColor.A,
                            (byte)(baseColor.R * red),
                            (byte)(baseColor.G * green),
                            (byte)(baseColor.B * blue));
                        baseColor = combinedColor;
#else
                        baseColor.R = (byte)(baseColor.R * red);
                        baseColor.G = (byte)(baseColor.G * green);
                        baseColor.B = (byte)(baseColor.B * blue);
#endif
                        SetPixel(x, y, baseColor);
                    }
                }
                break;

            case ColorOperation.Texture:
                // no-op
                break;

            default:
                throw new NotImplementedException();
            }
        }