16K Cassette or Disk
by Craig Patchett
EDITOR'S NOTE: The article accompanying this demonstration is intended primarily for advanced readers. The demo program itself, however, can be typed in and enjoyed by anybody.
Stars 3D illustrates a simple 3-dimensional graphics effect, produced by moving similar objects horizontally at different speeds. It uses a couple of assembly-language "tricks" to achieve the illusion of depth as efficiently as possible.
Listing 1 is an ATARI BASIC program that POKEs the Stars 3D routine into memory and activates it with a USR call. Listing 2 is the assembly-language source code, provided for programmers who want to see how the routine works.
After entering Listing 1 and
D:CHECKing it, SAVE the demo program to disk or tape and type RUN
The dramatic display in Stars 3D is accomplished by using multiple LMS (Load Memory Scan) instructions in the display list. Every DL instruction has its LMS option bit set. This allows me to have the same section of memory appear in more than one place on the screen.
When I set up the display list, I randomly pick one of the eight star memory segments and store its address in an LMS instruction. Normally, this would mean that all the stars from a given memory segment would appear at the same x-position on the screen. I avoid this by adding a random offset (0-48) to each memory address, thereby spreading the stars all over the x-range.
If you think about the offset I'm adding, you'll realize that an offset of, say, twenty will result in a mode line with twenty "orphan bytes" on the end (see Figure 1). This difficulty is corrected by assigning 96 bytes to each star memory segment instead of 48. But this solution creates yet another problem: scrolling a star across 96 bytes when only 48 are shown will cause the star to be invisible half the time (Figure 2). My solution is to give each memory segment two stars, separated by 48 bytes. Whenever one star moves off the edge of the screen, the other star will appear at the opposite side (Figure 3).
Each DL instruction in Stars 3D has its display list interrupt (DLI) option bit set. A small DLI service routine allows me to put a different star color on every mode line. Instead of going for a broad spectrum of colors, I decided to make each star a different shade of the same color, with slow-moving stars set to darker luminances than the fast movers. This further enhances the illusion of depth.
Fine scrolling would have required handling each of the 192 mode lines separately. This takes a lot of processing time. Instead, I chose to manipulate each of the 16 stars (two in each memory segment) directly, using the 6502 ASL instruction. When a star is shifted out of one byte it falls into the next, and when it shifts out of its memory segment it is stuck onto the other end. Each memory segment has its own timer which determines how quickly the stars in that segment will appear to move.
Those are the basics behind Stars 3D. You should be able to find any details I skipped over by studying my source code. I hope this demo will show you how an unconventional approach can lead to terrific savings in both processing time and memory (the entire program requires less than 2K, including the screen RAM), both of which are critical in today's high-performance graphics programs.
STARS3D.LST is available in ATASCII format.
100 REM ************************
110 REM * STARS 3D DEMO *
120 REM * BY CRAIG PATCHETT *
130 REM * ANALOG COMPUTING #16 *
140 REM ************************
150 REM
160 START=14336:REM * $3800 HEX
170 FOR I=START TO START+377
180 READ BYTE:POKE I,BYTE:NEXT I
190 X=USR(START)
200 DATA 32,49,56,32,145,56,169,0,141,
200,2,169,7,162,56,160,38,32,92,228,16
9,253,141,0,2
210 DATA 169,56,141,1,2,169,192,141,14
,212,76,35,56,32,13,57,169,0,141,173,6
3,76,98,228,169
220 DATA 189,141,189,62,169,59,141,205
,62,162,0,142,221,62,232,189,188,62,24
,105,48,157,189,62,189
230 DATA 204,62,105,0,157,205,62,169,0
,157,221,62,232,224,16,208,229,169,189
,133,204,169,59,133,205
240 DATA 160,255,162,3,169,0,145,204,1
36,192,255,208,249,202,240,5,230,205,7
6,106,56,162,0,189,189
250 DATA 62,133,204,189,205,62,133,205
,169,64,160,0,145,204,232,224,16,208,2
35,96,169,119,133,204,169
260 DATA 57,133,205,169,0,141,173,63,1
60,3,169,206,145,204,200,208,2,230,205
,173,10,210,41,7,72
270 DATA 170,189,111,57,174,173,63,157
,237,62,104,238,173,63,10,170,173,10,2
10,41,63,201,48,176,247
280 DATA 24,125,189,62,145,204,200,208
,2,230,205,189,205,62,105,0,145,204,20
0,208,2,230,205,192,67
290 DATA 208,189,169,65,145,204,200,16
9,119,145,204,141,48,2,200,169,57,145,
204,141,49,2,169,35,141
300 DATA 47,2,96,174,173,63,189,237,62
,141,10,212,141,22,208,238,173,63,64,1
62,13,32,50,57,32
310 DATA 50,57,162,12,32,50,57,32,50,5
7,202,48,17,222,87,57,208,248,32,50,57
,189,99,57,157
320 DATA 87,57,76,29,57,96,189,189,62,
133,204,189,205,62,133,205,188,221,62,
177,204,10,10,145,204
330 DATA 144,15,169,1,136,192,255,208,
2,160,47,145,204,152,157,221,62,96,8,8
,6,6,4,4,3
340 DATA 3,2,2,1,1,8,8,6,6,4,4,3,3,2,2
,1,1,34,36,38,40,42,44,46,34,112,112,2
40
100 DATA 90,594,809,30,102,89,35,33,51
0,627,171,627,89,203,37,4046
250 DATA 355,705,269,975,307,668,425,3
18,819,18,4859
Listing 2 appears on a separate page.