Skip to main content
GameDev.net gamedev.net
🔒 Locked

Stuck: Designing an InputManager

Started by AndreiVictor Jun 27, 2007 at 9:24 AM 2 replies 850+ views
Original Post
AndreiVictor
AndreiVictor
Hello again smart citizens of gamedev.net! I'm finally making my own multi-purpose game engine, and I tend to keep it cross-platform as much as possible (without having to relay on libraries on SDL). I'm currently at the phase of handling inputs on my engine and I've been trying to think of a good way to implement some sort of an input manager on my engine. What I want my input manager to do is to handle all input devices (keyboard, mouse, joysticks/gamepads) and simply recognize these input signals and translate these signals into standardized engine input signals. Also, I would like my engine to handle inputs received such that it will do the actions that correspond to a "MoveMap", a map that contains the mapping of the input signals to the appropriate action/function to be executed, that are stored as a stack. What would be the best strategy on designing an input manager such as this without making the dependencies so cluttered? In OOP-speak, which would be best inherited from, and/or abstracted from? I am also having a problem generalizing the point of entry of inputs such as Windows for mouse and keyboard inputs (the former uses windows mouse events, and latter uses GetAsyncKeyState() ).
c",)
CrazyCdn
CrazyCdn
I use an internal messaging system for the engine, which the game can use too. So when input is received it fires off a message that any listeners can get. And people who could care less about input get nothing.
"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin
oliii
oliii
Yeah, I second that.

device(s) -> mapping -> message -> listener(s).
Everything is better with Metal.
Chimaira
Chimaira
I've recently finished an inputmanager that i'm very satisfied with.

First of all i have a simple interface that looks like this:
 public interface IBaseControl    {        float Value        {            get;        }        float OldValue        {            get;        }    }

All kinds of inputdevices can then inherit from this interface like this
    public class KeyboardControl : IBaseControl    {        public Keys key;        public KeyboardControl(Keys k)        {            key = k;        }        public float Value        {            get            {                if (KeyboardManager.CurrentState.IsKeyDown(key))                    return 1;                return 0;                //On analog devices you can return values between 0 and 1            }        }[....]


In a class i call PlayerControls i have an array with IBaseControls that are indexed with an enum that looks like this
    public enum Controls : int    {        MoveForward,        MoveBack,        fire etc etc....    }

The playercontrols class looks like this
    public class PlayerControls    {        private IBaseControl[] buttons = new IBaseControl[Enum.GetValues(typeof(Controls)).Length];        const float deltaRequiredForClick = 0.5f;        public PlayerControls()        {                      //Setup default wasd controlmap            this[Controls.MoveForward] = new KeyboardControl(Keys.W);				[...]            this[Controls.LookLeft] = new MouseMovementController(Direction2d.X, true);        }        public IBaseControl this[Controls index]        {            get { return buttons[(int)index]; }            set { buttons[(int)index] = value; }        }        public float Delta(Controls key)        {            if (this[key] == null)                return 0;            return this[key].Value;        }        public bool IsKeyDown(Controls key)        {            if (this[key] == null)                return false;            return (this[key].Value > deltaRequiredForClick);                        }        public bool IsKeyClicked(Controls key)        {            if (this[key] == null)                return false;            return (this[key].Value > deltaRequiredForClick && this[key].OldValue <= deltaRequiredForClick);        }}


Now all you have to do is assign a PlayerControlls to each player and then you can use it to poll the controlls.
if (player1keyMap.IsKeyDown(Controls.MoveLeft))  moveplayer1Left();

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.