00001 00007 // /** 00008 // *@defgroup keyboard keyboard.c 00009 // *Handles keyboard input 00010 // *@note Does not trigger keyboard events; events are handled by the keyhandler function in the handler.c 00011 // *routines. The keyhandler function is called by this modules keycheck function via a function pointer. 00012 // *@{ 00013 // */ 00014 00015 /*----------------------------------------------------------------------------------------------------------*/ 00016 00017 #ifndef _ALLEGRO5_ 00018 #define _ALLEGRO5_ 00019 #include <allegro5/allegro5.h> 00020 #endif // _ALLEGRO5_ 00021 00022 #ifndef _KEYBOARD_ 00023 #define _KEYBOARD_ 00024 #include "../include/keyboard.h" 00025 #endif // _KEYBOARD_ 00026 00027 /*----------------------------------------------------------------------------------------------------------*/ 00028 00029 static char key[256]; // Keyboards only have upto 256 keycodes, so no array out of bounds can occur 00030 00031 /*----------------------------------------------------------------------------------------------------------*/ 00032 00033 // If a key is pressed down, add it to the key array 00034 void keydown(ALLEGRO_KEYBOARD_EVENT *kb) 00035 { 00036 key[kb->keycode] = KEYPRESSED | KEYNEW; 00037 } 00038 00039 /*----------------------------------------------------------------------------------------------------------*/ 00040 00041 // If a key is released, mark it as unpressed 00042 // But if it was not yet processed, leave the keynew flag 00043 void keyup(ALLEGRO_KEYBOARD_EVENT *kb) 00044 { 00045 key[kb->keycode] &= ~KEYPRESSED; 00046 } 00047 00048 /*----------------------------------------------------------------------------------------------------------*/ 00049 00050 // If an operating system repeat event comes in, set the flag 00051 void keyrepeat(ALLEGRO_KEYBOARD_EVENT *kb) 00052 { 00053 key[kb->keycode] |= KEYREPEAT; 00054 } 00055 00056 /*----------------------------------------------------------------------------------------------------------*/ 00057 00058 // Called once per frame: removes the KEYNEW and KEYREPEAT status from all keys 00059 void keyupdate(void) 00060 { 00061 int i; 00062 static int val = ((KEYNEW | KEYREPEAT) << 24) | ((KEYNEW | KEYREPEAT) << 16) | 00063 ((KEYNEW | KEYREPEAT) << 8) | KEYNEW | KEYREPEAT; 00064 00065 for(i=0; i<64; i++) 00066 { 00067 ((int*)key)[i] &= ~val; 00068 } 00069 } 00070 00071 /*----------------------------------------------------------------------------------------------------------*/ 00072 00073 // Empties the key buffer 00074 void keyclear(void) 00075 { 00076 memset(key, 0, sizeof(*key)*256); 00077 } 00078 00079 /*----------------------------------------------------------------------------------------------------------*/ 00080 00081 // Check for keyboard input 00082 void* keycheck(KEY_CALLBACK func, void *data) 00083 { 00084 return func(key, data); 00085 } 00086 00087 /*----------------------------------------------------------------------------------------------------------*/ 00088 00089 // /** 00090 // *@} 00091 // */