Borland C++ reference

initgraph

raw OCR

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

DOSUNIXWindowsANSI CC++ 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

constantNumeric value

constants

DETECT0 (requests autodetection)
CGA1
MCGA2
EGA3
EGA644
EGAMONO5
IBM85146
HERCMONO7
ATT4008
VGA9
PC327010

*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

number1
0LIGHTGREENLIGHTREDYELLOW
1LIGHTCYANLIGHTMAGENTAWHITE
2GREENREDBROWN
3CYANMAGENTALIGHTGRAY

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

GraphicsColumn

Graphics modes

drivergraphics_modesValuexrowPalettePages
CGACGACO0320x200CO
CGACl1320x200CI
CGAC22320x200C2
CGAC33320x200C3
CGAHI4640x2002 color
MCGAMCGACO0320x200CO
MCGACl1320x200CI
MCGAC22320x200C2
MCGAC33320x200C3
MCGAMED4640x2002 color
MCGAHI5640x4802 color
EGAEGALO0640x20016 color4
EGAHI1640x35016 color2
EGA64EGA64LO0640x20016 color1
EGA64HI1640x3504 color1
EGA-MONOEGAMONOHI3640x3502 color
EGAMONOHI3640x3502 color2**
HERCHERCMONOHI0720x3482 color2
ATT400ATT400C00320x200CO1
ATT400C11320x200CI1
ATT400C22320x200C21
ATT400C33320x200C31
ATT400MED4640x2002 color1
ATT400HI5640x4002 color1
VGAVGALO0640x20016 color2
VGAMED1640x35016 color2
VGAHI2640x48016 color1
PC3270PC3270HI0720x3502 color1
IBM8514IBM8514HI11024x768256 color
IBM8514LO0640x480256 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-2Cannot detect a graphics card
grFileNotFound-3Cannot find driver file
grlnvalidDriver-4Invalid driver
grNoLoadMem-5Insufficient memory to load driver

See also

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.