Difference between revisions of "C++ Input & Movement"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Joystick Input)
(Touch Input)
 
Line 30: Line 30:
 
=Touch Input=
 
=Touch Input=
 
<syntaxhighlight lang=c++>
 
<syntaxhighlight lang=c++>
 +
if (sf::Touch::isDown(0))
 +
{
 +
    // touch 0 is down
 +
}
 +
// get global position of touch 1
 +
sf::Vector2i globalPos = sf::Touch::getPosition(1);
 +
// get position of touch 1 relative to a window
 +
sf::Vector2i relativePos = sf::Touch::getPosition(1, window);
 +
</syntaxhighlight>
  
</syntaxhighlight>
 
 
=Joystick Input=
 
=Joystick Input=
 
<syntaxhighlight lang=c++>
 
<syntaxhighlight lang=c++>

Latest revision as of 14:17, 14 June 2019

Keyboard Input

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
    // move left...
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
    // move right...
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
    // quit...
}

Mouse Input

if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
    // left click...
}
// get global mouse position
sf::Vector2i position = sf::Mouse::getPosition();
// set mouse position relative to a window
sf::Mouse::setPosition(sf::Vector2i(100, 200), window);

Touch Input

if (sf::Touch::isDown(0))
{
    // touch 0 is down
}
// get global position of touch 1
sf::Vector2i globalPos = sf::Touch::getPosition(1);
// get position of touch 1 relative to a window
sf::Vector2i relativePos = sf::Touch::getPosition(1, window);

Joystick Input

// Is joystick #0 connected?
bool connected = sf::Joystick::isConnected(0);
// How many buttons does joystick #0 support?
unsigned int buttons = sf::Joystick::getButtonCount(0);
// Does joystick #0 define a X axis?
bool hasX = sf::Joystick::hasAxis(0, sf::Joystick::X);
// Is button #2 pressed on joystick #0?
bool pressed = sf::Joystick::isButtonPressed(0, 2);
// What's the current position of the Y axis on joystick #0?
float position = sf::Joystick::getAxisPosition(0, sf::Joystick::Y);