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

Author Topic: My scrolling appears quite jerky..  (Read 3341 times)

0 Members and 1 Guest are viewing this topic.

nelsk

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
My scrolling appears quite jerky..
« on: August 05, 2012, 03:01:12 am »
Hi all,

I've implemented a demo project to scroll across a Tiled map editor level (using a loader by a fellow on the forums named Quinn. Thanks!).

It's loading the tiles fine, but while scrolling, it seems quite jerky. I'm really looking for a bit of a sanity check to make sure I'm using some of the SFML features with best practices in mind:

Main loop:
#include <SFML/Graphics.hpp>
#include "level.h"

sf::Rect<float> GetViewportRect(sf::View& v)
{
        return sf::Rect<float>(
                v.getCenter().x - (v.getSize().x / 2),
                v.getCenter().y - (v.getSize().y / 2),
                v.getSize().x,
                v.getSize().y);
}

int main()
{
        sf::RenderWindow window(sf::VideoMode(1280,640), "Sandbox!", sf::Style::Fullscreen);
        window.setFramerateLimit(30);

        nelsk::Level _level;
        _level.LoadFromFile("Level1.tmx");

        sf::Rect<float> r(500,640,1280,640);
        sf::View v(r);
        _level.SetDrawingBounds(r);
        window.setView(v);
        sf::Vector2f moveVec;

        sf::Clock _clock;
        float _deltaT;
        float fps;

        while(window.isOpen())
        {
                _deltaT = _clock.restart().asSeconds();
                fps = 1.f / _deltaT;

                sf::Event event;

                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed ||
                                sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                                window.close();
                }
               
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        moveVec.x = 400 * _deltaT;
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        moveVec.x = -400 * _deltaT;

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        moveVec.y = 400 * _deltaT;
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                        moveVec.y = -400 *_deltaT;

                v.move(moveVec);               
                window.setView(v);

                _level.SetDrawingBounds(GetViewportRect(v));
                window.clear();
                _level.Draw(window);
               
                // Draw sprites to render frame
                window.display();
                moveVec.x = 0; moveVec.y = 0;
        }
        return EXIT_SUCCESS;
}
 

Relevent from Level.cpp

struct Layer{
                int opacity;
                std::vector <sf::Sprite> tiles;
        };

void Level::SetDrawingBounds(sf::Rect<float> bounds)
{
    drawingBounds = bounds;

    //Adjust the rect so that tiles are drawn just off screen, so you don't see them disappearing.
    drawingBounds.top -= tileHeight;
    drawingBounds.left -= tileWidth;
        drawingBounds.width += tileWidth;
        drawingBounds.height += tileHeight;
}

void Level::Draw(sf::RenderWindow &window)
{
    for (int layer = 0; layer < layers.size(); layer++)
    {
        for (int tile = 0; tile < layers[layer].tiles.size(); tile++)
        {
            if (drawingBounds.contains(layers[layer].tiles[tile].getPosition().x, layers[layer].tiles[tile].getPosition().y))
            {
                window.draw(layers[layer].tiles[tile]);
            }
        }
    }
}

Is there anything obviously wrong with this approach?
Side question: Perhaps this is a misunderstanding on my part (likely), but why does a RenderWindow not hold a reference to a view, rather than setting the view each loop through?

My fps is reporting very close to 30 and is pretty consistent, but every so often it will drop to around 19 for a couple loops. (I'm only running VStudio 2010 and my app on a pretty powerful rig)

Where should I start to smooth out this scrolling? (I've also got some pretty terrible tearing issues).

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: My scrolling appears quite jerky..
« Reply #1 on: August 05, 2012, 03:16:03 pm »
1.Why are you doing sf::Keyboard::isKeyPressed with every single event you process? It should be changed to checking if there was event of key press AND the key pressed was escape or put outside the event loop.

2. 1280x640 doesn't seem like valid full screen resolution(it isn't for me, I checked), you must run isValid() on your VideoMode or use one of resolutions returned by sf::VideoMode::getFullscreenModes() to make sure that you're using valid fullscreen resolution, because fullscreen doesn't work with all resolutions like window does. I think that might be it, but you must check with your rig to see if that resolution is valid for you.

3. In 1.6 windows used to hold references to views and if it went out of scope then interesting things happened and it was (at least for me) unituitive because view doesn't seem like something heavy to copy/store/create.

4. You must do .setSmooth(true) on texture your sprites use or it'll look ugly after any float rotation/movemenet/scaling, in 2.0 it isn't true by default like in 1.6.
« Last Edit: August 05, 2012, 03:19:55 pm by FRex »
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: My scrolling appears quite jerky..
« Reply #2 on: August 05, 2012, 03:31:37 pm »
I agree with FRex up to the point about setSmooth(true). It really depends on what you're doing and what effect you want. Because if you want a 1:1 representation of the pixel graphics you've in your texture then it's better not to activate smoothing since it will lead to unwanted artefacts.

Also your low FPS count probably comes from the use a vector of sf::Sprites. It's recommended to use one VertexArray and put all the needed textures in there. Graphic cards are layed out for processing millions or even billions of verticies per second, so this should boost your application significantly.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: My scrolling appears quite jerky..
« Reply #3 on: August 05, 2012, 03:44:19 pm »
That's true but he's doing a float around 13,(3) movement each frame so that 'tearing' might be from this.
That vector complain reminded me: maybe if you have A LOT of tiles (like - enough to fill screen several times) then instead of iterating through all of them you can get the points of your view rectangle and only display what fits between them by dividing top bottom left and right by tilesize(if it's constant) and then using these to iterate part of the vector.
« Last Edit: August 05, 2012, 03:45:55 pm by FRex »
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: My scrolling appears quite jerky..
« Reply #4 on: August 05, 2012, 04:10:28 pm »
That's true but he's doing a float around 13,(3) movement each frame so that 'tearing' might be from this.
Float positions do not affect the rendering, because they will simple be rounded and then still displayed 1:1 (you won't suddendly lose some pixels or anything).

maybe if you have A LOT of tiles (like - enough to fill screen several times) ...
No if you have A LOT of tiles and you care about a usefull FPS count you should use VertexArray. :D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

nelsk

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: My scrolling appears quite jerky..
« Reply #5 on: August 05, 2012, 04:46:58 pm »
I actually wasn't typically running it in fullscreen, I read in an older thread it might smooth things out a bit and tried it to no avail :P
Interestingly enough, it definitely ran fullscreen on both my monitor and TV.

Regardless, I'll make sure to verify the resolution I'm running is valid.

I'll also check into the vertex array, I did have some reservations about sticking all my sprites in a vector.

Thanks for your responses!