0
\$\begingroup\$

In my code below, I think it's correctly done. But idk, the image called by _bg is does not shown up. The screen only show White. Weirdly, there is 0 error message when I run it.

(The RenderTarget2D is for default-ing my view into landscape mode)

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

namespace GameName2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager _graphics;
        SpriteBatch _spriteBatch;
        Texture2D _bg;

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            _bg = Content.Load<Texture2D>(@"background");
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            // TODO: Add your drawing code here
            _spriteBatch.Begin();
            _spriteBatch.Draw(_bg,
                new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
                null,
                Color.White,
                0,
                Vector2.Zero,
                SpriteEffects.None,
                0);
            _spriteBatch.End();

            base.Draw(gameTime);


        }
    }
}

I use xnb file. And I put it in Content folder in the project. I've set 'Content' and 'Copy if newer' to the file property.

This is the .png file I use before converted into xnb:

Image

Or maybe the problem is in my .png file?

Any idea?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You should be using GraphicsDevice.Viewport instead of Window.ClientBounds when working with your SpriteBatch.

\$\endgroup\$
4
  • \$\begingroup\$ Why I wonder? What's the difference? \$\endgroup\$ Apr 29, 2013 at 14:52
  • \$\begingroup\$ I can't tell you why, I just know it causes problems when you use Window.ClientBounds when drawing with SpriteBatch. It technically should work. \$\endgroup\$
    – jgallant
    Apr 29, 2013 at 18:43
  • \$\begingroup\$ Good call Jon. Perhaps this needs to be submitted as a bug in MonoGame? \$\endgroup\$ Apr 29, 2013 at 22:58
  • \$\begingroup\$ msdn.microsoft.com/en-us/library/… has the answer as to what the difference is. Essentially ClientBounds.X is in screen space, not window. \$\endgroup\$
    – Phil
    Apr 30, 2013 at 4:10

Not the answer you're looking for? Browse other questions tagged .