home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / RAYCAST.ZIP / KEYBOARD.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-02  |  3.7 KB  |  155 lines

  1. #include "os.h"
  2. #include "keyinfo.h"
  3. #include "keyboard.h"
  4. #include "playint.h"
  5. #include "ray.h"
  6. #include "sprtypes.h"
  7. #include "rayspr.h"
  8.  
  9. int raw_key=0;                  // the global raw keyboard data aquired from the ISR
  10.  
  11. int key_table[MAX_TRACKED_INPUTS-1] = {0,0,0,0,0,0}; // the key state table for the motion keys
  12.  
  13. #ifdef OS_DOS
  14. #include <dos.h>
  15. #include <conio.h>
  16. #include <i86.h>
  17.  
  18. #define INT_CONTROL     0x20         // interrupt control register 
  19. #define KEYBOARD_INT    0x09         // the keyboard interrupt vector 
  20. #define KEY_BUFFER      0x60         // keyboard buffer area 
  21. #define KEY_CONTROL     0x61         // keyboard control register 
  22.  
  23. unsigned short getkey(void);
  24.  
  25. // Routine to read a key directly from the port
  26. #pragma aux getkey = \
  27.    " sti" \
  28.    "in al, 60h" \
  29.    "xor ah,ah" \
  30.    "mov bx, ax" \
  31.    "in al, 61h" \
  32.    "or al, 82h" \
  33.    "out 61h,al" \
  34.    "and al,7fh" \
  35.    "out 61h,al" \
  36.    "mov al,20h" \
  37.    "out 20h,al" \
  38.    modify [ax bx] \
  39.    value [bx];
  40.  
  41. void (_interrupt _far *Old_Key_Isr)(); // holds old keyboard interrupt handler 
  42.  
  43. void _interrupt _far New_Key_Int(void) 
  44. // this function links into the keyboard interrupt and takes over.  it is called 
  45. // when a key is pressed.  Note: how it differs from the one were saw in the 
  46. // chapter on I/O.  It has been modified to take into consideration the demo 
  47. // mode of the system 
  48.  
  49. int temp_key=getkey();
  50. // now for some C to update the arrow state table 
  51.  
  52. // process the key and update the table 
  53. Process_Key(temp_key);
  54.  
  55. } // end New_Key_Int 
  56.  
  57. #endif
  58.  
  59. void Init_Keyboard() {
  60. #ifdef OS_DOS
  61. Old_Key_Isr = _dos_getvect(KEYBOARD_INT); 
  62.  
  63. _dos_setvect(KEYBOARD_INT, New_Key_Int); 
  64. #endif
  65. }
  66.  
  67. void Close_Keyboard() {
  68. #ifdef OS_DOS
  69. _dos_setvect(KEYBOARD_INT, Old_Key_Isr); 
  70. #endif
  71. }
  72.  
  73. void Process_Key(short temp_key) {
  74.  
  75. switch(temp_key)
  76.       { 
  77.       case MAKE_UP:    // pressing up 
  78.            { 
  79.            key_table[INDEX_UP]    = Obj_Type_List[PLAYER_TYPE].stats.base_speed;
  80.            } break; 
  81.  
  82.       case MAKE_DOWN:  // pressing down 
  83.            { 
  84.            key_table[INDEX_DOWN]  = Obj_Type_List[PLAYER_TYPE].stats.base_speed;
  85.            } break; 
  86.  
  87.       case MAKE_RIGHT: // pressing right 
  88.            { 
  89.            key_table[INDEX_RIGHT] = PLAYER_ROT_SPEED;
  90.            } break; 
  91.  
  92.       case MAKE_LEFT:  // pressing left 
  93.            { 
  94.            key_table[INDEX_LEFT]  = PLAYER_ROT_SPEED;
  95.            } break; 
  96.  
  97.       case MAKE_LUP: // look up
  98.            {
  99.            key_table[LOOK_UP]=ANGLE_2;
  100.            } break;
  101.  
  102.       case MAKE_LDOWN: // look down
  103.            {
  104.            key_table[LOOK_DOWN]=ANGLE_2;
  105.            } break;
  106.  
  107.       case MAKE_CNTL: // ctrl key
  108.            {
  109.            key_table[INDEX_GUN]=1;
  110.            }
  111.            break;
  112.  
  113.       case BREAK_UP:    // releasing up 
  114.            { 
  115.            key_table[INDEX_UP]    = 0; 
  116.            } break; 
  117.  
  118.       case BREAK_DOWN:  // releasing down 
  119.            { 
  120.            key_table[INDEX_DOWN]  = 0; 
  121.            } break; 
  122.  
  123.       case BREAK_RIGHT: // releasing right 
  124.            { 
  125.            key_table[INDEX_RIGHT] = 0; 
  126.            } break; 
  127.  
  128.       case BREAK_LEFT:  // releasing left 
  129.            { 
  130.            key_table[INDEX_LEFT]  = 0; 
  131.            } break; 
  132.  
  133.       case BREAK_LUP: // releasing look up
  134.            {
  135.            key_table[LOOK_UP]=0;
  136.            } break;
  137.  
  138.       case BREAK_LDOWN: // releasing look down
  139.            {
  140.            key_table[LOOK_DOWN]=0;
  141.            } break;
  142.  
  143.       case BREAK_CNTL: // releasin control key
  144.            {
  145.            key_table[INDEX_GUN]=0;
  146.            }
  147.            break;
  148.  
  149.       default: raw_key=temp_key; break;
  150.  
  151.  
  152.       } // end switch 
  153. }
  154.