diff options
author | H. Peter Anvin <hpa@zytor.com> | 2003-03-24 16:31:19 +0000 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2003-03-24 16:31:19 +0000 |
commit | 1128ad360b5cccb1b82de092505e5ca1c4dbed8d (patch) | |
tree | 89c7ebe649b47fa1b901872d646a2183df023c8d /keyboard.c | |
download | grv-1128ad360b5cccb1b82de092505e5ca1c4dbed8d.tar.gz grv-1128ad360b5cccb1b82de092505e5ca1c4dbed8d.tar.xz grv-1128ad360b5cccb1b82de092505e5ca1c4dbed8d.zip |
Port of "grävning" to C/SDL, started 2003-03-22
Diffstat (limited to 'keyboard.c')
-rw-r--r-- | keyboard.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/keyboard.c b/keyboard.c new file mode 100644 index 0000000..06d57b9 --- /dev/null +++ b/keyboard.c @@ -0,0 +1,75 @@ +/* + * keyboard.c + * + * Basic keyboard handing functions -- allows event-handling loops + * to push keyboard events onto a queue so they can be processed at + * the appropriate time in the game rounds loop + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <math.h> +#include "graphics.h" +#include "grv.h" + +#define KEYQUEUELEN 16 + +static SDL_KeyboardEvent keyqueue[KEYQUEUELEN]; +static SDL_KeyboardEvent *kqh = keyqueue; +static SDL_KeyboardEvent *kqt = keyqueue; +static int queuedkeys = 0; + +/* + * Push a keyboard event onto the queue; use this in event loops + */ +void push_key(SDL_KeyboardEvent *ke) +{ + if ( queuedkeys >= KEYQUEUELEN ) + return; /* Drop it */ + + queuedkeys++; + *kqh++ = *ke; + if ( kqh >= &keyqueue[KEYQUEUELEN] ) + kqh = keyqueue; +} + +/* + * Poll the keyboard queue for a key event + */ +SDL_KeyboardEvent *poll_key(void) +{ + SDL_KeyboardEvent *ke; + + if ( queuedkeys ) { + queuedkeys--; + ke = kqt++; + if ( kqt >= &keyqueue[KEYQUEUELEN] ) + kqt = keyqueue; + return ke; + } else { + return NULL; +} +} + +/* + * Get a key, pausing if necessary + */ +SDL_KeyboardEvent *get_key(void) +{ + static SDL_Event event; + SDL_KeyboardEvent *ke; + + if ( (ke = poll_key()) ) + return ke; + + while ( SDL_WaitEvent(&event) ) { + if ( event.type == SDL_KEYDOWN ) + return &event.key; + else if ( event.type == SDL_USEREVENT && event.user.code == event_blink ) + update_blink(); + } + return NULL; +} + |