home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / GAMEKIT.ZIP / GAMES.TXT < prev    next >
Encoding:
Text File  |  1992-10-18  |  7.3 KB  |  199 lines

  1. x1How to use GAMES.TPU -- overview -- by Lou DuChez, Pascal-kind-of-guy
  2.  
  3. GAMES.TPU is a Turbo Pascal unit designed to rework the keyboard and timer
  4. interrupts to better function in game writing.  The keyboard is the biggest
  5. problem in a lot of games: the keyboard buffer reads keys sequentially, but you
  6. want simultaneous reads for action games.  Now scan codes for key presses and
  7. releases are transmitted through port 60h, so it seems like it should be
  8. possible just to keep an eye on that memory location.  Unfortunately, interrupt
  9. 09h (the keyboard interrupt) ends up resetting the port to 0 before you can
  10. rwad the contents.  So I wrote a new interrupt handler that does these steps:
  11.  
  12. 1)  reads port 60h and records presses / releases;
  13. 2)  calls the regular keyboard interrupt;
  14. 3)  clears the keyboard buffer.
  15.  
  16. All key presses are stored in an array of 128 boolean values.  I've found that
  17. you can keep track of about six keys simultaneously -- more than enough for
  18. most games.
  19.  
  20. I've also found it useful to nullify the "Ctrl-Break" interrupt.  Basically,
  21. you don't want to "Ctrl-Break" out of a Pascal program before resetting the
  22. keyboard interrupt, so I made a "Ctrl-Break" interrupt that does absolutely
  23. nothing.
  24.  
  25. Finally, when you write an action game, you want it to run at the same speed on
  26. all computers, be they original PC's or 486's.  The best way to do that is to
  27. monitor the computer's timer; I wrote an interrupt that will pause until a
  28. fixed number of "ticks" (18.2 per second) go by.
  29.  
  30.                       GAMES.TPU -- the Keyboard interrupt
  31.  
  32. You install the new keyboard interrupt by invoking procedure INITNEWKEYINT (and
  33. reset to the old one by invoking SETOLDKEYINT).  As mentioned before, the
  34. status of the keys is recorded in a boolean array (from 0 to 127) called
  35. KEYDOWN.  A "True" indicates the key is down; a "False" indicates it is not. 
  36. So your program just has to check this array periodically to see what keys are
  37. down.
  38.  
  39. Now as for figuring out which array elements correspond to which keys: I
  40. provide two ways.  First of all, there is a function "SCANOF": it takes a
  41. character argument, and returns (as a byte) what scan code corresponds to the
  42. character (more accurately, the key that makes the character).  If, for
  43. example, you need to know the scan code of the "1" key, you'd want the value
  44. returned by SCANOF('1') (or SCANOF('!'), since you're trying to see if the
  45. "1"/"!" key is down, and the "Shift" keys aren't an issue).  In particular,
  46. you'd know that the "1" key is down if KEYDOWN[SCANOF('1')] was "True".
  47.  
  48. The SCANOF function works for all the alphanumeric and punctuation keys; it
  49. doesn't work for the arrows, "NumLock", function keys, etc. because there's no
  50. particular characters to associate with them.  So here are some constants that
  51. you can use instead:
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. CONSTANT  VAL  DESCRIPTION              CONSTANT  VAL  DESCRIPTION
  68.  
  69. escscan   $01  "Esc"                    entscan   $1c  "Enter"
  70. backscan  $0e  Backspace                rshscan   $36  Right Shift
  71. ctrlscan  $1d  "Ctrl"                   prtscan   $37  "PrntScrn"
  72. lshscan   $2a  Left Shift               altscan   $38  "Alt"
  73. capscan   $3a  "CapLock"                homescan  $47  "Home"
  74. f1scan    $3b  F1                       upscan    $48  Up Arrow
  75. f2scan    $3c  F2                       pgupscan  $49  "Pg Up"
  76. f3scan    $3d  F3                       minscan   $4a  "-" on keypad
  77. f4scan    $3e  F4                       leftscan  $4b  Left Arrow
  78. f5scan    $3f  F5                       midscan   $4c  "5" on keypad
  79. f6scan    $40  F6                       rightscan $4d  Right Arrow
  80. f7scan    $41  F7                       plusscan  $4e  "+" on keypad
  81. f8scan    $42  F8                       endscan   $4f  "End"
  82. f9scan    $43  F9                       downscan  $50  Down Arrow
  83. f10scan   $44  F10                      pgdnscan  $51  "Pg Down"
  84. f11scan   $d9  F11                      insscan   $52  "Ins"
  85. f12scan   $da  F12                      delscan   $53  "Del"
  86. scrlscan  $46  "ScrollLock"             numscan   $45  "Num Lock"
  87. tabscan   $0f  Tab
  88.  
  89. Is the left arrow down?  It is, if KEYDOWN[LEFTSCAN] is "True".
  90.  
  91. There is a second array of booleans (0 to 127), called WASDOWN: it records
  92. whether or not a key has been depressed in a period of time.  This is more
  93. useful for keys that get tapped instead of continuously held down: for example,
  94. a movement key is held, but a fire button is tapped.  Has the Space Bar been
  95. pressed?  Only if WASDOWN[SCANOF(' ')] is "True".  A procedure to use with the
  96. WASDOWN array is CLEARWASDOWNARRAY:  resets all the elements of WASDOWN to
  97. "False".  ("FOR COUNTER := 0 TO 127 DO WASDOWN[COUNTER] := FALSE")  So you
  98. reset the array with CLEARWASDOWNARRAY, and WASDOWN will record all the keys
  99. that have been pressed until you call CLEARWASDOWNARRAY again.
  100.  
  101.                      GAMES.TPU -- the "Ctrl-Brk" interrupt
  102.  
  103. This one takes little to no explanation.  Call INITNEWBRKINT to call the new
  104. interrupt (essentially, to disable it); call SETOLDBRKINT to reset it.
  105.  
  106.                        GAMES.TPU -- the Timer interrupt
  107.  
  108. 18.2 times per second, the computer generates a "tick", calls hardware
  109. interrupt 08h, updates its clock/calendar, does various house-cleaning
  110. functions, and then calls interrupt 1Bh.  1Bh is an interrupt for programmers
  111. to hook their programs onto for timing purposes; that's exactly what GAMES.TPU
  112. does.  Invoke the new timer handler with INITNEWTIMINT (and disable it with
  113. SETOLDTIMINT); it first calls whatever TSR's might be using that interrupt
  114. already, then it increments a counter.  You indirectly access that counter by
  115. the procedure TICKWAIT: the computer waits until the counter gets to the number
  116. of ticks specified before doing anything else (byte values only).  Once that
  117. number has been reached, the counter resets to 0.
  118.  
  119. I think an example is in order: let's say you have an action game where each
  120. round should take one-half second on any machine.  So at the beginning of the
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. first round, call TICKWAIT(0) (to set the tick counter to 0).  At the end of
  134. each round, call TICKWAIT(9) (to wait out the half second).  Now between those
  135. steps, while the computer has been drawing things on the screen, updating enemy
  136. positions, etc., the tick counter has been automatically incrementing every
  137. .055 seconds.  On a 4.77Mhz XT, the counter may have gotten to 6 by the time
  138. the program gets to the TICKWAIT(9) statement, which waits three more ticks
  139. before proceeding to the next step.  On a 486, the tick counter might get only
  140. to 1 before encountering the TICKWAIT(9); in which case, the computer will wait
  141. eight ticks before proceeding.  So you can make programs that run at the same
  142. speed on any computer via the timer interrupt and TICKWAIT.
  143.  
  144. Some sample pseudocode:
  145.  
  146. program gamething;
  147. uses games;
  148.  
  149. procedure movefoes;
  150.      begin
  151.        .
  152.        .
  153.      end;
  154.  
  155. procedure firephasers;
  156.      begin
  157.        .
  158.        .
  159.      end;
  160.  
  161. procedure updatescore;
  162.      begin
  163.        .
  164.        .
  165.      end; 
  166.  
  167. procedure playgame;
  168.      begin
  169.      repeat begin
  170.           movefoes;
  171.           firephasers;
  172.           updatescore;
  173.           tickwait(2);
  174.           end until igotblownup;
  175.      end;
  176.  
  177. begin {main program}
  178. initnewtimint;
  179. tickwait(0);
  180. playgame;
  181. setoldtimint;
  182. end.
  183.  
  184. Included are GAMES.PAS (compile it yourself -- I wrote it via Turbo Pascal 6.0,
  185. but it ought to work on 4 and 5 too), and RAIDERS.EXE, a game I wrote using all
  186. those interrupts.  Enjoy!
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.