コード例 #1
0
        public static Matrix GetAbsoluteRotationMatrix(this IRenderableIpso ipso)
        {
            var flipHorizontal = ipso.GetAbsoluteFlipHorizontal();

            float rotationDegrees;

            if (flipHorizontal)
            {
                rotationDegrees = -ipso.GetAbsoluteRotation();
            }
            else
            {
                rotationDegrees = ipso.GetAbsoluteRotation();
            }

            return(Matrix.CreateRotationZ(-MathHelper.ToRadians(rotationDegrees)));
        }
コード例 #2
0
        public static void Render(SystemManagers managers, SpriteRenderer spriteRenderer,
                                  IRenderableIpso ipso, Texture2D texture, Color color,
                                  Rectangle?sourceRectangle   = null,
                                  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;

            var flipHorizontal = ipso.GetAbsoluteFlipHorizontal();
            var effectiveParentFlipHorizontal = ipso.Parent?.GetAbsoluteFlipHorizontal() ?? false;

            if (flipHorizontal)
            {
                effects |= SpriteEffects.FlipHorizontally;
                //rotationInDegrees *= -1;
            }

            var rotationInRadians = MathHelper.ToRadians(rotationInDegrees);


            float leftAbsolute = ipso.GetAbsoluteX();
            float topAbsolute  = ipso.GetAbsoluteY();

            Vector2 origin = Vector2.Zero;

            //if(flipHorizontal)
            //{
            //    var offsetX = (float)System.Math.Cos(rotationInRadians);
            //    var offsetY = (float)System.Math.Sin(rotationInRadians);
            //    origin.X = 1;



            //}

            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 DEBUG
                if (textureToUse != null && textureToUse.IsDisposed)
                {
                    throw new ObjectDisposedException($"Texture is disposed.  Texture name: {textureToUse.Name}, sprite scale: {scale}, Sprite name: {ipso.Name}");
                }
#endif

                spriteRenderer.Draw(textureToUse,
                                    new Vector2(leftAbsolute, topAbsolute),
                                    sourceRectangle,
                                    modifiedColor,
                                    -rotationInRadians,
                                    origin,
                                    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)(leftAbsolute),
                    (int)(topAbsolute),
                    width,
                    height);


                spriteRenderer.Draw(textureToUse,
                                    destinationRectangle,
                                    sourceRectangle,
                                    modifiedColor,
                                    rotationInRadians,
                                    origin,
                                    effects,
                                    0,
                                    objectCausingRenering
                                    );
            }
        }