initgraph


Syntax
#include <graphics.h>
void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
Description
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 .BGI 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.

pathtodriver specifies the directory path where initgraph looks for graphics drivers. initgraph first looks in the path specified in pathtodriver, then (if they are not there) in the current directory. Accordingly, if pathtodriver is null, the driver files (*.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, which is defined in graphics.h and listed below.

graphics_drivers constant     Numeric value
DETECT 0 (requests autodetect)
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, *graphmode 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_modes enumeration type, which is defined in graphics.h and listed below.

graphdriver and graphmode must be set to valid values from the following tables, or you will get unpredictable results. The exception is graphdriver = DETECT.

Palette listings C0, C1, 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.

Palette Number    Three Colors
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.
Graphics        Columns   
Driver graphics_mode    Value     x Rows Palette    Pages
CGA CGAC0 0 320 x 200 C0 1
CGAC1 1 320 x 200 C1 1
CGAC2 2 320 x 200 C2 1
CGAC3 3 320 x 200 C3 1
CGAHI 4 640 x 200 2 color 1
 
MCGA MCGAC0 0 320 x 200 C0 1
 
MCGAC1 1 320 x 200 C1 1
MCGAC2 2 320 x 200 C2 1
MCGAC3 3 320 x 200 C3 1
MCGAMED 4 640 x 200 2 color 1
MCGAHI 5 640 x 480 2 color 1
 
EGA EGALO 0 640 x 200 16 color 4
 
EGAHI 1 640 x 350 16 color 2
 
EGA64 EGA64LO 0 640 x 200 16 color 1
 
EGA64HI 1 640 x 350 4 color 1
 
EGA-MONO EGAMONOHI 3 640 x 350 2 color 1 w/64K
EGAMONOHI 3 640 x 350 2 color 2 w/256K
 
HERC HERCMONOHI 0 720 x 348 2 color 2
 
ATT400 ATT400C0 0 320 x 200 C0 1
ATT400C1 1 320 x 200 C1 1
ATT400C2 2 320 x 200 C2 1
ATT400C3 3 320 x 200 C3 1
ATT400MED 4 640 x 200 2 color 1
ATT400HI 5 640 x 400 2 color 1
 
VGA VGALO 0 640 x 200 16 color 2
VGAMED 1 640 x 350 16 color 2
VGAHI 2 640 x 480 16 color 1
 
PC3270 PC3270HI 0 720 x 350 2 color 1
IBM8514 IBM8514HI 0 640 x 480 256 color ?
IBM8514LO 0 1024 x 768 256 color ?

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 below:
Constant Name     Number    Meaning
grNotDetected -2 Cannot detect a graphics card
grFileNotFound -3 Cannot find driver file
grInvalidDriver -4 Invalid driver
grNoLoadMem -5 Insufficient memory to load driver

Windows Notes [WIN]
The winbgim version of initgraph uses its parameters only to determine the size of the window that will be created. For example, initgraph(CGA, CGAC3) will create a 320 x 200 window. As an alternative, the user may call initwindow(width, height) instead of initgraph.

See also
closegraph
detectgraph
getdefaultpalette
getdrivername
getgraphmode
getmoderange
graphdefaults
graphresult
installuserdriver
registerbgidriver
registerbgifont
restorecrtmode
setgraphbufsize
setgraphmode

Example
/* initgraph 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 != grOk)    /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");

      getch();
      exit(1);               /* return with error code */
   }

   /* draw a line */
   line(0, 0, getmaxx(), getmaxy());

   /* clean up */
   getch();
   closegraph();
   return 0;
}

Back to index