Skip to content

Commit

Permalink
Implement keyboard event functions (fixes #60)
Browse files Browse the repository at this point in the history
New functions include KeyInit(), KeyPoll(),
KeyIsDown(), KeyIsPressed(), KeyIsReleased()
and KeyGetPressed(). Callers do not need to
track cmd/prevcmd to detect new key presses.
  • Loading branch information
cxong committed May 19, 2013
1 parent bf6ec91 commit a3c1be5
Show file tree
Hide file tree
Showing 15 changed files with 485 additions and 304 deletions.
38 changes: 27 additions & 11 deletions src/automap.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013, Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "automap.h"

Expand Down Expand Up @@ -213,7 +240,6 @@ void DisplayAutoMap(int showAll)
unsigned char *p;
unsigned char *screen;
TTileItem *t;
int cmd1, cmd2;
int obj;

screen = p = GetDstScreen();
Expand Down Expand Up @@ -287,14 +313,4 @@ void DisplayAutoMap(int showAll)
DisplaySummary();

CopyToScreen();

if (!showAll) {
do {
cmd1 = cmd2 = 0;
GetPlayerCmd(gPlayer1 ? &cmd1 : NULL,
gPlayer2 ? &cmd2 : NULL);
}
while (((cmd1 | cmd2) & CMD_BUTTON3) != 0 || KeyDown(gOptions.mapKey));
memset(GetDstScreen(), 0, Screen_GetMemSize());
}
}
15 changes: 8 additions & 7 deletions src/cdogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include "campaigns.h"
#include "config.h"
#include "credits.h"
#include "events.h"
#include "joystick.h"
#include "objs.h"
#include "actors.h"
Expand Down Expand Up @@ -201,7 +202,7 @@ void CampaignIntro(void *bkg)
MissionDescription(y, gCampaign.setting->description);

CopyToScreen();
Wait();
GetKey(&gKeyboard);
}

void MissionBriefing(void *bkg)
Expand Down Expand Up @@ -243,7 +244,7 @@ void MissionBriefing(void *bkg)

CopyToScreen();

Wait();
GetKey(&gKeyboard);
}

void Summary(int x, struct PlayerData *data, int character)
Expand Down Expand Up @@ -416,7 +417,7 @@ void MissionSummary(void *bkg)

CopyToScreen();

Wait();
GetKey(&gKeyboard);
}

void ShowScore(void *bkg, int score1, int score2)
Expand All @@ -440,7 +441,7 @@ void ShowScore(void *bkg, int score1, int score2)
}

CopyToScreen();
Wait();
GetKey(&gKeyboard);
}

void FinalScore(void *bkg, int score1, int score2)
Expand All @@ -464,7 +465,7 @@ void FinalScore(void *bkg, int score1, int score2)
IS_WINNER);
}
CopyToScreen();
Wait();
GetKey(&gKeyboard);
}


Expand Down Expand Up @@ -532,7 +533,7 @@ void Victory(void *bkg)
PlaySound(SND_HAHAHA, 0, 255);

CopyToScreen();
Wait();
GetKey(&gKeyboard);
}


Expand Down Expand Up @@ -1060,10 +1061,10 @@ int main(int argc, char *argv[])

PlayMenuSong();

//LookForCustomCampaigns();
LoadAllCampaigns(&campaigns);

InitSticks();
KeyInit(&gKeyboard);

if (wait)
{
Expand Down
1 change: 1 addition & 0 deletions src/cdogsed.c
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,7 @@ int main(int argc, char *argv[])
memset(myScreen, 0, Screen_GetMemSize());
SetDstScreen(myScreen);

KeyInit(&gKeyboard);
InitMouse();
EditCampaign();

Expand Down
48 changes: 34 additions & 14 deletions src/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,32 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-------------------------------------------------------------------------------
events.c - event related functions
Author: $Author$
Rev: $Revision$
URL: $HeadURL$
ID: $Id$
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013, Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdlib.h>
Expand Down Expand Up @@ -66,12 +83,15 @@ void Mouse(int *x, int *y, int *button)
}
}

int GetKey(void)
int GetKey(keyboard_t *keyboard)
{

while (!AnyKeyDown());

return GetKeyDown();
int key_pressed = 0;
do
{
KeyPoll(keyboard);
key_pressed = KeyGetPressed(keyboard);
} while (!key_pressed);
return key_pressed;
}

void SetMouseRects(struct MouseRect *rects)
Expand Down
31 changes: 30 additions & 1 deletion src/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,39 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013, Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __EVENTS
#define __EVENTS

#include "keyboard.h"

struct MouseRect {
int left, top, right, bottom;
int tag;
Expand All @@ -30,7 +59,7 @@ struct MouseRect {

void InitMouse(void);
void Mouse(int *x, int *y, int *button);
int GetKey(void);
int GetKey(keyboard_t *keyboard);
void SetMouseRects(struct MouseRect *rects);
void SetSecondaryMouseRects(struct MouseRect *rects);
int GetMouseRectTag(int x, int y, int *tag);
Expand Down
63 changes: 36 additions & 27 deletions src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,16 @@ void DisplayMessage(const char *s)

int HandleKey(int *done, int cmd)
{
static int lastKey = 0;
int key = GetKeyDown();

if ((key == gOptions.mapKey || (cmd & CMD_BUTTON3) != 0) && !gCampaign.dogFight) {
while ((KeyIsDown(&gKeyboard, gOptions.mapKey) || (cmd & CMD_BUTTON3) != 0) &&
!gCampaign.dogFight)
{
DisplayAutoMap(0);
while (KeyIsDown(&gKeyboard, gOptions.mapKey) ||
(cmd & CMD_BUTTON3) != 0)
{
SDL_Delay(10);
KeyPoll(&gKeyboard);
}
}

if (((cmd & CMD_BUTTON4) != 0) && !gOptions.twoPlayers) {
Expand All @@ -503,47 +508,49 @@ int HandleKey(int *done, int cmd)
} else if (gameIsPaused && AnyButton(cmd))
gameIsPaused = NO;

if (key == lastKey)
return 0;

lastKey = key;
if (!key)
return 0;

if (!gPlayer1 && !gPlayer2)
*done = YES;
else if (key == keyEsc) {
{
*done = 1;
return 0;
}
else if (KeyIsPressed(&gKeyboard, keyEsc))
{
if (gameIsPaused && escExits)
*done = YES;
else if (!gameIsPaused) {
{
*done = 1;
return 1;
}
else if (!gameIsPaused)
{
gameIsPaused = YES;
escExits = YES;
}
} else
gameIsPaused = NO;
}
else if (KeyGetPressed(&gKeyboard))
{
gameIsPaused = 0;
}

// if (key >= key1 && key <= key0)
// ToggleTrack( key - key1);

return key;
return 0;
}

static void GetPlayerInput(int *cmd1, int *cmd2)
void GetPlayerInput(int *cmd1, int *cmd2)
{
*cmd1 = *cmd2 = 0;
GetPlayerCmd(gPlayer1 ? cmd1 : NULL, gPlayer2 ? cmd2 : NULL);
GetPlayerCmd(gPlayer1 ? cmd1 : NULL, gPlayer2 ? cmd2 : NULL, 0);
}

int gameloop(void)
{
struct Buffer *buffer;
struct Buffer *buffer = NewBuffer();
int ticks;
int c = 0;
int is_esc_pressed = 0;
int done = NO;
time_t t;
struct tm *tp;

buffer = NewBuffer();
CDogsSetClip(0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1);

if (ModuleStatus() != MODULE_OK)
Expand All @@ -552,7 +559,7 @@ int gameloop(void)
gameIsPaused = NO;

missionTime = 0;
//screenShaking = 0;
KeyInit(&gKeyboard);
while (!done)
{
int cmd1 = 0, cmd2 = 0;
Expand Down Expand Up @@ -615,6 +622,8 @@ int gameloop(void)

CopyToScreen();

KeyPoll(&gKeyboard);

if (!gameIsPaused) {
if (!gOptions.slowmotion || (frames & 1) == 0) {
UpdateAllActors(ticks);
Expand All @@ -640,11 +649,11 @@ int gameloop(void)
GetPlayerInput(&cmd1, &cmd2);
}

c = HandleKey(&done, cmd1 | cmd2);
is_esc_pressed = HandleKey(&done, cmd1 | cmd2);

Ticks_FrameEnd();
}
free(buffer);

return c != keyEsc;
return !is_esc_pressed;
}
Loading

0 comments on commit a3c1be5

Please sign in to comment.