After playing around a lot with 2D graphics and the keyboard kicking a mouse into the playground is a nice change. Below you can see a nice little screen cap of what the cursor looks like on a nice default background. The cursor isn’t created by me, I just found it through Google images :). Since [...]
After playing around a lot with 2D graphics and the keyboard kicking a mouse into the playground is a nice change. Below you can see a nice little screen cap of what the cursor looks like on a nice default background. The cursor isn’t created by me, I just found it through Google images :).

Since we have already become familiar with some of the basics of XNA in “How to display an image with XNA” I will just place the code snippets here and only explain the new items :).
// Declerations
Texture2D t2dCursor;
SpriteBatch sbDrawer;
MouseState msMouseState;
The first 2 lines explain themselves, we have already covered this before. As you can see though I did change the naming a bit since in the long run I will be using this kind of naming a lot, naming standards can make it a lot easier for you to recognize what is what instantly.
The last line is something new though, but it’s not that hard to guess what it does. It simply is a container for our mouse state. Unlike in a lot of other languages where mouse input is passed on through event handlers, with XNA we poll the mouse like the keyboard status is polled.
// Load resources
t2dCursor = Texture2D.FromFile(graphics.GraphicsDevice, @”cursor.png”);
sbDrawer = new SpriteBatch(graphics.GraphicsDevice);
Nothing new to see here, let’s move on.
// Update
msMouseState = Mouse.GetState();
This will call the state of our mouse and store it in the mouse state variable for later usage, one thing to keep in mind though. Although common in programing languages I just made that mistake and at first didn’t realize it, naming the project and through that directly the namespace as well “Mouse”. This resulted in Mouse refering to my namespace, not the actual Mouse as an input device. A stupid mistake, but it can happen :).
// Draw
sbDrawer.Begin(SpriteBlendMode.AlphaBlend);
sbDrawer.Draw(t2dCursor, new Rectangle(msMouseState.X, msMouseState.Y, t2dCursor.Width, t2dCursor.Height), Color.White);
sbDrawer.End();
The second line is the only thing containing some new things, the state of our mouse is being used here. Using the X and Y variables from our mouse state we will display the cursor at the location along, not much complicated stuff happening here but it does give a nice result of having a visible representation of the mouse.
Finally I would like to say that if you would just want to use the windows mouse, you can also simply set the IsMouseVisible property of the game to true, do not do this though when you are creating your own cursor though; it would make the cursors overlap eachother making the user see 2 cursors.

Sep 19 at 1:31 am
Comment: #1
[...] input in XNA is just as easy as handling mouse input, or perhaps even easier. Just like with the handling mouse input tutorial of yesterday we will be going over the keyboard input code rather quickly, only really [...]