SFML community forums

Help => Graphics => Topic started by: Raid3R on July 05, 2013, 11:46:13 pm

Title: Need Help With Scrolling
Post by: Raid3R on July 05, 2013, 11:46:13 pm
Hello guys....
i need help figuring out how to screen scroll...
here is the code i have so far....
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>

int main()
{
        enum Direction{DOWN,LEFT,RIGHT,UP};
        sf::Vector2f screenDimension(800,463),bPosition(0.0f,0.0f);
        sf::Vector2i source(1,RIGHT), spriteDimension(32,32);
        sf::RenderWindow window;
        std::string string;
        sf::Texture bTexture, pTexture;
        sf::Sprite bImage, pImage;
       
        window.create(sf::VideoMode((unsigned int)screenDimension.x,(unsigned int)screenDimension.y),"Text",sf::Style::Close | sf::Style::Titlebar);

        if(!bTexture.loadFromFile("landscape.png"))
                std::cout<<"Background image didnt load...\n";
        bImage.setTexture(bTexture);
       
        if(!pTexture.loadFromFile("Player.png"))
                std::cout<<"Player Image didn't load...\n";
        pImage.setTexture(pTexture);
        pImage.setPosition(3,355.5f);

        sf::View view;
        view.reset(sf::FloatRect(0.0f,0.0f,screenDimension.x,screenDimension.y));
        view.setViewport(sf::FloatRect(0,0,1.0f,1.0f));

        sf::Clock clock;
        float moveSpeed = 2500.0f;

        sf::Vector2f position(0,0);
        while(window.isOpen())
        {
                clock.restart();
                sf::Event mainEvent;
                while(window.pollEvent(mainEvent))
                {
                        switch(mainEvent.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::KeyPressed:
                                if(mainEvent.key.code==sf::Keyboard::Escape)
                                        window.close();
                                break;
                        }
                }
                source.x++;
                if(source.x*32 >= pTexture.getSize().x)
                        source.x=0;
                if((unsigned int)source.x*32 >= pTexture.getSize().x)
                        source.x=0;
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        pImage.move(moveSpeed*clock.getElapsedTime().asSeconds(),0);
                        source.y=RIGHT;
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        pImage.move(-moveSpeed*clock.getElapsedTime().asSeconds(),0);
                        source.y=LEFT;
                }

                pImage.setTextureRect(sf::IntRect(source.x*32,source.y*32,32,32));
                position.x = pImage.getPosition().x + (pImage.getPosition().x/2)-(screenDimension.x/2);
                position.y = pImage.getPosition().y + (pImage.getPosition().y/2)- (screenDimension.y/2);

                if(position.x < 0)
                        position.x=0;
                if(position.y < 0)
                        position.y=0;
               
               
               
                view.reset(sf::FloatRect(position.x,position.y,screenDimension.x,screenDimension.y));
                window.draw(bImage);
                window.setView(view);
                window.draw(pImage);
               
                window.display();
                window.clear();
        }

}
 
(http://i40.tinypic.com/1zsojq.jpg)

and a next problem is that, how can i stop my sprite animation to stop when it stop moving

Any Help is appreciated...
Title: Re: Need Help With Scrolling
Post by: AFS on July 06, 2013, 06:30:57 am
But what type of scrolling do you want?

If you want to keep your character always on the center of the screen, use this code AFTER the code that moves the character.
// Move the center of the view to the center of the character.
int X = pImage.getPosition().x;
int Y = pImage.getPosition().y;
view.setCenter(X, Y);

// or simply use...
view.setCenter(pImage.getPosition().x, pImage.getPosition().y);
 

Now, if you want a different kind of scrolling, you still need to use either "view.setCenter(X,Y)" or "view.move(X,Y)".

About the animation, I'm afraid I don't quite get how you are doing it, but try putting your animation code inside an "if" statement that is true if you either press the left key or the right key.
Title: Re: Need Help With Scrolling
Post by: Raid3R on July 06, 2013, 07:28:30 pm
Thanks alot, it worked... ;D