C ++ SDL帧率脉冲

时间:2012-08-31 08:04:08

标签: c++ sdl frame-rate

最近我一直在研究一些SDL包装器供我自己使用,自从我第一次使用SDL库以来,我遇到了一个问题。

与许多其他人一样,我一直在使用类似于http://lazyfoo.net/SDL_tutorials/lesson14/index.php的计时器来调节我的帧速率,并且运动永远不会平滑。它似乎不是一个双缓冲或vsync问题,而是运动是平滑的,但周期性地跳过和断断续续(实际上它有一定的脉冲和节奏)。每当帧率调节关闭时,脉冲消失 - 当然,一切都变得无法使用了,所以我大多肯定它与它有关。

我把一个小应用程序放在一起,只包含定时器和一个围绕它移动的红色方块。我将把代码放在这篇文章的末尾。代码是垃圾,不使用任何类型的精灵或任何东西:只要知道它不管我施加多少压力(例如,添加更多移动的东西或只更新屏幕的一部分),它都会断断续续。我尝试过不同的帧率设置,并且始终显示脉冲(只是使用不同的“速度”)。毋庸置疑,我已经在几台机器(linux机器,所有机器)上尝试了这一点。

无论如何,我一直在阅读并看到其他人有这个问题,但没有真正的答案。我,其中一个,关于delta计时tecniques,并很乐意尝试它,但我的问题是这个非常简单的应用程序上的计时器本身。它为什么口吃?这是SDL_GetTicks精度和准确度的问题吗?我该怎么做才能改善它?。

所以,这里是那些想要尝试的人的代码:

#include <SDL/SDL.h>

class fps_control
{
    private:

    bool apply;
    Uint32 ticks_frame_count;
    Uint32 ticks_frame_end;
    Uint32 ticks_frame_begin;
    Uint32 diff;
    unsigned int frame_count;   //Visible to the outside
    unsigned int frame_count_inner; //Keeps count until a second has passed, then overwrites former to give the elapsed frames in a second.
    unsigned int frame_length;
    unsigned int desired_framerate;

    private:

    void calculate_frame_length()
    {
        this->frame_length=1000 / this->desired_framerate;  
    }

    public:

    fps_control(unsigned int p_f=30):desired_framerate(p_f), apply(true), ticks_frame_count(0), ticks_frame_end(0), ticks_frame_begin(0), diff(0), frame_count(0), frame_count_inner(0), frame_length(0)
    {
        this->calculate_frame_length();
    }

    unsigned int get_frame_count() const {return this->frame_count;}
    unsigned int get_desired_framerate() const {return this->desired_framerate;}
    void framerate_increase(){this->set_framerate(this->desired_framerate+1);}
    void framerate_decrease(){this->set_framerate(this->desired_framerate+1);}
    void set_framerate(unsigned int p_param)
{
this->desired_framerate=p_param;
this->calculate_frame_length();
}
    void toggle_apply() {this->apply=!this->apply;}

    void init()
    {
        //Call this once before starting, to set the beginning frame count to the initial values.

        this->ticks_frame_count=SDL_GetTicks();
        this->ticks_frame_begin=this->ticks_frame_count;
        this->frame_count_inner=0;
        this->frame_count=0;
    }

    void turn()
    {
        //Call this when all drawing and logic is done.

        if(!this->apply) return;    //Only apply when asked for.

        //Ask for time before drawing and logic to calculate the difference. Add a frame.

        this->ticks_frame_end=SDL_GetTicks();
        this->frame_count_inner++;

        //Whenever a second has passed, update the visible frame count.
        if( (this->ticks_frame_end - this->ticks_frame_count) > 1000)
        {
            this->frame_count=this->frame_count_inner;
            this->frame_count_inner=0;
            this->ticks_frame_count=SDL_GetTicks();
        }

        //Calculate difference and apply delay when needed.

        this->diff=this->ticks_frame_end - this->ticks_frame_begin;

        if(this->diff < this->frame_length) SDL_Delay(this->frame_length-this->diff);


        //Get the beginning time and start again.
        this->ticks_frame_begin=SDL_GetTicks();
    }
};

class Box
{
    private:

    SDL_Rect position;
    int movement_x;
    int movement_y;

    public:

    Box():movement_x(4), movement_y(4)
    {
        this->position.x=100;
        this->position.y=100;
        this->position.w=30;
        this->position.h=30;
    }

    SDL_Rect get_position() {return this->position;}

    void move_around()
    {
        //Won't touch the edges, but doesn't really matter.

        if(this->position.x<=0 || this->position.x>=800-this->position.w) this->movement_x=-this->movement_x;
        if(this->position.y<=0 || this->position.y>=600-this->position.h) this->movement_y=-this->movement_y;

        this->position.x+=this->movement_x;
        this->position.y+=this->movement_y;
    }
};

bool init_sdl(){return SDL_Init( SDL_INIT_VIDEO ) >= 0;}
void quit_sdl(){SDL_Quit();}
void fill_screen(SDL_Surface * screen)
{
    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,0,0,0));
}

void update_screen(SDL_Surface * screen, Box& box)
{
    SDL_Rect b=box.get_position();
    SDL_FillRect(screen, &b, SDL_MapRGB(screen->format, 200, 20, 20));
    SDL_Flip(screen);
}

int get_input()
{
    SDL_Event event;

    while(SDL_PollEvent(&event))
    {   
        if(event.type==SDL_QUIT) return 1;
        else if(event.type==SDL_KEYDOWN)
        {
            switch(event.key.keysym.sym)
            {
                case SDLK_ESCAPE: return 1; break;
                case SDLK_SPACE: return 2; break;
                case SDLK_UP: return 3; break;
                case SDLK_DOWN: return 4; break;
                default: break;
            }
        }
    }

    return 0;
}

int main(int argc, char **argv)
{
    if(!init_sdl())
    {
        return 1;
    }
    else
    {
        //Init things...

//      SDL_Surface * screen=SDL_SetVideoMode(800, 600, 16, SDL_DOUBLEBUF | SDL_HWSURFACE); /*SDL_SWSURFACE | SDL_ANYFORMAT);*/
        SDL_Surface * screen=SDL_SetVideoMode(800, 600, 16, SDL_SWSURFACE | SDL_ANYFORMAT);
        Box box=Box();
        fps_control fps=fps_control(60);    //Framerate is set to 60.
        bool run=true;
        int input=0;

        SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,255,0,0));

        //Main loop

        fps.init();

        while(run)
        {
            input=get_input();

            switch(input)
            {
                case 1: run=false; break;   
                case 2: fps.toggle_apply(); break;
                case 3: fps.framerate_increase(); break;
                case 4: fps.framerate_decrease(); break;
                default: break;
            }

            box.move_around();
            fill_screen(screen);
            update_screen(screen, box);
            fps.turn();
        }

        quit_sdl();
    }
}

它是自包含的(再次,纯垃圾)所以只需将它与SDL链接并尝试...你在这里看到任何口吃的脉冲吗?

我会尝试应用增量时序来查看问题是否消失。我只想知道为什么会在这样简单的应用程序中发生这种情况非常感谢。

编辑:最后一件事,我知道我可能根本不应该使用SDL_Delay(关于系统正在为至少询问的值而睡觉)但是我对这种行为感到困惑看似强大的机器。

编辑:更正了“set_framerate”,感谢Yno的评论。

编辑2:由于我将所有代码更改为使用增量时间值而不是设置帧速率,所以一切都变得更好。我仍然会得到一些周期性的减速(这个时间每四十秒)但我可以归咎于系统上的那些,因为帧速率和减速度根据我正在使用的Linux桌面而变化很大(比如Gnome,Unity,Gnome2d ......)。 / p>

2 个答案:

答案 0 :(得分:4)

你的班级fps_control根本不做这项工作,它完全是错误的。 framerate_decrease()函数错误:

void framerate_decrease(){this->set_framerate(this->desired_framerate+1);}

这里应该是-1。每次更改所需的FPS时都应调用calculate_frame_length(),即set_framerate()

void set_framerate (unsigned int p_param) {
    desired_framerate = p_param;
    calculate_frame_length ();
}

同样要小心calculcate_frame_length()中的分歧,你可以除以零:

void framerate_decrease() {
    if (desired_framerate > 1)
        set_framerate (desired_framerate - 1);
}

除了这些问题,你的班级看起来也很复杂。如果要限制帧速率,则必须了解其计算方式。通常的方法是使用SDL_GetTicks()计算帧所需的时间:

int t;
while (run) {
    t = SDL_GetTicks ();
    // ...
    t = SDL_GetTicks () - t;
}

在循环结束时,t是您的帧完成所用的ms数。因此,1000 / t是您每秒的帧数。如果您的帧速率过高(即t太小),则必须通过调用SDL_Delay()填补当前帧时间与所需帧时间之间的差距。假设FPS是您的FPS限制,每帧应采用的时间为1000 / FPS

int t;
while (run) {
    t = SDL_GetTicks ();
    // ...
    t = SDL_GetTicks () - t;

    // if the framerate is too high
    if (t < 1000 / FPS) {
        // compute the difference to have a total frame time of 1000 / FPS
        SDL_Delay ((1000 / FPS) - t);
    }
}

希望这有帮助。

答案 1 :(得分:3)

我不知道您的SDL代码是什么,但如果它有任何用处我刚刚编写了一个GLFW使用的FpsManager类,它限制了一个高端值,并提供时间增量以保持运动一致,无论帧率。原则是相同的。

这不是有史以来最好的代码,但是当用键击改变目标FPS +/-时,它在10fps和1000fps之间非常清晰地记录并完全平滑。

如果它对您有用,您可以在以下位置找到它: FpsManager - A C++ helper class for framerate independent movement