Using DirectXTK Input for Keyboard, Mouse & Gamepad
For our game we have decided to make use of DirectXTK Toolkit that contains helper classes to work around with Direct3D 11with the help of DirectX SDK. We can utilize it for stuff like making audio using their Audio API, Sprite Batch & Font for rendering UI and text, finally game input through mouse, keyboard and gamepad class.
This blog is going to be about using input singleton. You guys are ready, I'm about to blow your mind!

Keyboard, Mouse & Gamepad
// Resource Manager - Initializing at resource manager class
std::unique_ptr<Keyboard>m_Keyboard;
std::unique_ptr<Mouse>m_Mouse;
std::unique_ptr<Gamepad>m_GamePad;
//Call reset
m_Keyboard.reset(new Keyboard());
m_Mouse.reset(new Mouse());
m_GamePad.reset(new GamePad());
ResourceManager* GetResource() { return m_refManager;}
//Using it inside the player class - Object of the singleton
Keyboard::State kb = GetResource()->m_Keyboard->GetState();
Mouse::State ms = GetResource()->m_Mouse->GetState();
GamePad::State gp = GetResource()->m_GamePad->GetState();
//Check if kb.W is pressed move the player forward and dynamically change the camera with it
if((gp.isConnected() && gp.isDPadUpPressed()) || kb.GetLastState().W)
{
//Character Movement
MoveForward();
//Rotate the forward/view matrix to look at the target
RotatePlayer();
//Switch the animation
m_AnimController.SwitchAnimation(PlayerActions::WALKING);
}
//Check to see if ms.RightButton or x button on gamepad is pressed, we wanna do some action
if(ms.rightButton || gp.isXPressed())
{
//Attack the target
attackTarget = true;
//Switch Animation
m_AnimController.SwitchAnimation(PlayerActions::ATTACKING);
}
//Press and Release Button State Tracker
GamePad::ButtonStateTracker gbt;
gbt.Update(resourceManager->m_GamePad->GetState(0);
if((gp.isConnected() && gbt.start == GamePad()::ButtonStateTracker::PRESSED) || kb.GetLastState().Enter())
{
//Start the game
}
This blog is going to be about using input singleton. You guys are ready, I'm about to blow your mind!
Keyboard, Mouse & Gamepad
// Resource Manager - Initializing at resource manager class
std::unique_ptr<Keyboard>m_Keyboard;
std::unique_ptr<Mouse>m_Mouse;
std::unique_ptr<Gamepad>m_GamePad;
//Call reset
m_Keyboard.reset(new Keyboard());
m_Mouse.reset(new Mouse());
m_GamePad.reset(new GamePad());
ResourceManager* GetResource() { return m_refManager;}
//Using it inside the player class - Object of the singleton
Keyboard::State kb = GetResource()->m_Keyboard->GetState();
Mouse::State ms = GetResource()->m_Mouse->GetState();
GamePad::State gp = GetResource()->m_GamePad->GetState();
//Check if kb.W is pressed move the player forward and dynamically change the camera with it
if((gp.isConnected() && gp.isDPadUpPressed()) || kb.GetLastState().W)
{
//Character Movement
MoveForward();
//Rotate the forward/view matrix to look at the target
RotatePlayer();
//Switch the animation
m_AnimController.SwitchAnimation(PlayerActions::WALKING);
}
//Check to see if ms.RightButton or x button on gamepad is pressed, we wanna do some action
if(ms.rightButton || gp.isXPressed())
{
//Attack the target
attackTarget = true;
//Switch Animation
m_AnimController.SwitchAnimation(PlayerActions::ATTACKING);
}
//Press and Release Button State Tracker
GamePad::ButtonStateTracker gbt;
gbt.Update(resourceManager->m_GamePad->GetState(0);
if((gp.isConnected() && gbt.start == GamePad()::ButtonStateTracker::PRESSED) || kb.GetLastState().Enter())
{
//Start the game
}
Comments
Post a Comment