SFML community forums

Help => Graphics => Topic started by: Antidote on August 05, 2013, 08:49:31 pm

Title: A better scrolling background technique
Post by: Antidote on August 05, 2013, 08:49:31 pm
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
(http://dl.dropboxusercontent.com/u/21757902/brinstar_bg1.png)

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;
}
 
Title: Re: A better scrolling background technique
Post by: zsbzsb on August 06, 2013, 12:11:51 am
Maybe you should have posted this in the wiki section since this isn't a question, but rather an example  ;)
Title: Re: A better scrolling background technique
Post by: Antidote on August 06, 2013, 02:39:18 am
I didn't post it here, it got moved here, but I see your point.
Title: Re: A better scrolling background technique
Post by: BaneTrapper on August 09, 2013, 11:21:14 pm
What you want to do is
sf::Sprite spr.setSize(INFINITE, INFINITE); "or the texture?"
But then drawing is slow...
So that actually is bad.
Title: Re: A better scrolling background technique
Post by: G. on August 10, 2013, 09:15:16 am
What are you talking about?
Title: Re: A better scrolling background technique
Post by: alluvian on August 13, 2013, 10:56:31 pm
Sounds interesting.  I am BRAND new to SFML (found it researching engines / libraries for a new game, not even sure if I want to go 2d or 3d yet), but I am curious if you have done any performance tests to see how this compares to 'standard' 2 sprite method.