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

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Mouse Input)
(Joystick Input)
Line 34: Line 34:
 
=Joystick Input=
 
=Joystick Input=
 
<syntaxhighlight lang=c++>
 
<syntaxhighlight lang=c++>
 
+
// 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);
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 13: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

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);