Custom Mouse Pointer in XNA

Monday, December 04 2006 - ,

One of my early game components was a Mouse input system that used a custom mouse pointer, I have now converted the code to use the beta 2 system. What I thought I would do is go through the process here in a small tutorial.

To start the project just fire up the GSE (Game Studio Express) System and start a new Windows Game Project. Before we go on I just wanted to mention that this will only work on a windows based system as the XBox 360 does not have support for a mouse...

With the application started and running the first step is to add the variables that the rest of the application will be using.

 private Texture2D mousePointer; private SpriteBatch mainSpriteBatch; private Vector2 mousePosition; private MouseState mouseState; 

Add the above code to the game1.cs file just under the declaration of the Device and Content manager. Next we need to load the content that the application will be using, as well as setting up the Sprite Batch.

 // Set up the main Sprite Batch for the Application this.mainSpriteBatch = new SpriteBatch(this.graphics.GraphicsDevice); // Load the pointer into the application this.mousePointer = content.Load<Texture2D>("content\\Arrow"); 

You will also need to make sure that at this moment you have added the content/Sprite that you would like to use for your Mouse Pointer. With the content now loaded we first need to update the mouse pointer so that it knows the correct position and such, Add the following lines to the Update call in your application.

 // Update the Mouse // Note that the Mouse can only be used in the windows System, it // will not work when the application is compiled for the Xbox. this.UpdateMouse(); 

 Here is the function that Updates the Mouse Information.

 private void UpdateMouse() { this.mouseState = Mouse.GetState(); this.mousePosition.X = this.mouseState.X; this.mousePosition.Y = this.mouseState.Y; // Restrict the Mouse so that it stays inside the current display if (this.mousePosition.X < 0) this.mousePosition.X = 0; if (this.mousePosition.X > this.Window.ClientBounds.Width) this.mousePosition.X = this.Window.ClientBounds.Width; if (this.mousePosition.Y < 0) this.mousePosition.Y = 0; if (this.mousePosition.Y > this.Window.ClientBounds.Height) this.mousePosition.Y = this.Window.ClientBounds.Height; } 

Add the above code to the end of the application. The last step that we need to do is to actually draw the pointer on the device/screen. Add the following section of code to the draw code in your application.

 // TODO: Add your drawing code here this.mainSpriteBatch.Begin(SpriteBlendMode.AlphaBlend); this.mainSpriteBatch.Draw(this.mousePointer, this.mousePosition, Color.White); this.mainSpriteBatch.End(); 

Now you should have a simple Custom Mouse Pointer that can be used in your applications, for those who are interested I will post the complete solution in the files section of www.virtualrealm.com.au over the next few days.

kick it on DotNetKicks.com kick it on gamedevkicks.com

Similar Posts

  1. Drawing a Basic Background in XNA
  2. XNA Game Component - Custom Mouse Pointer
  3. XNA Tutorial - Creating a Splash Screen

3 comment(s)

protoborg wrote on Tuesday, April 22 2008

I can get this to work when it is by itself as the only code, but it cannot seem to load the texture I want when this code is part of an existing project.  It is quite irritating.  I would show you my code, but it is far too complicated to put up here.  Besides, you can see the code if you look at the XNA tutorials DVD.  It is the result of the 3D tutorial.

Clicky Web Analytics