Skip to content
Gabor Szauer edited this page Aug 20, 2015 · 3 revisions

#Input Manager This is the largest of our manages in terms of lines of code. That is because the Input Manager manages the mouse, keyboard and up to four game pads! As such there is a LOT to cover. We're going to cover the generic aspects of the input manager here, but the device specific code is broken down into three sub sections.

Lets explore the input manager. We are going to be starting from the skeleton created on the repository's front page.

#Initialize and shutdown Like other managers the input manager needs to be initialized and shut down. The Initialize function takes an OpenTK.Window as the argument. The Shutdown has no arguments. In Program.cs do the following:

public static void Initialize(object sender, EventArgs e) {
    InputManager.Instance.Initialize(Window);
}

public static void Update(object sender, FrameEventArgs e) {
    // UPDATE LOGIC
}

public static void Render(object sender, FrameEventArgs e) {
    // RENDER LOGIC
}

public static void Shutdown(object sender, EventArgs e) {
    InputManager.Instance.Shutdown();
}

#Update The input manager is special. It's the only manager that has an Update function. This update has no arguments, it takes no delta time or anything. The update is used to properly track buffered input. The input manager will still work if you don't call update, the only things that will give back undefined results are it's buffered functions.

Example of buffered functions

  • MouseDelta
  • KeyPressed()

Example of non buffered functions

  • MousePosition
  • KeyDown()

Pretty much all you have to do is call the InputManager's Update function at the begenning of every update frame

public static void Initialize(object sender, EventArgs e) {
    InputManager.Instance.Initialize(Window);
}

public static void Update(object sender, FrameEventArgs e) {
    InputManager.Instance.Update();
}

public static void Render(object sender, FrameEventArgs e) {
    // RENDER LOGIC
}

public static void Shutdown(object sender, EventArgs e) {
    InputManager.Instance.Shutdown();
}

#Graphics One last thing, let's also initialize the graphics manager here to allow us to print text to the game window

public static void Initialize(object sender, EventArgs e) {
    GraphicsManager.Instance.Initialize(Window);
    InputManager.Instance.Initialize(Window);
}

public static void Update(object sender, FrameEventArgs e) {
    InputManager.Instance.Update();
}

public static void Render(object sender, FrameEventArgs e) {
    GraphicsManager.Instance.ClearScreen(Color.CadetBlue);

    // RENDER MOUSE THINGS

    int FPS = System.Convert.ToInt32(1.0 / e.Time);
    GraphicsManager.Instance.DrawString("FPS: " + FPS, new PointF(5, 5), Color.Black);
    GraphicsManager.Instance.DrawString("FPS: " + FPS, new PointF(4, 4), Color.White);

    GraphicsManager.Instance.SwapBuffers();
}

public static void Shutdown(object sender, EventArgs e) {
    InputManager.Instance.Shutdown();
    GraphicsManager.Instance.Shutdown();
}

That's it. You are now ready to explore the inner workings of how each input device works

Clone this wiki locally