Try this example.
#include <iostream>
#include <SFML\Graphics.hpp>
#include <vector>
using namespace sf ;
using namespace std ;
int main( int argc, char* argv[] )
{
bool exit = false ;
std::string Action ;
RenderWindow window( VideoMode( 800 , 600 ) , "myWindow" ) ;
window.setActive( false ) ;
window.setFramerateLimit( 65 ) ;
RectangleShape s_startGame( Vector2f( 300 , 100 ) ) ;
s_startGame.setFillColor( Color::Red ) ;
s_startGame.setPosition( 280 , 225 ) ;
while ( window.isOpen() )
{
Event event ;
while ( window.pollEvent( event ) )
{
if ( event.type == Event::Closed )
window.close() ;
if ( event.type == Event::MouseButtonPressed )
{
if ( event.mouseButton.button == sf::Mouse::Left )
{
Vector2i MousePos = Mouse::getPosition( window ) ;
FloatRect objPos = s_startGame.getGlobalBounds() ;
if ( objPos.contains( MousePos.x , MousePos.y ) )
{
if( exit )
{ exit = false ; Action = "Exit" ; }
else
{ exit = true ; Action = "Enter" ; }
cout << "Touched by mouse, action: " << Action << endl ;
}
}
}
}
window.clear() ;
window.draw( s_startGame ) ;
window.display() ;
}
return 0 ;
}
Please don't do that:
if (sf::Mouse::getPosition(window).y > s_startGame.getGlobalBounds().top &&
sf::Mouse::getPosition(window).y < s_startGame.getGlobalBounds().height + s_startGame.getGlobalBounds().top &&
sf::Mouse::getPosition(window).x < s_startGame.getGlobalBounds().width + s_startGame.getGlobalBounds().left &&
sf::Mouse::getPosition(window).x > s_startGame.getGlobalBounds().left)
Instead store the result in a variable and use the x,y / top,left,width,height properties
Vector2i mousePos = sf::Mouse::getPosition(window) ;
doWhatEver( mousePos.x , mousePos.y ) ;
And use the built-in method to check if a point intersect or contain.
FloatRect objRect = s_startGame.getGlobalBounds() ;
// objRect.contains( x , y )
// objRect.intersects( FloatRect( x , y , width , height ) )