From 174a7f9391bb9abeb19428ef28e7feb7d5705201 Mon Sep 17 00:00:00 2001 From: Mario Liebisch Date: Tue, 25 Oct 2022 10:19:52 +0200 Subject: [PATCH] Updated the Tennis example to properly support touch input outside iOS/fullscreen --- examples/tennis/Tennis.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/examples/tennis/Tennis.cpp b/examples/tennis/Tennis.cpp index 20939f5e6..84e3b7b54 100644 --- a/examples/tennis/Tennis.cpp +++ b/examples/tennis/Tennis.cpp @@ -93,8 +93,10 @@ int main() pauseMessage.setPosition({170.f, 200.f}); 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."); +#elif defined(SFML_SYSTEM_WINDOWS) + pauseMessage.setString("Welcome to SFML Tennis!\n\nPress space or touch the screen\nto start the game."); #else pauseMessage.setString("Welcome to SFML Tennis!\n\nPress space to start the game."); #endif @@ -124,7 +126,7 @@ int main() // Space key pressed: play 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) { @@ -144,6 +146,20 @@ int main() ballAngle = sf::degrees(std::uniform_real_distribution(0, 360)(rng)); } 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 @@ -171,13 +187,6 @@ int main() 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 if (((rightPaddleSpeed < 0.f) && (rightPaddle.getPosition().y - paddleSize.y / 2 > 5.f)) || ((rightPaddleSpeed > 0.f) && (rightPaddle.getPosition().y + paddleSize.y / 2 < gameHeight - 5.f)))