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

Author Topic: Perfomance hiccups/stuttering, need to find out what's wrong.  (Read 1136 times)

0 Members and 1 Guest are viewing this topic.

ch

  • Newbie
  • *
  • Posts: 1
    • View Profile
Perfomance hiccups/stuttering, need to find out what's wrong.
« on: January 05, 2013, 01:17:16 pm »
Hi.
I have a weird a problem - I can't get a smooth movement. Like from the first steps from tutorials - by multiplying velocity on GetFrameTime(), and then every possible variants - fixed timesteps, variable, fixed with interpolation - there is always some stuttering. Later variant though(implementation of deWitters game loop) works better than others - really smooth, but motion freezes for a frame or two every few seconds. It's actually barely noticeable with small sprites, but with view scrolling  over tilemap (no matter vertexaray or big sprite) this is quiet observably.
I use Netbeans with gcc 4.6  on Ubuntu 12.04 and build of the latest SFML snapshot, and i had the same perfomance with 2.0rc and 1.6 versions. Recently i tried the same code on Windows with visual studio 2010 and had no problems with no vsync and some similiar drops(but fewer) with vsync on. So I think now, that it is actually my system/hardware perfomance problem rather than something wrong with my code, but I want to be sure. I tried some opengl benchmarks on ubuntu and they worked slick, is there any common SFML test ? And it may be helpfull if someone could run my code and tell how it performs. Also I open to any suggestion which helps to improve this situation. Thanks.
Here is the code:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <math.h>

struct Focus{
sf::RectangleShape drawable;    
sf::Vector2f velocity;
sf::Vector2f coord;
sf::Vector2f previous_coord;
};

#define VELOCITY 250.f

int main(int argc, char** argv) {
    sf::RenderWindow window;
    sf::View view;
    sf::Texture texture;
    sf::Sprite tiles;
    sf::Clock timer;
   
    window.create(sf::VideoMode(800,600,32),"Test Scrolling",sf::Style::Default);
    //window.setFramerateLimit(60);
    //window.setVerticalSyncEnabled(true);
   
    Focus focus;
    focus.velocity = sf::Vector2f(0.f, 0.f);
    focus.coord = sf::Vector2f(0.f, 0.f);
    focus.drawable.setSize(sf::Vector2f(32,32));
    focus.drawable.setFillColor(sf::Color(255,100,100));    
    view.setCenter(0.f, 0.f);
    view.setSize(800,600);
    texture.loadFromFile("8OtUK.png");
    tiles.setTexture(texture);
   
    const float TIMESTEP = 0.04f;
    const int MAX_FRAMESKIP = 5;
    float nexttick = 0;
    bool running = true;
    while(running){
        int frameskips = 0;
        while(timer.getElapsedTime().asSeconds() > nexttick && frameskips < MAX_FRAMESKIP){                
            sf::Event event;
            while (window.pollEvent(event)){      
                if (event.type == sf::Event::Closed)
                    running = false;
                if(event.type == sf::Event::KeyPressed)
                    switch(event.key.code){
                        case sf::Keyboard::Up :
                            focus.velocity.y = -VELOCITY;
                            break;
                        case sf::Keyboard::Down :
                            focus.velocity.y = VELOCITY;
                            break;
                        case sf::Keyboard::Left :
                            focus.velocity.x = -VELOCITY;
                            break;
                        case sf::Keyboard::Right :
                            focus.velocity.x = VELOCITY;
                            break;    
                    }
            }
            sf::Time frametime = timer.getElapsedTime();
            //Update coordinates
            focus.previous_coord = focus.coord;
            focus.coord += focus.velocity * TIMESTEP;
            //
            frameskips++;
            nexttick += TIMESTEP;
        }
        sf::Time startdraw = timer.getElapsedTime();
        float interpolation = (timer.getElapsedTime().asSeconds() + TIMESTEP - nexttick) / TIMESTEP;
       
        //Draw
        window.clear(sf::Color(0,0,0));
        sf::Vector2f draw_coord = focus.previous_coord + ((focus.coord - focus.previous_coord) * interpolation);
        focus.drawable.setPosition(floor(draw_coord.x + 0.5f), floor(draw_coord.y + 0.5f));
        view.setCenter(focus.drawable.getPosition());
        window.setView(view);
        window.draw(tiles);
        window.draw(focus.drawable);
        window.display();
        //
        sf::Time frametime = timer.getElapsedTime() - startdraw;
       
        if(timer.getElapsedTime().asSeconds() > 2){
            nexttick = 0;
            timer.restart();  
        }
        //sf::sleep(sf::microseconds(16666) - frametime);
    }
    window.close();
    return 0;
}
 

link to image - http://i.imgur.com/8OtUK.png
(There are commented lines - options which I tried(independently of each other), but it seems now that there is less stuttering without them)

Also my specs: Core2DuoE7400 @ 2.80GHz × 2, GeForce9600Gt, 2Gb Ram DDR2,   on ubuntu latest beta drivers (310), also tried on current recommended version and (173),on Windows Xp - latest nvidia driver.

Also to note, I run this code in release mode, without output anything to console, and I tried to increase process priority on both systems, without visible success.