Classic Computer Magazine Archive COMPUTE! ISSUE 49 / JUNE 1984 / PAGE 110

PROGRAMMING THE TI

C. Regena

TI Graphics

Drawing graphics is one of the things that really make our TI computers fun. Chapter 5 of the Beginning BASIC book that comes with your computer tells how you can get going with graphics. Using high-resolution graphics allows you to define your own characters and make detailed drawings on the screen. We can combine high-resolution graphics with text on the same screen, and we can use all 16 colors in high-resolution graphics if we wish.

There are several ways to define the graphics characters; this month we'll look at the most common ways. The CALL CHAR statement defines a certain character number with a certain pattern. If you use a number from 32 to 127, the regular symbol or letter will be redefined.

110 CALL CHAR(131, "3838107C10284282
    " )

defines character number 131. Notice that the character definition pattern needs to be in quotes.

Using CALL CHAR

Another method is to define a string variable first, then use the CALL CHAR. This can save typing if you have several characters defined with the same shape:

150 A$ = "3838107C10284282"
160 CALL  CHAR(128, A$)
170 CALL  CHAR(136, A$)

If you have a lot of character definitions, DATA statements use less memory than many CALL CHAR statements. The disadvantage is that DATA statements are more difficult to type (and debug). This is an example:

200 FOR I = 1 TO 10
210 READ C, C$
220 CALL CHAR(C, C$)
230 NEXT I
240 DATA 128, 3838107C10284282, 129, F
    FFF, 130, FFFFFFFFFFFFFFFF, 136, 83
    E22618186447C1, 141, 204040808010
    102, 142
250 DATA 20404080808C936, 143, FFFF, 1
    44, 01020408, 145, 0, 151, FF

This loop defines ten characters, but instead of ten CALL CHAR statements, there are only six statements. This method is even more efficient when more graphics characters are defined. Within the loop, line 210 reads two values from the DATA statement (C and C$). Line 220 uses these two values to define character number C with definition C$.

If all your characters are in numerical order, you can use the character number as the loop counter. The DATA statements then contain only the definitions.

200 FOR C = 97 TO 127
210 READ C$
220 CALL CHAR(C, C$)
230 NEXT C
240 DATA FFFF,, 3838107C10284282, E0C
    8 (etc. for all the definitions
    )

Zeros Are Assumed

You can define a character with 16 numbers or letters (up to F). If you use fewer, the computer will automatically assume zeros for the rest of the definition. For example, FFFF really means FFFF000000000000. If you want to save memory and typing, arrange your graphics so the zeros are toward the bottom of the square defined. In other words, 0000FFFF00000000 and 000000000000FFFF and FFFF all look the same, but FFFF is the easiest to use. (The "bar" is positioned in different places in the graphics square.)

A character defined as null will be a blank square, or a square of the background color:

300 CALL CHAR<130, "")

In the DATA statement method, you can have commas with nothing between them:

310 DATA FFFF, , F0F

The middle definition is null. Both commas are vital. This particular DATA statement contains three definition strings.

Likely Errors

I mentioned that the data method of defining characters is more difficult to debug. If there is a problem, the most likely message is

BAD VALUE IN 220

You could also get the message

DATA ERROR IN 210

or

OUT OF DATA IN 210

Usually the typing in lines 210 and 220 is fine—the typing error is in the DATA statements. The DATA error messages occur if you don't have the commas placed correctly or if you're reading a string when it should be a number. The BAD VALUE message occurs because the program cannot define the character with what you have read in as data.

The easiest way to find the error is to RUN the program, then when it stops with the error message, print the variables involved. In this case PRINT C,C$ and press ENTER to see what values we have for those variables. You should be able to see exactly what is wrong with your variables. C will tell you how far in the loop you got. Perhaps C$ will have the letter O instead of the number zero, or maybe you've typed a period instead of a comma. In any case, you should be able to spot that error among your DATA statements so it can be corrected.

The CALL CHAR statement only defines the graphics character; you need to put the character on the screen using CALL HCHAR, CALL VCHAR, or PRINT. If a character is already on the screen and you use CALL CHAR to redefine it, all the characters on the screen with that character number will instantly change.

Changes On The Screen

Here's an example of changing character definitions while something is on the screen. Type this short program in, then RUN it.

100 PRINT "ABCDABCD"
110 FOR DELAY = 1 TO 400
120 NEXT DELAY
130 CALL CHAR(65, "00666600422418")
140 FOR DELAY = 1 TO 400
150 NEXT DELAY
160 END

The screen turns green when the program starts to run, and ABCDABCD is printed on the screen. After a delay loop, line 130 redefines character 65, which is the letter A. All the A's on the screen change. After another delay, the program ends. This technique might be useful to you in game situations when you want to change the graphics quickly.

I use a similar principle to PRINT graphics a little more quickly than using CALL HCHAR or CALL VCHAR (as long as you don't have to worry about scrolling). Redefine as graphics the characters 96 through 126. Now, instead of using several CALL HCHAR statements to put the graphics on the screen, use PRINT with the lowercase letters. Suppose you have a snake defined in six graphics characters, 97 to 102. You can use PRINT "abcdef" to draw the snake on the screen.

Using Lowercase Letters

Release the ALPHA LOCK key to type the lowercase letters (which are actually small capital letters). Use FCTN and the key to type any symbol on the fronts of the keys. The reason you can use characters 96 through 126 so often in programs is that you may rarely need the symbols or lowercase letters in the text within a program.

To use characters from 129 to 159 in this PRINT method, look at the CONTROL KEY CODES list on your Reference Card (or in the Appendix of the User's Reference Guide). You can still PRINT graphics and in the quotes use the control key and the appropriate letter for the character number you want. You'll see either a blank or a funny graphics character as you're typing, but it will work fine in the program.

Every so often I read an article complaining that the TI does not have the capability to print graphics using built-in graphics characters or character strings. My rebuttal is that we do have the means to PRINT graphics, but we are not limited to graphics shown on the keys (such as on VIC-20, MC-10, or Timex graphics keys). We can define high-resolution graphics any way we wish, then PRINT the graphics using either lowercase letters, symbols, control characters, or CHR$.

Changes For The TI-99/4

A special note to TI-99/4 (square-keyed console) owners: You cannot type in listings using lowercase letters, but a program typed on the TI-99/4A will work on the TI-99/4. If you don't have access to the 4A console, you can convert the PRINT statements by using the ASCII codes of the lowercase letters. 96 is ` (grave), then the lowercase letters start with 97 and go to 122. Instead of PRINT "abcdef", you can use

PRINT CHR$(97)&CHR$(98)&CHR$(99)&CHR$(100)
&CHR$(101)&CHR$(102)

You may use either the ampersand (&) or semicolons between the character numbers.

Our characters are grouped by eights into character sets which are used in defining colors. We use the CALL COLOR statement to define foreground and background colors for a particular character set—then all characters in that set will be the specified colors. If you need lots of colors on the screen, use different character sets.