I'm finding out that the default subrect does not change with the moving of the sprite. Please look at this compilable code. The mouse is contained within the sprite at the top left corner of the screen where it would be before the position was set. I left the 'IN BUTTON' cout statement to show what I mean.
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
int main()
{
sf::RenderWindow App(sf::VideoMode(1024,768,32),"Laraus", sf::Style::Close | sf::Style::Resize, sf::WindowSettings());
sf::Image button;
button.LoadFromFile("../Datas/paddle_left.png");
sf::Sprite Start(button);
Start.SetPosition(200,200);
sf::IntRect StartButton(Start.GetSubRect().Left, Start.GetSubRect().Top, Start.GetSubRect().Right, Start.GetSubRect().Bottom);
StartButton.Offset(200,200);
while(App.IsOpened())
{
float ElapsedTime = App.GetFrameTime();
sf::Event Event;
while(App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)App.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))App.Close();
}
// Convert local window input coordinates to game coordinates
sf::Vector2f mousePos = App.ConvertCoords(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());
// BUTTON CODE
///////////////////////////////////
if(App.GetInput().IsMouseButtonDown(sf::Mouse::Left) && StartButton.Contains(mousePos.x, mousePos.y)){
cout << "BUTTON PUSHED!!!!!" << endl;
}
else if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)){
cout << "Mouse DOWN" << endl;
}
if(App.GetInput().IsKeyDown(sf::Key::C)){
cout << "Mouse coord = " << mousePos.x << " , " << mousePos.y << endl;
cout << "Button Rect left bottom " << Start.GetSubRect().Left << " , " << Start.GetSubRect().Bottom << endl;
cout << "Button Rect right top " << Start.GetSubRect().Right << " , " << Start.GetSubRect().Top << endl;
}
if (Start.GetSubRect().Contains(mousePos.x, mousePos.y))
cout << "In BUTTON!!" << endl;
App.Clear();
App.Draw(Start);
App.Display();
} // while(App.IsOpened())
return 0;
}
As you can see, I got it to do what I wanted, but I feel like there has to be an easier way... Any suggestions?