Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: A better scrolling background technique  (Read 4213 times)

0 Members and 1 Guest are viewing this topic.

Antidote

  • Newbie
  • *
  • Posts: 35
    • View Profile
A better scrolling background technique
« 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


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;
}
 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: A better scrolling background technique
« Reply #1 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  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Antidote

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: A better scrolling background technique
« Reply #2 on: August 06, 2013, 02:39:18 am »
I didn't post it here, it got moved here, but I see your point.

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: A better scrolling background technique
« Reply #3 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.
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: A better scrolling background technique
« Reply #4 on: August 10, 2013, 09:15:16 am »
What are you talking about?

alluvian

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: A better scrolling background technique
« Reply #5 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.

 

anything