Beispiel #1
0
 public void Render(SpriteRenderer spriteRenderer, SystemManagers managers)
 {
     if (managers == null)
     {
         managers = SystemManagers.Default;
     }
     managers.Renderer.RenderLayer(managers, this);
 }
Beispiel #2
0
        private void RenderTiledSprite(SpriteRenderer spriteRenderer, SystemManagers managers)
        {
            float texelsWide = 0;
            float texelsTall = 0;

            int fullTexelsWide = 0;
            int fullTexelsTall = 0;

            if (this.AtlasedTexture != null)
            {
                fullTexelsWide = this.AtlasedTexture.SourceRectangle.Width;
                fullTexelsTall = this.AtlasedTexture.SourceRectangle.Height;
            }
            else
            {
                fullTexelsWide = this.Texture.Width;
                fullTexelsTall = this.Texture.Height;
            }

            texelsWide = fullTexelsWide;
            if (SourceRectangle.HasValue)
            {
                texelsWide = SourceRectangle.Value.Width;
            }
            texelsTall = fullTexelsTall;
            if (SourceRectangle.HasValue)
            {
                texelsTall = SourceRectangle.Value.Height;
            }


            float xRepetitions = texelsWide / (float)fullTexelsWide;
            float yRepetitions = texelsTall / (float)fullTexelsTall;


            if (xRepetitions > 0 && yRepetitions > 0)
            {
                float eachWidth = this.EffectiveWidth / xRepetitions;
                float eachHeight = this.EffectiveHeight / yRepetitions;

                float oldEffectiveWidth = this.EffectiveWidth;
                float oldEffectiveHeight = this.EffectiveHeight;

                // We're going to change the width, height, X, and Y of "this" to make rendering code work
                // by simply passing in the object. At the end of the drawing, we'll revert the values back
                // to what they were before rendering started.
                float oldWidth = this.Width;
                float oldHeight = this.Height;

                float oldX = this.X;
                float oldY = this.Y;

                var oldSource = this.SourceRectangle.Value;


                float texelsPerWorldUnitX = (float)fullTexelsWide / eachWidth;
                float texelsPerWorldUnitY = (float)fullTexelsTall / eachHeight;

                int oldSourceY = oldSource.Y;

                if (oldSourceY < 0)
                {
                    int amountToAdd = 1 - (oldSourceY / fullTexelsTall);

                    oldSourceY += amountToAdd * Texture.Height;
                }

                if (oldSourceY > 0)
                {
                    int amountToAdd = System.Math.Abs(oldSourceY) / fullTexelsTall;
                    oldSourceY -= amountToAdd * Texture.Height;
                }
                float currentY = -oldSourceY * (1 / texelsPerWorldUnitY);

                var matrix = this.GetRotationMatrix();

                for (int y = 0; y < yRepetitions; y++)
                {
                    float worldUnitsChoppedOffTop = System.Math.Max(0, oldSourceY * (1 / texelsPerWorldUnitY));
                    //float worldUnitsChoppedOffBottom = System.Math.Max(0, currentY + eachHeight - (int)oldEffectiveHeight);

                    float worldUnitsChoppedOffBottom = 0;

                    float extraY = yRepetitions - y;
                    if (extraY < 1)
                    {
                        worldUnitsChoppedOffBottom = System.Math.Max(0, (1 - extraY) * eachWidth);
                    }



                    int texelsChoppedOffTop = 0;
                    if (worldUnitsChoppedOffTop > 0)
                    {
                        texelsChoppedOffTop = oldSourceY;
                    }

                    int texelsChoppedOffBottom =
                        RenderingLibrary.Math.MathFunctions.RoundToInt(worldUnitsChoppedOffBottom * texelsPerWorldUnitY);

                    int sourceHeight = (int)(fullTexelsTall - texelsChoppedOffTop - texelsChoppedOffBottom);

                    if (sourceHeight == 0)
                    {
                        break;
                    }

                    this.Height = sourceHeight * 1 / texelsPerWorldUnitY;

                    int oldSourceX = oldSource.X;

                    if (oldSourceX < 0)
                    {
                        int amountToAdd = 1 - (oldSourceX / Texture.Width);

                        oldSourceX += amountToAdd * fullTexelsWide;
                    }

                    if (oldSourceX > 0)
                    {
                        int amountToAdd = System.Math.Abs(oldSourceX) / Texture.Width;

                        oldSourceX -= amountToAdd * fullTexelsWide;
                    }

                    float currentX = -oldSourceX * (1 / texelsPerWorldUnitX) + y * eachHeight * matrix.Up.X;
                    currentY = y * eachHeight * matrix.Up.Y;

                    for (int x = 0; x < xRepetitions; x++)
                    {
                        float worldUnitsChoppedOffLeft = System.Math.Max(0, oldSourceX * (1 / texelsPerWorldUnitX));
                        float worldUnitsChoppedOffRight = 0;

                        float extra = xRepetitions - x;
                        if (extra < 1)
                        {
                            worldUnitsChoppedOffRight = System.Math.Max(0, (1 - extra) * eachWidth);
                        }

                        int texelsChoppedOffLeft = 0;
                        if (worldUnitsChoppedOffLeft > 0)
                        {
                            // Let's use the hard number to not have any floating point issues:
                            //texelsChoppedOffLeft = worldUnitsChoppedOffLeft * texelsPerWorldUnit;
                            texelsChoppedOffLeft = oldSourceX;
                        }
                        int texelsChoppedOffRight =
                            RenderingLibrary.Math.MathFunctions.RoundToInt(worldUnitsChoppedOffRight * texelsPerWorldUnitX);

                        this.X = oldX + currentX + worldUnitsChoppedOffLeft;
                        this.Y = oldY + currentY + worldUnitsChoppedOffTop;

                        int sourceWidth = (int)(fullTexelsWide - texelsChoppedOffLeft - texelsChoppedOffRight);

                        if (sourceWidth == 0)
                        {
                            break;
                        }

                        this.Width = sourceWidth * 1 / texelsPerWorldUnitX;




                        if (AtlasedTexture != null)
                        {
                            var rectangle = new Rectangle(
                                AtlasedTexture.SourceRectangle.X + RenderingLibrary.Math.MathFunctions.RoundToInt(texelsChoppedOffLeft),
                                AtlasedTexture.SourceRectangle.Y + RenderingLibrary.Math.MathFunctions.RoundToInt(texelsChoppedOffTop),
                                sourceWidth,
                                sourceHeight);

                            Render(managers, spriteRenderer, this, AtlasedTexture.Texture, Color, rectangle, FlipHorizontal, FlipVertical, rotationInDegrees: Rotation);
                        }
                        else
                        {
                            this.SourceRectangle = new Rectangle(
                                RenderingLibrary.Math.MathFunctions.RoundToInt(texelsChoppedOffLeft),
                                RenderingLibrary.Math.MathFunctions.RoundToInt(texelsChoppedOffTop),
                                sourceWidth,
                                sourceHeight);

                            Render(managers, spriteRenderer, this, Texture, Color, SourceRectangle, FlipHorizontal, FlipVertical, rotationInDegrees: Rotation);
                        }
                        currentX = System.Math.Max(0, currentX);
                        currentX += this.Width * matrix.Right.X;
                        currentY += this.Width * matrix.Right.Y;

                    }
                }

                this.Width = oldWidth;
                this.Height = oldHeight;

                this.X = oldX;
                this.Y = oldY;

                this.SourceRectangle = oldSource;
            }
        }
Beispiel #3
0
        void IRenderable.Render(SpriteRenderer spriteRenderer, SystemManagers managers)
        {
            if (this.AbsoluteVisible && Width > 0 && Height > 0)
            {
                bool shouldTileByMultipleCalls = this.Wrap && (this as IRenderable).Wrap == false;
                if (shouldTileByMultipleCalls && (this.Texture != null || this.AtlasedTexture != null))
                {
                    RenderTiledSprite(spriteRenderer, managers);
                }
                else
                {
                    Rectangle? sourceRectangle = EffectiveRectangle;
                    Texture2D texture = Texture;
                    if (AtlasedTexture != null)
                    {
                        texture = AtlasedTexture.Texture;
                    }

                    Render(managers, spriteRenderer, this, texture, Color, sourceRectangle, FlipHorizontal, FlipVertical, Rotation);
                }
            }
        }
Beispiel #4
0
        void IRenderable.Render(SpriteRenderer spriteRenderer, SystemManagers managers)
        {
            if (this.AbsoluteVisible && this.Width > 0 && this.Height > 0)
            {
                Renderer renderer = null;
                if (managers == null)
                {
                    renderer = Renderer.Self;
                }
                else
                {
                    renderer = managers.Renderer;
                }

                var texture = renderer.SinglePixelTexture;
                Rectangle? sourceRect = null;
                if (mTexture != null)
                {
                    texture = mTexture;
                    sourceRect = mSourceRect;
                }

                Sprite.Render(managers, spriteRenderer, this, texture, Color, sourceRect, false, false, Rotation);
            }
        }
Beispiel #5
0
        void IRenderable.Render(SpriteRenderer spriteRenderer, SystemManagers managers)
        {
            UpdatePoints();
            if (Visible)
            {

                Texture2D textureToUse = AssociatedRenderer.SinglePixelTexture;

                if (IsDotted)
                {
                    textureToUse = AssociatedRenderer.DottedLineTexture;
                }

                mLinePrimitive.Render(spriteRenderer, managers, textureToUse, .2f * AssociatedRenderer.Camera.Zoom);
            }
        }
Beispiel #6
0
        private void RenderUsingSpriteFont(SpriteRenderer spriteRenderer)
        {

            Vector2 offset = new Vector2(this.Renderer.Camera.RenderingXOffset, Renderer.Camera.RenderingYOffset);

            float leftSide = offset.X + this.GetAbsoluteX();
            float topSide = offset.Y + this.GetAbsoluteY();

            SpriteFont font = LoaderManager.Self.DefaultFont;
            // Maybe this hasn't been loaded yet?
            if (font != null)
            {
                switch (this.VerticalAlignment)
                {
                    case Graphics.VerticalAlignment.Top:
                        offset.Y = topSide;
                        break;
                    case Graphics.VerticalAlignment.Bottom:
                        {
                            float requiredHeight = (this.mWrappedText.Count) * font.LineSpacing;

                            offset.Y = topSide + (this.Height - requiredHeight);

                            break;
                        }
                    case Graphics.VerticalAlignment.Center:
                        {
                            float requiredHeight = (this.mWrappedText.Count) * font.LineSpacing;

                            offset.Y = topSide + (this.Height - requiredHeight) / 2.0f;
                            break;
                        }
                }



                float offsetY = offset.Y;

                for (int i = 0; i < mWrappedText.Count; i++)
                {
                    offset.X = leftSide;
                    offset.Y = (int)offsetY;

                    string line = mWrappedText[i];

                    if (HorizontalAlignment == Graphics.HorizontalAlignment.Right)
                    {
                        offset.X = leftSide + (Width - font.MeasureString(line).X);
                    }
                    else if (HorizontalAlignment == Graphics.HorizontalAlignment.Center)
                    {
                        offset.X = leftSide + (Width - font.MeasureString(line).X) / 2.0f;
                    }

                    offset.X = (int)offset.X; // so we don't have half-pixels that render weird

                    spriteRenderer.DrawString(font, line, offset, Color, this);
                    offsetY += LoaderManager.Self.DefaultFont.LineSpacing;
                }
            }
        }
Beispiel #7
0
        public void Render(SpriteRenderer spriteRenderer, SystemManagers managers)
        {
            if (AbsoluteVisible)
            {
                // Moved this out of here - it's manually called by the TextManager
                // This is required because we can't update in the draw call now that
                // we're using RenderTargets
                //if (mNeedsBitmapFontRefresh)
                //{
                //    UpdateTextureToRender();
                //}
                if (RenderBoundary)
                {
                    LineRectangle.RenderLinePrimitive(mBounds, spriteRenderer, this, managers, false);
                }

                if (mTextureToRender == null)
                {
                    RenderUsingSpriteFont(spriteRenderer);
                }
                else
                {
                    RenderUsingBitmapFont(spriteRenderer, managers);
                }
            }
        }
Beispiel #8
0
        private Point DrawLines(IEnumerable<string> lines, HorizontalAlignment horizontalAlignment, object objectRequestingChange, int requiredWidth, List<int> widths, SpriteRenderer spriteRenderer)
        {
            Point point = new Point();

            int lineNumber = 0;

            foreach (string line in lines)
            {
                // scoot over to leave room for the outline
                point.X = mOutlineThickness;

                if (horizontalAlignment == HorizontalAlignment.Right)
                {
                    point.X = requiredWidth - widths[lineNumber];
                }
                else if (horizontalAlignment == HorizontalAlignment.Center)
                {
                    point.X = (requiredWidth - widths[lineNumber]) / 2;
                }

                foreach (char c in line)
                {
                    Rectangle destRect;
                    int pageIndex;
                    var sourceRect = GetCharacterRect(c, lineNumber, ref point, out destRect, out pageIndex);

                    spriteRenderer.Draw(mTextures[pageIndex], destRect, sourceRect, Color.White, objectRequestingChange);
                }
                point.X = 0;
                lineNumber++;
            }

            return point;
        }
Beispiel #9
0
        public static void RenderLinePrimitive(LinePrimitive linePrimitive, SpriteRenderer spriteRenderer, IRenderableIpso ipso, SystemManagers managers, bool isDotted)
        {
            linePrimitive.Position.X = ipso.GetAbsoluteX();
            linePrimitive.Position.Y = ipso.GetAbsoluteY();

            Renderer renderer;
            if (managers != null)
            {
                renderer = managers.Renderer;
            }
            else
            {
                renderer = Renderer.Self;
            }

            Texture2D textureToUse = renderer.SinglePixelTexture;

            if (isDotted)
            {
                textureToUse = renderer.DottedLineTexture;
            }

            linePrimitive.Render(spriteRenderer, managers, textureToUse, .2f * renderer.Camera.Zoom);
        }
Beispiel #10
0
        void IRenderable.Render(SpriteRenderer spriteRenderer, SystemManagers managers)
        {
            if (AbsoluteVisible && LocalVisible)
            {
                // todo - add rotation
                RenderLinePrimitive(mLinePrimitive, spriteRenderer, this, managers, IsDotted);

            }
        }
Beispiel #11
0
        void Render(Sprite sprite, SystemManagers managers, SpriteRenderer spriteRenderer)
        {
            var texture = sprite.Texture;
            var sourceRectangle = sprite.EffectiveRectangle;
            if (sprite.AtlasedTexture != null) texture = sprite.AtlasedTexture.Texture;

            Sprite.Render(managers, spriteRenderer, sprite, texture, sprite.Color, 
                sourceRectangle, sprite.FlipHorizontal, sprite.FlipVertical, sprite.Rotation, treat0AsFullDimensions:false);
        }
Beispiel #12
0
        void IRenderable.Render(SpriteRenderer spriteRenderer, SystemManagers managers)
        {
            if (AbsoluteVisible && Width > 0 && Height > 0)
            {
                RefreshSourceRectangles();

                RefreshSpriteDimensions();

                float y = this.GetAbsoluteY();

                mSprites[(int)NineSliceSections.TopLeft].X = this.GetAbsoluteX();
                mSprites[(int)NineSliceSections.TopLeft].Y = y;

                mSprites[(int)NineSliceSections.Top].X = mSprites[(int)NineSliceSections.TopLeft].X + mSprites[(int)NineSliceSections.TopLeft].EffectiveWidth;
                mSprites[(int)NineSliceSections.Top].Y = y;

                mSprites[(int)NineSliceSections.TopRight].X = mSprites[(int)NineSliceSections.Top].X + mSprites[(int)NineSliceSections.Top].Width;
                mSprites[(int)NineSliceSections.TopRight].Y = y;

                y = mSprites[(int)NineSliceSections.TopLeft].Y + mSprites[(int)NineSliceSections.TopLeft].EffectiveHeight;

                mSprites[(int)NineSliceSections.Left].X = this.GetAbsoluteX();
                mSprites[(int)NineSliceSections.Left].Y = y;

                mSprites[(int)NineSliceSections.Center].X = mSprites[(int)NineSliceSections.Left].X + mSprites[(int)NineSliceSections.Left].EffectiveWidth;
                mSprites[(int)NineSliceSections.Center].Y = y;

                mSprites[(int)NineSliceSections.Right].X = mSprites[(int)NineSliceSections.Center].X + mSprites[(int)NineSliceSections.Center].Width;
                mSprites[(int)NineSliceSections.Right].Y = y;

                y = mSprites[(int)NineSliceSections.Left].Y + mSprites[(int)NineSliceSections.Left].Height;

                mSprites[(int)NineSliceSections.BottomLeft].X = this.GetAbsoluteX();
                mSprites[(int)NineSliceSections.BottomLeft].Y = y;

                mSprites[(int)NineSliceSections.Bottom].X = mSprites[(int)NineSliceSections.BottomLeft].X + mSprites[(int)NineSliceSections.BottomLeft].EffectiveWidth;
                mSprites[(int)NineSliceSections.Bottom].Y = y;

                mSprites[(int)NineSliceSections.BottomRight].X = mSprites[(int)NineSliceSections.Bottom].X + mSprites[(int)NineSliceSections.Bottom].Width;
                mSprites[(int)NineSliceSections.BottomRight].Y = y;

                Render(mSprites[(int)NineSliceSections.TopLeft], managers, spriteRenderer);
                if (mSprites[(int)NineSliceSections.Center].Width > 0)
                {
                    Render(mSprites[(int)NineSliceSections.Top], managers, spriteRenderer);
                    Render(mSprites[(int)NineSliceSections.Bottom], managers, spriteRenderer);

                    if (mSprites[(int)NineSliceSections.Center].Height > 0)
                    {
                        Render(mSprites[(int)NineSliceSections.Center], managers, spriteRenderer);
                    }

                }
                if (mSprites[(int)NineSliceSections.Center].Height > 0)
                {
                    Render(mSprites[(int)NineSliceSections.Left], managers, spriteRenderer);
                    Render(mSprites[(int)NineSliceSections.Right], managers, spriteRenderer);
                }

                Render(mSprites[(int)NineSliceSections.TopRight], managers, spriteRenderer);
                Render(mSprites[(int)NineSliceSections.BottomLeft], managers, spriteRenderer);
                Render(mSprites[(int)NineSliceSections.BottomRight], managers, spriteRenderer);
            }
        }
Beispiel #13
0
        /// <summary>
        /// Used for rendering directly to screen with an atlased texture.
        /// </summary>
        public void RenderAtlasedTextureToScreen(List <string> lines, HorizontalAlignment horizontalAlignment,
                                                 float textureToRenderHeight, Color color, float rotation, float fontScale, SystemManagers managers, SpriteRenderer spriteRenderer,
                                                 object objectRequestingChange)
        {
            var        textObject = (Text)objectRequestingChange;
            var        point      = new Vector2();
            int        requiredWidth;
            int        requiredHeight;
            List <int> widths = new List <int>();

            GetRequiredWidthAndHeight(lines, out requiredWidth, out requiredHeight, widths);

            int lineNumber = 0;

            if (mCharRect == null)
            {
                mCharRect = new LineRectangle(managers);
            }

            var yoffset = 0f;

            if (textObject.VerticalAlignment == Graphics.VerticalAlignment.Center)
            {
                yoffset = (textObject.EffectiveHeight - textureToRenderHeight) / 2.0f;
            }
            else if (textObject.VerticalAlignment == Graphics.VerticalAlignment.Bottom)
            {
                yoffset = textObject.EffectiveHeight - textureToRenderHeight * fontScale;
            }

            foreach (string line in lines)
            {
                // scoot over to leave room for the outline
                point.X = mOutlineThickness;

                if (horizontalAlignment == HorizontalAlignment.Right)
                {
                    point.X = (int)(textObject.Width - widths[lineNumber] * fontScale);
                }
                else if (horizontalAlignment == HorizontalAlignment.Center)
                {
                    point.X = (int)(textObject.Width - widths[lineNumber] * fontScale) / 2;
                }

                foreach (char c in line)
                {
                    FloatRectangle destRect;
                    int            pageIndex;
                    var            sourceRect = GetCharacterRect(c, lineNumber, ref point, out destRect, out pageIndex, textObject.FontScale);

                    var origin = new Point((int)textObject.X, (int)(textObject.Y + yoffset));
                    var rotate = (float)-(textObject.Rotation * System.Math.PI / 180f);

                    var rotatingPoint = new Point(origin.X + (int)destRect.X, origin.Y + (int)destRect.Y);
                    MathFunctions.RotatePointAroundPoint(new Point(origin.X, origin.Y), ref rotatingPoint, rotate);

                    mCharRect.X      = rotatingPoint.X;
                    mCharRect.Y      = rotatingPoint.Y;
                    mCharRect.Width  = destRect.Width;
                    mCharRect.Height = destRect.Height;

                    if (textObject.Parent != null)
                    {
                        mCharRect.X += textObject.Parent.GetAbsoluteX();
                        mCharRect.Y += textObject.Parent.GetAbsoluteY();
                    }

                    Sprite.Render(managers, spriteRenderer, mCharRect, mTextures[0], color, sourceRect, false, false, rotation,
                                  treat0AsFullDimensions: false, objectCausingRenering: objectRequestingChange);
                }
                point.X = 0;
                lineNumber++;
            }
        }
Beispiel #14
0
        public void DrawTextLines(List <string> lines, HorizontalAlignment horizontalAlignment, object objectRequestingChange, int requiredWidth, List <int> widths, SpriteRenderer spriteRenderer,
                                  Color color,
                                  float xOffset = 0, float yOffset = 0, float rotation = 0, float scaleX = 1, float scaleY = 1)
        {
            var point = new Vector2();

            int lineNumber = 0;

            int xOffsetAsInt = MathFunctions.RoundToInt(xOffset);
            int yOffsetAsInt = MathFunctions.RoundToInt(yOffset);

            if (Renderer.NormalBlendState == BlendState.AlphaBlend)
            {
                // this is premultiplied, so premulitply the color value
                float multiple = color.A / 255.0f;

                color.R = (byte)(color.R * multiple);
                color.G = (byte)(color.G * multiple);
                color.B = (byte)(color.B * multiple);
            }

            var rotationRadians = MathHelper.ToRadians(rotation);

            Vector2 xAxis = Vector2.UnitX;
            Vector2 yAxis = Vector2.UnitY;

            if (rotation != 0)
            {
                xAxis.X = (float)System.Math.Cos(-rotationRadians);
                xAxis.Y = (float)System.Math.Sin(-rotationRadians);

                yAxis.X = (float)System.Math.Cos(-rotationRadians + MathHelper.PiOver2);
                yAxis.Y = (float)System.Math.Sin(-rotationRadians + MathHelper.PiOver2);
            }

            foreach (string line in lines)
            {
                // scoot over to leave room for the outline
                point.X = mOutlineThickness;

                if (horizontalAlignment == HorizontalAlignment.Right)
                {
                    point.X = scaleX * (requiredWidth - widths[lineNumber]);
                }
                else if (horizontalAlignment == HorizontalAlignment.Center)
                {
                    point.X = scaleX * (requiredWidth - widths[lineNumber]) / 2;
                }


                foreach (char c in line)
                {
                    FloatRectangle destRect;
                    int            pageIndex;
                    var            sourceRect = GetCharacterRect(c, lineNumber, ref point, out destRect, out pageIndex, scaleX);

                    var finalPosition = destRect.X * xAxis + destRect.Y * yAxis;

                    finalPosition.X += xOffset;
                    finalPosition.Y += yOffset;

                    if (Text.TextRenderingPositionMode == TextRenderingPositionMode.FreeFloating ||
                        // If rotated, need free floating positions since sprite positions will likely not line up with pixels
                        rotation != 0 ||
                        // If scaled up/down, don't use free floating
                        scaleX != 1)
                    {
                        var scale = new Vector2(scaleX, scaleY);
                        spriteRenderer.Draw(mTextures[pageIndex], finalPosition, sourceRect, color, -rotationRadians, Vector2.Zero, scale, SpriteEffects.None, 0, this);
                    }
                    else
                    {
                        // position:
                        destRect.X += xOffsetAsInt;
                        destRect.Y += yOffsetAsInt;

                        var position = new Vector2(destRect.X, destRect.Y);

                        spriteRenderer.Draw(mTextures[pageIndex], position, sourceRect, color, 0, Vector2.Zero, new Vector2(scaleX, scaleY), SpriteEffects.None, 0, this);
                    }
                }
                point.X = 0;
                lineNumber++;
            }
        }
Beispiel #15
0
        public static void Render(SystemManagers managers, SpriteRenderer spriteRenderer, IRenderableIpso ipso, Texture2D texture)
        {
            Color color = new Color(1.0f, 1.0f, 1.0f, 1.0f); // White

            Render(managers, spriteRenderer, ipso, texture, color);
        }
Beispiel #16
0
        public static void Render(SystemManagers managers, SpriteRenderer spriteRenderer,
            IRenderableIpso ipso, Texture2D texture, Color color,
            Rectangle? sourceRectangle = null,
            bool flipHorizontal = false,
            bool flipVertical = false,
            float rotationInDegrees = 0,
            bool treat0AsFullDimensions = false,
            // In the case of Text objects, we send in a line rectangle, but we want the Text object to be the owner of any resulting render states
            object objectCausingRenering = null
            )
        {
            if (objectCausingRenering == null)
            {
                objectCausingRenering = ipso;
            }

            Renderer renderer = null;
            if (managers == null)
            {
                renderer = Renderer.Self;
            }
            else
            {
                renderer = managers.Renderer;
            }

            Texture2D textureToUse = texture;

            if (textureToUse == null)
            {
                textureToUse = LoaderManager.Self.InvalidTexture;

                if (textureToUse == null)
                {
                    return;
                }
            }

            SpriteEffects effects = SpriteEffects.None;
            if (flipHorizontal)
            {
                effects |= SpriteEffects.FlipHorizontally;
            }
            if (flipVertical)
            {
                effects |= SpriteEffects.FlipVertically;
            }

            var modifiedColor = color;

            if (Renderer.NormalBlendState == BlendState.AlphaBlend)
            {
                // we are using premult textures, so we need to premult the color:
                var alphaRatio = color.A / 255.0f;

                modifiedColor.R = (byte)(color.R * alphaRatio);
                modifiedColor.G = (byte)(color.G * alphaRatio);
                modifiedColor.B = (byte)(color.B * alphaRatio);
            }

            if ((ipso.Width > 0 && ipso.Height > 0) || treat0AsFullDimensions == false)
            {
                Vector2 scale = Vector2.One;

                if (textureToUse == null)
                {
                    scale = new Vector2(ipso.Width, ipso.Height);
                }
                else
                {
                    float ratioWidth = 1;
                    float ratioHeight = 1;
                    if (sourceRectangle.HasValue)
                    {
                        ratioWidth = sourceRectangle.Value.Width / (float)textureToUse.Width;
                        ratioHeight = sourceRectangle.Value.Height / (float)textureToUse.Height;
                    }

                    scale = new Vector2(ipso.Width / (ratioWidth * textureToUse.Width),
                        ipso.Height / (ratioHeight * textureToUse.Height));
                }

                if (textureToUse != null && textureToUse.IsDisposed)
                {
                    throw new ObjectDisposedException("Texture is disposed.  Texture name: " + textureToUse.Name + ", sprite scale: " + scale);
                }

                spriteRenderer.Draw(textureToUse,
                    new Vector2(ipso.GetAbsoluteX(), ipso.GetAbsoluteY()),
                    sourceRectangle,
                    modifiedColor,
                    Microsoft.Xna.Framework.MathHelper.TwoPi * -rotationInDegrees / 360.0f,
                    Vector2.Zero,
                    scale,
                    effects,
                    0,
                    objectCausingRenering);
            }
            else
            {
                int width = textureToUse.Width;
                int height = textureToUse.Height;

                if (sourceRectangle != null && sourceRectangle.HasValue)
                {
                    width = sourceRectangle.Value.Width;
                    height = sourceRectangle.Value.Height;
                }

                Rectangle destinationRectangle = new Rectangle(
                    (int)(ipso.GetAbsoluteX()),
                    (int)(ipso.GetAbsoluteY()),
                    width,
                    height);


                spriteRenderer.Draw(textureToUse,
                    destinationRectangle,
                    sourceRectangle,
                    modifiedColor,
                    rotationInDegrees / 360.0f,
                    Vector2.Zero,
                    effects,
                    0,
                    objectCausingRenering
                    );
            }
        }
Beispiel #17
0
 /// <summary>
 /// Renders the primtive line object.
 /// </summary>
 /// <param name="spriteRenderer">The sprite renderer to use to render the primitive line object.</param>
 public void Render(SpriteRenderer spriteRenderer, SystemManagers managers)
 {
     Render(spriteRenderer, managers, mTexture, .2f);
 }
Beispiel #18
0
        /// <summary>
        /// Used for rendering directly to screen with an atlased texture.
        /// </summary>
        public void RenderAtlasedTextureToScreen(List<string> lines, HorizontalAlignment horizontalAlignment,
            float textureToRenderHeight, Color color, float rotation, float fontScale, SystemManagers managers, SpriteRenderer spriteRenderer,
            object objectRequestingChange)
        {
            var textObject = (Text)objectRequestingChange;
            var point = new Point();
            int requiredWidth;
            int requiredHeight;
            List<int> widths;
            GetRequiredWithAndHeight(lines, out requiredWidth, out requiredHeight, out widths);

            int lineNumber = 0;

            if (mCharRect == null) mCharRect = new LineRectangle(managers);

            var yoffset = 0f;
            if (textObject.VerticalAlignment == Graphics.VerticalAlignment.Center)
            {
                yoffset = (textObject.EffectiveHeight - textureToRenderHeight) / 2.0f;
            }
            else if (textObject.VerticalAlignment == Graphics.VerticalAlignment.Bottom)
            {
                yoffset = textObject.EffectiveHeight - textureToRenderHeight * fontScale;
            }

            foreach (string line in lines)
            {
                // scoot over to leave room for the outline
                point.X = mOutlineThickness;

                if (horizontalAlignment == HorizontalAlignment.Right)
                {
                    point.X = (int)(textObject.Width - widths[lineNumber] * fontScale);
                }
                else if (horizontalAlignment == HorizontalAlignment.Center)
                {
                    point.X = (int)(textObject.Width - widths[lineNumber] * fontScale) / 2;
                }

                foreach (char c in line)
                {
                    Rectangle destRect;
                    int pageIndex;
                    var sourceRect = GetCharacterRect(c, lineNumber, ref point, out destRect, out pageIndex, textObject.FontScale);

                    var origin = new Point((int)textObject.X, (int)(textObject.Y + yoffset));
                    var rotate = (float)-(textObject.Rotation * System.Math.PI / 180f);

                    var rotatingPoint = new Point(origin.X + destRect.X, origin.Y + destRect.Y);
                    MathFunctions.RotatePointAroundPoint(new Point(origin.X, origin.Y), ref rotatingPoint, rotate);

                    mCharRect.X = rotatingPoint.X;
                    mCharRect.Y = rotatingPoint.Y;
                    mCharRect.Width = destRect.Width;
                    mCharRect.Height = destRect.Height;

                    if(textObject.Parent != null)
                    {
                        mCharRect.X += textObject.Parent.GetAbsoluteX();
                        mCharRect.Y += textObject.Parent.GetAbsoluteY();
                    }

                    Sprite.Render(managers, spriteRenderer, mCharRect, mTextures[0], color, sourceRect, false, false, rotation,
                        treat0AsFullDimensions: false, objectCausingRenering: objectRequestingChange);
                }
                point.X = 0;
                lineNumber++;
            }
        }
Beispiel #19
0
        public void Render(SpriteRenderer spriteRenderer, SystemManagers managers, Texture2D textureToUse, float repetitionsPerLength)
        {
            if (mVectors.Count < 2)
                return;

            Renderer renderer;
            if (managers == null)
            {
                renderer = Renderer.Self;
            }
            else
            {
                renderer = managers.Renderer;
            }

            Vector2 offset = new Vector2(renderer.Camera.RenderingXOffset, renderer.Camera.RenderingYOffset);

            int extraStep = 0;
            if (BreakIntoSegments)
            {
                extraStep = 1;
            }
            for (int i = 1; i < mVectors.Count; i++)
            {
                Vector2 vector1 = mVectors[i - 1];
                Vector2 vector2 = mVectors[i];

                // calculate the distance between the two vectors
                float distance = Vector2.Distance(vector1, vector2);

                int repetitions = (int)(distance * repetitionsPerLength);

                if (repetitions < 1)
                {
                    repetitions = 1;
                }

                //repetitions = 128;

                // calculate the angle between the two vectors
                float angle = (float)System.Math.Atan2((double)(vector2.Y - vector1.Y),
                    (double)(vector2.X - vector1.X));

                Rectangle sourceRectangle = new Rectangle(
                    0, 
                    0, 
                    textureToUse.Width * repetitions, 
                    textureToUse.Height);

                // stretch the pixel between the two vectors
                spriteRenderer.Draw(textureToUse,
                    offset + Position + vector1,
                    sourceRectangle,
                    Color,
                    angle,
                    Vector2.Zero,
                    new Vector2(distance / ((float)repetitions * textureToUse.Width), 1/renderer.CurrentZoom),
                    SpriteEffects.None,
                    Depth,
                    this);

                i += extraStep;
            }
        }
Beispiel #20
0
        private void RenderUsingBitmapFont(SpriteRenderer spriteRenderer, SystemManagers managers)
        {
            if (mTempForRendering == null)
            {
                mTempForRendering = new LineRectangle(managers);
            }

            mTempForRendering.X = this.X;
            mTempForRendering.Y = this.Y;
            mTempForRendering.Width = this.mTextureToRender.Width * mFontScale;
            mTempForRendering.Height = this.mTextureToRender.Height * mFontScale;

            //mTempForRendering.Parent = this.Parent;

            float widthDifference = this.EffectiveWidth - mTempForRendering.Width;

            if (this.HorizontalAlignment == Graphics.HorizontalAlignment.Center)
            {
                mTempForRendering.X += widthDifference / 2.0f;
            }
            else if (this.HorizontalAlignment == Graphics.HorizontalAlignment.Right)
            {
                mTempForRendering.X += widthDifference;
            }

            if (this.VerticalAlignment == Graphics.VerticalAlignment.Center)
            {
                mTempForRendering.Y += (this.EffectiveHeight - mTextureToRender.Height) / 2.0f;
            }
            else if (this.VerticalAlignment == Graphics.VerticalAlignment.Bottom)
            {
                mTempForRendering.Y += this.EffectiveHeight - mTempForRendering.Height;
            }

            if(this.Parent != null)
            {
                mTempForRendering.X += Parent.GetAbsoluteX();
                mTempForRendering.Y += Parent.GetAbsoluteY();

            }

            if (mBitmapFont?.AtlasedTexture != null)
            {
                mBitmapFont.RenderAtlasedTextureToScreen(mWrappedText, this.HorizontalAlignment, mTextureToRender.Height,
                    new Color(mRed, mGreen, mBlue, mAlpha), Rotation, mFontScale, managers,spriteRenderer, this);
            }
            else
            {
                Sprite.Render(managers, spriteRenderer, mTempForRendering, mTextureToRender,
                    new Color(mRed, mGreen, mBlue, mAlpha), null, false, false, Rotation, treat0AsFullDimensions: false,
                    objectCausingRenering: this);
                
            }
        }
 void IRenderable.Render(SpriteRenderer spriteRenderer, SystemManagers managers)
 {
     mContainedObjectAsIpso.Render(spriteRenderer, managers);
 }
 public void Render(SpriteRenderer spriteRenderer, SystemManagers managers)
 {
     throw new NotImplementedException();
 }
Beispiel #23
0
 public void Render(SpriteRenderer spriteRenderer, SystemManagers managers)
 {
     if (Visible)
     {
         mLinePrimitive.Render(spriteRenderer, managers);
     }
 }