Classic Computer Magazine Archive COMPUTE! ISSUE 90 / NOVEMBER 1987 / PAGE 80

Dynamic Graphics For The 64

Ronald Carnell

You don't have to program in machine language to get lightning-fast graphics. This impressive, full-featured program lets you easily and quickly generate high-resolution graphics from BASIC. A disk drive is recommended.


The Commodore 64's hardware support of high-resolution (hi-res) graphics is among the best found in an eight-bit computer. Unfortunately, that hardware is very difficult to access from BASIC. Like many graphics enhancements, "Dynamic Graphics" solves this problem by adding commands that allow you to draw on the hi-res screen in BASIC. It does this, however, in an unusually ingenious—and fast—way.

The graphics commands added by most programs are interpreted, just like any other BASIC statement. This technique, however, leads to slow execution time. For example, to plot 500 points using a FOR-NEXT loop, BASIC has to interpret the plot command 500 different times—and that can take quite a while. Dynamic Graphics operates under the philosophy that graphics commands should be executed only once, without the burden of BASIC's interpreter.

Complex high-resolution graphics can be drawn instantaneously from BASIC.

When Dynamic Graphics encounters one of its graphics commands, it does not execute it, so to speak. Instead, the points plotted by each graphics command are assembled into a specialized shape table. Later, when it's time to display the graphics, a dedicated machine language routine plots each point, quickly and efficiently to the hi-res screen. Shape tables can be quite complex, and may be saved to disk for later use.

Typing It In

Dynamic Graphics is listed below as Program 1. This program is written in machine language, so you'll need to use the "MLX" machine language entry program to type it in. MLX is found elsewhere in this issue. When you run MLX, you will be asked for a starting address and an ending address for the data you'll be entering. For Dynamic Graphics, use the following values:

Starting Address: C000
Ending Address: CA7F

After typing in Dynamic Graphics, save it to disk or tape. Because Dynamic Graphics is written in machine language, it must be loaded with a, 1 extension (LOAD "filename", 8,1). To activate Dynamic Graphics after it has been loaded into memory, enter SYS 49152. Dynamic Art can be disabled with the command SYS 49155.

Using The Program

Dynamic Graphics' commands fall into three different categories: Build, Execute, and Immediate. Build commands, such as Point and Line, are used to build the graphics shape table. An Execute command displays the hi-res screen, plotting each point specified in the shape table. Remember: Until an Execute command is used, no graphics are output to the screen. Immediate commands are commands that execute immediately and do not affect the shape table.

All of Dynamic Graphics' commands begin with an exclamation point (!). Parameters are placed on the line following the graphics command. Parameters must be preceded by a colon. If there is more than one parameter (which there usually is), they too must be separated by colons. Do not use commas for separating data. For example, the syntax for the Line command might appear as

100 !DO (LINE)
110 :X1:Y1:X2:Y2
120 !END LINE

Parameters may be constants, variables, or even expressions such as X*SIN(3.14159265). If a parameter contains a syntax error, the line number in which the command is located (line 100 in the example above) is .listed as the offending line. Some parameters are enclosed in parentheses directly following the command.

Parameters for hi-res screen coordinates are entered in :X:Y (:horizontal:vertical) format. The X coordinate can vary between 0 and 319. The Y coordinate can vary between 0 and 199.

Below is a description of each of Dynamic Graphics' commands.

Build Commands

!DO (POINT). This command plots points on the hi-res screen. Following this command can be any number of point coordinates. The following example plots points in the center and four corners of the screen:

100 !DO (POINT)
110 :0:0:319:199
120 :0:199:319:0
130 :160:100
140 !END POINT

You must end the Point command with an !END POINT statement.

!DO (LINE). This command allows you to draw one, or several connected lines. Like the Point command, Line can contain any number of point coordinates. Here's an example:

100 !DO (LINE)
110 :10:10:132:10
120 :132:100:10:100
130 :10:10
140 !END LINE

This routine draws four connecting lines in the shape of a square. When one set of coordinates is specified, only a single point is plotted. When two coordinates are specified, a line is drawn.

The !DO (LINE) command is exited by !END LINE. An alternative exit is !STOP LINE. Normally, lines drawn by different !DO (LINE) commands are kept separate. If, however, a Line command ends with !STOP LINE, succeeding !DO (LINE) commands start plotting from the endpoint of the previously drawn line. Executing a !FINLINE returns things back to normal by keeping lines created by different !DO (LINE) commands separate.

!DO (SPRITE). This command allows you to define up to 96 sprite shapes. Although Dynamic Graphics does not provide commands for displaying or moving sprites—this still must be done with POKEs—it does allow you to define sprite shapes.

Exactly 64 numeric parameters must follow the !DO (SPRITE) command. For example, a solid-square sprite would be defined by the following:

100 !DO (SPRITE)
120 :255:255:255:255:255:255:255:255
130 :255:255:255:255:255:255:255:255
140 :255:255:255:255:255:255:255:255
150 :255:255:255:255:255:255:255:255
160 :255:255:255:255:255:255:255:255
170 :255:255:255:255:255:255:255:255
180 :255:255:255:255:255:255:255:255
190 :255:255:255:255:255:255:255:255
200 !END SPRITE

Every !DO (SPRITE) must terminate with an !END SPRITE statement. Later, sprite shapes are referenced by number. This number is determined by the order in which the sprites were defined. The first sprite defined is referred to as sprite 0, the second sprite is referred to as sprite 1, and so on.

!COLOR (bg,fg,x,y,n). The !COLOR command allows you to set the hi-res screen's colors. On the hi-res screen, you can have separate background and foreground colors for each 8 × 8-pixel area of the screen (often referred to as character boundaries). The parameters bg and fg specify the background and foreground colors of the character boundary defined by the x and y coordinates. The color parameters can range from 0 to 15. The x coordinate can range from 0 to 39, while y can range from 0 to 24.

The final parameter, n, specifies how many character boundaries the command affects (a maximum of 1000). As an example, !COLOR (0,5,0,0,1000) sets the background color to black and the foreground color to green for the entire screen. It is a good idea to execute a !COLOR command at the beginning of a program. Otherwise, you never know what strange colors you'll end up with.

!HUE (bg,fg). This command sets the colors of all pixels drawn after its use. As with the !COLOR command, bg and fg represent the background and foreground colors, respectively. If a line of a different color is drawn through a character boundary previously set by the !COLOR command, the new color supersedes the old.

!BRUSH (n). !BRUSH allows you to set the pixel width of your brush. The n parameter specifies the new width, and can range from 1 to 15. The actual size of lines drawn, however, depends on the angle of the lines. It is much like working with a chisel-point pencil. Experiment with different brush sizes to see a variety of effects.

TEXTURE (n). Unlike any other Dynamic Graphics command, !TEXTURE can be used only once within a program. This command determines the texture of all lines drawn with a brush size greater than one. The n parameter specifies a brushes pattern. As an example, a line drawn with !BRUSH (2) and a !TEXTURE (2) results in a pixel/blank/pixel brush. Again, experiment with this command to get a feel for its capabilities.

!ERASE. This command tells Dynamic Graphics to erase points instead of draw them. This command affects all of Dynamic Graphics' drawing commands. Erase mode is terminated with the statement !END ERASE.

!QUITPIC. This very important command informs Dynamic Graphics that you're finished defining a shape table. You must use this command prior to an Execute command. If you do not, the computer will try to plot points found beyond the shape table, possibly locking up your computer system.

Execute Commands

!VIEW. This command clears and displays the hi-res screen, drawing anything found in the shape table. Points and lines found in the shape table are drawn in the order in which they were defined. Because of this, it's possible to create an animation effect by drawing and redrawing various objects. !VIEW continues to display the hi-res screen until a key is pressed.

!MAP (ON/OFF) This command turns the hi-res screen on and off. When a !MAP (ON) is executed, the hi-res screen is displayed and the shape table is drawn. This command does not clear the hi-res screen, so it's possible to draw on top of previously drawn graphics. If you prefer, you may clear the hi-res screen prior to a !MAP (ON) with the !CLRMAP command described below. To return to the text screen, use the command !MAP (OFF). Unlike !VIEW, !MAP (ON) does not automatically return to the text screen after a key is pressed.

Immediate Commands

!SAVE "filename". The !SAVE command saves the current shape table to disk. Dynamic Graphics normally saves shape tables to device 8. If you wish to save your shape table to the drive known as device 9, enter a POKE 49159,9 prior to using the !SAVE command. To load a shape table, simply use BASIC's built-in load command. For example, LOAD "filename",8,1 loads the specified shape table into memory, ready to be displayed by an execute command.

!NEWPIC. Dynamic Graphics allows you to keep two separate shape tables in memory at once. This command switches between the two. When you execute a !NEWPIC, the current shape table is switched out and a new one is put in its place. To access the old shape table, simply execute another !NEWPIC.

Execute commands always display the current shape table. To see which shape table is currently active, PEEK location 49158. If this location contains a 0, the default shape table—the one active when you first run your program—is in use. If this location returns a 1, then you're using the alternate shape table. Note that once you have defined a shape table and executed the !QUITPIC command, you can not alter it. To redefine a shape table, you must either exit and rerun your program, or load in a new shape table from disk.

!DEFSPR(s,n). This command assigns a sprite shape defined by !DO (SPRITE) to one of the 64's sprites. The s represents the desired sprite shape (0–95), and n represents one of the 64's eight sprites (0–7). To assign the first sprite shape stored in the shape table to the 64's eighth sprite, for example, use the command !DEFSPR(0,7). Remember, in order to display a sprite, you must provide the proper POKEs.

!GETCHARS (ct). This command allows you to pick the type of characters produced by the !STRING statement (see below). The ct parameter determines the character, type. Below is a list of ct's possible values:

ValueCharacter Type
0Normal uppercase/graphics
128Reverse Uppercase/graphics
256Normal Lowercase/uppercase
384Reverse Lowercase/uppercase

Notice that ct is simply an offset into the 64's built-in character set. You can use your own custom character sets by loading them into memory at 52000.

!STRING (x,y,s$). This command prints characters on the hi-res screen. The x and y parameters specify the horizontal and vertical position of your text. The x coordinate can range from 0 to 39 while y can range from 0 to 24. When !STRING is executed, s$ is output to the hi-res screen. This parameter can be a string literal—like "Score"—or a string variable. Before using !STRING, you should use the !GETCHARS command to select the type of characters desired.

All of Dynamic Graphics' commands work from within a program only. And remember, before you can use any of these commands, you must first load Program 1 and execute a SYS 49152.

Sample Programs

Programs 2–4 offer examples on what you can do with Dynamic Graphics. Before any of these programs can be run, you must load Program 1 first.

Program 2 uses Dynamic Graphics to draw a potential game screen, complete with text. Program 3 displays a three-dimensional drawing in two different perspectives (see photo). Finally, Program 4 shows off Dynamic Graphics' ability to create animated art using a single shape table.