Okay, this is as minimal as I could get a complete example which demonstrates my issue. I'm probably missing something obvious, or I have some logic error somewhere.
#include <SFML/Graphics.hpp>
int main(int argc, char **argv) {
int x=0,xs=0,ys=0,speed=25;
float left=0.0,right=0.0;
sf::RenderWindow App(sf::VideoMode(1280, 720, 32), "Game");
App.SetFramerateLimit(60);
App.UseVerticalSync(true);
sf::Image back;
back.LoadFromFile("sky.png");
sf::Sprite bg(back);
bg.Resize(1280,720);
sf::Image image;
image.LoadFromFile("character.png");
sf::Sprite sprite(image);
sf::View BGCamera(sf::FloatRect(0,0,1280,720));
sf::View Camera(sf::FloatRect(-200,-500,1280,720));
while (App.IsOpened()) {
sf::Event event;
while (App.GetEvent(event)) {
if (event.Type == sf::Event::Closed)
App.Close();
}
const sf::Input& inputs = App.GetInput();
if (inputs.IsKeyDown(sf::Key::Right)) xs=speed;
else if (inputs.IsKeyDown(sf::Key::Left)) xs=-speed;
else xs=0;
sprite.Move(xs,ys);
if (xs) sprite.FlipX(xs<0);
left=Camera.GetCenter().x-(Camera.GetSize().x/3);
right=Camera.GetCenter().x+(Camera.GetSize().x/3);
x=sprite.GetPosition().x;
if (!xs) xs=speed;
if (xs<0) xs=-xs;
if (x<left) Camera.Move(-xs,0);
if (x>right) Camera.Move(xs,0);
App.Clear();
App.SetView(BGCamera);
App.Draw(bg);
App.SetView(Camera);
App.Draw(sprite);
App.Display();
}
return 0;
}
Fixed. The camera and the sprite must move at 100% the same speed. Marking this topic as solved, hopefully I can get this fixed in my game as well.