fromFloats() public static method

returns a rectangle from the passed in floats
public static fromFloats ( float x, float y, float width, float height ) : Rectangle
x float The x coordinate.
y float The y coordinate.
width float Width.
height float Height.
return Microsoft.Xna.Framework.Rectangle
Esempio n. 1
0
        public override void Render(Graphics graphics, Camera camera)
        {
            var topLeft         = Entity.transform.position + localOffset;
            var destinationRect = RectangleExt.fromFloats(topLeft.X, topLeft.Y, _sourceRect.Width * Entity.transform.scale.X * TextureScale.X, _sourceRect.Height * Entity.transform.scale.Y * TextureScale.Y);

            graphics.batcher.draw(Subtexture, destinationRect, _sourceRect, Color, Entity.transform.rotation, Origin * _inverseTexScale, SpriteEffects, layerDepth);
        }
        public override void render(Graphics graphics, Camera camera)
        {
            var topLeft         = entity.transform.position + _localOffset;
            var destinationRect = RectangleExt.fromFloats(topLeft.X, topLeft.Y, _sourceRect.Width * entity.transform.scale.X * textureScale.X, _sourceRect.Height * entity.transform.scale.Y * textureScale.Y);

            graphics.batcher.draw(subtexture, destinationRect, _sourceRect, color, entity.transform.rotation, origin * _inverseTexScale, spriteEffects, _layerDepth);
        }
Esempio n. 3
0
        void updateResolutionScaler()
        {
            var designSize        = _designResolutionSize;
            var screenSize        = new Point(Screen.width, Screen.height);
            var screenAspectRatio = (float)screenSize.X / (float)screenSize.Y;

            var renderTargetWidth  = screenSize.X;
            var renderTargetHeight = screenSize.Y;

            var resolutionScaleX = (float)screenSize.X / (float)designSize.X;
            var resolutionScaleY = (float)screenSize.Y / (float)designSize.Y;

            var rectCalculated = false;

            // calculate the scale used by the PixelPerfect variants
            pixelPerfectScale = 1;
            if (_resolutionPolicy != SceneResolutionPolicy.None)
            {
                if ((float)designSize.X / (float)designSize.Y > screenAspectRatio)
                {
                    pixelPerfectScale = screenSize.X / designSize.X;
                }
                else
                {
                    pixelPerfectScale = screenSize.Y / designSize.Y;
                }

                if (pixelPerfectScale == 0)
                {
                    pixelPerfectScale = 1;
                }
            }

            switch (_resolutionPolicy)
            {
            case SceneResolutionPolicy.None:
                _finalRenderDestinationRect.X      = _finalRenderDestinationRect.Y = 0;
                _finalRenderDestinationRect.Width  = screenSize.X;
                _finalRenderDestinationRect.Height = screenSize.Y;
                rectCalculated = true;
                break;

            case SceneResolutionPolicy.ExactFit:
                // exact design size render texture
                renderTargetWidth  = designSize.X;
                renderTargetHeight = designSize.Y;
                break;

            case SceneResolutionPolicy.NoBorder:
                // exact design size render texture
                renderTargetWidth  = designSize.X;
                renderTargetHeight = designSize.Y;

                resolutionScaleX = resolutionScaleY = Math.Max(resolutionScaleX, resolutionScaleY);
                break;

            case SceneResolutionPolicy.NoBorderPixelPerfect:
                // exact design size render texture
                renderTargetWidth  = designSize.X;
                renderTargetHeight = designSize.Y;

                // we are going to do some cropping so we need to use floats for the scale then round up
                pixelPerfectScale = 1;
                if ((float)designSize.X / (float)designSize.Y < screenAspectRatio)
                {
                    var floatScale = (float)screenSize.X / (float)designSize.X;
                    pixelPerfectScale = Mathf.ceilToInt(floatScale);
                }
                else
                {
                    var floatScale = (float)screenSize.Y / (float)designSize.Y;
                    pixelPerfectScale = Mathf.ceilToInt(floatScale);
                }

                if (pixelPerfectScale == 0)
                {
                    pixelPerfectScale = 1;
                }

                _finalRenderDestinationRect.Width  = Mathf.ceilToInt(designSize.X * pixelPerfectScale);
                _finalRenderDestinationRect.Height = Mathf.ceilToInt(designSize.Y * pixelPerfectScale);
                _finalRenderDestinationRect.X      = (screenSize.X - _finalRenderDestinationRect.Width) / 2;
                _finalRenderDestinationRect.Y      = (screenSize.Y - _finalRenderDestinationRect.Height) / 2;
                rectCalculated = true;

                break;

            case SceneResolutionPolicy.ShowAll:
                resolutionScaleX = resolutionScaleY = Math.Min(resolutionScaleX, resolutionScaleY);

                renderTargetWidth  = designSize.X;
                renderTargetHeight = designSize.Y;
                break;

            case SceneResolutionPolicy.ShowAllPixelPerfect:
                // exact design size render texture
                renderTargetWidth  = designSize.X;
                renderTargetHeight = designSize.Y;

                _finalRenderDestinationRect.Width  = Mathf.ceilToInt(designSize.X * pixelPerfectScale);
                _finalRenderDestinationRect.Height = Mathf.ceilToInt(designSize.Y * pixelPerfectScale);
                _finalRenderDestinationRect.X      = (screenSize.X - _finalRenderDestinationRect.Width) / 2;
                _finalRenderDestinationRect.Y      = (screenSize.Y - _finalRenderDestinationRect.Height) / 2;
                rectCalculated = true;

                break;

            case SceneResolutionPolicy.FixedHeight:
                resolutionScaleX = resolutionScaleY;
                designSize.X     = Mathf.ceilToInt(screenSize.X / resolutionScaleX);

                // exact design size render texture for height but not width
                renderTargetWidth  = designSize.X;
                renderTargetHeight = designSize.Y;
                break;

            case SceneResolutionPolicy.FixedHeightPixelPerfect:
                // start with exact design size render texture height. the width may change
                renderTargetHeight = designSize.Y;

                _finalRenderDestinationRect.Width  = Mathf.ceilToInt(designSize.X * resolutionScaleX);
                _finalRenderDestinationRect.Height = Mathf.ceilToInt(designSize.Y * pixelPerfectScale);
                _finalRenderDestinationRect.X      = (screenSize.X - _finalRenderDestinationRect.Width) / 2;
                _finalRenderDestinationRect.Y      = (screenSize.Y - _finalRenderDestinationRect.Height) / 2;
                rectCalculated = true;

                renderTargetWidth = (int)(designSize.X * resolutionScaleX / pixelPerfectScale);
                break;

            case SceneResolutionPolicy.FixedWidth:
                resolutionScaleY = resolutionScaleX;
                designSize.Y     = Mathf.ceilToInt(screenSize.Y / resolutionScaleY);

                // exact design size render texture for width but not height
                renderTargetWidth  = designSize.X;
                renderTargetHeight = designSize.Y;
                break;

            case SceneResolutionPolicy.FixedWidthPixelPerfect:
                // start with exact design size render texture width. the height may change
                renderTargetWidth = designSize.X;

                _finalRenderDestinationRect.Width  = Mathf.ceilToInt(designSize.X * pixelPerfectScale);
                _finalRenderDestinationRect.Height = Mathf.ceilToInt(designSize.Y * resolutionScaleY);
                _finalRenderDestinationRect.X      = (screenSize.X - _finalRenderDestinationRect.Width) / 2;
                _finalRenderDestinationRect.Y      = (screenSize.Y - _finalRenderDestinationRect.Height) / 2;
                rectCalculated = true;

                renderTargetHeight = (int)(designSize.Y * resolutionScaleY / pixelPerfectScale);

                break;

            case SceneResolutionPolicy.BestFit:
                var safeScaleX = (float)screenSize.X / (designSize.X - _designBleedSize.X);
                var safeScaleY = (float)screenSize.Y / (designSize.Y - _designBleedSize.Y);

                var resolutionScale = MathHelper.Max(resolutionScaleX, resolutionScaleY);
                var safeScale       = MathHelper.Min(safeScaleX, safeScaleY);

                resolutionScaleX = resolutionScaleY = MathHelper.Min(resolutionScale, safeScale);

                renderTargetWidth  = designSize.X;
                renderTargetHeight = designSize.Y;

                break;
            }

            // if we didnt already calculate a rect (None and all pixel perfect variants calculate it themselves) calculate it now
            if (!rectCalculated)
            {
                // calculate the display rect of the RenderTarget
                var renderWidth  = designSize.X * resolutionScaleX;
                var renderHeight = designSize.Y * resolutionScaleY;

                _finalRenderDestinationRect = RectangleExt.fromFloats((screenSize.X - renderWidth) / 2, (screenSize.Y - renderHeight) / 2, renderWidth, renderHeight);
            }


            // set some values in the Input class to translate mouse position to our scaled resolution
            var scaleX = renderTargetWidth / (float)_finalRenderDestinationRect.Width;
            var scaleY = renderTargetHeight / (float)_finalRenderDestinationRect.Height;

            Input._resolutionScale  = new Vector2(scaleX, scaleY);
            Input._resolutionOffset = _finalRenderDestinationRect.Location;

            // resize our RenderTargets
            if (_sceneRenderTarget != null)
            {
                _sceneRenderTarget.Dispose();
            }
            _sceneRenderTarget = RenderTarget.create(renderTargetWidth, renderTargetHeight);

            // only create the destinationRenderTarget if it already exists, which would indicate we have PostProcessors
            if (_destinationRenderTarget != null)
            {
                _destinationRenderTarget.Dispose();
                _destinationRenderTarget = RenderTarget.create(renderTargetWidth, renderTargetHeight);
            }

            // notify the Renderers, PostProcessors and FinalRenderDelegate of the change in render texture size
            for (var i = 0; i < _renderers.length; i++)
            {
                _renderers.buffer[i].onSceneBackBufferSizeChanged(renderTargetWidth, renderTargetHeight);
            }

            for (var i = 0; i < _afterPostProcessorRenderers.length; i++)
            {
                _afterPostProcessorRenderers.buffer[i].onSceneBackBufferSizeChanged(renderTargetWidth, renderTargetHeight);
            }

            for (var i = 0; i < _postProcessors.length; i++)
            {
                _postProcessors.buffer[i].onSceneBackBufferSizeChanged(renderTargetWidth, renderTargetHeight);
            }

            if (_finalRenderDelegate != null)
            {
                _finalRenderDelegate.onSceneBackBufferSizeChanged(renderTargetWidth, renderTargetHeight);
            }

            camera.onSceneRenderTargetSizeChanged(renderTargetWidth, renderTargetHeight);
        }
Esempio n. 4
0
        public void drawInto(
            Batcher batcher,
            ref FontCharacterSource text,
            Vector2 position,
            Color color,
            float rotation,
            Vector2 origin,
            Vector2 scale,
            SpriteEffects effect,
            float depth)
        {
            var flipAdjustment = Vector2.Zero;

            var flippedVert = (effect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically;
            var flippedHorz = (effect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally;

            if (flippedVert || flippedHorz)
            {
                Vector2 size;
                measureString(ref text, out size);

                if (flippedHorz)
                {
                    origin.X        *= -1;
                    flipAdjustment.X = -size.X;
                }

                if (flippedVert)
                {
                    origin.Y        *= -1;
                    flipAdjustment.Y = _font.LineSpacing - size.Y;
                }
            }

            // TODO: This looks excessive... i suspect we could do most of this with simple vector math and avoid this much matrix work.
            var requiresTransformation = flippedHorz || flippedVert || rotation != 0f || scale != Vector2.One;

            if (requiresTransformation)
            {
                Matrix2D temp;
                Matrix2D.createTranslation(-origin.X, -origin.Y, out _transformationMatrix);
                Matrix2D.createScale(flippedHorz ? -scale.X : scale.X, flippedVert ? -scale.Y : scale.Y, out temp);
                Matrix2D.multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.createTranslation(flipAdjustment.X, flipAdjustment.Y, out temp);
                Matrix2D.multiply(ref temp, ref _transformationMatrix, out _transformationMatrix);
                Matrix2D.createRotation(rotation, out temp);
                Matrix2D.multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.createTranslation(position.X, position.Y, out temp);
                Matrix2D.multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
            }

            // Get the default glyph here once.
            SpriteFont.Glyph?defaultGlyph = null;
            if (_font.DefaultCharacter.HasValue)
            {
                defaultGlyph = _glyphs[_font.DefaultCharacter.Value];
            }

            var currentGlyph     = SpriteFont.Glyph.Empty;
            var offset           = requiresTransformation ? Vector2.Zero : position - origin;
            var firstGlyphOfLine = true;

            for (var i = 0; i < text.Length; ++i)
            {
                var c = text[i];

                if (c == '\r')
                {
                    continue;
                }

                if (c == '\n')
                {
                    offset.X         = requiresTransformation ? 0f : position.X - origin.X;
                    offset.Y        += _font.LineSpacing;
                    firstGlyphOfLine = true;
                    continue;
                }

                if (!_glyphs.TryGetValue(c, out currentGlyph))
                {
                    if (!defaultGlyph.HasValue)
                    {
                        throw new ArgumentException("Errors.TextContainsUnresolvableCharacters", "text");
                    }

                    currentGlyph = defaultGlyph.Value;
                }

                // The first character on a line might have a negative left side bearing.
                // In this scenario, SpriteBatch/SpriteFont normally offset the text to the right,
                // so that text does not hang off the left side of its rectangle.
                if (firstGlyphOfLine)
                {
                    offset.X        += Math.Max(currentGlyph.LeftSideBearing, 0);
                    firstGlyphOfLine = false;
                }
                else
                {
                    offset.X += _font.Spacing + currentGlyph.LeftSideBearing;
                }

                var p = offset;

                if (flippedHorz)
                {
                    p.X += currentGlyph.BoundsInTexture.Width;
                }
                p.X += currentGlyph.Cropping.X;

                if (flippedVert)
                {
                    p.Y += currentGlyph.BoundsInTexture.Height - _font.LineSpacing;
                }
                p.Y += currentGlyph.Cropping.Y;

                // transform our point if we need to
                if (requiresTransformation)
                {
                    Vector2Ext.transform(ref p, ref _transformationMatrix, out p);
                }

                var destRect = RectangleExt.fromFloats(
                    p.X,
                    p.Y,
                    currentGlyph.BoundsInTexture.Width * scale.X,
                    currentGlyph.BoundsInTexture.Height * scale.Y);

                batcher.draw(
                    _font.Texture,
                    destRect,
                    currentGlyph.BoundsInTexture,
                    color,
                    rotation,
                    Vector2.Zero,
                    effect,
                    depth);

                offset.X += currentGlyph.Width + currentGlyph.RightSideBearing;
            }
        }