Today I was playing around with a background I wanted to scroll, and was using the old 2 sf::Sprite technique, but I personally found that distasteful and got to thinking: sf::Texture has setRepeated, and sf::Sprite has setTextureRect, could I use these to my advantage?
And absolutely yes, what I got was a scrolling background with one sf::Sprite which gives me absolute control, I can move it, i can scale it, i can rotate it, i can do anything i want with it. without having to use any external variables either.
The code itself is really simple:
First here is the image
And here is the code
int main()
{
sf::RenderWindow app(sf::VideoMode(640, 480), "Platform Physics");
// x = wind, y = gravity
sf::Vector2f gravity(0.f, .5f);
sf::RectangleShape playerShape;
playerShape.setSize(sf::Vector2f(16, 64));
playerShape.setPosition(32, 0);
playerShape.setFillColor(sf::Color(140, 36, 58));
sf::RectangleShape floor;
floor.setPosition(0, 324);
floor.setSize(sf::Vector2f(640, 16));
sf::Vector2f velocity;
sf::Time jumpTime;
sf::Time jumpMaxTime = sf::seconds(0.1f);
sf::Clock clock;
sf::Time lastTime;
sf::Texture texture;
sf::Sprite background;
if (texture.loadFromFile("brinstar_bg1.png"))
{
texture.setRepeated(true);
background.setTexture(texture);
}
const float MAX_VELOCITY = .2f;
while(app.isOpen())
{
sf::Time currentTime = clock.restart();
lastTime = currentTime;
sf::Event event;
while(app.pollEvent(event))
{
if (event.type == sf::Event::Closed)
app.close();
}
velocity.y += gravity.y*lastTime.asSeconds();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
// Immediate stop
if (velocity.x > 0)
velocity.x = 0;
velocity.x -= .2f*lastTime.asSeconds();
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
// Immediate stop
if (velocity.x < 0)
velocity.x = 0;
velocity.x += .2f*lastTime.asSeconds();
}
else
velocity.x *= .75f;
if (velocity.x < -MAX_VELOCITY)
velocity.x = -MAX_VELOCITY;
else if (velocity.x > MAX_VELOCITY)
velocity.x = MAX_VELOCITY;
if (velocity.y < -MAX_VELOCITY)
velocity.y = -MAX_VELOCITY;
else if (velocity.y > MAX_VELOCITY)
velocity.y = MAX_VELOCITY;
playerShape.move(velocity);
if (playerShape.getPosition().x < 0)
playerShape.setPosition(0, playerShape.getPosition().y);
if (playerShape.getPosition().x + playerShape.getSize().x > app.getSize().x)
playerShape.setPosition(app.getSize().x - playerShape.getSize().x, playerShape.getPosition().y);
background.setOrigin(background.getLocalBounds().width/2, background.getLocalBounds().height/2);
background.setPosition(background.getLocalBounds().width/2, background.getLocalBounds().height/2);
background.setTextureRect(sf::IntRect(-((playerShape.getPosition().x*.125f)), -(playerShape.getPosition().y*.25f), 640, 256));
// Test rotation;
background.rotate(16.f*lastTime.asSeconds());
app.clear(sf::Color::Magenta);
app.draw(background);
app.draw(floor);
app.draw(playerShape);
app.display();
}
return 0;
}