Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm having an issue with viewports not appearing where they should. Two viewports appear a good 10 pixels below where they should be. I created a very simple test project that does nothing more than place the viewports and draw a texture in each. I created this in both monogame 3.0.1 and XNA. I only encounter this issue in monogame. In XNA, it works fine.

I tried to post screenshots, but I'm a new user so I'm out of luck there. The top is exactly where it should be. Everything else is shifted down 10 pixels. Does anyone have any thoughts on why this is happening and how to fix it? Thanks!

Link to Monogame Screenshot Link to XNA screenshot

Here is the code:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ViewportTest
{
public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Viewport _topViewport;
    Viewport _leftViewport;
    Viewport _rightViewport;
    Viewport _bottomViewport;

    Texture2D _topTexture;
    Texture2D _leftTexture;
    Texture2D _rightTexture;
    Texture2D _bottomTexture;

    public Game1()
        : base()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        graphics.PreferredBackBufferWidth = 1024;
        graphics.PreferredBackBufferHeight = 768;
        graphics.ApplyChanges();

        // stretches across the top of the screen. 
        // It starts at the top left corner (0,0), is 1024 pixels wide and 28 tall
        _topViewport = new Viewport(0, 0, 1024, 28);

        // Stretches across the left side of the screen
        // It starts at the bottom of the status bar on the left edge of the screen
        // it's 37 pixels wide and 700 tall
        _leftViewport = new Viewport(0, 28, 37, 700);

        // Stretches across the right side of the screen
        // It starts 37 pixels short of the right edge of the screen
        // it's 37 pixels wide and 700 tall
        _rightViewport = new Viewport((1024 - 37), 28, 37, 700);

        // Stretches across the bottom of the screen
        // it starts at the bottom of the left and right borders on the left edge of the screen
        // The starting Y coordinate would be the height of the top bar plus the height of the side bars: 700 + 28
        // It's 1024 pixels wide and 40 high
        _bottomViewport = new Viewport(0, (28 + 700), 1024, 40);
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        _topTexture = Content.Load<Texture2D>(@"Textures\top_bar");
        _leftTexture = Content.Load<Texture2D>(@"Textures\left_bar");
        _rightTexture = Content.Load<Texture2D>(@"Textures\right_bar");
        _bottomTexture = Content.Load<Texture2D>(@"Textures\bottom_bar");
    }

    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);

        // Draw top
        spriteBatch.Begin();
        graphics.GraphicsDevice.Viewport = _topViewport;
        spriteBatch.Draw(_topTexture, new Rectangle(0, 0, 1024, 28), Color.White);
        spriteBatch.End();

        // Draw left
        spriteBatch.Begin();
        graphics.GraphicsDevice.Viewport = _leftViewport;
        spriteBatch.Draw(_leftTexture, new Rectangle(0, 0, 37, 700), Color.White);
        spriteBatch.End();

        // Draw Right
        spriteBatch.Begin();
        graphics.GraphicsDevice.Viewport = _rightViewport;
        spriteBatch.Draw(_rightTexture, new Rectangle(0, 0, 37, 700), Color.White);
        spriteBatch.End();

        // Draw Bottom
        spriteBatch.Begin();
        graphics.GraphicsDevice.Viewport = _bottomViewport;
        spriteBatch.Draw(_bottomTexture, new Rectangle(0, 0, 1024, 40), Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

}

share|improve this question
    
Neither of you images will load for me. Also is your window taller than your screen? –  ClassicThunder Apr 24 '13 at 0:07
1  
Did you try posting an issue on GitHub? –  JimmyBoh Apr 24 '13 at 17:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.