Updated the Tennis example to properly support touch input outside iOS/fullscreen

This commit is contained in:
Mario Liebisch 2022-10-25 10:19:52 +02:00
parent d8dd4b9bf9
commit 174a7f9391
No known key found for this signature in database
GPG Key ID: 32B56AC1F87EFBF9

View File

@ -93,8 +93,10 @@ int main()
pauseMessage.setPosition({170.f, 200.f}); pauseMessage.setPosition({170.f, 200.f});
pauseMessage.setFillColor(sf::Color::White); pauseMessage.setFillColor(sf::Color::White);
#ifdef SFML_SYSTEM_IOS #if defined(SFML_SYSTEM_IOS)
pauseMessage.setString("Welcome to SFML Tennis!\nTouch the screen to start the game."); pauseMessage.setString("Welcome to SFML Tennis!\nTouch the screen to start the game.");
#elif defined(SFML_SYSTEM_WINDOWS)
pauseMessage.setString("Welcome to SFML Tennis!\n\nPress space or touch the screen\nto start the game.");
#else #else
pauseMessage.setString("Welcome to SFML Tennis!\n\nPress space to start the game."); pauseMessage.setString("Welcome to SFML Tennis!\n\nPress space to start the game.");
#endif #endif
@ -124,7 +126,7 @@ int main()
// Space key pressed: play // Space key pressed: play
if (((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Space)) || if (((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Space)) ||
(event.type == sf::Event::TouchBegan)) ((event.type == sf::Event::TouchBegan) || (event.type == sf::Event::TouchMoved)))
{ {
if (!isPlaying) if (!isPlaying)
{ {
@ -144,6 +146,20 @@ int main()
ballAngle = sf::degrees(std::uniform_real_distribution<float>(0, 360)(rng)); ballAngle = sf::degrees(std::uniform_real_distribution<float>(0, 360)(rng));
} while (std::abs(std::cos(ballAngle.asRadians())) < 0.7f); } while (std::abs(std::cos(ballAngle.asRadians())) < 0.7f);
} }
else if (((event.type == sf::Event::TouchMoved) || (event.type == sf::Event::TouchBegan)) &&
(event.touch.finger == 0))
{
sf::Vector2f newPos = window.mapPixelToCoords({event.touch.x, event.touch.y});
// Fixed x position
newPos.x = 10.f + paddleSize.x / 2.f;
// Constraint y position movement on top
if (newPos.y < 5.f + paddleSize.y / 2)
newPos.y = 5.f + paddleSize.y / 2;
// Constraint y position movement on bottom
if (newPos.y > gameHeight - 5.f - paddleSize.y / 2)
newPos.y = gameHeight - 5.f - paddleSize.y / 2;
leftPaddle.setPosition(newPos);
}
} }
// Window size changed, adjust view appropriately // Window size changed, adjust view appropriately
@ -171,13 +187,6 @@ int main()
leftPaddle.move({0.f, paddleSpeed * deltaTime}); leftPaddle.move({0.f, paddleSpeed * deltaTime});
} }
if (sf::Touch::isDown(0))
{
sf::Vector2i pos = sf::Touch::getPosition(0);
sf::Vector2f mappedPos = window.mapPixelToCoords(pos);
leftPaddle.setPosition({leftPaddle.getPosition().x, mappedPos.y});
}
// Move the computer's paddle // Move the computer's paddle
if (((rightPaddleSpeed < 0.f) && (rightPaddle.getPosition().y - paddleSize.y / 2 > 5.f)) || if (((rightPaddleSpeed < 0.f) && (rightPaddle.getPosition().y - paddleSize.y / 2 > 5.f)) ||
((rightPaddleSpeed > 0.f) && (rightPaddle.getPosition().y + paddleSize.y / 2 < gameHeight - 5.f))) ((rightPaddleSpeed > 0.f) && (rightPaddle.getPosition().y + paddleSize.y / 2 < gameHeight - 5.f)))