为什么我的 SDL 2 应用程序中的帧速率会减半,除非我将鼠标移动到某个位置?

Why is the frame rate in my SDL 2 application being halved unless I move the mouse around in a certain position?

有一个非常奇怪的错误,它似乎无处不在,我的 SDL 2 应用程序的帧速率大幅下降(从 ~60 到 ~30)。这个错误真正奇怪的是,当我将光标快速移动到 周围并超出 window 时,帧速率会加速恢复正常。

可能是什么问题?或者,我怎样才能正确地将帧率限制在 60(或我喜欢的任何值)?

编辑:这是成功复制问题的框架代码:

#include <cstdio>
#include <SDL.h>

#define WIDTH 640
#define HEIGHT 480
#define FPS 60

using namespace std;

int main(int argc, char** args) {

    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window* window = SDL_CreateWindow("Cool Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
    SDL_Surface* winsurf = SDL_GetWindowSurface(window);
    SDL_Renderer* winren = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_SetRenderDrawColor(winren, 255, 255, 255, 255);

    bool quit = false;
    int counter = 0;
    int looptime = SDL_GetTicks();

    while (!quit) {

        SDL_RenderClear(winren);

        SDL_Event e;
        while (SDL_PollEvent(&e))
            if (e.type == SDL_QUIT)
                quit = true;

        printf("%d\n", counter++); // notice how quickly the counter speeds up
                                   // while moving your cursor around outside
                                   // of the window

        if (SDL_GetTicks() - looptime < 1000/FPS)
            SDL_Delay(1000/FPS - (SDL_GetTicks() - looptime));
        looptime = SDL_GetTicks();

        SDL_RenderPresent(winren);
    }

    SDL_Quit();

}

如果有人感兴趣,我通过启用 Vsync 解决了这个问题。我在调用中添加了 SDL_RENDERER_PRESENTVSYNC 标志,如下所示:

SDL_Renderer* winren = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

这使动画更加流畅。