home *** CD-ROM | disk | FTP | other *** search
/ UpTime Volume 2 #8 / utv2n8s1.d64 / tricks < prev    next >
Encoding:
Text File  |  1988-01-01  |  12.0 KB  |  271 lines

  1. @@
  2.  
  3.  
  4.             Tips & Tricks
  5.  
  6.        Volume Two, Number Eight
  7.  
  8.    Copyright 1989 by Jon Perregaux
  9.          All Rights Reserved
  10.  
  11.  
  12.              Published by
  13.       Softdisk Publishing, Inc.
  14.  
  15.  
  16.  
  17. %cMouse Basics n' Other Stuff
  18.  
  19.      This month we offer some tips for BASIC programmers, a little insight on a little-known CP/M feature and a valuable collection of handy 1351 mouse tips & tricks.
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. 1. DISPOSING NEXT
  28.  
  29.      FOR/NEXT loops have only one drawback: there is no way to abort a loop in progress without leaving it "hanging."  BASIC 7.0 on the C128 uses an EXIT command as a way out for DO/LOOP but no such command exists for FOR/NEXT.  But by its very nature, the FOR/NEXT loop is easy to abort.  Just set the index variable equal to the end-of-loop value and use a single NEXT command to close the loop.  In English, that translates to "make the computer stop counting by telling it it's done counting."  It works rather well and looks something like this:
  30.  
  31. 10 FOR X = 1 TO 10000
  32. 20 PRINT X
  33. 30 GET X$
  34. 40 IF X$<>"" THEN X=10000 : NEXT : PRINT "ABORTED FOR/NEXT LOOP!": END
  35. 50 NEXT : PRINT "LOOP COMPLETE"
  36.  
  37.      Keyboard input is checked in line 40.  If the user presses any key, the loop will be terminated.  The "X=10000 : NEXT" works like an imaginary "DISPOSE NEXT" command.  The loop is immediately stopped.
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. 2. CP/M'S BUILT-IN "KEYFIG"
  48.  
  49.      There is a utility program on the CP/M 3.0 boot disk called KEYFIG.  This nifty program lets you redefine any key on the C128 keyboard to whatever you like: strings of text, control characters, color changes, etc.  It is a little known fact that CP/M 3.0 has a built-in KEYFIG feature that can be used whenever you see the disk drive letter prompt.  Detailed instructions on how to use it can be called up by inserting the CP/M boot disk and typing "HELP KEYFIG FOR{$e4}EXPERTS" at the system prompt.  The underline character is generated by pressing the back-arrow key just below ESC.
  50.  
  51.      The built-in KEYFIG feature allows editing in either string format or hexadecimal.  Keys that may be edited as strings are HELP and the eight function keys.  Strangely enough, the RUN key cannot be redefined with a string as in 128-mode.
  52.  
  53.      The following keys must be pressed in order to enter either of the two editing modes prior to selecting a key to edit:
  54.  
  55. String edit mode:
  56. CONTROL + RIGHT SHIFT + RIGHT ARROW
  57.  
  58. Hexadecimal edit mode:
  59. CONTROL + RIGHT SHIFT + LEFT ARROW
  60.  
  61.      Hexadecimal mode is for changing the character codes generated from the C128 keyboard.  Any key which generates a character or control code may be edited in this fashion.
  62.  
  63.      String edit mode is for the function keys and HELP.  In string edit mode there are ways to insert and delete text and move the cursor left and right.  The methods used are rather clumsy but do work...
  64.  
  65.  
  66. Move cursor left:
  67. CONTROL + RIGHT SHIFT + LEFT ARROW
  68.  
  69. Move cursor right:
  70. CONTROL + RIGHT SHIFT + RIGHT ARROW
  71.  
  72. Insert spaces for text:
  73. CONTROL + RIGHT SHIFT + PLUS KEY
  74.  
  75. Delete characters:
  76. CONTROL + RIGHT SHIFT + MINUS KEY
  77.  
  78. Accept string & return to CP/M prompt:
  79. CONTROL + RIGHT SHIFT + RETURN
  80.  
  81.  
  82. 3. IT'S NOTHING, REALLY
  83.  
  84.      Commodore 128 BASIC handles null strings a little better than C64 BASIC.  Using the ASC() function on an empty string in C64 BASIC will result in an ?ILLEGAL QUANTITY ERROR while C128 BASIC returns a value of 0.
  85.  
  86.      The difference between a null string and an empty string is not often clearly understood.  A null string has a valid character value of zero (as in X$=CHR$(0), for example) while an empty string contains NO characters (X$="").
  87.  
  88.      When retrieving single bytes of data from a disk file using GET# in C64 BASIC, it is necessary to use a special technique in case any zero-byte characters are retrieved.  Because of the way GET# works, zero-byte characters are inputted as empty strings, not CHR$(0)'s.  Line 40 in the following program demonstrates one way to handle empty strings:
  89.  
  90. %c10 OPEN 2,8,2, "0:FILENAME,P,R"
  91. %c20 FOR X = 1 TO 254            
  92. %c30 GET#2, X$                   
  93. %c40 PRINT ASC( X$ + CHR$(0) )   
  94. %c50 NEXT                        
  95. %c60 CLOSE 2                     
  96.  
  97.      With C128 BASIC, the "+CHR$(0)" in line 40 is not needed because of BASIC 7.0's improved ASC() function.  Alternate ways of trapping empty strings are:
  98.  
  99. %c1) X$=X$+CHR$(0)
  100. %c2) IF X$="" THEN X$=CHR$(0)
  101.  
  102.  
  103.  
  104. 4. OF MICE AND MEN
  105.  
  106.      Unlike a joystick, Commodore's 1351 proportional mouse is very difficult to program unless you are a machine language expert.  Realizing that most of us mere humans would have no idea how to use the mouse in our own programs, Commodore provided a useful demo disk with the device.  In true Commodore tradition, though, details on using the mouse are still rather sketchy.  Here are some tips on how to use the mouse drivers provided on the 1351 demo disk.
  107.  
  108.      First of all, let's identify the two files we are most interested in.  The mouse driver designed for use in 64-mode is called "M1351.64.BIN" and loads into memory location 49152 ($C000).  The 128-mode version is called "M1351.128.BIN" and loads into location 6144 ($1800).
  109.  
  110.      The beauty of these particular mouse drivers is that they can support either control port.  Both drivers use sprite #0.  The other seven are undisturbed.
  111.  
  112.      There are a few drawbacks.  The biggest is that there are no programmable boundaries for the moving sprite.  It can be moved completely off-screen; it will even "wrap" around to the other side!  Another drawback is that while the driver is installed, things slow down ever-so-slightly.  This is more noticable on the C128.
  113.  
  114.      Finally, there is no "joystick driver" supplied for performing the same sprite-moving activities as the mouse.  If you want your program to offer a choice to the user in case he/she has no mouse, the routine will have to be written from scratch.  Commodore has provided source code for both drivers on the demo disk, however, and this can be built upon.  The C64 version is saved as "M1351.64.SRC" and the C128 version is called "M1351.128.SRC".  Both are SEQ files and, unfortunately, are not compatible with some assemblers.
  115.  
  116.      To activate the C64 mouse driver, first load it into memory.  In a BASIC program, this can be done like this:
  117.  
  118. %c10 IF X=0 THEN X=1 : LOAD"M1351.64.BIN",8,1
  119.  
  120.      Next, we have to install the mouse driver.  The program hooks into your computer's regular interrupt schedule, which is performed 60 times per second.  So in addition to normal housekeeping tasks, your C64 will also update the position of sprite #0 based on coordinates received from the mouse.  This scheme allows BASIC programs and other routines to run at the same time as the mouse driver.  In a very general sense, your computer will be "multitasking."
  121.  
  122.      The mouse driver wedge has two "kick start" SYS addresses that correspond to each of the two control ports.  You may use either one, although port #2 is recommended because port #1 often interferes with the keyboard.  This is also true on the C128.
  123.  
  124.      To install the mouse driver in control port #1 use:
  125.  
  126. %cSYS 49152
  127.  
  128.      To install the mouse driver in control port #2 use:
  129.  
  130. %cSYS 49155
  131.  
  132.      Once installed, the only way to switch ports is to use the disable feature.  Here it is:
  133.  
  134. %cSYS 49158
  135.  
  136.      This shuts off the mouse driver, leaving sprite #0 hanging motionless on the screen.  When the mouse driver is disabled, it can be restarted again with either of the two SYS enable commands.
  137.  
  138.      The mouse driver does NOT automatically turn on sprite #0.  This must be done by your program.  Use this command to turn on sprite #0 without affecting the others:
  139.  
  140. %cPOKE 53269, PEEK (53269) OR 1
  141.  
  142.      Conversely, to turn off sprite #0 without affecting the others use:
  143.  
  144. %cPOKE 53269, PEEK (53269) AND 254
  145.  
  146.      In 128-mode, things are only slightly different.  In a BASIC program, load the mouse driver file into BANK 0 memory with this command:
  147.  
  148. %c10 BLOAD "M1351.128.BIN",B0
  149.  
  150.      Once loaded, the driver may be installed in one of two ways.
  151.  
  152.      To install the mouse driver in control port #1 use:
  153.  
  154. %cSYS 6144
  155.  
  156.      To install the mouse driver in control port #2 use:
  157.  
  158. %cSYS 6147
  159.  
  160.      To disable the mouse driver use:
  161.  
  162. %cSYS 6150
  163.  
  164.      Like its C64 counterpart, the C128 mouse driver does not turn on sprite #0 when installed.  Turn on sprite #0 with this command:
  165.  
  166. %cSPRITE 1,1
  167.  
  168.      (Refer to your C128 user's manual for more information on the SPRITE command and its parameters.)
  169.  
  170.      To turn off sprite #0, use:
  171.  
  172. %cSPRITE 1,0
  173.  
  174.  
  175. 5. OF MICE AND MEN II: THE SEQUEL
  176.  
  177.      Having a mouse-driven sprite skating around the screen is nice but how do we actually USE it?  Good question.
  178.  
  179.      "Point-and-click" is one of the simplest user interfaces ever devised, yet writing such a program requires a great deal of effort.  What the end-user gains in ease-of-use is paid for by the time and efforts of the programmer who must write the elaborate code to support it.
  180.  
  181.      In BASIC, it is not as difficult as you may imagine to detect a mouse-click and branch when a specific area of the screen has been clicked on.  In fact, you could do it like this: (for control port #1)
  182.  
  183. %c10 WAIT 56321,16,16
  184.  
  185.      Or... (control port #2)
  186.  
  187. %c10 WAIT 56320,16,16
  188.  
  189.      Using either of these two lines causes BASIC to wait for a fire-button press at the appropriate control port.  On the 1351 mouse, a fire button value corresponds to the left input button.  (The C128 should be in BANK 15 for these commands to work.)
  190.  
  191.      Conversely, the right input button on the 1351 mouse can be detected as an "up" joystick value.  Use this line to WAIT for a right input button press, substituting 56321 or 56320 for "control port":
  192.  
  193. %c10 WAIT control port,1,1
  194.  
  195.      Both input buttons can be checked at the same time with these commands:
  196.  
  197. %c10 WAIT control port,17,17 : X=PEEK (control port) AND 17
  198.  
  199.      This technique will yield one of four possible values in variable X after an input button is pressed:
  200.  
  201.  
  202. %c17 = no button pressed *
  203. %c1  = left button        
  204. %c16 = right button       
  205. %c0  = both buttons       
  206.  
  207.      * A value of 17 may occasionally slip through if the user quickly taps the mouse button.  What happens is that the WAIT statement is executed but because BASIC is so slow, the button is released before the "X=PEEK (control port) AND 17" command can get the proper control port value.
  208.  
  209.      After the button click, it is necessary to get the coordinates of sprite #0.  Use a line like this:
  210.  
  211. %c20 X=PEEK(53248)+ 256*(PEEK(53264) AND 1) : Y= PEEK (53249)
  212.  
  213.      Now let's say we have something on the screen such as an OK box.  Picture it as a solid box made out of reversed characters and having the word "OK" in the middle.  For us to determine if the sprite coordinates are within this box, we have to figure out the coordinates for the upper-left and lower-right corners.  Let's assume that these X,Y coordinates are 112,154 and 175,177.  The following line will tell us if the sprite coordinates are within our OK box:
  214.  
  215. BOX COORDINATES: 112,154  175,177
  216.  
  217. 30 Z=(X>111)+(Y>153)+(X<176)+(Y<178)
  218. 35 IF Z=-4 THEN sprite is within the
  219.                 boundaries of our
  220.                 OK box!
  221. 40 GOTO 10:REM loop back to WAIT routine
  222.  
  223.      There may be instances where keyboard input is expected as well as an input button click.  In this case we must run a continuous loop that checks for both.  Try this, substituting 56320 or 56321 for "control port":
  224.  
  225. 10 GET X$
  226. 12 X=PEEK(control port) AND 16
  227. 13 IF X$="" AND X=16 THEN 10
  228.  
  229.  
  230.  
  231.      To check what kind of input was used try:
  232.  
  233. 20 IF X=0 THEN mouse click routine
  234. 30 IF X$<>"" THEN keypress routine
  235. 40 GOTO 10
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.      That wraps it up for this issue.  If you have any tips or tricks for performing simple yet useful procedures with your C64 or C128, send them in to:
  246.  
  247. %cJon Perregaux          
  248. %cCommodore Tips & Tricks
  249. %cc/o UpTime Magazine    
  250. %c6 John Clarke Road     
  251. %cMiddletown, RI  02840  
  252.  
  253.  
  254.  
  255.  
  256.      We are looking for tips and tricks that deal in the following subjects:
  257.  
  258. * POKEs and PEEKs for both the Commodore 64 and 128
  259.  
  260. * Undocumented features you may have discovered
  261.  
  262. * Using GEOS on both the 64 and 128
  263.  
  264. * CP/M Plus on the 128
  265.  
  266. * The 1541, 1571, and 1581 disk drives
  267.  
  268. * Important hints about major applications software
  269.  
  270. * Simple hardware tricks
  271.