Strict 60 Hz update rate without busyloops.

This commit is contained in:
Guus Sliepen 2011-08-24 20:02:35 +02:00
parent 0f278a2748
commit fc9dac5195
5 changed files with 32 additions and 33 deletions

View File

@ -185,6 +185,8 @@ class Math {
class Graphics {
unsigned long frameLimit;
int thirds;
public:
@ -446,8 +448,28 @@ class Graphics {
void updateScreen()
{
SDL_Flip(screen);
// Give the audio (and possibly the X server) time to work...
SDL_Delay(1);
}
/*
* Delay until the next 60 Hz frame
*/
void delayFrame()
{
unsigned long now = SDL_GetTicks();
// Add 16 2/3 to frameLimit
frameLimit += 16;
if(thirds >= 2) {
thirds = 0;
} else {
thirds++;
frameLimit++;
}
if(now < frameLimit)
SDL_Delay(frameLimit - now);
else
frameLimit = now;
}
/*

View File

@ -108,7 +108,6 @@ int mainGameLoop()
engine.musicVolume = 100;
int rtn = 0;
unsigned long frameLimit = SDL_GetTicks();
int allowableAliens = 999999999;
@ -276,13 +275,7 @@ int mainGameLoop()
break;
}
// (Attempt to) Limit us to 60 frame a second
while (SDL_GetTicks() < (frameLimit + 16))
{
// Do nothing. If we were to insert an SDL_Delay(1) in here
// then we would actually lose around 10 frames per second!!
}
frameLimit = SDL_GetTicks();
graphics.delayFrame();
}
graphics.flushBuffer();

View File

@ -596,8 +596,6 @@ int galaxyMap()
else
player.shield = player.maxShield;
unsigned long frameLimit = SDL_GetTicks();
flushInput();
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = engine.keyState[SDLK_SPACE] = 0;
engine.done = 0;
@ -832,9 +830,7 @@ int galaxyMap()
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = engine.keyState[SDLK_SPACE] = 0;
doCursor();
// Limit us to 60 frame a second
while (SDL_GetTicks() < (frameLimit + 16)){}
frameLimit = SDL_GetTicks();
graphics.delayFrame();
}
Mix_HaltMusic();

View File

@ -237,7 +237,6 @@ void doCutscene(int scene)
enemy[i].engineY = (enemy[i].image[0]->h / 2);
}
unsigned long frameLimit = SDL_GetTicks();
signed char showMessage = 0;
signed char currentMessage = -1;
int timer = 60 * 4;
@ -298,9 +297,8 @@ void doCutscene(int scene)
if ((showMessage) && (graphics.messageBox != NULL))
graphics.blit(graphics.messageBox, (800 - graphics.messageBox->w) / 2, 500);
while (SDL_GetTicks() < (frameLimit + 16)){}
frameLimit = SDL_GetTicks();
graphics.delayFrame();
if (engine.keyState[SDLK_ESCAPE])
break;
}

View File

@ -234,7 +234,6 @@ int doTitle()
signed char menuType = 0;
graphics.drawBackGround();
unsigned long frameLimit = SDL_GetTicks();
engine.done = 0;
flushInput();
@ -453,8 +452,7 @@ int doTitle()
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = engine.keyState[SDLK_SPACE] = 0;
}
while (SDL_GetTicks() < (frameLimit + 16)){}
frameLimit = SDL_GetTicks();
graphics.delayFrame();
}
Mix_HaltMusic();
@ -529,8 +527,6 @@ void showStory()
graphics.blit(graphics.background, 0, 0);
graphics.flushBuffer();
unsigned long frameLimit = SDL_GetTicks();
flushInput();
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = engine.keyState[SDLK_SPACE] = 0;
@ -558,8 +554,7 @@ void showStory()
break;
}
while (SDL_GetTicks() < (frameLimit + 16)){}
frameLimit = SDL_GetTicks();
graphics.delayFrame();
}
}
@ -593,7 +588,6 @@ void gameover()
int x = (800 - gameover->w) / 2;
int y = (600 - gameover->h) / 2;
unsigned long frameLimit = SDL_GetTicks();
graphics.updateScreen();
flushInput();
@ -613,9 +607,7 @@ void gameover()
y = ((600 - gameover->h) / 2) - Math::rrand(-2, 2);
graphics.blit(gameover, x, y);
// Limit us to 60 frame a second
while (SDL_GetTicks() < (frameLimit + 16)){}
frameLimit = SDL_GetTicks();
graphics.delayFrame();
}
SDL_FreeSurface(gameover);
@ -693,7 +685,6 @@ void doCredits()
SDL_Delay(10000);
graphics.drawBackGround();
unsigned long frameLimit = SDL_GetTicks();
engine.done = 0;
lastCredit = numberOfCredits - 1;
@ -724,8 +715,7 @@ void doCredits()
SDL_FillRect(graphics.screen, &r1, graphics.black);
SDL_FillRect(graphics.screen, &r2, graphics.black);
while (SDL_GetTicks() < (frameLimit + 16)){}
frameLimit = SDL_GetTicks();
graphics.delayFrame();
}
for (int i = 0 ; i < numberOfCredits ; i++)