Skip to content

Latest commit

 

History

History
115 lines (86 loc) · 4.22 KB

File metadata and controls

115 lines (86 loc) · 4.22 KB

Input Handling

For handling input the SRL library provides the SRL::Input namespace.

In order to keep the function calls short we will use using namespace SRL::Input; at the beginning of our file, similarly to what we've done with SRL::Types and SRL::Math::Types .

Digital Gamepad

Quick start

First we must declare a SRL::Input::Digital. We must provide the port number to the constructor. Since we already have declared at the beginning that we are also using the SRL::Input namespace, we can initiate it by :

Digital port(0); // Initializes the controller port 0 (the first controller port)

This class stores the gamepad's button states during the execution of our program.

Once it is initialized, we must check its state inside the render loop.

Before reading the input from the gamepad, we must check if it's connected to the console.

Is it connected ?

We must check if the gamepad is connected via IsConnected() method.

The code, to Initialize the gamepad port, and check if its connected, is shown below :

#include <srl.hpp>

// Using to shorten names for Vector and HighColor
using namespace SRL::Types;
using namespace SRL::Math::Types;
using namespace SRL::Input; // Using to shorten names for input

int main()
{
    SRL::Core::Initialize(HighColor::Colors::Black); // Initialize library
    SRL::Debug::Print(1,1, "04_Tutorial");
    Digital port(0); // Initializes the controller port 0 (the first controller port)

    while(1) // Main program loop
	{   
      SRL::Debug::PrintClearLine(2);
      if(port.IsConnected())
      {
        SRL::Debug::Print(1,2, "Connected");
      }else
      {
        SRL::Debug::Print(1,2, "Not connected");
      }
      SRL::Core::Synchronize(); // Refresh screen
	}

	return 0;
}

Is it being pressed ?

Now that we can determine if the gamepad is plugged in, we can check if there are any buttons pressed. This is done with SRL::Input::Digital::IsHeld method.

For example, if I want to check if the Up button is pressed on the gamepad this is done by :

if(port.IsHeld(Digital::Button::Up) == true)
{
    SRL::Debug::Print(1,3, "UP is pressed");
}else
{
    SRL::Debug::Print(1,3, "UP is NOT pressed");
}

The Button definition list can be found here

Multiple buttons at the same time

If you wish to see if 2 or more buttons are being pressed at the same time (for example for diagonals), you can do :

 if((port.IsHeld(Digital::Button::Up) && port.IsHeld(Digital::Button::Right)) == true)
    {
        SRL::Debug::Print(1,3, "UP + Right are being pressed");
    }

Warning

You may be tempted to use port.IsHeld(Digital::Button::Up | Digital::Button::Right) however it wont work since Digital::Button is an enum class.

Summary

  • In order to read the Digital Gamepad Input, you use the SRL::Input::Digital class.
  • When creating the Object to read the gamepad, you must provide the gamepad port number. The first port number is 0.
  • It is always a best practice to check if gamepad is connected. This is done via IsConnected() method.
  • To read what a given button is pressed , you use the IsHeld(const Button &button) method where Button can take the following values :
    • SRL::Input::Digital::Button::Right
    • SRL::Input::Digital::Button::Left
    • SRL::Input::Digital::Button::Down
    • SRL::Input::Digital::Button::Up
    • SRL::Input::Digital::Button::START
    • SRL::Input::Digital::Button::A
    • SRL::Input::Digital::Button::B
    • SRL::Input::Digital::Button::C
    • SRL::Input::Digital::Button::X
    • SRL::Input::Digital::Button::Y
    • SRL::Input::Digital::Button::Z
    • SRL::Input::Digital::Button::R
    • SRL::Input::Digital::Button::L
  • You can test if several keys are pressed at the same time by using the && operator with several isHeld() functions.