I need to find the equivalent of SetFromRect() for SFML 2.0. I’m using it in a camera class where I am updating the position of the rectangle where the sprite can move freely. Once it reaches the middle of the screen it will start scrolling the camera by re-updating the rectangle with the new co-ordinates.
void Camera::Update(int x, int y)
{
cameraX = -(ScreenWidth / 2) + x;
cameraY = -(ScreenHeight / 2) + y;
std::cout << "cameraX: " << cameraX << std::endl;
std::cout << "cameraY: " << cameraY << std::endl;
if(cameraX < 0)
cameraX = 0;
if(cameraY < 0)
cameraY = 0;
CameraPosition.setViewport(sf::FloatRect(cameraX, cameraY, cameraX + ScreenWidth, cameraY + ScreenHeight));
std::cout << "cameraX + ScreenWidth: " << cameraX + ScreenWidth << std::endl;
std::cout << "cameraY + ScreenWidth: " << cameraY + ScreenWidth << std::endl;
}
That all works fine except for:
CameraPosition.setViewport(sf::FloatRect(cameraX, cameraY, cameraX + ScreenWidth, cameraY + ScreenHeight));
It’s obtaining all the right numbers but I think it’s the wrong function for it.
I did try reset() but it didn’t work either.
EDIT:
The correct code is:
CameraPosition.reset(sf::FloatRect(cameraX, cameraY, ScreenWidth, ScreenHeight));
CameraPosition.setViewport(sf::FloatRect(0,0,1,1));
SFML 2.0 rectangle types were changed from (left, top, right, bottom) to (left, top, width, height). I make that mistake too all the time.
To move the camera around all you need to do is to move the
sf::View. In your code that is as simple as callingCameraPosition.setCenter(x, y);.