我正在学习SDL2的基础知识,当我运行我的程序(vsync,窗口)时,在窗口之间切换焦点和拖动窗口时会出现延迟。这适用于所有窗口,而不仅仅是SDL2应用程序。如果未启用vsync,则所有窗口都会按预期顺畅运行。
我正在运行ubuntu 14.04,使用的是nvidia卡,驱动程序版本为349.12。以下是在我的机器上重现行为的最小工作示例。
#include <SDL2/SDL.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
void init(bool vsync);
int main(int argc, char* args[]) {
init(true);
bool quit = false;
SDL_Event e;
while (!quit) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
}
}
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer);
SDL_Rect fillRect = {SCREEN_WIDTH/4, SCREEN_HEIGHT/4, SCREEN_WIDTH/2, SCREEN_HEIGHT/2};
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0x00, 0x00, 0xFF); //RGBA
SDL_RenderFillRect(gRenderer, &fillRect);
SDL_RenderPresent(gRenderer);
}
SDL_DestroyRenderer(gRenderer);
gRenderer = NULL;
SDL_DestroyWindow(gWindow);
gWindow = NULL;
SDL_Quit();
return 0;
}
void init(bool vsync) {
SDL_Init(SDL_INIT_VIDEO);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
gWindow = SDL_CreateWindow("SDL test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (vsync) {
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
} else {
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
}
}
在第二台计算机(14.04,bumblebee驱动程序)上运行此代码,无论是否使用vsync,都会产生平滑的窗口行为。我也尝试了无数vsync和无vsync的游戏,唯一一个重现行为的游戏是FTL(我认为是SDL游戏)。
如何进一步修复此调试?