Strict 60 Hz update rate without busyloops.
This commit is contained in:
parent
0f278a2748
commit
fc9dac5195
|
@ -185,6 +185,8 @@ class Math {
|
||||||
|
|
||||||
|
|
||||||
class Graphics {
|
class Graphics {
|
||||||
|
unsigned long frameLimit;
|
||||||
|
int thirds;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
@ -446,8 +448,28 @@ class Graphics {
|
||||||
void updateScreen()
|
void updateScreen()
|
||||||
{
|
{
|
||||||
SDL_Flip(screen);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -108,7 +108,6 @@ int mainGameLoop()
|
||||||
engine.musicVolume = 100;
|
engine.musicVolume = 100;
|
||||||
|
|
||||||
int rtn = 0;
|
int rtn = 0;
|
||||||
unsigned long frameLimit = SDL_GetTicks();
|
|
||||||
|
|
||||||
int allowableAliens = 999999999;
|
int allowableAliens = 999999999;
|
||||||
|
|
||||||
|
@ -276,13 +275,7 @@ int mainGameLoop()
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// (Attempt to) Limit us to 60 frame a second
|
graphics.delayFrame();
|
||||||
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.flushBuffer();
|
graphics.flushBuffer();
|
||||||
|
|
|
@ -596,8 +596,6 @@ int galaxyMap()
|
||||||
else
|
else
|
||||||
player.shield = player.maxShield;
|
player.shield = player.maxShield;
|
||||||
|
|
||||||
unsigned long frameLimit = SDL_GetTicks();
|
|
||||||
|
|
||||||
flushInput();
|
flushInput();
|
||||||
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = engine.keyState[SDLK_SPACE] = 0;
|
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = engine.keyState[SDLK_SPACE] = 0;
|
||||||
engine.done = 0;
|
engine.done = 0;
|
||||||
|
@ -832,9 +830,7 @@ int galaxyMap()
|
||||||
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = engine.keyState[SDLK_SPACE] = 0;
|
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = engine.keyState[SDLK_SPACE] = 0;
|
||||||
doCursor();
|
doCursor();
|
||||||
|
|
||||||
// Limit us to 60 frame a second
|
graphics.delayFrame();
|
||||||
while (SDL_GetTicks() < (frameLimit + 16)){}
|
|
||||||
frameLimit = SDL_GetTicks();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Mix_HaltMusic();
|
Mix_HaltMusic();
|
||||||
|
|
|
@ -237,7 +237,6 @@ void doCutscene(int scene)
|
||||||
enemy[i].engineY = (enemy[i].image[0]->h / 2);
|
enemy[i].engineY = (enemy[i].image[0]->h / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long frameLimit = SDL_GetTicks();
|
|
||||||
signed char showMessage = 0;
|
signed char showMessage = 0;
|
||||||
signed char currentMessage = -1;
|
signed char currentMessage = -1;
|
||||||
int timer = 60 * 4;
|
int timer = 60 * 4;
|
||||||
|
@ -298,8 +297,7 @@ void doCutscene(int scene)
|
||||||
if ((showMessage) && (graphics.messageBox != NULL))
|
if ((showMessage) && (graphics.messageBox != NULL))
|
||||||
graphics.blit(graphics.messageBox, (800 - graphics.messageBox->w) / 2, 500);
|
graphics.blit(graphics.messageBox, (800 - graphics.messageBox->w) / 2, 500);
|
||||||
|
|
||||||
while (SDL_GetTicks() < (frameLimit + 16)){}
|
graphics.delayFrame();
|
||||||
frameLimit = SDL_GetTicks();
|
|
||||||
|
|
||||||
if (engine.keyState[SDLK_ESCAPE])
|
if (engine.keyState[SDLK_ESCAPE])
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -234,7 +234,6 @@ int doTitle()
|
||||||
signed char menuType = 0;
|
signed char menuType = 0;
|
||||||
|
|
||||||
graphics.drawBackGround();
|
graphics.drawBackGround();
|
||||||
unsigned long frameLimit = SDL_GetTicks();
|
|
||||||
|
|
||||||
engine.done = 0;
|
engine.done = 0;
|
||||||
flushInput();
|
flushInput();
|
||||||
|
@ -453,8 +452,7 @@ int doTitle()
|
||||||
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = engine.keyState[SDLK_SPACE] = 0;
|
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = engine.keyState[SDLK_SPACE] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (SDL_GetTicks() < (frameLimit + 16)){}
|
graphics.delayFrame();
|
||||||
frameLimit = SDL_GetTicks();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Mix_HaltMusic();
|
Mix_HaltMusic();
|
||||||
|
@ -529,8 +527,6 @@ void showStory()
|
||||||
graphics.blit(graphics.background, 0, 0);
|
graphics.blit(graphics.background, 0, 0);
|
||||||
graphics.flushBuffer();
|
graphics.flushBuffer();
|
||||||
|
|
||||||
unsigned long frameLimit = SDL_GetTicks();
|
|
||||||
|
|
||||||
flushInput();
|
flushInput();
|
||||||
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = engine.keyState[SDLK_SPACE] = 0;
|
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = engine.keyState[SDLK_SPACE] = 0;
|
||||||
|
|
||||||
|
@ -558,8 +554,7 @@ void showStory()
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (SDL_GetTicks() < (frameLimit + 16)){}
|
graphics.delayFrame();
|
||||||
frameLimit = SDL_GetTicks();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -593,7 +588,6 @@ void gameover()
|
||||||
int x = (800 - gameover->w) / 2;
|
int x = (800 - gameover->w) / 2;
|
||||||
int y = (600 - gameover->h) / 2;
|
int y = (600 - gameover->h) / 2;
|
||||||
|
|
||||||
unsigned long frameLimit = SDL_GetTicks();
|
|
||||||
graphics.updateScreen();
|
graphics.updateScreen();
|
||||||
|
|
||||||
flushInput();
|
flushInput();
|
||||||
|
@ -613,9 +607,7 @@ void gameover()
|
||||||
y = ((600 - gameover->h) / 2) - Math::rrand(-2, 2);
|
y = ((600 - gameover->h) / 2) - Math::rrand(-2, 2);
|
||||||
graphics.blit(gameover, x, y);
|
graphics.blit(gameover, x, y);
|
||||||
|
|
||||||
// Limit us to 60 frame a second
|
graphics.delayFrame();
|
||||||
while (SDL_GetTicks() < (frameLimit + 16)){}
|
|
||||||
frameLimit = SDL_GetTicks();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_FreeSurface(gameover);
|
SDL_FreeSurface(gameover);
|
||||||
|
@ -693,7 +685,6 @@ void doCredits()
|
||||||
SDL_Delay(10000);
|
SDL_Delay(10000);
|
||||||
graphics.drawBackGround();
|
graphics.drawBackGround();
|
||||||
|
|
||||||
unsigned long frameLimit = SDL_GetTicks();
|
|
||||||
engine.done = 0;
|
engine.done = 0;
|
||||||
|
|
||||||
lastCredit = numberOfCredits - 1;
|
lastCredit = numberOfCredits - 1;
|
||||||
|
@ -724,8 +715,7 @@ void doCredits()
|
||||||
SDL_FillRect(graphics.screen, &r1, graphics.black);
|
SDL_FillRect(graphics.screen, &r1, graphics.black);
|
||||||
SDL_FillRect(graphics.screen, &r2, graphics.black);
|
SDL_FillRect(graphics.screen, &r2, graphics.black);
|
||||||
|
|
||||||
while (SDL_GetTicks() < (frameLimit + 16)){}
|
graphics.delayFrame();
|
||||||
frameLimit = SDL_GetTicks();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0 ; i < numberOfCredits ; i++)
|
for (int i = 0 ; i < numberOfCredits ; i++)
|
||||||
|
|
Loading…
Reference in New Issue