1
由於某些原因,在窗口模式下,正方形的運動看起來很不舒服。我很確定我沒有正確優化我的代碼,這不是一個驅動程序問題,因爲我以前在窗口模式下玩過OpenGL + SDL遊戲,並沒有問題。爲什麼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();
}
}
但爲什麼會在全屏影響呢?它似乎不是這是什麼原因造成的。 – w4etwetewtwet
不確定,但是當全屏模式時,幀緩衝區是屏幕緩衝區。在窗口模式下,幀緩衝區是一個離屏內存塊。然後需要一個額外的階段,將窗口與存在於應用程序外部的屏幕元素組合在一起。 – doron
我已經添加了一個計時器,並相應地改變了運動,但是這似乎不能解決它。顛簸發生在定期的時間間隔。 (它每半秒停止移動幾毫秒)。 – w4etwetewtwet