Wasn't easy, it seems to happen at very particular window configurations. By example, I got it to occur in windowed mode in 650x480, but not at 640x480 or 649x490... odd. And it only happens if RenderWindow is initialized with the flag "sf::Style::Close". I was testing it in my desktop's resolution at 1280x720 and it also occurred.
By the way, I didn't mention that I'm running SFML in Windows XP.
The code below creates a window, draws a rectangle at 50,50 to 150,150 and moves the camera to the left if the user places the mouse close to the left screen edge (stopping when the view's left side == 0) or to the right if the user places the mouse close to the right edge.
The bug is detected when the user places the mouse very near the left screen edge, where the x mouse coordinate turns from 0 to the large value, while the MouseLeft event is not launched, and the camera pans incorrectly (to the right instead of left or stopping).
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
sf::Vector2f getCameraPosition(sf::RenderWindow& rw)
{
sf::Vector2f pos(rw.GetDefaultView().GetCenter());
sf::Vector2f halfDims(rw.GetDefaultView().GetHalfSize());
return sf::Vector2f(pos.x - halfDims.x, pos.y - halfDims.y);
}
void setCameraPosition(sf::RenderWindow& rw, const sf::Vector2f& newPosition)
{
sf::Vector2f halfDims(rw.GetDefaultView().GetHalfSize());
rw.GetDefaultView().SetCenter(newPosition.x + halfDims.x, newPosition.y + halfDims.y);
}
int main()
{
sf::Clock Clock;
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(650, 480, 32), "SFML Graphics", sf::Style::Close);
bool mouseIsInside = true;
sf::Shape rectangle = sf::Shape::Rectangle(50, 50, 150, 150, sf::Color(255,0,0,255));
// Start game loop
while (App.IsOpened())
{
float timeSinceLastUpdate = Clock.GetElapsedTime();
Clock.Reset();
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
if(Event.Type == sf::Event::MouseLeft)
mouseIsInside = false;
if(Event.Type == sf::Event::MouseEntered)
mouseIsInside = true;
}
sf::Vector2f mousePosition(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());
sf::Vector2f cameraPosition = getCameraPosition(App);
float scrollSpeed = 100;
float coordLeftWindowBorder = cameraPosition.x;
if(mouseIsInside)
{
std::cout << "mouse pos:" << mousePosition.x << ", "<< mousePosition.y << "\n";
if(mousePosition.x > 590)
setCameraPosition(App, cameraPosition + sf::Vector2f(scrollSpeed * timeSinceLastUpdate,0));
else
if((mousePosition.x < 0 + 50) && (coordLeftWindowBorder > 0))
setCameraPosition(App, cameraPosition - sf::Vector2f(scrollSpeed * timeSinceLastUpdate,0));
}
//else
// std::cout << "out ";
App.Draw(rectangle);
// Display window contents on screen
App.Display();
// Clear the screen (fill it with black color)
App.Clear();
}
return EXIT_SUCCESS;
}