i guess you need to create window boundary as sf::FloatRect.
sf::FloatRect windowBounds(sf::Vector2f(0.f, 0.f), window.getDefaultView().getSize());
then at end of update adapt the shape postilion based on whither or not it is within window boundary. it can be done by using std::min and std::max instead of using bock of if-statements
here simple example by using sf::RectangleShape. to show the idea.
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window({720, 480}, "test");
sf::FloatRect windowBounds({0.f, 0.f}, window.getDefaultView().getSize());
sf::RectangleShape shape({100.f, 100.f});
// set position in the middle of window
shape.setPosition((window.getView().getSize() - shape.getSize())/2.f);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
shape.move(-0.1, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
shape.move(0.1, 0);
}
// adapt shape position tobe within window boundary
sf::Vector2f position = shape.getPosition();
position.x = std::max(position.x, windowBounds.left);
position.x = std::min(position.x, windowBounds.left + windowBounds.width - shape.getSize().x);
shape.setPosition(position);
window.clear();
window.draw(shape);
window.display();
}
}