Thread: [SFML C++] Help me with screen scrolling

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    [SFML C++] Help me with screen scrolling

    Hello. I've looked on screen scrolling and one of the way to do it is to use the sf::View::setViewport(sf::floatRect( left co-dt, top co-dt, rect width, rect height).

    But the problem now is that even after I write the code as below (following some tutorials on SFML 1.6, and I understand how it works, at least on SFML 1.6), the camera still doesn't move even after I walk the character towards the center of the screen.

    The sf:floatRect effects is there (appears like it zoomed out of the screen, not that I intended to do so), but still the screen isn't scrolling like I thought it would. I think I'm confused this using the setFromRect SFML 1.6 with setViewPort SFML 2.0

    Here's the codes:

    Main.cpp
    Code:
    #include <SFML/Graphics.hpp>
    #include <iostream>
    
    
    #include "Global.h"
    #include "Player.h"
    #include "Map.h"
    
    
    int main()
    {
        sf::RenderWindow Window(sf::VideoMode(ScreenWidth,ScreenHeight,32),"SFML ADVANCED SPRITE TUTORIAL 2" );
    
    
        Player player;
        Camera camera;
        Map map;
    
    
        player.LoadTexture();
        player.Initialize();
        camera.Initialize();
        map.loadMap();
    
    
    
    
        while(Window.isOpen())
        {
            sf::Event Event;
    
    
            while(Window.pollEvent(Event))
            {
                if(Event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                {
                    Window.close();
                }
            }//END EVENT LOOP
    
    
            player.Update(Window);
            camera.Update( player.getCameraX() , player.getCameraY() );
    
    
            Window.clear();
            map.Draw(Window);
            player.Draw(Window);
            Window.display();
    
    
            system("cls");
    
    
        }
    
    
        return 0;
    }
    Animation.h
    Code:
    #ifndef ANIMATION_H
    #define ANIMATION_H
    
    
    class Animation
    {
        public:
            Animation();
            virtual ~Animation();
    
    
            void setTexture(sf::Texture &tempTexture);
    
    
            void Draw(sf::RenderWindow &Window);
            void Update(sf::RenderWindow &Window);
            void Initialize(float &x, float &y, int amountOfFramesX, int amountOfFramesY, float textureWidth, float textureHeight);
    
    
            float getPositionX(float x);
            float getPositionY(float y);
    
    
            float getFrameHeight();
            float getFrameWidth();
    
    
            bool getMoveActive();
            void setMoveActive(bool MoveActive);
    
    
            void setCurrentFrameY(int FrameY);
    
    
            float getClock();
    
    
        protected:
    
    
        private:
            sf::Texture tempTexture;
            sf::Sprite tempSprite;
            sf::Clock clock;
    
    
            float x, y;
            float move_x;
            float move_y;
    
    
            float frameHeight, frameWidth;
            int amountOfFramesX, amountOfFramesY;
            int textureWidth, textureHeight;
            bool MoveActive_activated;
    
    
            int currentFrameX, currentFrameY;
    
    
            float frameTime;
    
    
    };
    
    
    #endif // ANIMATION_H
    Animation.cpp
    Code:
    #include <SFML/Graphics.hpp>
    #include <iostream>
    
    
    #include "Animation.h"
    
    
    Animation::Animation()
    {
        //ctor
    }
    
    
    Animation::~Animation()
    {
        //dtor
    }
    
    
    bool Animation::getMoveActive()
    {
        return MoveActive_activated;
    }
    
    
    float Animation::getClock()
    {
        return clock.getElapsedTime().asSeconds();
    }
    
    
    
    
    void Animation::setMoveActive(bool MoveActive)
    {
        MoveActive_activated = MoveActive;
    }
    
    
    void Animation::setCurrentFrameY(int FrameY)
    {
        currentFrameY = FrameY;
    }
    
    
    
    
    void Animation::Initialize(float &x, float &y, int amountOfFramesX , int amountOfFramesY, float textureWidth, float textureHeight)
    {
        this-> x = x;
        this-> y = y;
        this-> amountOfFramesX = amountOfFramesX;
        this-> amountOfFramesY = amountOfFramesY;
        this-> textureWidth = textureWidth;
        this-> textureHeight = textureHeight;
    
    
        currentFrameY = 0;
    
    
    }
    
    
    float Animation::getFrameWidth()
    {
        frameWidth = (float) textureWidth / amountOfFramesX;
        return frameWidth;
    }
    
    
    float Animation::getFrameHeight()
    {
        frameHeight = (float) textureHeight / amountOfFramesY;
        return frameHeight;
    }
    
    
    float Animation::getPositionX(float x)
    {
        move_x = x;
        return move_x;
    }
    
    
    float Animation::getPositionY(float y)
    {
        move_y = y;
        return move_y;
    }
    
    
    void Animation::Update(sf::RenderWindow &Window)
    {
        std::cout << "setPosition move_x: " << move_x << std::endl;
        std::cout << "setPosition move_y: " << move_y << std::endl;
        std::cout << "frameWidth: " << getFrameWidth() << std::endl;
        std::cout << "frameHeight: " << getFrameHeight() << std::endl;
        std::cout << "Texture width: " << textureWidth << std::endl;
        std::cout << "Texture height: " << textureHeight << std::endl;
        std::cout << "Amount of frames X: " << amountOfFramesX << std::endl;
        std::cout << "Amount of frames Y: " << amountOfFramesY << std::endl;
        std::cout << "-----------" << std::endl;
        std::cout << "currentFrameX: " << currentFrameX << std::endl;
        std::cout << "currentFrameY: " << currentFrameY << std::endl << std::endl;
        std::cout << "In tempSprite.setTextureRect(sf::IntRect(x1,y1,frameWidth,frameHeight)) : " << std::endl << std::endl;
        std::cout << "x1: " << currentFrameX * frameWidth << std::endl;
        std::cout << "y1: " << currentFrameY * frameHeight << std::endl;
        std::cout << "frameHeight: " << frameWidth << std::endl;
        std::cout << "frameWidth: " << frameHeight << std::endl;
        std::cout << "frameTime: " << frameTime << std::endl;
    
    
        if(MoveActive_activated)
        {
            frameTime = getClock();
    
    
            std::cout <<"[MoveActive_activated is now TRUE] " << std::endl;
            if ( frameTime > 0.2) //change to next currentFrameX after 2 second.
            {
                currentFrameX++;
                clock.restart();
                if( currentFrameX >= amountOfFramesX)
                   {
                       currentFrameX = 0;
                   }
            }
        }
        else
        {
            currentFrameX = 1;
        }
    
    
        tempSprite.setTextureRect(sf::IntRect( currentFrameX * frameWidth , currentFrameY * frameHeight , frameWidth , frameHeight ));
        tempSprite.setPosition(move_x,move_y);
    
    
    }
    
    
    void Animation::setTexture(sf::Texture &tempTexture)
    {
        tempSprite.setTexture(tempTexture);
    }
    
    
    void Animation::Draw(sf::RenderWindow &Window)
    {
        Window.draw(tempSprite);
    }
    Player.h
    Code:
    #ifndef PLAYER_H
    #define PLAYER_H
    
    
    #include "Animation.h"
    #include "Camera.h"
    
    
    class Player
    {
        public:
            Player();
            virtual ~Player();
    
    
            void LoadTexture();
            void Initialize();
            void Draw(sf::RenderWindow &Window);
            void Update(sf::RenderWindow &Window);
    
    
            float getCameraX();
            float getCameraY();
    
    
        protected:
    
    
            Animation playerAnimation;
            Camera camera;
        private:
    
    
            sf::Texture playerTexture;
    
    
            int textureWidth, textureHeight;
    
    
            float x , y;
            float moveSpeed;
    };
    
    
    #endif // PLAYER_H
    Player.cpp
    Code:
    #include <SFML/Graphics.hpp>
    #include <iostream>
    
    
    #include "Player.h"
    #include "Animation.h"
    
    
    Player::Player()
    {
        //ctor
    }
    
    
    Player::~Player()
    {
        //dtor
    }
    
    
    
    
    void Player::LoadTexture()
    {
        if(!playerTexture.loadFromFile("C:/Program Files (x86)/CodeBlocks/Projects/SFML ANIMATION ADVANCED TUTORIAL 2/resources/Sprite animation/$Cloaked dude.png"))
             {
                std::cout << "Error loading image\n";
             }
             else
             {
                playerAnimation.setTexture(playerTexture);
                std::cout << "Loaded the image.png" << std::endl;
                // system("pause");
             }
    
    
    }
    
    
    void Player::Initialize()
    {
        std::cout << "Initializing from Player::Initialize()..." << std::endl;
        x = 0;
        y = 0;
        moveSpeed = 25;
        textureWidth = playerTexture.getSize().x;
        textureHeight = playerTexture.getSize().y;
    
    
        playerAnimation.Initialize(x,y,3,4,textureWidth,textureHeight);
    }
    
    
    void Player::Update(sf::RenderWindow &Window)
    {
    
    
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) //UP
        {
            playerAnimation.setMoveActive(true);
            y -= moveSpeed * playerAnimation.getClock();
            playerAnimation.setCurrentFrameY(3);
            std::cout << "Moved up" << std::endl;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) //DOWN
        {
            playerAnimation.setMoveActive(true);
            y += moveSpeed * playerAnimation.getClock();
            playerAnimation.setCurrentFrameY(0);
            std::cout << "Moved down" << std::endl;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) //LEFT
        {
            playerAnimation.setMoveActive(true);
            x -= moveSpeed * playerAnimation.getClock();;
            playerAnimation.setCurrentFrameY(1);
            std::cout << "Moved left" << std::endl;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) //RIGHT
        {
            playerAnimation.setMoveActive(true);
            x += moveSpeed * playerAnimation.getClock();
            playerAnimation.setCurrentFrameY(2);
            std::cout << "Moved right" << std::endl;
        }
        else
        {
            playerAnimation.setMoveActive(false);
            std::cout <<"[MoveActive_activated is now FALSE] " << std::endl;
        }
    
    
        std::cout << "x-position: " << x << std::endl;
        std::cout << "y-position: " << y << std::endl;
        std::cout << "moveSpeed : " << moveSpeed << std::endl;
    
    
        playerAnimation.getPositionX(x);
        playerAnimation.getPositionY(y);
        playerAnimation.Update(Window);
        camera.Update(x,y);
    
    
    }
    
    
    void Player::Draw(sf::RenderWindow &Window)
    {
        playerAnimation.Draw(Window);
        std::cout << "Player::Draw is ON" << std::endl;
    }
    
    
    float Player::getCameraX()
    {
        return x;
    }
    
    
    float Player::getCameraY()
    {
        return y;
    }
    Camera.h
    Code:
    #ifndef CAMERA_H
    #define CAMERA_H
    
    
    #include <SFML/Graphics.hpp>
    
    
    class Camera
    {
        public:
            Camera();
            virtual ~Camera();
    
    
            void Initialize();
            void Update(float playerX, float playerY);
            void Draw(sf::RenderWindow &Window);
    
    
            void setView(sf::View CameraPosition);
            sf::View getView();
        protected:
            sf::View CameraPosition;
            float cameraX , cameraY;
        private:
    };
    
    
    #endif // CAMERA_H
    Camera.cpp
    Code:
    #include <SFML/Graphics.hpp>
    #include <iostream>
    
    
    #include "Player.h"
    #include "Camera.h"
    #include "Global.h"
    
    
    
    
    Camera::Camera()
    {
        //ctor
    }
    
    
    Camera::~Camera()
    {
        //dtor
    }
    
    
    void Camera::Initialize()
    {
        cameraX = 0;
        cameraY = 0;
    }
    
    
    void Camera::Update(float playerX, float playerY)
    {
        cameraX = -(ScreenWidth/2) + playerX;
        cameraY = -(ScreenHeight/2) + playerY;
    
    
        if(cameraX < 0)
            {cameraX = 0;}
    
    
        if(cameraY < 0)
            {cameraY = 0;}
    
    
        CameraPosition.setViewport(sf::FloatRect( playerX , playerY , ScreenWidth , ScreenHeight ));
        setView(CameraPosition);
    
    
        std::cout << "---CAMERA UPDATE---" << std::endl;
        std::cout << "playerX: " << playerX << std::endl;
        std::cout << "playerY: " << playerY << std::endl;
        std::cout << "cameraX: " << cameraX << std::endl;
        std::cout << "cameraY: " << cameraY << std::endl;
    
    
    }
    
    
    //void Camera::Draw(sf::RenderWindow &Window)
    //{
    //    Window.setView(CameraPosition);
    //}
    
    
    void Camera::setView(sf::View CameraPosition)
    {
        this-> CameraPosition = CameraPosition;
    }
    
    
    sf::View Camera::getView()
    {
        return CameraPosition;
    
    
    }
    Map.h
    Code:
    #ifndef MAP_H
    #define MAP_H
    
    
    #include <SFML/Graphics.hpp>
    #include <iostream>
    
    
    #include "Global.h"
    #include "Camera.h"
    
    
    class Map
    {
        public:
            Map();
            virtual ~Map();
    
    
            void loadMap();
            void Draw(sf::RenderWindow &Window);
        protected:
            Camera camera;
        private:
            sf::Texture map;
            sf::Sprite mapSprite;
    
    
            sf::View mapPosition;
    };
    
    
    #endif // MAP_H
    Map.cpp
    Code:
    #include "Map.h"
    
    
    Map::Map()
    {
        //ctor
    }
    
    
    Map::~Map()
    {
        //dtor
    }
    
    
    void Map::loadMap()
    {
        if(map.loadFromFile("C:/Program Files (x86)/CodeBlocks/Projects/SFML ANIMATION ADVANCED TUTORIAL 2/resources/Background/CodingMadeEasy Background.png"))
            mapSprite.setTexture(map);
    }
    
    
    void Map::Draw(sf::RenderWindow &Window)
    {
        mapPosition = camera.getView();
        Window.setView(mapPosition);
        Window.draw(mapSprite);
    }
    Global.h
    Code:
    #ifndef GLOBAL_H_INCLUDED
    #define GLOBAL_H_INCLUDED
    
    
    #define ScreenWidth 800
    #define ScreenHeight 600
    
    
    #endif // GLOBAL_H_INCLUDED
    If you're wondering who's tutorial, its CodingMadeEasy's tuts. I try to avoid reading

  2. #2
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Camera.cpp line 45, that was careless of me. Now I changed them to cameraX and cameraY from playerX to playerY respectively. But still I got the same problem.

  3. #3
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    I haven't used SFML like this as I just used it to window my GL programs, so sorry if I missed something obvious.

    Why do you have the entire draw function commented out? Doesn't the window need to have its view set?
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-13-2013, 11:30 AM
  2. SFML Audio Problems...
    By JM1082 in forum Game Programming
    Replies: 1
    Last Post: 02-22-2012, 02:55 PM
  3. )': Visual C++ & SFML compilation problems! :'(
    By JM1082 in forum Game Programming
    Replies: 12
    Last Post: 02-20-2012, 08:48 AM
  4. Need Help with LCD scrolling
    By aries_isis in forum C Programming
    Replies: 0
    Last Post: 03-26-2006, 10:37 AM
  5. C Scrolling
    By Hexxx in forum C Programming
    Replies: 8
    Last Post: 10-13-2003, 02:52 PM