home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / basic / edpat.bas < prev    next >
Encoding:
BASIC Source File  |  1989-11-09  |  5.1 KB  |  203 lines

  1. DECLARE SUB DrawPattern ()
  2. DECLARE SUB EditPattern ()
  3. DECLARE SUB Initialize ()
  4. DECLARE SUB ShowPattern (OK$)
  5.  
  6. DIM Bit%(0 TO 7), Pattern$, PatternSize%
  7. DO
  8.    Initialize
  9.    EditPattern
  10.    ShowPattern OK$
  11. LOOP WHILE OK$ = "Y"
  12.  
  13. END
  14. ' ======================= DRAWPATTERN ====================
  15. '  Draws a patterned rectangle on the right side of screen
  16. ' ========================================================
  17.  
  18. ' ======================= EDITPATTERN =====================
  19. '                  Edits a tile-byte pattern
  20. ' =========================================================
  21.  
  22.  
  23. ' ======================= INITIALIZE ======================
  24. '             Sets up starting pattern and screen
  25. ' =========================================================
  26.  
  27. ' ======================== SHOWPATTERN ====================
  28. '   Prints the CHR$ values used by PAINT to make pattern
  29. ' =========================================================
  30.  
  31. SUB DrawPattern STATIC
  32. SHARED Pattern$
  33.    VIEW (320, 24)-(622, 160), 0, 1  ' Set view to rectangle.
  34.    PAINT (1, 1), Pattern$       ' Use PAINT to fill it.
  35.    VIEW                 ' Set view to full screen.
  36.  
  37. END SUB
  38.  
  39. SUB EditPattern STATIC
  40. SHARED Pattern$, Bit%(), PatternSize%
  41.  
  42.    ByteNum% = 1     ' Starting position.
  43.    BitNum% = 7
  44.    Null$ = CHR$(0)  ' CHR$(0) is the first byte of the
  45.                     ' two-byte string returned when a
  46.                     ' direction key such as UP or DOWN is
  47.                     ' pressed.
  48.    DO
  49.  
  50.       ' Calculate starting location on screen of this bit:
  51.       X% = ((7 - BitNum%) * 16) + 80
  52.       Y% = (ByteNum% + 2) * 8
  53.  
  54.       ' Wait for a key press (flash cursor each 3/10 second):
  55.       State% = 0
  56.       RefTime = 0
  57.       DO
  58.  
  59.      ' Check timer and switch cursor state if 3/10 second:
  60.      IF ABS(TIMER - RefTime) > .3 THEN
  61.         RefTime = TIMER
  62.         State% = 1 - State%
  63.  
  64.         ' Turn the  border of bit on and off:
  65.         LINE (X% - 1, Y% - 1)-STEP(15, 8), State%, B
  66.      END IF
  67.  
  68.      Check$ = INKEY$    ' Check for keystroke.
  69.  
  70.       LOOP WHILE Check$ = ""    ' Loop until a key is pressed.
  71.  
  72.       ' Erase cursor:
  73.       LINE (X% - 1, Y% - 1)-STEP(15, 8), 0, B
  74.  
  75.       SELECT CASE Check$    ' Respond to keystroke.
  76.  
  77.       CASE CHR$(27)     ' ESC key pressed:
  78.          EXIT SUB       ' exit this subprogram.
  79.       CASE CHR$(32)     ' SPACEBAR pressed:
  80.                         ' reset state of bit.
  81.  
  82.          ' Invert bit in pattern string:
  83.          CurrentByte% = ASC(MID$(Pattern$, ByteNum%, 1))
  84.          CurrentByte% = CurrentByte% XOR Bit%(BitNum%)
  85.          MID$(Pattern$, ByteNum%) = CHR$(CurrentByte%)
  86.  
  87.          ' Redraw bit on screen:
  88.          IF (CurrentByte% AND Bit%(BitNum%)) <> 0 THEN
  89.              CurrentColor% = 1
  90.          ELSE
  91.              CurrentColor% = 0
  92.          END IF
  93.          LINE (X% + 1, Y% + 1)-STEP(11, 4), CurrentColor%, BF
  94.  
  95.       CASE CHR$(13)      ' ENTER key pressed: draw
  96.          DrawPattern         ' pattern in box on right.
  97.  
  98.       CASE Null$ + CHR$(75)  ' LEFT key: move cursor left.
  99.  
  100.          BitNum% = BitNum% + 1
  101.          IF BitNum% > 7 THEN BitNum% = 0
  102.  
  103.       CASE Null$ + CHR$(77)  ' RIGHT key: move cursor right.
  104.  
  105.          BitNum% = BitNum% - 1
  106.          IF BitNum% < 0 THEN BitNum% = 7
  107.  
  108.       CASE Null$ + CHR$(72)  ' UP key: move cursor up.
  109.  
  110.          ByteNum% = ByteNum% - 1
  111.          IF ByteNum% < 1 THEN ByteNum% = PatternSize%
  112.  
  113.       CASE Null$ + CHR$(80)  ' DOWN key: move cursor down.
  114.  
  115.          ByteNum% = ByteNum% + 1
  116.          IF ByteNum% > PatternSize% THEN ByteNum% = 1
  117.       END SELECT
  118.    LOOP
  119. END SUB
  120.  
  121. SUB Initialize STATIC
  122. SHARED Pattern$, Bit%(), PatternSize%
  123.  
  124.    ' Set up an array holding bits in positions 0 to 7:
  125.    FOR I% = 0 TO 7
  126.       Bit%(I%) = 2 ^ I%
  127.    NEXT I%
  128.  
  129.    CLS
  130.  
  131.    ' Input the pattern size (in number of bytes):
  132.    LOCATE 5, 5
  133.    PRINT "Enter pattern size (1-16 rows):";
  134.    DO
  135.       LOCATE 5, 38
  136.       PRINT "     ";
  137.       LOCATE 5, 38
  138.       INPUT "", PatternSize%
  139.    LOOP WHILE PatternSize% < 1 OR PatternSize% > 16
  140.  
  141.    ' Set initial pattern to all bits set:
  142.    Pattern$ = STRING$(PatternSize%, 255)
  143.  
  144.    SCREEN 2     ' 640 x 200 monochrome graphics mode
  145.  
  146.    ' Draw dividing lines:
  147.    LINE (0, 10)-(635, 10), 1
  148.    LINE (300, 0)-(300, 199)
  149.    LINE (302, 0)-(302, 199)
  150.  
  151.    ' Print titles:
  152.    LOCATE 1, 13: PRINT "Pattern Bytes"
  153.    LOCATE 1, 53: PRINT "Pattern View"
  154.  
  155.  
  156. ' Draw editing screen for pattern:
  157.    FOR I% = 1 TO PatternSize%
  158.  
  159.       ' Print label on left of each line:
  160.       LOCATE I% + 3, 8
  161.       PRINT USING "##:"; I%
  162.  
  163.       ' Draw "bit" boxes:
  164.       X% = 80
  165.       Y% = (I% + 2) * 8
  166.       FOR J% = 1 TO 8
  167.         LINE (X%, Y%)-STEP(13, 6), 1, BF
  168.         X% = X% + 16
  169.       NEXT J%
  170.    NEXT I%
  171.  
  172.    DrawPattern      ' Draw  "Pattern View" box.
  173.  
  174.    LOCATE 21, 1
  175.    PRINT "DIRECTION keys........Move cursor"
  176.    PRINT "SPACEBAR............Changes point"
  177.    PRINT "ENTER............Displays pattern"
  178.    PRINT "ESC.........................Quits";
  179.  
  180. END SUB
  181.  
  182. SUB ShowPattern (OK$) STATIC
  183. SHARED Pattern$, PatternSize%
  184.  
  185.    ' Return screen to 80-column text mode:
  186.    SCREEN 0, 0
  187.    WIDTH 80
  188.  
  189.    PRINT "The following characters make up your pattern:"
  190.    PRINT
  191.  
  192.    ' Print out the value for each pattern byte:
  193.    FOR I% = 1 TO PatternSize%
  194.       PatternByte% = ASC(MID$(Pattern$, I%, 1))
  195.       PRINT "CHR$("; LTRIM$(STR$(PatternByte%)); ")"
  196.    NEXT I%
  197.    PRINT
  198.    LOCATE , , 1
  199.    PRINT "New pattern? ";
  200.    OK$ = UCASE$(INPUT$(1))
  201. END SUB
  202.  
  203.