Accepting Input

Closing the game with a key, rotating an object with the mouse.

Since we now care about our code running alongside the game, we now need to write all the code in between the update call and render call so that we have latest inputs and not too late for rendering.

Lets start of by using the include library #include <Unified-Engine/input/input.h>. This library gives us access to an item called InputPointer which will hold all methods of checking for mouse movement, mouse presses and keyboard presses.

Let's start by seeing if the user presses Escape and then we can close the window if so: (Don't forget this goes in-between the update and render functions in the game loop, otherwise it wont keep running on each frame.

if(UnifiedEngine::InputPointer->Keyboard.KeyPressed(UnifiedEngine::Key_ESCAPE)){
    break;
}

Now test, this should close the game whenever you press escape. It's as simple as that. How about mouse add || UnifiedEngine::InputPointer->Mouse.MousePressed(UnifiedEngine::Mouse_1) and you should find that the right click will also close.

Now movements is a bit different, it is still accessable through InputPointer->Mouse but instead you can get position using UnifiedEngine::InputPointer->Mouse.GetPosition() which returns a 2D vector that you can use the X and Y coordinates to calcualte a difference for movement. We will make use of this in a later project.

Scrolling is similar and you can use UnifiedEngine::InputPointer->Mouse.Scroll to get either 1, 0, -1 for direction or neutral. See if you can get the scroll to control the direction of the cube rotation.

Last updated