Small project to show drawing items with the mouse in XNA

I saw a post today on the XNA Community Forums asking how to give a player the possibility of adding things to a game, and storing the position of the added images.

I made a Downloadlittle project to show how to do this. Basically it adds a Vector2 to a generic List when the left mouse button is pressed and removes a Vector2 when the right mousebutton is pressed.

Here's a small demo:

using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SmallXNADrawingGame
{
    /// <summary>
    /// Small class to show how to be able to draw and store objects in XNA.
    /// 
    /// Jakob "XNAFAN" Krarup - February 2010
    /// http://www.xnafan.net
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //a list to store the dots in
        List<Vector2> positionOfDots = new List<Vector2>();

        //the dot to use for drawing
        Texture2D textureDot;

        //stores half the size of the dot for centering the texture
        Vector2 halfSizeOfDot;

        //stores the current and previous states of the mouse
        //they are used to compare in Update() to make sure
        //a mouseclick is a new one and not just the button being held
        MouseState currentMouse, oldMouse;

        //font for writing instructions
        SpriteFont defaultFont;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            //set size of screen
            graphics.PreferredBackBufferHeight = 600;
            graphics.PreferredBackBufferWidth = 800;
            Content.RootDirectory = "Content";

            //make sure to display mouse cursor
            this.IsMouseVisible = true;
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            textureDot = Content.Load<Texture2D>("dot");
            //calculate half the size of a dot and store it 
            //for drawing the dot centered on the mouseclick (se Draw())
            halfSizeOfDot = new Vector2(textureDot.Width / 2, textureDot.Height / 2);

            //load the font
            defaultFont = Content.Load<SpriteFont>("DefaultFont");
        }

        protected override void Update(GameTime gameTime)
        {
            //store the current state of the mouse (buttons, position, etc.)
            currentMouse = Mouse.GetState();

            //if the mousebutton was pressed between this and last update...
            //this check makes certain that one click doesn't create multiple dots because the button is held down
            if (currentMouse.LeftButton == ButtonState.Pressed && oldMouse.LeftButton == ButtonState.Released)
            {
                positionOfDots.Add(new Vector2(currentMouse.X, currentMouse.Y));
            }

            //if right mousebutton was pressed
            if (currentMouse.RightButton == ButtonState.Pressed && oldMouse.RightButton == ButtonState.Released)
            {
                //and there are still dots left
                if (positionOfDots.Count > 0)
                {
                    //remove the last
                    //"-1" is because the list i zero-indexed,
                    //so a count of 1 would remove the dot at position 1-1 (zero).
                    positionOfDots.RemoveAt(positionOfDots.Count - 1);
                }
            }

            base.Update(gameTime);

            //store the current state in oldMouse 
            //to be able to determine when buttons have JUST been pressed 
            //by comparing the two states in an update
            oldMouse = currentMouse;
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();

            //write instructions
            spriteBatch.DrawString(defaultFont, "Left mousebutton to draw dot", new Vector2(20, 20), Color.White);
            spriteBatch.DrawString(defaultFont, "Right mousebutton to delete dots", new Vector2(20, 40), Color.White);

            foreach (Vector2 position in positionOfDots)
            {
                //draw the dot centered on the position of the mouse
                //by subtracting the Vector 
                //which has half the textures width for X and half the textures height for Y
                //from the position stored.
                spriteBatch.Draw(textureDot, position - halfSizeOfDot, Color.White);

                //draw the dot's position
                spriteBatch.DrawString(defaultFont, position.ToString(), position, Color.White);
            }
            base.Draw(gameTime);
            spriteBatch.End();
        }
    }
}

Tags: ,

Leave a Reply