So the problem is that I am trying to click on the window to place an object on the screen. It works perfectly until I add views.
When I move the view to the right 100 pixels and click on the position of (300, 300), then the object is moved to the new view coordinates of (200, 300). I need the object to be drawn to the new view coordinates of (300, 300). I have experimented with the mapCoordsToPixel function but I can't get it working.
Here is the sample code:
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(600, 400), "View");
window.setFramerateLimit(60);
sf::CircleShape circle;
circle.setFillColor(sf::Color::Black);
circle.setRadius(15);
sf::View view = window.getView();
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
}
if( event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left )
{
circle.setPosition(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A))
view.move(-5.f, 0.f);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D))
view.move(5.f, 0.f);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W))
view.move(0.f, -5.f);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S))
view.move(0.f, 5.f);
window.clear(sf::Color::White);
window.setView(view);
window.draw(circle);
window.display();
}
return 0;
}