我需要为 SFML 2.0 找到等效的 SetFromRect()。我在相机类中使用它,我正在更新精灵可以自由移动的矩形的位置。一旦它到达屏幕中间,它将通过使用新坐标重新更新矩形来开始滚动相机。
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;
}
一切正常,除了:
CameraPosition.setViewport(sf::FloatRect(cameraX, cameraY, cameraX + ScreenWidth, cameraY + ScreenHeight));
它获得了所有正确的数字,但我认为这是错误的功能。
我确实尝试了 reset() 但它也没有工作。
编辑:
正确的代码是:
CameraPosition.reset(sf::FloatRect(cameraX, cameraY, ScreenWidth, ScreenHeight));
CameraPosition.setViewport(sf::FloatRect(0,0,1,1));