Click here to Skip to main content
15,888,527 members
Articles / Programming Languages / C#
Article

Simple Input Interface for XNA

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
16 Jul 2007CPOL 28.3K   283   25   1
Makes event handlers available to game developers using Microsoft's XNA.

Introduction

As input is an important part of every game, it should not be difficult to use. C# provides a great feature - event handlers - which have not been used in the XNA framework to handle input. This class, CInput, will enable event handlers for XNA.

Using the code

All you have to do to use this code is add the Input.cs file to your project, create an instance of CInput, and call its Update function in the game.

C#
//Excerpts from Game.cs

namespace YourGame
{
    public class YourGame : Microsoft.Xna.Framework.Game
    {
        //The variable holding a reference to the input system

        VisualIllusions.CInput Input;
        
        //Constructor

        public YourGame()
        {
            //Create the input device

            Input = new VisualIllusions.CInput();
            //Add any event handlers here or in the initialize function

            Input.OnMouseDown += new 
              VisualIllusions.CInput.OnMouseDownDelegate(Input_OnMouseDown);
        }
        
        //Update the game

        protected override void Update(GameTime gameTime)
        {
            //Update the input

            Input.Update();

            base.Update(gameTime);
        }
        
        //A function called by an event

        void Input_OnMouseDown(VisualIllusions.CInput.MouseButtons b)
        {
            System.Diagnostics.Debug.Print("A mouse button was pressed: " + 
                                           b.ToString());
        }
        
        [...]
    }
}

The class is quite simple to use, and you should be able to integrate it into your project easily by taking a look at the sample project.

Points of interest

I am currently working on GamePad support and a binding class to make it easy to dynamically bind actions to specific keys. This binding class will be serializable so that everyone can configure his/her own set of keys.

History

  • First release, and probably buggy. Let me know about any problems you notice!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAbout event base systems in games... Pin
Amadrias24-Jul-07 6:24
Amadrias24-Jul-07 6:24 
Hi, I do like the .Net event based system and I always wanted to use it in my own game developments but one thing that it doesn't manage easily is order calls.

I'll explain a bit:

Say you have two classes that you want to react to mouse down events, the order call will be determined by the event subscription order.

So, if your two classes are a Player class that manages your game avatar in game and a GUI class that manages for instance your character inventory, you will mostly like to get your mouse down event being first handled by the GUI class and then the Player one.

If you didn't make sure that your Player class registers before the GUI then you'll end up getting wrong results.

And it goes even more badly with dynamic event subscriptions (when the class is instanciated, it subscribes to a given event in another class) and worst case when you have more than 2 or 3 classes responding to the same input event.

That's the main reason why game developers prefer to manage directly input states in their "Update" game loop (in XNA at least that's the method's name).

I've also implemented a similar event based input system in the Xna eXperience Libraries (XXL) project that you can find at codeplex: http://www.codeplex.com/xxl

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.