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 .
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.
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;
}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
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.
- In order to read the Digital Gamepad Input, you use the
SRL::Input::Digitalclass. - 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 whereButtoncan take the following values :SRL::Input::Digital::Button::RightSRL::Input::Digital::Button::LeftSRL::Input::Digital::Button::DownSRL::Input::Digital::Button::UpSRL::Input::Digital::Button::STARTSRL::Input::Digital::Button::ASRL::Input::Digital::Button::BSRL::Input::Digital::Button::CSRL::Input::Digital::Button::XSRL::Input::Digital::Button::YSRL::Input::Digital::Button::ZSRL::Input::Digital::Button::RSRL::Input::Digital::Button::L
- You can test if several keys are pressed at the same time by using the
&&operator with severalisHeld()functions.