home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Devil's Doorknob BBS Capture (1996-2003)
/
devilsdoorknobbbscapture1996-2003.iso
/
Dloads
/
PROGRAMM
/
FGL112B.ZIP
/
USER03.DOC
< prev
next >
Wrap
Text File
|
1992-10-05
|
23KB
|
547 lines
Chapter 3
Initializing the
Video Environment
28 Fastgraph User's Guide
Overview
Before Fastgraph can perform any text or graphics video operations, you
must select a video mode in which your program will run. An important part
of this selection depends on whether your program will run in a text mode, a
graphics mode, or both. This chapter discusses the necessary video
initialization for each case.
Establishing a Text Mode
When you write a program that only uses text modes, you must determine
if the program will run on monochrome systems, color systems, or both. In
general, there is no reason to exclude one type of system, because the
additional programming required to support both is rather trivial.
The Fastgraph routine fg_setmode establishes a video mode and
initializes Fastgraph's internal parameters for that mode. This routine has
a single integer argument whose value is a video mode number between 0 and
23. Its value can also be -1, which tells Fastgraph to use the current video
mode. Specifying an fg_setmode argument of -1 is often useful in programs
that only use text video modes.
When you establish a text video mode, the ROM BIOS text cursor is made
visible, and this is often undesirable. The Fastgraph routine fg_cursor
controls the visibility of the text cursor. The fg_cursor routine has a
single integer argument that specifies the cursor visibility. If its value
is 0, the cursor is made invisible; if its value is 1, the cursor is made
visible.
At this point, an example may help to clarify things. The following
program shows how to initialize Fastgraph for the 80-column color text mode
(mode 3) and turn off the text mode cursor. It uses two Fastgraph routines
that we have not yet discussed, fg_setcolor and fg_text. These routines will
be discussed in later sections of this document. For now, it should suffice
to know the call to fg_setcolor makes subsequent text appear in bright white,
and the call to fg_text displays the characters passed to it.
Example 3-1.
#include <fastgraf.h>
void main(void);
void main()
{
fg_setmode(3);
fg_cursor(0);
fg_setcolor(15);
fg_text("Hello, world.",13);
}
If you run example 3-1, notice the text displayed by the program appears
in the upper left corner of the screen. On the line below this, the DOS
prompt appears, waiting for your next DOS command. Furthermore, if your
system uses the ANSI.SYS driver to set screen attributes (such as with
Chapter 3: Initializing the Video Environment 29
Norton's SA program), you should also notice only the DOS prompt appears in
the colors defined by the screen attributes -- the rest of the screen is
blank.
A more graceful return to DOS is needed. In example 3-2, we'll use the
Fastgraph routine fg_reset. This routine erases the screen, and if the
ANSI.SYS driver is loaded, fg_reset also restores any previously set screen
attributes. We've also included a call to the Fastgraph routine fg_waitkey
to wait for a keystroke before exiting. If we didn't do this, we would never
see the program's output.
Example 3-2.
#include <fastgraf.h>
void main(void);
void main()
{
fg_setmode(3);
fg_cursor(0);
fg_setcolor(15);
fg_text("Hello, world.",13);
fg_waitkey();
fg_reset();
}
Since examples 3-1 and 3-2 specifically used video mode 3, they would
not work on a monochrome system. Ideally, we would like to use fg_setmode(3)
for color systems and fg_setmode(7) for monochrome systems. To do this, we
need a way to determine whether the program is being run on a color system or
on a monochrome system. The next example illustrates an easy way to
accomplish this.
Example 3-3 uses the Fastgraph routine fg_testmode to determine if the
user's system will support the video mode number specified as its first
argument (the second argument is the number of video pages required, which
will be 1 for all examples in this section). The fg_testmode routine returns
a value of 1 (as its function value) if the requested video mode can be used,
and it returns 0 if not. The program first sees if an 80-column color text
mode is available (mode 3), and if so, it selects that mode. If the color
mode is not available, it checks if the monochrome text mode is available
(mode 7), and if so, it chooses the monochrome mode. If neither mode is
available, then the program assumes the user's system has a 40-column
display, issues a message indicating the program requires an 80-column
display, and then exits.
Example 3-3.
#include <fastgraf.h>
#include <stdio.h>
#include <stdlib.h>
void main(void);
void main()
30 Fastgraph User's Guide
{
int old_mode;
old_mode = fg_getmode();
if (fg_testmode(3,1))
fg_setmode(3);
else if (fg_testmode(7,1))
fg_setmode(7);
else {
printf("This program requires\n");
printf("an 80-column display.\n");
exit(1);
}
fg_cursor(0);
fg_setcolor(15);
fg_text("Hello, world.",13);
fg_waitkey();
fg_setmode(old_mode);
fg_reset();
}
Example 3-3 also illustrates another useful procedure. It is
recommended, especially in graphics modes, to restore the original video mode
and screen attributes before a program returns to DOS. We've already seen
how the fg_reset routine restores the screen attributes, but how do we
restore the original video mode? The Fastgraph routine fg_getmode returns
the current video mode as its function value. If we call fg_getmode before
calling fg_setmode, we can use the return value from fg_getmode and again
call fg_setmode before the program exits.
You also can use another Fastgraph routine, fg_bestmode, to determine if
a video mode with a specific resolution is available on the user's system.
The fg_bestmode routine requires three integer arguments: a horizontal
resolution, a vertical resolution, and the number of video pages required.
As its function value, fg_bestmode returns the video mode number that offers
the most capabilities for the resolution and number of pages requested. It
returns a value of -1 if no available video mode offers the requested
criteria.
For example, if we require an 80 by 25 text mode, we can use the
function call fg_bestmode(80,25,1) to pick the "best" video mode available
that offers this capability. In text modes, the term best means to give
preference to a color text mode over a monochrome text mode. Example 3-4
performs the same function as example 3-3, but it uses fg_bestmode rather
than fg_testmode.
Example 3-4.
#include <fastgraf.h>
#include <stdio.h>
#include <stdlib.h>
Chapter 3: Initializing the Video Environment 31
void main(void);
void main()
{
int old_mode;
int new_mode;
old_mode = fg_getmode();
new_mode = fg_bestmode(80,25,1);
if (new_mode < 0) {
printf("This program requires\n");
printf("an 80-column display.\n");
exit(1);
}
fg_setmode(new_mode);
fg_cursor(0);
fg_setcolor(15);
fg_text("Hello, world.",13);
fg_waitkey();
fg_setmode(old_mode);
fg_reset();
}
43-line and 50-line Text Modes
When using an 80-column text mode on a system equipped with an EGA, VGA,
or MCGA video display and adapter, you can extend the screen size from 25
lines to 43 or 50 lines. While all systems offer 25-line text modes, EGA
systems also offer 43-line modes, MCGA systems also offer 50-line modes, and
VGA systems offer both 43-line and 50-line modes. The 43-line mode is not
available on EGA systems equipped with an RGB display. If you extend the
screen size to 43 or 50 lines, the physical character size is reduced
proportionally so all lines appear on the screen.
The fg_setlines routine defines the number of text rows per screen. It
has a single integer argument whose value must be 25, 43, or 50. If you pass
any other value to fg_setlines, or pass a value not supported by the host
system's video configuration, fg_setlines does nothing. In addition, calling
fg_setlines makes the text cursor visible. Another Fastgraph routine,
fg_getlines, returns as its function value the number of text rows currently
in effect. You also can use fg_getlines in graphics video modes.
Example 3-5 illustrates the use of the fg_setlines and fg_getlines
routines. The program first establishes the 80-column color text mode (this
sets the screen size to its 25-line default) and makes the text cursor
invisible. It then displays the words "first line" in the upper left corner
of the screen. Next, the program checks if an EGA with enhanced display is
available, and if so, changes the screen to 43 lines (video mode 16 is only
available on EGA systems equipped with an enhanced display). Next, the
program checks if a VGA or MCGA is available, and if so changes the screen to
50 lines (video mode 17 is only available on VGA and MCGA systems). Finally,
the program restores the original video mode, restores the number of lines
per screen to its original setting, and restores the original screen
attributes before exiting.
32 Fastgraph User's Guide
Example 3-5.
#include <fastgraf.h>
void main(void);
void main()
{
int lines;
int old_lines;
int old_mode;
old_lines = fg_getlines();
old_mode = fg_getmode();
fg_setmode(3);
fg_cursor(0);
fg_setcolor(15);
fg_text("first line",10);
fg_waitkey();
if (fg_testmode(16,0)) {
fg_setlines(43);
fg_cursor(0);
fg_waitkey();
}
if (fg_testmode(17,0)) {
fg_setlines(50);
fg_cursor(0);
fg_waitkey();
}
fg_setmode(old_mode);
fg_setlines(old_lines);
fg_reset();
}
Establishing a Graphics Mode
The steps for establishing a graphics mode are similar to establishing a
text mode. However, there are more restrictions since some systems may not
support all the graphics video modes. For example, a program could not run
in mode 13 on a CGA system, nor could a program run in mode 9 on anything
except a Tandy 1000 or PCjr system.
Example 3-6 shows one way to write an EGA-specific program. The program
in this example uses mode 16, the 640 x 350 EGA mode that requires an
Enhanced Color Display (ECD). It uses the Fastgraph routine fg_egacheck to
determine if an EGA and ECD are present. The fg_egacheck routine returns a
value of 0 if an EGA is not found, or if there is an EGA but no ECD. If an
EGA and ECD are found, it returns a positive integer indicating the number of
64K-byte increments of video memory on the EGA. Since mode 16 requires
112,000 bytes of video memory for a single video page, there must be at least
128K bytes of video memory on the EGA to run this program. Hence, we must be
sure that fg_egacheck returns a value of at least 2.
Chapter 3: Initializing the Video Environment 33
Example 3-6.
#include <fastgraf.h>
#include <stdio.h>
#include <stdlib.h>
void main(void);
void main()
{
int mode;
if (fg_egacheck() < 2) {
printf("This program requires an Enhanced Graphics Adapter\n");
printf("(EGA) and an Enhanced Color Display (ECD).\n");
exit(1);
}
mode = fg_getmode();
fg_setmode(16);
fg_setcolor(15);
fg_text("Hello, world.",13);
fg_waitkey();
fg_setmode(mode);
fg_reset();
}
For graphics programs, it may suffice to write a program to run in a
specific video mode, but it is often more desirable to write a program that
will run in any of several video modes. This is especially true for
commercial products, since they should run on as many different video
configurations as possible.
Fastgraph includes a routine named fg_automode that determines the
graphics video mode that offers the most functionality for the user's video
hardware configuration. For example, the Tandy 1000 series computers support
all three CGA modes (4, 5, and 6) and the 320 by 200 16-color Tandy 1000 mode
(9). Of these modes, mode 9 offers the most features from a graphics
standpoint, so fg_automode will return a value of 9 when run on a Tandy 1000
computer. The following table summarizes the video mode numbers returned by
fg_automode for given adapter-display combinations.
------- display -------
adapter mono RGB ECD VGA
MDA 7 0 7 7
HGC 11 0 0 11
CGA 0 4 0 0
EGA 15 13 16 0
VGA 17 17 17 18
MCGA 17 17 17 19
Tandy 7 9 0 0
PCjr 7 9 0 0
34 Fastgraph User's Guide
Example 3-7 shows how to use fg_automode to determine the "best"
graphics mode for the user's video hardware. In graphics modes, the term
best means the highest resolution, followed by the number of available
colors. The program displays a message that includes the selected video mode
number.
Example 3-7.
#include <fastgraf.h>
#include <stdio.h>
void main(void);
void main()
{
int old_mode;
int new_mode;
char string[4];
old_mode = fg_getmode();
new_mode = fg_automode();
fg_setmode(new_mode);
fg_setcolor(15);
fg_text("I'm running in mode ",20);
sprintf(string,"%d.",new_mode);
fg_text(string,3);
fg_waitkey();
fg_setmode(old_mode);
fg_reset();
}
For simple programs such as example 3-7, different screen resolutions
may not be an issue. However, in more complex graphics programs it is often
desirable to write a program for a fixed screen resolution. A common
practice is to develop graphics programs to run in modes 4 (for CGA), 9
(Tandy 1000 or PCjr), 12 (Hercules), 13 (EGA or VGA), and 19 or 20 (MCGA or
VGA). The reason for selecting these five modes is they all use the same 320
by 200 resolution and will run on any IBM PC or PS/2 with graphics
capabilities.
Example 3-8 performs the same function as example 3-7, but it uses the
fg_bestmode routine instead of fg_automode to restrict the program to 320 by
200 graphics modes. For this resolution, the fg_bestmode routine will first
check the availability of mode 19, followed by modes 13, 9, 4, and 12. If
fg_bestmode determines no 320 by 200 graphics mode is available (indicated by
a return value of -1), the program prints an informational message and exits.
Otherwise it selects the video mode fg_bestmode proposes and continues.
Example 3-8.
#include <fastgraf.h>
#include <stdio.h>
#include <stdlib.h>
void main(void);
Chapter 3: Initializing the Video Environment 35
void main()
{
int old_mode;
int new_mode;
char string[4];
old_mode = fg_getmode();
new_mode = fg_bestmode(320,200,1);
if (new_mode < 0) {
printf("This program requires a 320 by 200 graphics mode.\n");
exit(1);
}
fg_setmode(new_mode);
fg_setcolor(15);
fg_text("I'm running in mode ",20);
sprintf(string,"%d.",new_mode);
fg_text(string,3);
fg_waitkey();
fg_setmode(old_mode);
fg_reset();
}
If your program will not support all PC and PS/2 video modes with the
same resolution (for example, it will run in some but not all 320 by 200
graphics modes), you may want to consider using the fg_testmode routine
instead of fg_bestmode to check for available video modes. You also may want
to use fg_testmode to change the video mode precedence used by fg_bestmode.
For example, mode 13 (EGA) is faster than mode 19 (MCGA), so you may want to
consider giving EGA precedence over MCGA, especially if your program does not
use more than 16 colors.
Example 3-9 is similar to example 3-8, but it will only run in the 320
by 200 EGA, MCGA, and CGA graphics modes (video modes 13, 19, and 4
respectively). The program uses fg_testmode to select its video mode. Note
the order of calls to fg_testmode gives EGA precedence over MCGA, and MCGA
precedence over CGA.
Example 3-9.
#include <fastgraf.h>
#include <stdio.h>
#include <stdlib.h>
void main(void);
void main()
{
int old_mode;
char string[4];
old_mode = fg_getmode();
if (fg_testmode(13,1))
36 Fastgraph User's Guide
fg_setmode(13);
else if (fg_testmode(19,1))
fg_setmode(19);
else if (fg_testmode(4,1))
fg_setmode(4);
else {
printf("This program requires an EGA, MCGA, or CGA.\n");
exit(1);
}
fg_setcolor(15);
fg_text("I'm running in mode ",20);
sprintf(string,"%d.",getmode());
fg_text(string,3);
fg_waitkey();
fg_setmode(old_mode);
fg_reset();
}
Summary of Video Initialization Routines
This section summarizes the functional descriptions of the Fastgraph
routines presented in this chapter. More detailed information about these
routines, including their arguments and return values, may be found in the
Fastgraph Reference Manual.
FG_AUTOMODE determines the graphics video mode that offers the most
features for the user's display and adapter configuration. The value it
returns helps determine a suitable value to pass to the fg_setmode routine.
FG_BESTMODE is similar to fg_automode, but it excludes video modes that
do not offer the specified resolution and video page requirements.
FG_CURSOR makes the text mode cursor visible or invisible. This routine
has no effect when used in a graphics mode.
FG_EGACHECK returns information about the active EGA or VGA adapter and
display. It is useful in checking if the adapter has enough memory to run an
EGA-specific program.
FG_GETLINES returns the number of text rows per screen for the current
video mode.
FG_GETMODE returns the current video mode. It is typically one of the
first Fastgraph routines called in a program. The value returned by
fg_getmode can be retained to restore the original video mode when a program
transfers control back to DOS.
FG_RESET is generally the last Fastgraph routine called in a program.
It only functions in text video modes. When the ANSI.SYS driver is not
loaded, fg_reset merely erases the screen. When ANSI.SYS is loaded, fg_reset
also restores any previously set screen attributes.
Chapter 3: Initializing the Video Environment 37
FG_SETLINES extends an 80-column text mode to 25, 43, or 50 lines per
screen. This routine is only meaningful when running in 80-column text modes
on EGA, VGA, or MCGA systems (in other cases it does nothing).
FG_SETMODE establishes a video mode and initializes Fastgraph's internal
parameters for that video mode. It must be called before any Fastgraph
routine that performs video output. A program can call fg_setmode as many
times as needed to switch between different video modes.
FG_TESTMODE determines whether or not a specified video mode (with a
given number of video pages) is available on the user's system.
38 Fastgraph User's Guide