Classic Computer Magazine Archive COMPUTE! ISSUE 40 / SEPTEMBER 1983 / PAGE 258

Sprite Editor For TI

Larry Long

Here's a way to get maximum use of sprites on the TI-99/4A-and a program that generates listings for your sprite creations.



A very powerful yet often unused feature of the TI-99/4A is its ability to display and control sprites. With the 99/4A and the Extended BASIC Module, it is possible to generate 28 sprites for display and independent simultaneous movement. Program 1 should convince any doubters that this can be done. Although a lot of colored letters floating around the screen are a bit pointless, if we can modify and control the sprites, we will have a most useful feature.
    Sprites can be designed by drawing on a piece of graph paper and then converting the on/off pixels to a hexadecimal number. If the two largest sizes of sprites are used, the hexadecimal number describing the shape of the sprite would be 64 characters long. A solution is a sprite editor that will allow us to draw the pattern we want on the screen and then have the computer create the program we need to make that sprite pattern. Program 2 will do exactly that, and more. It will allow us to edit the sprite pattern. Then, when we press the L key, it will display a complete listing that would, if copied on paper and then entered into the computer, provide a sprite and the necessary routine to control its movement.

Your Options
When you run the program, the first display screen will be a design grid with a box-shaped cursor. The area under the cursor will initially be white (signifying an "off" pixel). Press 1 to change the color beneath the cursor to black (representing an "on" pixel) or to move the cursor about the grid with the arrow keys. To turn off a particular pixel, press 0 and the background color will be returned to white. When you have completed your design, press the P key to see it displayed as a sprite.
    At this point, you are given several options. You can magnify your newly constructed sprite (M key), change its color (C key), change its background color (B key), or set it in motion (E, S, D, X keys). If you are not pleased with the sprite's shape, you can modify it by striking the T key or (if the changes required are quite drastic) simply press the A key to start with a fresh grid. On the other hand, if you are satisfied with your sprite and its color and directional parameters, press the L key to create the BASIC statements needed to achieve these effects.
    If using the sprite editor is your only concern, then skip the rest of this article and go straight to Program 2 and enjoy this easy access to sprites.

How The Editor Works
To understand what makes the editor work, let's take a general overview of the program:

Lines
 

100-260 Set up screen display.
270-460 Are the main loop of the designing portion of the program.
470-680 Evaluate the design, put its values in an array, read the values in the array, convert them to hexadecimal numbers, and then build a 64-character string to describe the sprite pattern.
690-770 Put the sprite on the screen and display new program instructions.
780-930 Main loop of the implementation portion of the program.
940-980 Change size of sprite.
1000-1150 Display a listing of the sprite program.
1160-1220 Change the color of the sprite and screen.

    A cursor is needed to indicate where you are located on the design grid. I chose to use a sprite (line 220) because I could move it around freely without disturbing the display under it. Repositioning the cursor is accomplished in line 380 with a CALL LOCATE. The arrow keys reposition the cursor, and the ENTER key changes the area under the cursor.
    What makes "Sprite Editor" so valuable is its ability to generate the hexadecimal pattern for the sprite. The loop from line 500 through line 560 determines the character in each position of the design grid and stores that value in the array B (R,C). Line 570 provides a string with all of the possible hexadecimal digits placed in ascending order. Line 580 sets M$ to be "null." The loop from line 590 to line 630 evaluates the array elements and converts each row in the left half of the design grid to a pair of hexadecimal digits and concatenates them to M$. Line 620 is probably the most significant line in this loop, as it provides the hexadecimal numbers. It causes the computer to look at a particular digit (element) in HEX$ determined by the values calculated for HIGH and LOW. Lines 630-680 perform the same operation as 590-630, only for the right half of the design grid.
    Line 690 assigns the hexadecimal numbers to ASCII characters 104, 105, 106, and 107. It is necessary to specify only the first character number in the CALL CHAR statement. When this feature is used, it is required that you start with a character that is evenly divisible by 4. Line 730 actually displays the sprite.
    Lines 740-770 provide instructions for the implementation portion of the program. Lines 780-830 check for specific key presses and provide appropriate branching to list the program; end the program; start from the beginning; change the background color; modify the existing sprite; change sprite size; or change sprite color. Lines 840-920 check for arrow key presses and then increment or decrement sprite speed.
    Lines 940-980 change sprite size. Lines 10001150 display a program listing that would generate a sprite like the one designed by the Sprite Editor. One problem with listing the program is displaying the quote character. The computer interprets it to mean that you want to end the PRINT statement. The solution is to redefine an unused character (I chose the lowercase "n") to look like the quote character.
    Finally, lines 1160-1220 allow you to change the color of the sprite and screen.

Program 1: Sprite Generation
100 CALL MAGNIFY(2)::FOR X=1 TO 28::
    CALL SPRITE(#X,64+X,X/2,96,128,I
    NT(RND*100)-50,INT(RND*100)-50):
    :NEXT X::GOTO 100

Program 2: Sprite Editor
100 REM SPRITE EDITOR
110 DIM B(16,16):: SC=1
130 C1=7
140 CALL CHAR(100,"")
150 CALL CHAR(101,"FFFFFFFFFFFFFFFF
    ")
160 CALL CHAR(102,"FFFFC3C3C3C3FFFF
    ")
170 CALL COLOR(9,2,16)
180 CALL CLEAR
190 DISPLAY AT(1,10):"SPRITE EDITOR"
200 FOR R=1 TO 16 :: CALL HCHAR(4+R
    ,2,100,16):: NEXT R
210 CALL MAGNIFY(1)
212 IF K=84 THEN GOTO 217
215 CALL SCREEN(8)
217 CALL DELSPRITE(ALL)
220 CALL SPRITE(#28,102,14,32,8)
225 CALL HCHAR(21,1,32,31):: CALL H
    CHAR(22,1,32,31)
230 DISPLAY AT(22,2):"E=UP X=DOWN S
    =LEFT D=RIGHT"
240 DISPLAY AT(23,2):"PRESS 1 - FIX
    EL ON ,0 - OFF"
250 DISPLAY AT(24,2):"PRESS P TO DI
    SPLAY SPRITE"
260 R=1 :: C=1
270 CALL KEY(0,K,S)
271 IF S=0 THEN 270
272 IF K=48 THEN KHAR=100
274 IF K=49 THEN KHAR=101
280 IF K=83 THEN C=C-1 :: GOTO 320
290 IF K=68 THEN C=C+1 :: GOTO 320
300 IF K=69 THEN R=R-1 :: GOTO 320
310 IF K=88 THEN R=R+1 :: GOTO 320
312 IF K=80 THEN 470
320 IF C<1 THEN C=16
330 IF C>16 THEN C=1
340 IF R<1 THEN R=16
150 IF R>16 THEN R=1
380 CALL LOCATE(#28,(8*R)+25,8*C+1)
420 CALL HCHAR(4+R,1+C,KHAR)
430 CALL SOUND(20,200,5)
460 GOTO 270
470 CALL DELSPRITE(ALL)
480 CALL HCHAR(21,1,32,128)
490 DISPLAY AT(22,2):"PLEASE WAIT W
    HILE I THINK."
500 FOR R=1 TO 16
510 FOR C=1 TO 16
520 CALL GCHAR(4+R,1+C,GC)
530 GC=GC-100
540 B(R,C)=GC
550 NEXT C
560 NEXT R
570 HEX$="0123456789ABCDEF"
580 M$=""
590 FOR R=1 TO 16
600 LOW=B(R,5)*B+B(R,6)*4+B(R,7)*2+
    B(R,8)+1
610 HIGH=B(R,1)*B+B(R,2)*4+B(R,3)*2
    +B(R,4)+1
620 M$=M$&SEG$(HEX$,HIGH,1)&SEG$(HE
    X$,LOW,1)
630 NEXT R
640 FOR R=1 TO 16
650 LOW=B(R,13)*B+B(R,14)*4+B(R,15)
    *2+B(R,16)+1
660 HIGH=B(R,9)*B+B(R,10)*4+B(R,11)
    *2+B(R,12)+1
670 M$=M$&SEG$(HEX$,HIGH,1)&SEG$(HE
    X$,LOW,1)
680 NEXT R
690 CALL CHAR(104,M$)
700 CALL MAGNIFY(3)
710 MM=3
720 M=4
730 CALL SPRITE(#1,104,C1,50,170,0,
    0)
740 DISPLAY AT(21,2):"C COLOR M MA
    GNIFY T EDIT"
750 DISPLAY AT(22,2):"A ERASE Q QU
    IT B BACKGRD"
760 DISPLAY AT(23,2):"E=UP X=DOWN S
    =LEFT D=RIGHT"
770 DISPLAY AT(24,8):"L LISTS PROGR
    AM"
780 CALL KEY(0,K,S)
790 IF K=76 THEN GOTO 1000
800 IF K=81 THEN GOTO 990
810 IF K=65 THEN GOTO 100
812 IF K=66 THEN GOSUB 1200
815 IF K=84 THEN GOTO 210
820 IF K=77 THEN GOTO 940
830 IF K=67 THEN GOTO 1160
840 IF K=83 THEN H=H-2
850 IF K=68 THEN H=H+2
860 IF K=69 THEN V=V-2
870 IF K=88 THEN V=V+2
880 IF V>120 THEN V=120
890 IF V<-120 THEN V=-120
900 IF H>120 THEN H=120
910 IF H<-120 THEN H=-120
920 CALL MOTION(#1,V,H)
930 GOTO 780
940 CALL MAGNIFY(M)
950 MM=M
960 IF M=3 THEN M=4 ELSE M=3
970 FOR D=1 TO 20 :: NEXT D
980 GOTO 780
990 STOP
1000 REM PROGRAM LISTER
1010 CALL CHAR(110,"002424")
1020 CALL CLEAR
1030 PRINT "{6 SPACES}PROGRAM LISTI
     NG"
1035 CALL DELSPRITE(ALL)
1040 PRINT
1050 PRINT ">100 CALL CHAR(104,n";:
     : FOR W=1 TO 64 :: PRINT SEG$(
     M$,W,1);:: NEXT W :: PRINT "n)"
1055 PRINT ">105 CALL SCREEN(";SC;"
     )"
1060 PRINT ">110 CALL MAGNIFY(";MM;
     ")"
1070 PRINT ">120 CALL SPRITE(#1,104
     ,";C1;",150,150,";V;",";H;")"
1080 PRINT ">130 CALL KEY(0,K,S)"
1090 PRINT ">140 IF K=68 THEN H=H+2
     "
1100 PRINT ">150 IF K=83 THEN H=H-2
     "
1110 PRINT ">160 IF K=88 THEN V=V+2
1120 PRINT ">170 IF K=69 THEN V=V-2
1130 PRINT ">180 CALL MOTION(#1,V,H
)
1140 PRINT ">190 GOTO 130"
1150 PRINT :: PRINT :: PRINT :: PRI
     NT :: PRINT
1155 DISPLAY AT(21,3):"A - ERASE
     {3 SPACES}Q - QUIT"
1156 CALL KEY(0,K,ST):: IF ST=0 THE
     N 1156
1157 IF K=81 THEN GOTO 990
1158 IF K=65 THEN GOTO 100
1159 GOTO 1156
1160 C1=C1+1 :: IF C1>16 THEN 1180
1170 CALL COLOR(#1,C1):: GOTO 780
1180 C1=2 :: CALL COLOR(#1,C1):: GO
     TO 780
1200 REM SCREEN COLOR CHANGE
1210 SC=SC+1 :: IF SC=17 THEN SC=2
1220 CALL SCREEN(SC):: RETURN