One of the most common tasks of making a game is making a custom pointer for the mouse. This short class allows to add any 2D texture as the mouse pointer.
It also reads mouse position and buttons statuses.
LIBRARY:
DOWNLOAD: XNAMouse.cs
EXAMPLE OF USE:
First of all you will have to make default pointer invisible. Preferably you can put the code in your Game.Initialize() method:
this.IsMouseVisible = false;
Then initialize the XNAMouse object somewhere and use XNAMouse’s Render method inside the Game’s Draw method, XNAMouse’s Update inside Game’s Update method and XNAMouse’s Dispose method inside Game’s UnloadContent method:
public class Game1 : Microsoft.Xna.Framework.Game { ... public XNAMouse mouse = new XNAMouse(this.spriteBatch, Texture2D.FromStream(graphics.GraphicsDevice, File.OpenRead("your\\path")), mouse_size); ... protected override void Update(GameTime gameTime) { ... mouse.Update(); ... } protected override void Draw(GameTime gameTime) { ... mouse.Render(); ... } protected override void UnloadContent() { ... mouse.Dispose(); ... } }
You give mouse size and texture as arguments in the XNAMouse’s constructor.
I also put button check methods inside this class. I used a little trick to check if mouse buttons were pressed and released:
public bool leftMouseButtonState() { if (mouseState.LeftButton == ButtonState.Pressed) LMBpressed = true; if (mouseState.LeftButton == ButtonState.Released && LMBpressed) { LMBpressed = false; return true; } return false; }