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

Author Topic: How do I implement smooth scrolling? (example inside)  (Read 6154 times)

0 Members and 1 Guest are viewing this topic.

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
How do I implement smooth scrolling? (example inside)
« on: February 28, 2013, 08:27:52 pm »
I've seen those threads here before but none of them really helped me
Here's a code


#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
int main()
{
        sf::RenderWindow window(sf::VideoMode(256 * 3, 240 * 3), "My window");
        window.setVerticalSyncEnabled(true);

        sf::Texture backgroundTexture;
        backgroundTexture.loadFromFile("background.png");
        sf::Clock mainClock;
        sf::View view(sf::FloatRect(0, 0, 256, 240));
        sf::Sprite backgroundSprite;
        backgroundSprite.setTexture(backgroundTexture);
        sf::Vector2f cameraPos(0.0f, 0.0f);

        float backWidth = backgroundTexture.getSize().x;
        float backHeight = backgroundTexture.getSize().y;
       

    // run the program as long as the window is open
    while (window.isOpen())
    {
                int dt = mainClock.restart().asMilliseconds();      
                sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

                       
                }

                float dx = 0.075f * dt;
                cameraPos.x += dx;
                view.move(dx, 0);
                window.setView(view);

                // I just render two sprites side by side
                int k = cameraPos.x / backWidth;
                backgroundSprite.setPosition(backWidth * k, 0);
                window.draw(backgroundSprite);
                backgroundSprite.setPosition(backWidth * (k + 1), 0);
                window.draw(backgroundSprite);

                window.display();
    }

    return 0;
}
 
Here's the image I'm using: http://i.imgur.com/u0wzBX2.png

What's strange is that my laptop monitor shows very bad results. Scrolling is not smooth at all. My second monitor is showing much better results, but sometimes they're bad too, which is odd. What am I doing wrong?  >:(
« Last Edit: February 28, 2013, 08:35:56 pm by moarthenfeeling »
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How do I implement smooth scrolling? (example inside)
« Reply #1 on: February 28, 2013, 08:33:12 pm »
Don't activate framerate limit and vertical sync at the same time, it doesn't make sense.
Laurent Gomila - SFML developer

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: How do I implement smooth scrolling? (example inside)
« Reply #2 on: February 28, 2013, 09:49:23 pm »
Don't activate framerate limit and vertical sync at the same time, it doesn't make sense.
Can it cause problems?
I've always assumed enabling vertical sync would simply override framerate limit.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10916
    • View Profile
    • development blog
    • Email
Re: How do I implement smooth scrolling? (example inside)
« Reply #3 on: February 28, 2013, 10:22:10 pm »
Can it cause problems?
It can, otherwise Laurent wouldn't say so. ;D
VSync will try to do it's magic and the sf::sleep from setFramerateLimit constatly disturbes that process. Thus it's advised not to activate both at once.

I've always assumed enabling vertical sync would simply override framerate limit.
That's a false assuption. Not sure how you got that idea. ;)

What's strange is that my laptop monitor shows very bad results. Scrolling is not smooth at all. My second monitor is showing much better results, but sometimes they're bad too, which is odd. What am I doing wrong?  >:(
What graphics card are you using?
Some graphics card can show different results for different screens.

Also what do you get at the moment, or why do you feel it's not moving smoothly?
« Last Edit: February 28, 2013, 10:24:20 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: How do I implement smooth scrolling? (example inside)
« Reply #4 on: March 01, 2013, 08:52:43 pm »
I've always assumed enabling vertical sync would simply override framerate limit.
That's a false assuption. Not sure how you got that idea. ;)

The documentation states that enabling vertical synchronization will limit the framerate anyway, that's why I assumed calling setFramerateLimit wouldn't have any effect.
Quote from: the friendly manual
Activating vertical synchronization will limit the number of frames displayed to the refresh rate of the monitor.

If this can lead to a buggy behaviour, maybe calling setVerticalSyncEnabled(true) should enforce the framerate limit to 0?
« Last Edit: March 01, 2013, 08:55:30 pm by Haze »

N_K

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: How do I implement smooth scrolling? (example inside)
« Reply #5 on: March 05, 2013, 05:44:44 am »
Maybe the problem is my graphic card

Yes, it is. I have a Radeon HD 4200, and no matter what I do, it's simply impossible to achieve smooth interpolation with it. After trying out all the various fixed timestep/smooth scroll algorithms I could find, and still failing to do smooth scrolling/movement, I decided to try out some commercial games. It was a good idea, because all of them had this issue, both 2D and 3D games, with both OpenGL and Direct3D.

I can turn on VSync, but the ATI driver developers thought it wouldn't be fun if this would just work, so they messed this up as well to constantly generate 90% CPU load, even when the app does nothing but displays an empty window. (Again, not an SFML issue, because this happens with all 3D apps.) But then it seems they've "fixed" this for the 4500 so you can't even turn on VSync...

But, at least, they release hardware specifications and some code to the open source community, and this is a good thing. Under Linux with the open source ATI driver, everything works perfectly. I'm not sure why they can't do this under Windows.
« Last Edit: March 05, 2013, 05:47:41 am by N_K »

 

anything