MCS 585 Winter 1997 Graphics Package, Level 0 Due Thurs., Jan. 30

This is the first in a series of programs which implement a graphics package. The routines developed here will be used in all subsequent assignments. Because future levels depend heavily on these routines, be sure to write your routines as described here. Test them carefully and make very certain they are well-structured and correct.

You are to write the routines described below, and link them with a driver provided by your instructor.

For this assignment, you are to write a driver for the Tektronix 4014 series graphics terminal. Your package will provide an interface for programming the Tektronix 4014 in a manner compatible with the Macintosh and Windows graphics environments. Future assignments will assume the availability of this command interface.

There are several reasons for choosing the Tektronix 4014:

(1) The 4014 is a de facto standard for simple vector graphics packages.
(2) Many computing environments provide Tektronix 4014 emulation capabilities, including SunView's tektool (available on MCSNET) and the NCSA Telnet communication software (public domain software available in Macintosh and Windows versions which allows remote connection to telnet hosts on TCP/IP networks). NCSA Telnet is available on many of the Apple Macintoshes on campus and may be used to conduct a remote telnet session on MCSNET.

Creation of a driver for the Tektronix 4014 will be the cornerstone to creating a graphics package which is portable across multiple platforms in use at Drexel, including remote connection.


About the Tektronix 4014:

A graphics session with the Tektronix 4014 involves a sequence of ASCII characters sent to the screen which are interpreted as commands and acted on accordingly. The ASCII control characters 0-31 (octal 000-037) are used as control codes . The ASCII printing characters 32-127 (octal 040-177) are used as data codes . Only the low (rightmost) seven bits of each (8-bit) character are relevant. The interpretation of data codes depends upon the current display mode.

In alphabetic mode , data characters are displayed literally as text.
In graphic mode , data characters encode (x,y)-coordinate information and drawing commands in a non-obvious fashion. The addressability is 0 x 1023 and 0 y 767. Thus, the screen has a 4:3 aspect ratio (x:y), even though the format allows y coordinate values as large as 1023. (Actually y may have a maximum of 774, but we use a maximum of 767 to get a 4:3 aspect ratio).

Notice that 10 bits are required to specify an x or y coordinate in graphic mode. This presents a problem, as a character only contains 8 bits. Thus, two characters are required to represent one coordinate. This is done by splitting each 10-bit value into "high 5 bits" and "low 5 bits" portions which are communicated separately. The extra bits in each character are used to identify whether the 5-bit portions are high or low, x or y (explained further below).

The following control codes are important:


31 Enter alphabetic mode .
(From keyboard, ^_ , that is <ctrl>-underscore )

29 Enter graphic mode . ( ^[ )
This generally precedes a vector command, which is therefore interpreted as meaning 'move' to the given vector. Subsequent vectors are then interpreted as meaning 'draw' from the previous vector to the current one.

27 12 Screen erase . ( <esc> ^l )
Erase the screen, enter alphabetic mode, and position the beam at the upper left corner.

27 26 Select with crosshair . ( <esc> ^z )
Display the crosshair cursor on the screen, and wait for input from the keyboard. When the next character is typed on the keyboard, the terminal returns
<CC> <Hx> <Lx> <Hy> <Ly> <CR>
where these six characters are:
<CC> = the character just typed
<Hx> <Lx> <Hy> <Ly> = high x, low x, high y, low y
<CR> = carriage return .
The terminal returns to alphabetic mode afterwards.


Vector Commands:

The Tektronix maintains 10-bit x and y registers which can be modified by transmitting appropriate data codes while in graphic mode. These data codes are interpreted as follows:

11yyyyy - Set the low 5 bits of the y register to yyyyy

10xxxxx - Set the low 5 bits of the x register to xxxxx , and either move or draw to the current (x,y) position specified by the registers. (The 'move or draw' decision depends on whether this is the first vector transmitted since entering graphic mode, as described above.)

01aaaaa - If the previous data code was "set low y", then set the high 5 bits of x to aaaaa . Otherwise, set the high 5 bits of y to aaaaa.

When in graphic mode, writing the four-character sequence
( 01YYYYY ) ( 11yyyyy ) ( 01XXXXX ) ( 10xxxxx )
therefore transmits both a 10-bit x-coordinate and a 10-bit y-coordinate. This also causes the beam to move to the appropriate (x,y) position. As indicated above, a line is also drawn unless this is the first coordinate pair following an "enter graphic mode" command.

Note that the order in which coordinate information is transmitted to the Tektronix differs from the order in which the Tektronix returns cursor information.


Details:

Your assignment is to construct a driver for the Tektronix 4014 which will be the first part of a simple graphics package in C. The first step is to write the following routines. Although it is generally a good practice to include error-handling in your routines, you will notice that some of these "level 1" routines do not check for valid arguments. This is because the higher-level routines you write in future exercises will perform this error-checking.

void InitGraphics ( void ) ;
Initialize the bottom level of the graphics package. Enter graphics mode, and call NewPicture, described next.

void NewPicture ( void ) ;
Begin a new picture by clearing the screen.

void WriteText ( char *s ) ;
Enter alphabetic mode, and print the given string.

void MoveTo ( int x,y ) ;
Change to graphic mode, and output the four characters necessary to move to the point (x,y). Don't worry about the values of x and y. They are guaranteed to be in the range
0 x 1023 , 0 y 767 .
The higher-level routines you write in future exercises will ensure that the values of x and y are always correct.

void LineTo ( int x,y ) ;
You may assume that the terminal is already in graphic mode and that a MoveTo or LineTo has just been performed. Output the four characters needed to draw a vector to (x,y). The coordinates are identical to those described for MoveTo.

int GetCursor ( int x ; int y ) ;
Read the coordinates of the crosshair cursor and scale them to 15 bits, returning them in x and y. The function returns 0 if the 'q' or 'Q' character is typed on the keyboard, and 1 otherwise.

void TerminateGraphics ( void ) ;
Return the state of the terminal to normal use. You should first move to the lower left corner of the screen.


What to do, what to turn in:

Write and test your routines. Your instructor will provide some test files which you should use to call your routines. The names and locations of the test files will be announced shortly on the class newsgroup.

You can test your programs in one of several ways, including:
using tektool on the workstations in MCS lab;
using NCSA Telnet in a telnet session with the MCS servers;
using another Tektronix emulator
using an actual Tektronix 4014!

Print the program output in the manner prescribed by the system you use. Hand in your output along with your routines.

Notes :

Users of NCSA Telnet : you may need to configure a terminal option with Tektronix 4014 capabilities. You can do this by choosing "Sessions" in the "Preferences" menu item, clicking the "new" button, and selecting the "TEK 4014" option on the accompanying dialog box. Give your session an appropriate name, and give "queen.mcs.drexel.edu" as the hostname. A session so configured will automatically open a graphics window when a "Clear Screen" control sequence is sent by your program. A session may be ended by sending the control sequence "ESC ^x" (decimal 27 24).

Users of tektool : see the UNIX man page for tektool.


In your next homework assignment, you will (among other things) be asked to write equivalent routines for your choice of a Macintosh or Windows environment. Specifics will be distributed with the next assignment, but you may wish to get a head start by doing this now.