Update examples for iOS including touch support, proper handling of window resize, launch screens and icons

This commit is contained in:
Jonny Paton 2019-01-18 15:04:03 +00:00 committed by Lukas Dürrenberger
parent 9f4a0c3c4f
commit 353c846c87
12 changed files with 141 additions and 9 deletions

View file

@ -84,7 +84,12 @@ int main()
pauseMessage.setCharacterSize(40);
pauseMessage.setPosition(170.f, 150.f);
pauseMessage.setFillColor(sf::Color::White);
#ifdef SFML_SYSTEM_IOS
pauseMessage.setString("Welcome to SFML pong!\nTouch the screen to start the game");
#else
pauseMessage.setString("Welcome to SFML pong!\nPress space to start the game");
#endif
// Define the paddles properties
sf::Clock AITimer;
@ -134,6 +139,15 @@ int main()
while (std::abs(std::cos(ballAngle)) < 0.7f);
}
}
// Window size changed, adjust view appropriately
if (event.type == sf::Event::Resized)
{
sf::View view;
view.setSize(gameWidth, gameHeight);
view.setCenter(gameWidth/2.f, gameHeight/2.f);
window.setView(view);
}
}
if (isPlaying)
@ -154,8 +168,9 @@ int main()
if (sf::Touch::isDown(0))
{
sf::Vector2i pos = sf::Touch::getPosition(0, window);
leftPaddle.setPosition(0.f, pos.y);
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
@ -181,16 +196,22 @@ int main()
float factor = ballSpeed * deltaTime;
ball.move(std::cos(ballAngle) * factor, std::sin(ballAngle) * factor);
#ifdef SFML_SYSTEM_IOS
const std::string inputString = "Touch the screen to restart";
#else
const std::string inputString = "Press space to restart or\nescape to exit";
#endif
// Check collisions between the ball and the screen
if (ball.getPosition().x - ballRadius < 0.f)
{
isPlaying = false;
pauseMessage.setString("You lost!\nPress space to restart or\nescape to exit");
pauseMessage.setString("You Lost!\n" + inputString);
}
if (ball.getPosition().x + ballRadius > gameWidth)
{
isPlaying = false;
pauseMessage.setString("You won!\nPress space to restart or\nescape to exit");
pauseMessage.setString("You Won!\n" + inputString);
}
if (ball.getPosition().y - ballRadius < 0.f)
{