More cleanup. Also rewrote the method of frame delaying a bit.

I rewrote the method because what it was doing was so confusing, I
couldn't figure out whether it actually worked right or not. I think
it did, and it's only 2/3 of a milllisecond anyway (not noticeable
at all), but this new way of writing it is much clearer.
This commit is contained in:
onpon4 2015-11-06 21:35:07 -05:00
parent 7694d668ff
commit 17b5f60958
12 changed files with 83 additions and 89 deletions

View File

@ -86,12 +86,12 @@ int main(int argc, char **argv)
if (cheatAttempt && !engine.cheat) if (cheatAttempt && !engine.cheat)
{ {
clearScreen(black); screen_clear(black);
screen_renderString("That doesn't work anymore", -1, 285, FONT_WHITE); screen_renderString("That doesn't work anymore", -1, 285, FONT_WHITE);
screen_renderString("Try harder...", -1, 315, FONT_WHITE); screen_renderString("Try harder...", -1, 315, FONT_WHITE);
renderer_update(); renderer_update();
SDL_Delay(2000); SDL_Delay(2000);
clearScreen(black); screen_clear(black);
renderer_update(); renderer_update();
SDL_Delay(500); SDL_Delay(500);
} }

View File

@ -22,6 +22,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
Game game; Game game;
static Star stars[STARS_NUM]; static Star stars[STARS_NUM];
static Uint32 frameLimit = 0;
static int thirds = 0;
void game_init() void game_init()
{ {
@ -205,7 +207,7 @@ void game_doStars()
WRAP_ADD(stars[i].y, (engine.ssy + engine.smy) * stars[i].speed, 0, WRAP_ADD(stars[i].y, (engine.ssy + engine.smy) * stars[i].speed, 0,
screen->h - 1); screen->h - 1);
putpixel(screen, (int)stars[i].x, (int)stars[i].y, color); gfx_putPixel(screen, (int)stars[i].x, (int)stars[i].y, color);
r.x = (int)stars[i].x; r.x = (int)stars[i].x;
r.y = (int)stars[i].y; r.y = (int)stars[i].y;
r.w = 1; r.w = 1;
@ -1906,6 +1908,28 @@ static void game_doHud()
} }
} }
/*
* Delay until the next 60 Hz frame
*/
void game_delayFrame()
{
Uint32 now = SDL_GetTicks();
// Add 16 2/3 (= 1000 / 60) to frameLimit
frameLimit += 16;
thirds += 2;
while (thirds >= 3)
{
thirds -= 3;
frameLimit++;
}
if(now < frameLimit)
SDL_Delay(frameLimit - now);
else
frameLimit = now;
}
/* /*
Checked during the main game loop. When the game is paused Checked during the main game loop. When the game is paused
it goes into a constant loop checking this routine. If escape is it goes into a constant loop checking this routine. If escape is
@ -2234,7 +2258,7 @@ int game_mainLoop()
while (engine.paused) while (engine.paused)
{ {
engine.done = game_checkPauseRequest(); engine.done = game_checkPauseRequest();
delayFrame(); game_delayFrame();
} }
audio_resumeMusic(); audio_resumeMusic();
@ -2271,7 +2295,7 @@ int game_mainLoop()
(aliens[ALIEN_BOSS].flags & FL_ESCAPED)) (aliens[ALIEN_BOSS].flags & FL_ESCAPED))
{ {
audio_playSound(SFX_DEATH, aliens[ALIEN_BOSS].x); audio_playSound(SFX_DEATH, aliens[ALIEN_BOSS].x);
clearScreen(white); screen_clear(white);
renderer_update(); renderer_update();
for (int i = 0 ; i < 300 ; i++) for (int i = 0 ; i < 300 ; i++)
{ {
@ -2283,7 +2307,7 @@ int game_mainLoop()
break; break;
} }
delayFrame(); game_delayFrame();
} }
screen_flushBuffer(); screen_flushBuffer();

View File

@ -25,6 +25,7 @@ extern Game game;
void game_init(); void game_init();
void game_doStars(); void game_doStars();
void game_doExplosions(); void game_doExplosions();
void game_delayFrame();
bool game_collision(float x0, float y0, int w0, int h0, float x2, float y2, int w1, int h1); bool game_collision(float x0, float y0, int w0, int h0, float x2, float y2, int w1, int h1);
int game_mainLoop(); int game_mainLoop();

View File

@ -21,9 +21,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "Starfighter.h" #include "Starfighter.h"
static unsigned long frameLimit;
static int thirds;
SDL_Surface *gfx_background; SDL_Surface *gfx_background;
SDL_Surface *shape[MAX_SHAPES]; SDL_Surface *shape[MAX_SHAPES];
SDL_Surface *shipShape[MAX_SHIPSHAPES]; SDL_Surface *shipShape[MAX_SHIPSHAPES];
@ -58,8 +55,6 @@ void gfx_init()
gfx_background = NULL; gfx_background = NULL;
messageBox = NULL; messageBox = NULL;
frameLimit = 0;
thirds = 0;
screen = NULL; screen = NULL;
} }
@ -186,41 +181,11 @@ int gfx_renderString(const char *in, int x, int y, int fontColor, int wrap, SDL_
return gfx_renderStringBase(in, x, y, fontColor, wrap, dest); return gfx_renderStringBase(in, x, y, fontColor, wrap, dest);
} }
void clearScreen(Uint32 color)
{
SDL_FillRect(screen, NULL, color);
}
/*
* 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;
}
/* /*
* Set the pixel at (x, y) to the given value * Set the pixel at (x, y) to the given value
* NOTE: The surface must be locked before calling this! * NOTE: The surface must be locked before calling this!
*/ */
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel) void gfx_putPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{ {
int bpp = surface->format->BytesPerPixel; int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */ /* Here p is the address to the pixel we want to set */
@ -272,7 +237,7 @@ void drawLine(SDL_Surface *dest, int x1, int y1, int x2, int y2, int col)
while(1) while(1)
{ {
putpixel(dest, x1, y1, col); gfx_putPixel(dest, x1, y1, col);
if (x1 > x2) x1--; if (x1 > x2) x1--;
if (x1 < x2) x1++; if (x1 < x2) x1++;
@ -307,10 +272,10 @@ void circle(int xc, int yc, int R, SDL_Surface *PIX, int col)
int y = R, yy = 2 * R; int y = R, yy = 2 * R;
int p = 1 - R; int p = 1 - R;
putpixel(PIX, xc, yc - y, col); gfx_putPixel(PIX, xc, yc - y, col);
putpixel(PIX, xc, yc + y, col); gfx_putPixel(PIX, xc, yc + y, col);
putpixel(PIX, xc - y, yc, col); gfx_putPixel(PIX, xc - y, yc, col);
putpixel(PIX, xc + y, yc, col); gfx_putPixel(PIX, xc + y, yc, col);
while (x < y) while (x < y)
{ {
@ -324,22 +289,22 @@ void circle(int xc, int yc, int R, SDL_Surface *PIX, int col)
} }
p += xx + 1; p += xx + 1;
putpixel(PIX, xc - x, yc - y, col); gfx_putPixel(PIX, xc - x, yc - y, col);
putpixel(PIX, xc + x, yc - y, col); gfx_putPixel(PIX, xc + x, yc - y, col);
putpixel(PIX, xc - x, yc + y, col); gfx_putPixel(PIX, xc - x, yc + y, col);
putpixel(PIX, xc + x, yc + y, col); gfx_putPixel(PIX, xc + x, yc + y, col);
putpixel(PIX, xc - y, yc - x, col); gfx_putPixel(PIX, xc - y, yc - x, col);
putpixel(PIX, xc + y, yc - x, col); gfx_putPixel(PIX, xc + y, yc - x, col);
putpixel(PIX, xc - y, yc + x, col); gfx_putPixel(PIX, xc - y, yc + x, col);
putpixel(PIX, xc + y, yc + x, col); gfx_putPixel(PIX, xc + y, yc + x, col);
} }
if ((x = y)) if ((x = y))
{ {
putpixel(PIX, xc - x, yc - y, col); gfx_putPixel(PIX, xc - x, yc - y, col);
putpixel(PIX, xc + x, yc - y, col); gfx_putPixel(PIX, xc + x, yc - y, col);
putpixel(PIX, xc - x, yc + y, col); gfx_putPixel(PIX, xc - x, yc + y, col);
putpixel(PIX, xc + x, yc + y, col); gfx_putPixel(PIX, xc + x, yc + y, col);
} }
} }

View File

@ -35,9 +35,7 @@ void gfx_init();
SDL_Surface *gfx_setTransparent(SDL_Surface *sprite); SDL_Surface *gfx_setTransparent(SDL_Surface *sprite);
void gfx_blit(SDL_Surface *image, int x, int y, SDL_Surface *dest); void gfx_blit(SDL_Surface *image, int x, int y, SDL_Surface *dest);
int gfx_renderString(const char *in, int x, int y, int fontColor, int wrap, SDL_Surface *dest); int gfx_renderString(const char *in, int x, int y, int fontColor, int wrap, SDL_Surface *dest);
extern void clearScreen(Uint32 color); void gfx_putPixel(SDL_Surface *surface, int x, int y, Uint32 pixel);
extern void delayFrame();
extern void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel);
extern void drawLine(SDL_Surface *dest, int x1, int y1, int x2, int y2, int col); extern void drawLine(SDL_Surface *dest, int x1, int y1, int x2, int y2, int col);
extern void drawLine(int x1, int y1, int x2, int y2, int col); extern void drawLine(int x1, int y1, int x2, int y2, int col);
extern void circle(int xc, int yc, int R, SDL_Surface *PIX, int col); extern void circle(int xc, int yc, int R, SDL_Surface *PIX, int col);

View File

@ -45,7 +45,7 @@ be seen by people unless something really stoopid happens!
*/ */
void showErrorAndExit(int errorId, const char *name) void showErrorAndExit(int errorId, const char *name)
{ {
clearScreen(black); screen_clear(black);
if (errorId != 2) if (errorId != 2)
{ {
@ -89,7 +89,7 @@ void showErrorAndExit(int errorId, const char *name)
while (!engine.keyState[KEY_ALTFIRE]) while (!engine.keyState[KEY_ALTFIRE])
{ {
getPlayerInput(); getPlayerInput();
delayFrame(); game_delayFrame();
} }
exit(1); exit(1);

View File

@ -575,9 +575,9 @@ int intermission()
// do not perform certain keyboard actions // do not perform certain keyboard actions
engine.gameSection = SECTION_INTERMISSION; engine.gameSection = SECTION_INTERMISSION;
clearScreen(black); screen_clear(black);
renderer_update(); renderer_update();
clearScreen(black); screen_clear(black);
initSaveSlots(); initSaveSlots();
@ -948,7 +948,7 @@ int intermission()
engine.keyState[KEY_ALTFIRE] = 0; engine.keyState[KEY_ALTFIRE] = 0;
intermission_doCursor(); intermission_doCursor();
delayFrame(); game_delayFrame();
} }
audio_haltMusic(); audio_haltMusic();

View File

@ -753,12 +753,12 @@ mission begins playing here.
*/ */
void missionBriefScreen() void missionBriefScreen()
{ {
clearScreen(black); screen_clear(black);
renderer_update(); renderer_update();
if (game.area != MISN_INTERCEPTION) if (game.area != MISN_INTERCEPTION)
{ {
clearScreen(black); screen_clear(black);
drawBriefScreen(); drawBriefScreen();
if (currentMission.timeLimit1[0] > 0) if (currentMission.timeLimit1[0] > 0)
@ -828,16 +828,16 @@ void missionBriefScreen()
while (true) while (true)
{ {
delayFrame(); game_delayFrame();
getPlayerInput(); getPlayerInput();
if ((engine.keyState[KEY_FIRE]) || (engine.keyState[KEY_ALTFIRE]) || if ((engine.keyState[KEY_FIRE]) || (engine.keyState[KEY_ALTFIRE]) ||
(engine.keyState[KEY_ESCAPE])) (engine.keyState[KEY_ESCAPE]))
break; break;
} }
clearScreen(black); screen_clear(black);
renderer_update(); renderer_update();
clearScreen(black); screen_clear(black);
} }
engine.gameSection = SECTION_GAME; engine.gameSection = SECTION_GAME;
@ -856,13 +856,13 @@ void missionFinishedScreen()
if (game.area != MISN_INTERCEPTION) if (game.area != MISN_INTERCEPTION)
{ {
clearScreen(black); screen_clear(black);
renderer_update(); renderer_update();
if (game.shots > 0) if (game.shots > 0)
game.accuracy = (game.hits * 100) / game.shots; game.accuracy = (game.hits * 100) / game.shots;
clearScreen(black); screen_clear(black);
drawBriefScreen(); drawBriefScreen();
for (int i = 0 ; i < 3 ; i++) for (int i = 0 ; i < 3 ; i++)
@ -931,7 +931,7 @@ void missionFinishedScreen()
while (true) while (true)
{ {
delayFrame(); game_delayFrame();
getPlayerInput(); getPlayerInput();
if ((engine.keyState[KEY_FIRE])) if ((engine.keyState[KEY_FIRE]))
break; break;

View File

@ -112,3 +112,8 @@ void screen_unBuffer()
screen_bufferHead->next = NULL; screen_bufferHead->next = NULL;
} }
void screen_clear(Uint32 color)
{
SDL_FillRect(screen, NULL, color);
}

View File

@ -33,5 +33,6 @@ void screen_drawBackground();
void screen_addBuffer(int x, int y, int w, int h); void screen_addBuffer(int x, int y, int w, int h);
void screen_flushBuffer(); void screen_flushBuffer();
void screen_unBuffer(); void screen_unBuffer();
void screen_clear(Uint32 color);
#endif #endif

View File

@ -186,9 +186,9 @@ static void setScene(int scene)
void doCutscene(int scene) void doCutscene(int scene)
{ {
clearScreen(black); screen_clear(black);
renderer_update(); renderer_update();
clearScreen(black); screen_clear(black);
engine.keyState[KEY_FIRE] = 0; engine.keyState[KEY_FIRE] = 0;
engine.keyState[KEY_ALTFIRE] = 0; engine.keyState[KEY_ALTFIRE] = 0;
@ -303,7 +303,7 @@ void doCutscene(int scene)
screen_renderString("Press [Escape] to skip", -1, 580, FONT_WHITE); screen_renderString("Press [Escape] to skip", -1, 580, FONT_WHITE);
delayFrame(); game_delayFrame();
if ((engine.keyState[KEY_ESCAPE]) || (engine.keyState[KEY_FIRE]) || if ((engine.keyState[KEY_ESCAPE]) || (engine.keyState[KEY_FIRE]) ||
(engine.keyState[KEY_ALTFIRE])) (engine.keyState[KEY_ALTFIRE]))
@ -312,6 +312,6 @@ void doCutscene(int scene)
screen_flushBuffer(); screen_flushBuffer();
freeGraphics(); freeGraphics();
clearScreen(black); screen_clear(black);
renderer_update(); renderer_update();
} }

View File

@ -190,9 +190,9 @@ int doTitle()
loadGameGraphics(); loadGameGraphics();
clearScreen(black); screen_clear(black);
renderer_update(); renderer_update();
clearScreen(black); screen_clear(black);
signed char continueSaveSlot = initSaveSlots(); signed char continueSaveSlot = initSaveSlots();
@ -530,7 +530,7 @@ int doTitle()
engine.keyState[KEY_FIRE] = engine.keyState[KEY_ALTFIRE] = 0; engine.keyState[KEY_FIRE] = engine.keyState[KEY_ALTFIRE] = 0;
} }
delayFrame(); game_delayFrame();
} }
audio_haltMusic(); audio_haltMusic();
@ -622,7 +622,7 @@ void showStory()
break; break;
} }
delayFrame(); game_delayFrame();
} }
} }
@ -640,9 +640,9 @@ void gameover()
SDL_Surface *gameover = loadImage("gfx/gameover.png"); SDL_Surface *gameover = loadImage("gfx/gameover.png");
clearScreen(black); screen_clear(black);
renderer_update(); renderer_update();
clearScreen(black); screen_clear(black);
SDL_Delay(1000); SDL_Delay(1000);
audio_playMusic("music/death.ogg", -1); audio_playMusic("music/death.ogg", -1);
@ -669,7 +669,7 @@ void gameover()
y = ((screen->h - gameover->h) / 2) - RANDRANGE(-2, 2); y = ((screen->h - gameover->h) / 2) - RANDRANGE(-2, 2);
screen_blit(gameover, x, y); screen_blit(gameover, x, y);
delayFrame(); game_delayFrame();
} }
SDL_FreeSurface(gameover); SDL_FreeSurface(gameover);
@ -693,9 +693,9 @@ void doCredits()
textObject *credit; textObject *credit;
clearScreen(black); screen_clear(black);
renderer_update(); renderer_update();
clearScreen(black); screen_clear(black);
screen_drawBackground(); screen_drawBackground();
@ -751,7 +751,7 @@ void doCredits()
credit[i].y -= speed; credit[i].y -= speed;
} }
delayFrame(); game_delayFrame();
} }
for (i = 0 ; i <= lastCredit ; i++) for (i = 0 ; i <= lastCredit ; i++)