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

Author Topic: unsmooth movement/remnant sprite when using a view  (Read 1886 times)

0 Members and 1 Guest are viewing this topic.

snakypoutz

  • Newbie
  • *
  • Posts: 3
    • View Profile
unsmooth movement/remnant sprite when using a view
« on: April 03, 2015, 07:15:02 pm »
Hello

can you tell me what's wrong with this code? When I move the character in any direction, it is displayed twice. There is like a transparent remnant of the sprite.

It does not appear on screenshot so I took a picture instead


If I remove the setVerticalSync it will be smooth, but from time to time the remnant will appear for like a fraction of second.

It seems to be caused by the view. Before using one, movements were smooth.

#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>


int main()
{
        sf::RenderWindow window(sf::VideoMode(640, 480), "Test");
        window.setMouseCursorVisible(false);
        window.setVerticalSyncEnabled(true);

        sf::View view = window.getView();

        sf::Texture texCharacter;
        if (!texCharacter.loadFromFile("character.png"))
                return -1;
        sf::Sprite character;
        character.setTexture(texCharacter);
       

        sf::Texture texMap;
        if (!texMap.loadFromFile("map.png"))
                return -1;
        sf::Sprite map;
        map.setTexture(texMap);
        map.setPosition(sf::Vector2f(-640, -480));

        sf::Music music;
        if (!music.openFromFile("music.ogg"))
                return -1;
       
        sf::SoundBuffer buffer;
        if (!buffer.loadFromFile("sound.wav"))
                return -1;

        sf::Sound sound;
        sound.setBuffer(buffer);

        music.play();
        music.setLoop(true);


        while (window.isOpen())
        {

                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
                                sound.play();

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                                character.move(-32, 0);
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                                character.move(+32, 0);
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                                character.move(0, -32);
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                                character.move(0, +32);
                }

                window.clear();

                window.draw(map);
                window.draw(character);

                view.setCenter(character.getPosition());
                window.setView(view);

                window.display();
        }

        return 0;
}
 

Thanks for your help.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: unsmooth movement/remnant sprite when using a view
« Reply #1 on: April 04, 2015, 03:28:12 pm »
Check your graphics card driver.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

snakypoutz

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: unsmooth movement/remnant sprite when using a view
« Reply #2 on: April 04, 2015, 04:24:12 pm »
They are up to date.
I have a 144hz monitor so I thought maybe this could be an issue, but I tried on my laptop and the results were the same.

I honnestly don't know what's wrong.

I did an other test. Instead of moving the character and having a view centered on it, I got rid of the view and move the map arround the character, leaving it in the center.
With this method there is absolutely no visual artifact. So it confirms that the problem comes from using a view.

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: unsmooth movement/remnant sprite when using a view
« Reply #3 on: April 04, 2015, 04:40:30 pm »
What if you set the view before drawing the character ?

snakypoutz

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: unsmooth movement/remnant sprite when using a view
« Reply #4 on: April 04, 2015, 04:58:23 pm »
What if you set the view before drawing the character ?

God thank you so much it works. I should have thought of that.

 

anything