protected override void Initialize()
        {
            base.Initialize();

            // the default viewport adapater is the simplest, it doesn't do any scaling at all
            // but is used by a Camera2D if no other adapter is specified.
            // this is often useful if you have a game with a large map and you want the player to see 
            // more of the map on a bigger screen.
            _defaultViewportAdapter = new DefaultViewportAdapter(GraphicsDevice);

            // the scaling viewport adapter stretches the output to fit in the viewport, ignoring the aspect ratio
            // this works well if the aspect ratio doesn't change a lot between devices 
            // or you don't like the black bars of the boxing adapter
            _scalingViewportAdapter = new ScalingViewportAdapter(GraphicsDevice, 800, 480);

            // the boxing viewport adapter uses letterboxing or pillarboxing to maintain aspect ratio
            // it's a little more complicated and needs to listen to the window client size changing event
            _boxingViewportAdapter = new BoxingViewportAdapter(GraphicsDevice, 800, 480);

            Window.ClientSizeChanged += (s, e) => _currentViewportAdapter.OnClientSizeChanged(); 
            
            // typically you'll only ever want to use one viewport adapter for a game, but in this sample we'll be 
            // switching between them.
            _currentViewportAdapter = _boxingViewportAdapter;
        }
Exemplo n.º 2
0
        public void Camera2D_LookAt_Test()
        {
            var graphicsDevice = TestHelper.CreateGraphicsDevice();
            var viewportAdapter = new DefaultViewportAdapter(graphicsDevice);
            var camera = new Camera2D(viewportAdapter);
            
            camera.LookAt(new Vector2(100, 200));

            Assert.AreEqual(new Vector2(-300, -40), camera.Position);
        }
        public void DefaultViewportAdapter_Test()
        {
            var graphicsDevice = TestHelper.CreateGraphicsDevice();
            var viewportAdapter = new DefaultViewportAdapter(graphicsDevice);

            graphicsDevice.Viewport = new Viewport(0, 0, 1024, 768);

            Assert.AreEqual(1024, viewportAdapter.ViewportWidth);
            Assert.AreEqual(768, viewportAdapter.ViewportHeight);
            Assert.AreEqual(viewportAdapter.ViewportWidth, viewportAdapter.VirtualWidth);
            Assert.AreEqual(viewportAdapter.ViewportHeight, viewportAdapter.VirtualHeight);
            Assert.AreEqual(Matrix.Identity, viewportAdapter.GetScaleMatrix());
        }
Exemplo 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);
        }