Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Oct 27, 2013
1 parent 42c5fbe commit 49ec125
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 39 deletions.
56 changes: 26 additions & 30 deletions src/cdogs/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ static int frames = 0;

static int screenShaking = 0;

static int gameIsPaused = NO;
static int escExits = NO;
long oldtime;

// This is referenced from CDOGS.C to determine time bonus
Expand Down Expand Up @@ -429,7 +427,7 @@ void GetPlayerInput(int *cmd1, int *cmd2)
GetPlayerCmd(gPlayer1 ? cmd1 : NULL, gPlayer2 ? cmd2 : NULL, 0);
}

int HandleKey(int *done, int cmd)
int HandleKey(int cmd, int *isPaused)
{
if (IsAutoMapEnabled(gCampaign.Entry.mode))
{
Expand All @@ -452,39 +450,29 @@ int HandleKey(int *done, int cmd)
}
}

if (gameIsPaused && AnyButton(cmd))
if (*isPaused && AnyButton(cmd))
{
gameIsPaused = NO;
*isPaused = 0;
}

if (!gPlayer1 && !gPlayer2)
{
*done = 1;
return 0;
}
else if (KeyIsPressed(&gInputDevices.keyboard, SDLK_ESCAPE) ||
if (KeyIsPressed(&gInputDevices.keyboard, SDLK_ESCAPE) ||
JoyIsPressed(&gInputDevices.joysticks.joys[0], CMD_BUTTON4) ||
JoyIsPressed(&gInputDevices.joysticks.joys[1], CMD_BUTTON4))
{
if (gameIsPaused && escExits)
{
*done = 1;
return 1;
}
else if (!gameIsPaused)
if (!*isPaused)
{
gameIsPaused = YES;
escExits = YES;
*isPaused = 1;
}
return 1;
}
else if (KeyGetPressed(&gInputDevices.keyboard))
{
gameIsPaused = 0;
*isPaused = 0;
}

if (!gameIsPaused && !(SDL_GetAppState() & SDL_APPINPUTFOCUS))
if (!*isPaused && !(SDL_GetAppState() & SDL_APPINPUTFOCUS))
{
gameIsPaused = 1;
*isPaused = 1;
}

return 0;
Expand All @@ -494,7 +482,8 @@ int gameloop(void)
{
DrawBuffer buffer;
int is_esc_pressed = 0;
int done = NO;
int isDone = 0;
int isPaused = 0;
HUD hud;
Vec2i lastPosition = Vec2iZero();

Expand All @@ -506,11 +495,9 @@ int gameloop(void)
HUDDisplayMessage(&hud, MusicGetErrorMessage(&gSoundDevice));
}

gameIsPaused = NO;

missionTime = 0;
InputInit(&gInputDevices, NULL);
while (!done)
while (!isDone)
{
int cmd1 = 0, cmd2 = 0;
int ticks = 1;
Expand All @@ -519,9 +506,13 @@ int gameloop(void)
MusicSetPlaying(&gSoundDevice, SDL_GetAppState() & SDL_APPINPUTFOCUS);
InputPoll(&gInputDevices, ticks_now);
GetPlayerInput(&cmd1, &cmd2);
is_esc_pressed = HandleKey(&done, cmd1 | cmd2);
is_esc_pressed = HandleKey(cmd1 | cmd2, &isPaused);
if (is_esc_pressed && isPaused)
{
isDone = 1;
}

if (!gameIsPaused)
if (!isPaused)
{
if (!gConfig.Game.SlowMotion || (frames & 1) == 0)
{
Expand Down Expand Up @@ -557,7 +548,7 @@ int gameloop(void)
gMission.pickupTime -= ticks;
if (gMission.pickupTime <= 0)
{
done = 1;
isDone = 1;
}
}
else
Expand All @@ -571,6 +562,11 @@ int gameloop(void)
MissionUpdateObjectives();
}

if (!gPlayer1 && !gPlayer2)
{
isDone = 1;
}

lastPosition = DrawScreen(&buffer, gPlayer1, gPlayer2, lastPosition);

if (screenShaking) {
Expand All @@ -587,7 +583,7 @@ int gameloop(void)
}

HUDUpdate(&hud, ticks_now - ticks_then);
HUDDraw(&hud, gameIsPaused, escExits);
HUDDraw(&hud, isPaused);

BlitFlip(&gGraphicsDevice, &gConfig.Graphics);

Expand Down
11 changes: 2 additions & 9 deletions src/cdogs/hud.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ void DrawKeycards(HUD *hud)
}
}

void HUDDraw(HUD *hud, int isPaused, int isEscExit)
void HUDDraw(HUD *hud, int isPaused)
{
char s[50];
static time_t ot = -1;
Expand Down Expand Up @@ -407,14 +407,7 @@ void HUDDraw(HUD *hud, int isPaused, int isEscExit)

if (isPaused)
{
if (isEscExit)
{
CDogsTextStringAtCenter("Press Esc again to quit");
}
else
{
CDogsTextStringAtCenter("Paused");
}
CDogsTextStringAtCenter("Press Esc again to quit");
}

if (hud->messageTicks > 0)
Expand Down

0 comments on commit 49ec125

Please sign in to comment.