initgraph
Initializes the graphics system.
Defined in header <graphics.h>
Syntax
#include <graphics.h>
void far initgraph(mt far *graphdriver, int far *graphmode,
char far *pathtodriver);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ |
Remarks
initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode.
To start the graphics system, first call the initgraph function, initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver.
If you tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode, initgraph also resets all graphics settings to their defaults (current position, palette, color, viewport, and so on) and resets graphresult to 0.
Normally, initgraph loads a graphics driver by allocating memory for the driver (through _graphgetmem), then loading the appropriate .BGl file from disk. As an alternative to this dynamic loading scheme, you can link a graphics driver file (or several of them) directly into your executable program file. See UTIL.DOC (included with your distribution disks) for more information on BGIOBJ.
pathtodriver specifies the directory path where initgraph looks for graphics drivers, initgraph first looks in the path specified in pathtodriver, then (if they're not there) in the current directory. Accordingly, if pathtodriver is null, the driver files C^.BGI) must be in the current directory. This is also the path settextstyle searches for the stroked character font files (*.CHR).
*graphdriver is an integer that specifies the graphics driver to be used. You can give it a value using a constant of the graphics_drivers enumeration type, defined in graphics.h and listed in Table 2.3.
Table 2.3
graphics_drivers
Graphics drivers
| constant | Numeric value |
constants
| DETECT | 0 (requests autodetection) |
| CGA | 1 |
| MCGA | 2 |
| EGA | 3 |
| EGA64 | 4 |
| EGAMONO | 5 |
| IBM8514 | 6 |
| HERCMONO | 7 |
| ATT400 | 8 |
| VGA | 9 |
| PC3270 | 10 |
*graphmode is an integer that specifies the initial graphics mode (unless *graphdriver equals DETECT; in which case, *gmphmode is set by initgraph to the highest resolution available for the detected driver). You can give *graphmode a value using a constant of the graphics jnodes enumeration type, defined in graphics.h and listed in Table 2.5.
graphdriver and graphmode must be set to valid values from tables 2.3 and 2.5, or you'll get unpredictable results. The exception is graphdriver = DETECT.
In Table 2.5, the Palette listings CO, CI, C2, and C3 refer to the four predefined four-color palettes available on CGA (and compatible) systems. You can select the background color (entry #0) in each of these palettes, but the other colors are fixed. These palettes are described in greater detail in Chapter 11, "Video functions" in the Programmer's Guide (in the section titled "Color control," toward the end of the chapter) and summarized in Table 2.4.
Table 2.4
Color assigned to pixel value
Color palettes
Palette
| number | 1 | ||
| 0 | LIGHTGREEN | LIGHTRED | YELLOW |
| 1 | LIGHTCYAN | LIGHTMAGENTA | WHITE |
| 2 | GREEN | RED | BROWN |
| 3 | CYAN | MAGENTA | LIGHTGRAY |
After a call to initgraph, *graphdriver is set to the current graphics driver, and *graphmode is set to the current graphics mode.
Table 2.5
| Graphics | Column |
Graphics modes
| driver | graphics_modesValue | xrow | Palette | Pages |
| CGA | CGACO | 0 | 320x200 | CO | |
| CGACl | 1 | 320x200 | CI | ||
| CGAC2 | 2 | 320x200 | C2 | ||
| CGAC3 | 3 | 320x200 | C3 | ||
| CGAHI | 4 | 640x200 | 2 color | ||
| MCGA | MCGACO | 0 | 320x200 | CO | |
| MCGACl | 1 | 320x200 | CI | ||
| MCGAC2 | 2 | 320x200 | C2 | ||
| MCGAC3 | 3 | 320x200 | C3 | ||
| MCGAMED | 4 | 640x200 | 2 color | ||
| MCGAHI | 5 | 640x480 | 2 color | ||
| EGA | EGALO | 0 | 640x200 | 16 color | 4 |
| EGAHI | 1 | 640x350 | 16 color | 2 | |
| EGA64 | EGA64LO | 0 | 640x200 | 16 color | 1 |
| EGA64HI | 1 | 640x350 | 4 color | 1 | |
| EGA-MONO | EGAMONOHI | 3 | 640x350 | 2 color | |
| EGAMONOHI | 3 | 640x350 | 2 color | 2** | |
| HERC | HERCMONOHI | 0 | 720x348 | 2 color | 2 |
| ATT400 | ATT400C0 | 0 | 320x200 | CO | 1 |
| ATT400C1 | 1 | 320x200 | CI | 1 | |
| ATT400C2 | 2 | 320x200 | C2 | 1 | |
| ATT400C3 | 3 | 320x200 | C3 | 1 | |
| ATT400MED | 4 | 640x200 | 2 color | 1 | |
| ATT400HI | 5 | 640x400 | 2 color | 1 | |
| VGA | VGALO | 0 | 640x200 | 16 color | 2 |
| VGAMED | 1 | 640x350 | 16 color | 2 | |
| VGAHI | 2 | 640x480 | 16 color | 1 | |
| PC3270 | PC3270HI | 0 | 720x350 | 2 color | 1 |
| IBM8514 | IBM8514HI | 1 | 1024x768 | 256 color | |
| IBM8514LO | 0 | 640x480 | 256 color |
^ 64K on EGAMONO card 256K on EGAMONO card
Return value
initgraph always sets the internal error code; on success, it sets the code to 0. If an error occurred, *graphdriver is set to -2, -3, -4, or -5, and graphresult returns the same value as listed here:
| grNotDetected | -2 | Cannot detect a graphics card |
| grFileNotFound | -3 | Cannot find driver file |
| grlnvalidDriver | -4 | Invalid driver |
| grNoLoadMem | -5 | Insufficient memory to load driver |
See also
closegraphdetectgraphgetdefaultpalettegetdrivernamegetgraphmodegetmoderangegraphdefaults_graphgetmemgraphresultinstalluserdriverregisterbgidriverregisterbgifontrestorecrtmodesetgraphbufsizesetgraphmode
Example
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode 1= grOk) /* an error occurred */
printf("Graphics error: %s\n", grapherrormsg (errorcode)
printf("Press any key to halt:");
getchO ;
exit(l); /* return with error code */
}
/* draw a line */
line(0, 0, getmaxxO, getmaxyO);
/* clean up */
getchO ;
closegraphO ;
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/graphics.h/initgraph/
· short form /3.1/initgraph/