Classic Computer Magazine Archive ANTIC VOL. 9, NO. 1 / APRIL/MAY 1990


Starfield Invasion

Fast-action game featuring a spectacular graphics technique
By Norman Lin

Experience the 3-D thrills of this alien Invasion -- and learn how its spectacular starfield display works. The BASIC type-in loader will produce a machine language version of Starfield Invasion that runs on any 8-bit Atari computer with disk drive or cassette. Assembly language programmers can examine the Invasion source code on this issue's Antic Disk as an extra bonus.

Unquestionably, computer graphics have improved remarkably over the past few years. New 16-bit computers clearly have more graphics capabilities than the first 8-bit Atari. This is not to say, however, that impressive displays cannot be produced on our old 8-bits. In fact, the Atari's unique capability for easily accessed Display List Interrupts (DLIs) can create striking screens, even by today's standards. The accompanying game program, Invasion, utilizes the Atari's powerful DLI capability to create a realistic 3D starfield.

Type in INVASION.BAS, Listing 1, check it with TYPO II, SAVE it to disk, then RUN it. You will be asked whether you want to put the program on Disk or Cassette. Type [D] for disk and press [RETURN]. At the prompt, put a freshly formatted disk with DOS 2 or 2.5 on it into drive 1. When you press [RETURN], the Invasion machine code will be created and saved on disk in an AUTORUN.SYS file. When the disk drive stops whining and the program signals you that it has finished, you're ready to boot with the new disk in drive 1. Turn off your computer and remove all cartridges (XL/XE users hold down [OPTION]), and turn on your computer again. Invasion will automatically load and run. Disk users do not need to type in Listing 2.

Antic Disk Users: The machine language version of Starfield Invasion is on disk as INVASION.OBJ. To RUN this version, go to DOS and use DOS command C to copy the file onto another disk with Atari DOS 2.0 on it. (Users with only one drive use DOS command O to copy the file.) Rename the file AUTORUN.SYS, put the disk in drive 1, turn off your computer, remove all cartridges (XL/XE users hold down [OPTION]), and turn the computer on. The program should load and run automatically.

Cassette users also type in Listing 1, INVASION.BAS, checking it with TYPO II. Save this program to cassette, then RUN it, and at the prompt respond that you wish to create a cassette. Then follow the instructions on screen. Place a cassette into the recorder, position it, and press [RECORD] and [PLAY]. Note the counter number. After you press [RETURN], the program will write the Invasion machine code to the cassette. When the recorder stops, rewind the cassette back to the counter number you noted earlier. Then type NEW and press [RETURN]. Next, type in Listing 2, CASLOADR.BAS, and RUN it. When the program prompts you to do so, press [PLAY] and then [RETURN]. The Invasion program will load and run.

For programmers, we've included the source code for Starfield Invasion on disk as well, in the file INVASION.SRC. This code was created with Atari's Assembler Editor cartridge and stored on disk with the LIST command.

(Mr. Lin provided custom loaders for Starfield Invasion that appear to work fine. But we don't have the equipment to test for every possible hardware configuration.-ANTIC ED)

The Game

You are aboard a space ship, having just finished a routine mission. You are traveling back to your home planet for a nice, long rest. All of a sudden, several fighter ships pop out of nowhere and begin attacking you! Monitoring their radio transmissions, you gather that their mission is to conquer your home planet and use it as an outpost for their fiendish plots. Yours is the only ship in the region which can hope to stop this onslaught.

You must repel the enemy invasion forces by destroying all, or as many as possible, of the 99 ships in the attacking fleet. You do this by utilizing your high-energy, tightly focused laser beam, aimed by means of a joystick-controlled crosshair and activated by depressing the fire button. The enemy ships, though, move in erratic patterns, making them difficult targets. In addition, the invaders fire at you and drain your shield energy by two units every time they hit you. You gain one shield energy unit each time you destroy an enemy, however. If your shield energy ever drops below zero, you are destroyed and the invaders succeed in overtaking your home planet.

You can alter your enemies' speed and firing rate at the title screen. The level number determines the frequency of enemy fire, and the speed factor controls the rapidity of enemy movements. On Level 1 with a Speed of 1, it is quite easy to repel the invaders. On Level 3 with a Speed of 3, it is nearly impossible. No score is kept, but the more enemies you repel, the better.

                FIGURE 1: Screen with Player Band                                            FIGURE 2: Screen with Player Band and DLI

FIGURE 3: Screen with Player Band and Three DLIs

Technical Details

The most striking aspect of this game is clearly the 3-D starfield. It adds a tremendous amount of depth to the display. To understand how this works, it is first necessary to understand how the screen is created.

To create an image on the screen of your television set, an electron gun sends a beam sweeping sideways across the screen. Then the gun is deactivated briefly, reset back to the beginning of the next line, and sweeps back again, creating the next line. This process continues until the beam reaches the bottom of the screen. Then the screen is blanked and the process begins again at the top.

When the beam reaches the bottom of the screen, a special thing happens --- the Vertical Blank Interrupt, or VBI, named for the momentary screen "blanking" that occurs during this period. On your computer, the VBI is an Operating System routine that actually stops the computer in the middle of what it is doing, takes care of "housekeeping" operations (such as updating shadow registers and other miscellaneous information) and then re-starts the computer back where it was before the VBI.

Interrupts, however, don't have to occur only at the bottom of the screen. We can create an interrupt anywhere we want to onscreen, and at that specific point on the screen, the computer will stop, do the interrupt, then continue its operations. This is a Display list Interrupt, or DLI.

The advantage of the DLI is that by stopping the computer at a specific point on the screen, we can then change the screen from that point on, while leaving the screen before our DLI unchanged. In Figure 1 we just have a player band stretching the height of the screen. Now, if we set up a DLI in the middle of the screen and use this DLI to move the player band by changing its horizontal position register, then we get Figure 2. In effect, we have two players. Each half is individually controllable because the DLI is only changing the screen after our specific point.

This can be taken further. In Figure3 we have three DLIs. The top one-fourth of the screen is normal. The next one-fourth is controlled by the first DLI and has its own independent horizontal position. The next one-fourth is controlled by the next DLI, and has its own horizontal position, as does the last one fourth. And remember, this is all coming out of one player band.

The Starfield

The starfield uses one missile (missile 3) and the equivalent of 192 DLIs (actually it's one DLI executed on 192 lines) to create 192 individually controllable stars. The starfield's DLI, in addition to making each star individually movable, also makes each eighth star the same luminance. This produces eight "layers" of 24 stars each -- a layer being defined as all stars of a particular luminance.

Now we move each layer at a different speed, the brightest layer the fastest, and the darkest layer the slowest. This is done with a routine which "hooks into" the OS VBI (by means of the VBI vectors at $0222-$0225). The starfield's VBI keeps track of eight counters, one for each layer. Every time the VBI is accessed (every time the electron gun reaches the bottom of the screen, which is every 1/60th of a second), each counter is decreased by one. Whenever a counter reaches zero, it is reset (counter 1 is reset to 1, counter 8 is reset to 8. This makes each counter reach zero at different, but regular, intervals) and the corresponding layer of stars is moved.

That's how the starfield works, in theory. If you study the source code on this issue's Antic Disk as INVASION.SRC, you may notice some strange sections in the starfield's DLI and VBI.

You will notice first that the DLI code is not explicitly stated in the source code. This is because, as stated earlier, the DLI is one humongous DLI that re-executes itself on 192 scan lines. So, I had 192 sets of instructions, each set positioning and coloring each star. So, rather than burdening you with typing 192 sets of almost identical code, the program enters this section of code into memory.

A DLI is a tricky thing. It must complete its operation before the electron gun finishes a line and starts drawing the next. That is very little time. If I used a loop, on each line I would need to check to see if the loop had finished, which consumes 10 machine cycles -- too much, and over 192 lines that's 1,920 machine cycles wasted. By making the code repetitive, I saved 1,920 machine cycles, and thus the DLI can finish on each line before the electron gun begins to draw the next line.

Similarly, the VBI routine, to move a layer of stars, does a JSR to the DECS subroutine. The DECS subroutine must move 24 stars. But, again for timing reasons, DECS could not be made into a loop. So, to avoid the tedium of typing in repetitive code, the computer creates this subroutine as well.

A final quirk that you may notice is in the initialization code. I used a countdown timer routine (which is just like a VBI) to set up the VBI vectors. I couldn't just set up the VBI vectors manually in the main code, because if an interrupt happened to occur when I had changed one byte of the VBI vector but not the other, the computer would go off to some unknown address, which could crash the program. By changing the interrupt vectors within an actual interrupt, I could be sure that no interrupts would occur during the vector changing.

If you're an advanced programmer, you may wonder why I didn't just use the built-in OS routine at $E45C to set the VBI vectors. The reason is that when I'm using the Atari Assembler Editor cartridge, deferred VBI's can't be set up -- the $E45C routine simply doesn't change the vectors (I don't know why). Since the program used deferred VBIs, I had to set up the vectors through a countdown timer routine.

Enemy Movement

The rest of the program is fairly straightforward. Another VBI controls your crosshair and the enemies' movements, and a countdown timer routine (which is just like a VBI) takes care of miscellaneous things such as pulsing your laser beam, turning off explosion sounds, etc. The main game routine checks if you're pressing fire, and if you are, it draws and erases your laser, then checks if any enemies were hit. If enemies were hit, they're blown up and subtracted from the remaining enemy counter, and you receive extra shield energy for each enemy destroyed. If the enemy counter is zero, you win.

If the enemy counter is not zero, then the enemies get a chance to fire. The probability for their firing was chosen by your selection of level on the title screen. If they fire, their laser is drawn and erased, and your shield is decreased. If your shield drops below zero, you lose.

As stated earlier, the enemies' movements are controlled in a VBI routine. The theory behind the movements themselves is simple -- the invaders move on random vectors a random number of times. Then the enemy recedes, another advances out of the stars and it begins the same vector movement process. The advancing and receding of the invaders' ships is done by repeatedly drawing a different shape into player memory, each shape a different size than the next. This gives the illusion of growing larger or smaller, and makes it appear that the invaders are advancing or receding.

Studying the source code, you should be able to fully understand all the details of how the starfield and game program work. Experimentation with the starfield routine will undoubtedly produce interesting results. You may want to try to move 1,536 stars by utilizing all four players and all four missiles as stars. An amazing display could be created by incorporating vertical as well as horizontal movement into the starfield. Or you could try moving cars, planes, or clouds instead of stars. Understanding how to move objects in 3-D will allow you to create eye-catching displays and and games.

While at high school in Jenks, Oklahoma, Norman Lin taught himself BASIC on an old Atrai 400 his father bought years ago. This is his first appearance in Antic.

Listing1:INVASION.OBJ Download

Listing2:INVASION.SRC Download / View