由于某种原因,在窗口模式下,正方形的运动看起来很颠簸。我很确定我没有正确优化我的代码,这不是驱动程序问题,因为我以前在窗口模式下玩过 OpenGL+SDL 游戏,一切都很好。
这是代码(更新为包括与帧速率无关的运动):
#include <SDL/SDL.h>
#include <GL/gl.h>
const int width = 1600;
const int height = 900;
const int colourDepth = 32;
void initVideo(){
SDL_Surface *screen;
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(width, height, colourDepth, SDL_OPENGL|SDL_GL_DOUBLEBUFFER
|SDL_FULLSCREEN//if I comment this out it lags
);
if(!screen){
SDL_Quit();
exit(1);
}
SDL_WM_SetCaption("JERKINESS TEST", "");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
}
int main(int argc, char** argv){
initVideo();
int x;
SDL_Event event;
long lastTime = 0;
while(true){
while(SDL_PollEvent(&event)){
if(event.type==SDL_KEYDOWN){
SDL_Quit();
}
}
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glVertex2i(x, 425);
glVertex2i(x+50, 425);
glVertex2i(x+50, 475);
glVertex2i(x, 475);
glEnd();
long elapsedTimeMilis = SDL_GetTicks() - lastTime;
x+= 1 * (elapsedTimeMilis/(1000/60));
lastTime=SDL_GetTicks();
SDL_GL_SwapBuffers();
}
}