Esempio n. 1
0
        public void Draw(Camera2D camera, bool useMapBackgroundColor = false)
        {
            // it's important to get the camera state before setting the render target
            // because the render target changes the size of the viewport
            var boundingRectangle = camera.GetBoundingRectangle();
            var viewMatrix        = camera.GetViewMatrix();
            var viewport          = _graphicsDevice.Viewport;

            _graphicsDevice.SetRenderTarget(_renderTarget);

            if (useMapBackgroundColor && BackgroundColor.HasValue)
            {
                _graphicsDevice.Clear(BackgroundColor.Value);
            }
            else
            {
                _graphicsDevice.Clear(Color.Transparent);
            }

            foreach (var layer in _layers)
            {
                layer.Draw(boundingRectangle);
            }

            _graphicsDevice.SetRenderTarget(null);

            _graphicsDevice.Viewport = viewport;
            _spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied, SamplerState.PointClamp, transformMatrix: viewMatrix);
            _spriteBatch.Draw(_renderTarget, Vector2.Zero, Color.White);
            _spriteBatch.End();
        }
Esempio n. 2
0
        public bool IsInView(Sprite sprite)
        {
            // if not within the horizontal bounds of the screen
            if (sprite.Position.X - sprite.Texture.Width > camera.Position.X + camera.GetBoundingRectangle().Width || sprite.Position.X + sprite.Texture.Width < camera.Position.X)
            {
                return(false);
            }

            // if not within the vertical bounds of the screen
            if (sprite.Position.Y - sprite.Texture.Height > camera.Position.Y + camera.GetBoundingRectangle().Height || sprite.Position.Y + sprite.Texture.Height < camera.Position.Y)
            {
                return(false);
            }

            // if in view
            return(true);
        }
Esempio n. 3
0
        public void Camera2D_GetBoundingRectangle_Test()
        {
            var graphicsDevice = TestHelper.CreateGraphicsDevice();
            var viewport = new DefaultViewportAdapter(graphicsDevice);
            var camera = new Camera2D(viewport);
            camera.Move(new Vector2(2, 0));
            camera.Move(new Vector2(0, 3));

            var boundingRectangle = camera.GetBoundingRectangle();

            Assert.AreEqual(2, boundingRectangle.Left, 0.01);
            Assert.AreEqual(3, boundingRectangle.Top, 0.01);
            Assert.AreEqual(802, boundingRectangle.Right, 0.01);
            Assert.AreEqual(483, boundingRectangle.Bottom, 0.01);
        }
Esempio n. 4
0
        public void Camera2D_GetBoundingRectangle_Test()
        {
            var graphicsDevice = TestHelper.CreateGraphicsDevice();
            var viewport       = new DefaultViewportAdapter(graphicsDevice);
            var camera         = new Camera2D(viewport);

            camera.Move(new Vector2(2, 0));
            camera.Move(new Vector2(0, 3));

            var boundingRectangle = camera.GetBoundingRectangle();

            Assert.AreEqual(2, boundingRectangle.Left, 0.01);
            Assert.AreEqual(3, boundingRectangle.Top, 0.01);
            Assert.AreEqual(802, boundingRectangle.Right, 0.01);
            Assert.AreEqual(483, boundingRectangle.Bottom, 0.01);
        }
Esempio n. 5
0
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // the camera produces a view matrix that can be applied to any sprite batch
            var transformMatrix = _camera.GetViewMatrix(Vector2.Zero);

            _spriteBatch.Begin(transformMatrix: transformMatrix);
            _spriteBatch.Draw(_backgroundSky, Vector2.Zero, Color.White);
            _spriteBatch.Draw(_backgroundClouds, new Vector2(-_cloudsOffset, 0), Color.White);
            _spriteBatch.Draw(_backgroundClouds, new Vector2(_cloudsOffset, 10), Color.White);
            _spriteBatch.End();

            for (var layerIndex = 0; layerIndex < 4; layerIndex++)
            {
                // different layers can have a parallax factor applied for a nice depth effect
                var parallaxFactor = Vector2.One * (0.25f * layerIndex);
                var viewMatrix     = _camera.GetViewMatrix(parallaxFactor);
                _spriteBatch.Begin(transformMatrix: viewMatrix);

                for (var repeatIndex = -3; repeatIndex <= 3; repeatIndex++)
                {
                    var texture  = _backgroundHills[layerIndex];
                    var position = new Vector2(repeatIndex * texture.Width, 0);
                    _spriteBatch.Draw(texture, position, Color.White);
                }

                _spriteBatch.End();
            }

            // not all sprite batches need to be affected by the camera
            var rectangle     = _camera.GetBoundingRectangle();
            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine($"WASD: Move [{_camera.Position.X:0}, {_camera.Position.Y:0}]");
            stringBuilder.AppendLine($"EQ: Rotate [{MathHelper.ToDegrees(_camera.Rotation):0.00}]");
            stringBuilder.AppendLine($"RF: Zoom [{_camera.Zoom:0.00}]");
            stringBuilder.AppendLine($"World Pos: [{_worldPosition.X:0}, {_worldPosition.Y:0}]");
            stringBuilder.AppendLine($"Bounds: [{rectangle.X:0}, {rectangle.Y:0}, {rectangle.Width:0}, {rectangle.Height:0}]");

            _spriteBatch.Begin(blendState: BlendState.AlphaBlend);
            _spriteBatch.DrawString(_bitmapFont, stringBuilder.ToString(), new Vector2(5, 5), Color.DarkBlue);
            _spriteBatch.End();

            base.Draw(gameTime);
        }
 public static void Draw(this SpriteBatch spriteBatch, TiledLayer layer, Camera2D camera)
 {
     layer.Draw(spriteBatch, camera.GetBoundingRectangle().ToRectangle());
 }
Esempio n. 7
0
        public void Draw(SpriteBatch spriteBatch, Camera2D camera)
        {
            var visibleRectangle = camera.GetBoundingRectangle().ToRectangle();

            Draw(spriteBatch, visibleRectangle);
        }
Esempio n. 8
0
 public static void Draw(this SpriteBatch spriteBatch, TiledLayer layer, Camera2D camera)
 {
     layer.Draw(spriteBatch, camera.GetBoundingRectangle().ToRectangle());
 }