Borland Graphics Interface For Windows
Borland Graphics Interface For Windows
The following functions are mostly from the original Borland Graphics Interface for DOS
programs. The BGI graphics functions may also be used with Windows programs created
by the Borland 5.0 compiler, the free GNU C++ compiler, and possibly other compilers.
Extra Windows functions are also available, described in
www.cs.colorado.edu/~main/cs1300/doc/bgi/bgi.html. These extra functions are indicated
below by . Also, any of the functions that use colors can use RGB colors in addition
to the 16-color BGI palette.
Functions:
void bar (int left, int top, int right, int bottom);
void bar3d (int left, int top, int right, int bottom, int
depth, int topflag);
ostringstream bgiout;
void getimage (int left, int top, int right, int bottom,
void *bitmap);
int graphresult(void);
unsigned imagesize (int left, int top, int right, int bottom);
void line (int x1, int y1, int x2, int y2);
void printimage (
);
void putimage (int left, int top, void *bitmap, int op);
void readimagefile (
);
void rectangle (int left, int top, int right, int bottom);
RGB functions:
COLOR(r,g,b),
IS_BGI_COLOR(v), IS_RGB_COLOR(v)
void writeimagefile (
);
arc
Syntax
#include <graphics.h>
Description
arc draws a circular arc in the current drawing color centered at (x,y) with a radius
given by radius. The arc travels from stangle to endangle. If stangle equals 0 and
endangle equals 360, the call to arc draws a complete circle.
The linestyle parameter does not affect arcs, circles, ellipses, or pie slices. Only the
thickness parameter is used.
If you are using a CGA in high resolution mode or a monochrome graphics adapter,
the examples in online Help that show how to use graphics functions might not
produce the expected results. If your system runs on a CGA or monochrome
adapter, pass the value 1 to those functions that alter the fill or drawing color
(setcolor, setfillstyle, and setlinestyle, for example), instead of a symbolic color
constant (defined in graphics.h).
Return Value
None.
See also
circle
ellipse
fillellipse
getarcoords
getaspectratio
pieslice
sector
Example
/* arc example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* draw arc */
/* clean up */
getch();
closegraph();
return 0;
bar
Syntax
#include <graphics.h>
void bar(int left, int top, int right, int bottom);
Description
bar draws a filled-in, rectangular, two-dimensional bar. The bar is filled using the
current fill pattern and fill color. bar does not outline the bar; to draw an outlined
two-dimensional bar, use bar3d with depth equal to 0.
The upper left and lower right corners of the rectangle are given by (left, top) and
(right, bottom), respectively. The coordinates refer to pixels.
Return Value
None.
See also
bar3d
rectangle
setcolor
setfillstyle
setlinestyle
Example
/* bar example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setfillstyle(i, getmaxcolor());
getch();
/* clean up */
closegraph();
return 0;
}
bar3d
Syntax
#include <graphics.h>
void bar3d(int left, int top, int right, int bottom, int depth, int
topflag);
Description
bar3d draws a three-dimensional rectangular bar, then fills it using the current fill
pattern and fill color. The three-dimensional outline of the bar is drawn in the
current line style and color. The bar's depth in pixels is given by depth. The topflag
parameter governs whether a three-dimensional top is put on the bar. If topflag is
nonzero, a top is put on; otherwise, no top is put on the bar (making it possible to
stack several bars on top of one another). The upper left and lower right corners of
the rectangle are given by (left, top) and (right, bottom), respectively.
To calculate a typical depth for bar3d, take 25% of the width of the bar, like this:
bar3d(left,top,right,bottom, (right-left)/4,1);
Return Value
None.
See also
bar
rectangle
setcolor
setfillstyle
setlinestyle
Example
/* bar3d example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setfillstyle(i, getmaxcolor());
getch();
}
/* clean up */
closegraph();
return 0;
Syntax
#include "graphics.h"
Description
The winbgim package supports a globally defined output stream called bgiout.
This output stream can be written to just like cout or any other output stream.
However, the information written to bgiout does not appear in the graphics window
until the program calls outstream or outstreamxy.
The outstream and outstreamxy functions work like outtext and outtextxy
functions except that they print from an ostringstream rather than from an
ordinary string. The outstream function prints its information in the active graphics
window at the current pointer. The outstreamxy function prints its information in
the active graphics window at coordinates (x,y).
Both functions use the global bgiout as the default value for the ostringstream.
bgiout Examples:
#include "graphics.h"
initwindow(400, 400);
bgiout << "Hello, World!" << endl;
outstreamxy(100, 100);
getch( );
closegraph( );
circle
Syntax
#include <graphics.h>
Description
circle draws a circle in the current drawing color with its center at (x,y) and the
radius given by radius.
The linestyle parameter does not affect arcs, circles, ellipses, or pie slices. Only the
thickness parameter is used.
If your circles are not perfectly round, adjust the aspect ratio.
Return Value
None.
See also
arc
ellipse
fillellipse
getaspectratio
sector
setaspectratio
Example
/* circle example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* clean up */
getch();
closegraph();
return 0;
cleardevice
Syntax
#include <graphics.h>
void cleardevice(void);
Description
cleardevice erases (that is, fills with the current background color) the entire
graphics screen and moves the CP (current position) to home (0,0).
Return Value
None.
See also
clearviewport
Example
/* cleardevice example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
settextjustify(CENTER_TEXT, CENTER_TEXT);
getch();
closegraph();
return 0;
clearmouseclick
Syntax
#include "graphics.h"
Description
The clearmouseclick function is available in the winbgim implementation of BGI
graphics. This is just like getmouseclick, except it does not provide the x and y
coordinates of the event. The value of the argument kind may be any of the
constants listed above. After calling getmouseclick, for a particular kind of event,
the ismouseclick will return false for that kind of event until another such event
occurs.
WM_MOUSEMOVE
if you want to detect a mouse movement
WM_LBUTTONDBLCLK
...detect when the left mouse button is double clicked
WM_LBUTTONDOWN
...detect when the left mouse button is clicked down
WM_LBUTTONUP
...detect when the left mouse button is released up
WM_MBUTTONDBLCLK
...detect when the middle mouse button is double clicked
WM_MBUTTONDOWN
...detect when the middle mouse button is clicked down
WM_MBUTTONUP
...detect when the middle mouse button is released up
WM_RBUTTONDBLCLK
...detect when the right mouse button is double clicked
WM_RBUTTONDOWN
...detect when the right mouse button is clicked down
WM_RBUTTONUP
...detect when the right mouse button is released up
The middle mouse button handlers aren't working on my machine. I haven't yet
tracked down the reason--it could be a broken mouse or it could be a bug in my
programming.
See also
ismouseclick
getmouseclick
Example
/* mouse example */
#include "graphics.h"
void main(void)
// Put the machine into graphics mode and get the maximum
coordinates:
initwindow(450, 300);
maxx = getmaxx( );
maxy = getmaxy( );
setfillstyle(SOLID_FILL, RED);
setcolor(WHITE);
outtextxy(20, 20, "Left click " << LIMIT << " times to end.");
setcolor(BLUE);
divisor = 2;
triangle(maxx/divisor, maxy/divisor);
delay(500);
divisor++;
if (ismouseclick(WM_LBUTTONDOWN))
clearmouseclick(WM_LBUTTONDOWN));
count++;
closegraph( );
clearviewport
Syntax
#include <graphics.h>
void clearviewport(void);
Description
clearviewport erases the viewport and moves the CP (current position) to home
(0,0), relative to the viewport.
Return Value
None.
See also
cleardevice
getviewsettings
setviewport
Example
/* clearviewport example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
setcolor(getmaxcolor());
ht = textheight("W");
/* clean up */
getch();
closegraph();
return 0;
}
closegraph
Syntax
#include <graphics.h>
Description
closegraph deallocates all memory allocated by the graphics system, then restores
the screen to the mode it was in before you called initgraph. (The graphics system
deallocates memory, such as the drivers, fonts, and an internal buffer, through a call
to _graphfreemem.)
Return Value
None.
Windows Notes
The windows version of closegraph has an optional parameter called wid which is
the window id (returned by initwindow) of the window that is to be closed. This
parameter may also be one of two special constant values: CURRENT_WINDOW
(causing closegraph to close only the current window), or ALL_WINDOWS (which
is the default, causing closegraph to close all open graphics windows). If the current
window is closed, then there is no longer a current window and no further drawing
operations may be done until a new window is created or a current window is set by
calling setcurrentwindow.
See also
initgraph
initwindow
setgraphbufsize
setcurrentwindow
Example
/* closegraph example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
int gdriver = DETECT, gmode, errorcode, x, y;
errorcode = graphresult();
getch();
x = getmaxx() / 2;
y = getmaxy() / 2;
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
closegraph();
return 0;
1. The sixteen ordinary BGI colors. These are the integers 0 through 15 or you may
use the symbolic names:
2.
3. BLACK BLUE GREEN CYAN
4.
5. RED MAGENTA BROWN LIGHTGRAY
6.
7. DARKGRAY LIGHTBLUE LIGHTGREEN LIGHTCYAN
8.
9. LIGHTRED LIGHTMAGENTA YELLOW WHITE
10.
11. A color may be specified from red, green and blue components using a new
function called COLOR(r,g,b). Each of the r,g,b arguments must be a number in the
range 0 to 255. For example, COLOR(255,100,0) is a mostly red color with some
green and no blue. If you create one of these colors, it may be used as an argument
to any of the BGI functions that expect a color. These colors may also be returned
from BGI functions such as getbkcolor and the new function getdisplaycolor (which
tells you what actual color will be displayed on the current monitor).
RGB Examples:
cout << "Current BGI drawing color is: " << current << endl;
cout << "The usual Windows RGB color int value is:\n"
delay
Syntax
#include "graphics.h"
Description
The delay function is available in the winbgim implementation of BGI graphics.
You do not need to include conio.h; just include graphics.h. The function pauses the
computation for the the specified number of milliseconds.
Return Value
None.
See also None.
Example
/* delay example */
#include "graphics.h"
int main(void)
initwindow(100, 100);
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setfillstyle(i, getmaxcolor());
delay(4000);
/* clean up */
closegraph();
return 0;
detectgraph
Syntax
#include <graphics.h>
Description
detectgraph detects your system's graphics adapter and chooses the mode that
provides the highest resolution for that adapter. If no graphics hardware is detected,
*graphdriver is set to grNotDetected (-2), and graphresult returns grNotDetected (-
2).
*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 as follows:
Columns
Graphics
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
Return Value
None.
Windows Notes
The winbgim version of detectgraph returns VGA for the graphdriver and VGAHI
for the graphmode, regardless of the machine's hardware. However, the screen is not
necessarily 640 x 480.
See also
graphresult
initgraph
Example
/* detectgraph example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
"a CGA",
"an MCGA",
"an EGA",
"a VGA",
int main(void)
detectgraph(&gdriver, &gmode);
errorcode = graphresult();
getch();
clrscr();
getch();
return 0;
}
drawpoly
Syntax
#include <graphics.h>
Description
drawpoly draws a polygon with numpoints points, using the current line style and
color.
In order to draw a closed figure with n vertices, you must pass n + 1 coordinates to
drawpoly where the nth coordinate is equal to the 0th.
Return Value
None.
See also
fillpoly
floodfill
graphresult
setwritemode
Example
/* drawpoly example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
maxx = getmaxx();
maxy = getmaxy();
poly[1] = maxy / 2;
poly[3] = 20;
poly[7] = maxy / 2;
/* clean up */
getch();
closegraph();
return 0;
ellipse
Syntax
#include <graphics.h>
Description
ellipse draws an elliptical arc in the current drawing color with its center at (x,y) and
the horizontal and vertical axes given by xradius and yradius, respectively. The
ellipse travels from stangle to endangle. If stangle equals 0 and endangle equals
360, the call to ellipse draws a complete ellipse.
The linestyle parameter does not affect arcs, circles, ellipses, or pie slices. Only the
thickness parameter is used.
Return Value
None.
See also
arc
circle
fillellipse
sector
Example
/* ellipse example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* draw ellipse */
/* clean up */
getch();
closegraph();
return 0;
fillellipse
Syntax
#include <graphics.h>
Description
Draws an ellipse using (x,y) as a center point and xradius and yradius as the
horizontal and vertical axes, and fills it with the current fill color and fill pattern.
Return Value
None.
See also
arc
circle
ellipse
pieslice
Example
/* fillellipse example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setfillstyle(i, getmaxcolor());
getch();
/* clean up */
closegraph();
return 0;
fillpoly
Syntax
#include <graphics.h>
Description
fillpoly draws the outline of a polygon with numpoints points in the current line
style and color (just as drawpoly does), then fills the polygon using the current fill
pattern and fill color.
Return Value
None.
See also
drawpoly
floodfill
graphresult
setfillstyle
Example
/* fillpoly example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
int poly[8];
errorcode = graphresult();
getch();
exit(1); /* terminate with an error code */
maxx = getmaxx();
maxy = getmaxy();
poly[1] = maxy / 2;
poly[3] = 20;
setfillstyle(i, getmaxcolor());
fillpoly(4, poly);
getch();
/* clean up */
closegraph();
return 0;
floodfill
Syntax
#include <graphics.h>
Description
floodfill fills an enclosed area on bitmap devices. (x,y) is a "seed point" within the
enclosed area to be filled. The area bounded by the color border is flooded with the
current fill pattern and fill color. If the seed point is within an enclosed area, the
inside will be filled. If the seed is outside the enclosed area, the exterior will be
filled.
Use fillpoly instead of floodfill whenever possible so that you can maintain code
compatibility with future versions.
Return Value
If an error occurs while flooding a region, graphresult returns a value of -7.
Windows Notes
The winbgim version allows the border argument to be an ordinary BGI color (from
0 to 15) or an RGB color.
See also
drawpoly
fillpoly
graphresult
setcolor
setfillstyle
Example
/* floodfill example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
maxx = getmaxx();
maxy = getmaxy();
setcolor(getmaxcolor());
/* select fill color */
setfillstyle(SOLID_FILL, getmaxcolor());
getch();
floodfill(2, 2, getmaxcolor());
/* clean up */
getch();
closegraph();
return 0;
getactivepage
Syntax
#include "graphics.h"
int getactivepage(void);
Description
The getactivepage function is available in the winbgim implementation of BGI
graphics. getactivepage gets the page number of the currently active page (where
drawing takes place).
The active graphics page might not be the one you see onscreen, depending on how
many graphics pages are available on your system.
The original winbgi was designed to support up to 16 pages, but I have only used
pages 1 and 2 myself. NOTE: Using page number 0 might mess up the colors. I use
pages 1-2 for double buffering.
Return Value
The page number of the currently active page.
See also
getvisualpage
setactivepage
swapbuffers
getarccoords
Syntax
#include <graphics.h>
Description
getarccoords fills in the arccoordstype structure pointed to by arccoords with
information about the last call to arc. The arccoordstype structure is defined in
graphics.h as follows:
struct arccoordstype {
int x, y;
};
The members of this structure are used to specify the center point (x,y), the starting
position (xstart, ystart), and the ending position (xend, yend) of the arc. These
values are useful if you need to make a line meet at the end of an arc.
Return Value
None.
See also
arc
fillellipse
sector
Example
/* getarccoords example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
getarccoords(&arcinfo);
/* clean up */
getch();
closegraph();
return 0;
}
getaspectration
Syntax
#include <graphics.h>
Description
The y aspect factor, *yasp, is normalized to 10,000. On all graphics adapters except
the VGA, *xasp (the x aspect factor) is less than *yasp because the pixels are taller
than they are wide. On the VGA, which has "square" pixels, *xasp equals *yasp. In
general, the relationship between *yasp and *xasp can be stated as
*yasp = 10,000
Return Value
None.
See also
None.
Example
/* getaspectratio example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
main()
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
getaspectratio(&xasp, &yasp);
getch();
setaspectratio(xasp/2, yasp);
getch();
cleardevice();
setaspectratio(xasp, yasp/2);
/* clean up */
getch();
closegraph();
return 0;
getbkcolor
Syntax
#include <graphics.h>
int getbkcolor(void);
Description
getbkcolor returns the current background color. (See the table in setbkcolor for
details.)
Return Value
getbkcolor returns the current background color.
Windows Notes
In the winbgim version, the user might set the background color to an RGB color.
Therefore, the return value from getbkcolor might be an ordinary BGI color (integer
from 0 to 15) or an RGB color.
See also
getcolor
getmaxcolor
getpalette
setbkcolor
Example
/* getbkcolor example */
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
char bkname[35];
errorcode = graphresult();
getch();
exit(1); /* terminate with an error code */
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
settextjustify(CENTER_TEXT, CENTER_TEXT);
bkcolor = getbkcolor();
/* display a message */
/* clean up */
getch();
closegraph();
return 0;
}
getch
Syntax
#include "graphics.h"
int getch(void);
Description
The getch function is available in the winbgim implementation of BGI graphics.
You do not need to include conio.h; just include graphics.h. The function reads one
character from the keyboard and returns its ASCII value (without waiting for a
return key). In order to work, the user must click in the graphics window (i.e., the
Windows focus must be in the graphics window). For special keys, the getch
function first returns ASCII 0. The next call will then return one of these special
keys:
#define KEY_HOME 71
#define KEY_UP 72
#define KEY_PGUP 73
#define KEY_LEFT 75
#define KEY_CENTER 76
#define KEY_RIGHT 77
#define KEY_END 79
#define KEY_DOWN 80
#define KEY_PGDN 81
#define KEY_INSERT 82
#define KEY_DELETE 83
#define KEY_F1 59
#define KEY_F2 60
#define KEY_F3 61
#define KEY_F4 62
#define KEY_F5 63
#define KEY_F6 64
#define KEY_F7 65
#define KEY_F8 66
#define KEY_F9 67
Return Value
The ASCII value of a key that has been pressed.
See also
kbhit
Example
#include "graphics.h"
int main( )
int i;
char c;
initwindow(400, 300);
do
c = (char) getch( );
if (c != 0)
cout << "That is ASCII value: " << (int) c << endl;
else
c = (char) getch( );
switch (c)
closegraph( );
char digit_string[20];
outtextxy(x, y, digit_string);
}
getcolor
Syntax
#include <graphics.h>
int getcolor(void);
Description
getcolor returns the current drawing color. The drawing color is the value to which
pixels are set when lines and so on are drawn. For example, in CGAC0 mode, the
palette contains four colors: the background color, light green, light red, and yellow.
In this mode, if getcolor returns 1, the current drawing color is light green.
Return Value
getcolor returns the current drawing color.
Windows Notes
In the winbgim version, the user might set the drawing color to an RGB color.
Therefore, the return value from getcolor might be an ordinary BGI color (integer
from 0 to 15) or an RGB color.
See also
getbkcolor
getmaxcolor
getpalette
setcolor
Example
/* getcolor example */
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
settextjustify(CENTER_TEXT, CENTER_TEXT);
color = getcolor();
/* clean up */
getch();
closegraph();
return 0;
getcurrentwindow
Syntax
#include "graphics.h"
int getcurrentwindow( );
Description
The getcurrentwindow function is available in the winbgim implementation of BGI
graphics. You do not need to include conio.h; just include graphics.h.
The function gets the current window number, which is the window where all
graphics operations occur.
See also
setcurrentwindow
initgraph
initwindow
Example
/* setcurrentwindow example */
#include
setcurrentwindow(window);
// window...
setcurrentwindow(oldwindow);
getdefaultpalette
Syntax
#include <graphics.h>
Description
getdefaultpalette finds the palettetype structure that contains the palette initialized
by the driver during initgraph.
Return Value
getdefaultpalette returns a pointer to the default palette set up by the current driver
when that driver was initialized.
See also
getpalette
initgraph
Example
/* getdefaultpalette example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
int i;
errorcode = graphresult();
getch();
pal = getdefaultpalette();
getch();
/* clean up */
getch();
closegraph();
return 0;
getdisplaycolor
Syntax
#include <graphics.h>
Description
The getdisplaycolor function is available in the winbgim implementation of BGI
graphics. getdisplaycolor is used to get an color that the current actual display
device can display.
Return Value
getdisplaycolor(color) returns the color number that will actually be drawn when a
program calls putpixel(0, 0, color). This is not always identical to color because
some display devices cannot display all possible colors. However, the display color
will always be as close as possible to the requested color.
Windows Notes
In the winbgim version, the user might set colors to an RGB color. Therefore, the
return value from getdisplaycolor might be an ordinary BGI color (integer from 0 to
15) or an RGB color.
See also
getpixel
putpixel
wincolor
Example
/* getdisplaycolor example */
#include <graphics.h>
#include <iostream>
int main(void)
initwindow(300, 300);
cout << "Please enter amounts of red, green and blue: ";
color_actual = getdisplaycolor(color_request);
if (IS_BGI_COLOR(color_actual))
cout << "That will display as BGI color number " <<
color_actual << endl;
else
getdrivername
Syntax
#include <graphics.h>
char *getdrivername(void);
Description
After a call to initgraph, getdrivername returns the name of the driver that is
currently loaded.
Return Value
getdrivername returns a pointer to a string with the name of the currently loaded
graphics driver.
Windows Notes
The winbgim version of getdrivername returns "EGAVGA" for the driver name,
regardless of how initgraph was called.
See also
initgraph
Example
/* getdrivername example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
char *drivername;
errorcode = graphresult();
getch();
setcolor(getmaxcolor());
drivername = getdrivername();
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* clean up */
getch();
closegraph();
return 0;
getfillpattern
Syntax
#include <graphics.h>
Description
getfillpattern copies the user-defined fill pattern, as set by setfillpattern, into the 8-
byte area pointed to by pattern.
char checkboard[8] = {
};
Return Value
None.
See also
getfillsettings
setfillpattern
Example
/* getfillpattern example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void) {
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
int maxx, maxy;
char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x25, 0x27, 0x04, 0x04};
/* initialize graphics and local variables */
Syntax
#include <graphics.h>
Description
getfillsettings fills in the fillsettingstype structure pointed to by fillinfo with
information about the current fill pattern and fill color. The fillsettingstype structure
is defined in graphics.h as follows:
struct fillsettingstype {
};
The functions bar, bar3d, fillpoly, floodfill, and pieslice all fill an area with the
current fill pattern in the current fill color. There are 11 predefined fill pattern styles
(such as solid, crosshatch, dotted, and so on). Symbolic names for the predefined
patterns are provided by the enumerated type fill_patterns in graphics.h, as shown
here:
Name Value Description
EMPTY_FILL 0 Fill with background color
SOLID_FILL 1 Solid fill
LINE_FILL 2 Fill with ---
LTSLASH_FILL 3 Fill with ///
SLASH_FILL 4 Fill with ///, thick lines
BKSLASH_FILL 5 Fill with \\\, thick lines
LTBKSLASH_FILL 6 Fill with \\\
HATCH_FILL 7 Light hatch fill
XHATCH_FILL 8 Heavy crosshatch fill
INTERLEAVE_FILL 9 Interleaving line fill
WIDE_DOT_FILL 10 Widely spaced dot fill
CLOSE_DOT_FILL 11 Closely spaced dot fill
USER_FILL 12 User-defined fill pattern
Note: All but EMPTY_FILL fill with the current fill color; EMPTY_FILL uses the
current background color. In addition, you can define your own fill pattern. If
pattern equals 12 (USER_FILL), then a user-defined fill pattern is being used;
otherwise, pattern gives the number of a predefined pattern.
Return Value
None.
Windows Notes
In the winbgim version, the user might set the fill color to an RGB color. Therefore,
the color in the fillsettingstype struct might be an ordinary BGI color (integer from
0 to 15) or an RGB color.
See also
getfillpattern
setfillpattern
setfillstyle
Example
/* getfillsettings example */
#include
#include
#include
#include
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
getfillsettings(&fillinfo);
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* clean up */
getch();
closegraph();
return 0;
getgraphmode
Syntax
#include <graphics.h>
int getgraphmode(void);
Description
Your program must make a successful call to initgraph before calling
getgraphmode.
Return Value
getgraphmode returns the graphics mode set by initgraph or setgraphmode.
Windows Notes
The winbgim version of getgraphmode returns VGAHI for the graphmode,
regardless of how initgraph was called. However, the screen is not necessarily
640 x 480.
See also
getmoderange
initgraph
restorecrtmode
setgraphmode
Example
/* getgraphmode example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
mode = getgraphmode();
sprintf(numname, "%d is the current mode number.", mode);
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* clean up */
getch();
closegraph();
return 0;
getimage
Syntax
#include <graphics.h>
void getimage(int left, int top, int right, int bottom, void
*bitmap);
Description
getimage copies an image from the screen to memory.
left, top, right, and bottom define the screen area to which the rectangle is copied.
bitmap points to the area in memory where the bit image is stored. The first two
words of this area are used for the width and height of the rectangle; the remainder
holds the image itself.
Return Value
None.
See also
getpixel
imagesize
putimage
Example
/* getimage example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
int main(void)
void *ptr[4];
if (errorcode != grOk) {
getch();
exit(1);
}
maxx = getmaxx();
maxy = getmaxy();
closegraph();
return 0;
unsigned size;
yincr = (maxy+1) / 4;
yend = yincr;
closegraph();
exit(1);
ystart = yend + 1;
yend += yincr + 1;
yincr = (maxy+1) / 4;
yend = yincr;
farfree(buf[block]);
ystart = yend + 1;
yend += yincr + 1;
}
getlinesettings
Syntax
#include <graphics.h>
Description
getlinesettings fills a linesettingstype structure pointed to by lineinfo with
information about the current line style, pattern, and thickness.
struct linesettingstype {
int linestyle;
unsigned upattern;
int thickness;
};
linestyle specifies in which style subsequent lines will be drawn (such as solid,
dotted, centered, dashed). The enumeration line_styles, defined in graphics.h, gives
names to these operators:
Name Value Description
SOLID_LINE 0 Solid line
DOTTED_LINE 1 Dotted line
CENTER_LINE 2 Centered line
DASHED_LINE 3 Dashed line
USERBIT_LINE 4 User-defined line style
thickness specifies whether the width of subsequent lines drawn will be normal or
thick.
Name Value Description
NORM_WIDTH 1 1 pixel wide
THICK_WIDTH 3 3 pixels wide
Return Value
None.
See also
setlinestyle
Example
/* getlinesettings example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
getlinesettings(&lineinfo);
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* clean up */
getch();
closegraph();
return 0;
}
getmaxcolor
Syntax
#include <graphics.h>
int getmaxcolor(void);
Description
getmaxcolor returns the highest valid color value for the current graphics driver and
mode that can be passed to setcolor.
For example, on a 256K EGA, getmaxcolor always returns 15, which means that
any call to setcolor with a value from 0 to 15 is valid. On a CGA in high-resolution
mode or on a Hercules monochrome adapter, getmaxcolor returns a value of 1.
Windows Notes
The winbgim version of getmaxcolor returns 15 for the maximum color. However,
in addition to the usual BGI colors (0 through 15), the programmer may also use
RGB colors.
Return Value
getmaxcolor returns the highest available color value.
See also
getbkcolor
getcolor
getpalette
getpalettesize
setcolor
Example
/* getmaxcolor example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request autodetection */
char colstr[80];
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
getch();
closegraph();
return 0;
getmaxmode
Syntax
#include <graphics.h>
int getmaxmode(void);
Description
getmaxmode lets you find out the maximum mode number for the currently loaded
driver, directly from the driver. This gives it an advantage over getmoderange,
which works for Borland drivers only. The minimum mode is 0.
Return Value
getmaxmode returns the maximum mode number for the current driver.
See also
getmodename
getmiderange
Example
/* getmaxmode example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request autodetection */
char modestr[80];
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, modestr);
/* clean up */
getch();
closegraph();
return 0;
getmaxheight
Syntax
#include <graphics.h>
int getmaxheight(void);
Description
The getmaxheight function is available in the winbgim implementation of BGI
graphics. The function returns the maximum height that will fit on the screen when
creating a window with initwindow. This is one of the few BGI functions that may
be called before calling initwindow. Note: The value returned is correct for a
window with a non-empty title. If you create a window with the empty string
(no characters) for a title, then the height can be increased by
GetSystemMetrics(SM_CYCAPTION).
Return Value
getmaxheight returns the maximum window height
See also
getmaxy
getwindowheight
getmaxwidth
initwindow
Example
/* getmaxwidth and getmaxheight example */
#include <graphics.h>
int main(void)
{
/* clean up */
getch();
closegraph();
return 0;
getmaxwidth
Syntax
#include <graphics.h>
int getmaxwidth(void);
Description
The getmaxwidth function is available in the winbgim implementation of BGI
graphics. The function returns the maximum width that will fit on the screen when
creating a window with initwindow. This is one of the few BGI functions that may
be called before calling initwindow.
Return Value
getmaxwidth returns the maximum window width
See also
getmaxx
getwindowwidth
getmaxheight
initwindow
Example
/* getmaxwidth and getmaxheight example */
#include <graphics.h>
int main(void)
{
/* clean up */
getch();
closegraph();
return 0;
getmaxx
Syntax
#include <graphics.h>
int getmaxx(void);
Description
getmaxx returns the maximum (screen-relative) x value for the current graphics
driver and mode.
Return Value
getmaxx returns the maximum x screen coordinate.
See also
getmaxy
getwindowwidth
getx
Example
/* getmaxx example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* clean up */
getch();
closegraph();
return 0;
getmaxy
Syntax
#include <graphics.h>
int getmaxy(void);
Description
getmaxy returns the maximum (screen-relative) y value for the current graphics
driver and mode.
Return Value
getmaxy returns the maximum y screen coordinate.
See also
getmaxx
getwindowheight
gety
Example
/* getmaxy example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* clean up */
getch();
closegraph();
return 0;
getmodename
Syntax
#include <graphics.h>
Description
getmodename accepts a graphics mode number as input and returns a string
containing the name of the corresponding graphics mode. The mode names are
embedded in each driver. The return values ("320*200 CGA P1," "640*200 CGA",
and so on) are useful for building menus or displaying status.
Return Value
getmodename returns a pointer to a string with the name of the graphics mode.
See also
getmaxmode
getmoderange
Example
/* getmodename example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
mode = getgraphmode();
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* clean up */
getch();
closegraph();
return 0;
getmoderange
Syntax
#include <graphics.h>
Description
getmoderange gets the range of valid graphics modes for the given graphics driver,
graphdriver. The lowest permissible mode value is returned in *lomode, and the
highest permissible value is *himode. If graphdriver specifies an invalid graphics
driver, both *lomode and *himode are set to -1. If the value of graphdriver is -1, the
currently loaded driver modes are given.
Return Value
None.
See also
getgraphmode
getmaxmode
getmodename
initgraph
setgraphmode
Example
/* getmoderange example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
char mrange[80];
errorcode = graphresult();
if (errorcode != grOk) { /* an error occurred */
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* clean up */
getch();
closegraph();
return 0;
}
getmouseclick
Syntax
#include "graphics.h"
Description
The getmouseclick function is available in the winbgim implementation of BGI
graphics. This function sets x and y to the pixel coordinates of an unprocessed event
of the specified kind. If there is no such event, then the function sets both x and y to
-1. The value of the argument kind may be any of the constants listed above. After
calling getmouseclick, for a particular kind of event, the ismouseclick will
return false for that kind of event until another such event occurs.
WM_MOUSEMOVE
if you want to detect a mouse movement
WM_LBUTTONDBLCLK
...detect when the left mouse button is double clicked
WM_LBUTTONDOWN
...detect when the left mouse button is clicked down
WM_LBUTTONUP
...detect when the left mouse button is released up
WM_MBUTTONDBLCLK
...detect when the middle mouse button is double clicked
WM_MBUTTONDOWN
...detect when the middle mouse button is clicked down
WM_MBUTTONUP
...detect when the middle mouse button is released up
WM_RBUTTONDBLCLK
...detect when the right mouse button is double clicked
WM_RBUTTONDOWN
...detect when the right mouse button is clicked down
WM_RBUTTONUP
...detect when the right mouse button is released up
The middle mouse button handlers aren't working on my machine. I haven't yet
tracked down the reason--it could be a broken mouse or it could be a bug in my
programming.
Note: Normally, getmouseclick returns the coordinates of the most recent event of
the requested kind. If you want mouse clicks of a particular kind to be queued for
processing, then call setmousequeuestatus.
See also
clearmouseclick
ismouseclick
setmousequeuestatus
Example
/* mouse example */
#include "graphics.h"
void main(void)
// Put the machine into graphics mode and get the maximum
coordinates:
initwindow(450, 300);
maxx = getmaxx( );
maxy = getmaxy( );
setfillstyle(SOLID_FILL, RED);
setcolor(WHITE);
setcolor(BLUE);
divisor = 2;
while (!ismouseclick(WM_LBUTTONDOWN))
triangle(maxx/divisor, maxy/divisor);
delay(500);
divisor++;
getmouseclick(WM_LBUTTONDOWN, x, y);
closegraph( );
getpalette
Syntax
#include <graphics.h>
Description
getpalette fills the palettetype structure pointed to by palette with information about
the current palette's size and colors.
The MAXCOLORS constant and the palettetype structure used by getpalette are
defined in graphics.h as follows:
#define MAXCOLORS 15
struct palettetype {
};
size gives the number of colors in the palette for the current graphics driver in the
current mode.
colors is an array of size bytes containing the actual raw color numbers for each
entry in the palette.
Return Value
None.
Windows Notes
The winbgim version of getpalette returns a palettetype object of 16 colors. Each
color is either one of the 16 BGI color numbers (0 through 15) or it is -1 to indicate
that the location of the palette has been set to an RGB color.
See also
getbkcolor
getcolor
getdefaultpalette
getmaxcolor
setallpalette
setpalette
Example
/* getpalette example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
/* request autodetection */
int i, ht;
int y = 10;
errorcode = graphresult();
getch();
getpalette(&pal);
outtextxy(0, y, psize);
if (pal.size != 0) {
ht = textheight("W");
y += 2*ht;
y += 2*ht;
outtextxy(0, y, pval);
/* clean up */
getch();
closegraph();
return 0;
getpalettesize
Syntax
#include <graphics.h>
int getpalettesize(void);
Description
getpalettesize is used to determine how many palette entries can be set for the
current graphics mode. For example, the EGA in color mode returns 16.
Return Value
getpalettesize returns the number of palette entries in the current palette.
Windows Notes
The winbgim version of getpalettesize returns 16 for the palette color. However, in
addition to the palette colors, the programmer may also use RGB colors.
See also
setallpalette
setpalette
Example
/* getpalettesize example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
/* request autodetection */
char psize[80];
errorcode = graphresult();
getch();
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* clean up */
getch();
closegraph();
return 0;
getpixel
Syntax
#include <graphics.h>
Description
getpixel gets the color of the pixel located at (x,y).
Return Value
getpixel returns the color of the given pixel.
Windows Notes
In the winbgim version, the user might set a pixel color to an RGB color. Therefore,
the return value from getpixel might be an ordinary BGI color (integer from 0 to 15)
or an RGB color.
See also
getimage
putpixel
Example
/* getpixel example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
maxx = getmaxx() + 1;
maxy = getmaxy() + 1;
maxcolor = getmaxcolor() + 1;
while (!kbhit()) {
srand(seed);
x = random(maxx);
y = random(maxy);
color = random(maxcolor);
putpixel(x, y, color);
delay(DELAY_TIME);
srand(seed);
x = random(maxx);
y = random(maxy);
color = random(maxcolor);
putpixel(x, y, 0);
}
}
/* clean up */
getch();
closegraph();
return 0;
gettextsettings
Syntax
#include <graphics.h>
Description
gettextsettings fills the textsettingstype structure pointed to by textinfo with
information about the current text font, direction, size, and justification. The
textsettingstype structure used by gettextsettings is defined in graphics.h as follows:
struct textsettingstype {
int font;
int direction;
int charsize;
int horiz;
int vert;
};
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
gettextsettings(&textinfo);
ht = textheight("W");
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* clean up */
getch();
closegraph();
return 0;
getviewsettings
Syntax
#include <graphics.h>
Description
getviewsettings fills the viewporttype structure pointed to by viewport with
information about the current viewport.
The viewporttype structure used by getviewport is defined in graphics.h as follows:
struct viewporttype {
int clip;
};
Return Value
None.
See also
clearviewport
getx
gety
setviewport
Example
/* getviewsettings example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
getviewsettings(&viewinfo);
settextjustify(CENTER_TEXT, CENTER_TEXT);
ht = textheight("W");
outtextxy(midx, midy, topstr);
/* clean up */
getch();
closegraph();
return 0;
getvisualpage
Syntax
#include "graphics.h"
int getvisualpage(void);
Description
The getvisualpage function is available in the winbgim implementation of BGI
graphics. getvisualpage gets the page number of the currently visable page (which is
visible on the screen).
The visual graphics page might not be the one where drawing currnetly takes place.
The original winbgi was designed to support up to 16 pages, but I have only used
pages 1 and 2 myself. NOTE: Using page number 0 might mess up the colors. I use
pages 1-2 for double buffering.
Return Value
The page number of the currently visible page.
See also
getactivepage
setvisualpage
swapbuffers
getwindowheight
Syntax
#include <graphics.h>
int getwindowheight(void);
Description
The getwindowheight function is available in the winbgim implementation of BGI
graphics. The function returns the total height of the window including nondrawable
border areas.
Return Value
getwindowheight returns the total height of the window
See also
getmaxy
getwindowwidth
gety
Example
/* getwindowheight example */
#include <graphics.h>
int main(void)
height = getwindowheight( );
/* clean up */
getch();
closegraph();
return 0;
getwindowwidth
Syntax
#include <graphics.h>
int getwindowwidth(void);
Description
The getwindowwidth function is available in the winbgim implementation of BGI
graphics. The function returns the total width of the window including nondrawable
border areas.
Return Value
getwindowwidth returns the total width of the window
See also
getmaxx
getwindowheight
getx
Example
/* getwindowwidth example */
#include <graphics.h>
int main(void)
width = getwindowwidth( );
getch();
closegraph();
return 0;
getx
Syntax
#include <graphics.h>
int getx(void);
Description
getx finds the current graphics position's x-coordinate. The value is viewport-
relative.
Return Value
getx returns the x-coordinate of the current position.
See also
getmaxx
getviewsettings
gety
moveto
Example
/* getx example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request autodetection */
char msg[80];
errorcode = graphresult();
getch();
outtext(msg);
/* clean up */
getch();
closegraph();
return 0;
gety
Syntax
#include <graphics.h>
int gety(void);
Description
gety returns the current graphics position's y-coordinate. The value is viewport-
relative.
Return Value
gety returns the y-coordinate of the current position.
See also
getmaxy
getviewsettings
getx
moveto
Example
/* gety example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
outtext(msg);
/* clean up */
getch();
closegraph();
return 0;
}
graphdefaults
Syntax
#include <graphics.h>
void graphdefaults(void);
Description
graphdefaults resets all graphics settings to their defaults:
Return Value
None.
See also
initgraph
setgraphmode
Example
/* graphdefaults example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
maxx = getmaxx();
maxy = getmaxy();
setlinestyle(DOTTED_LINE, 0, 3);
getch();
graphdefaults();
cleardevice();
/* output line with default settings */
/* clean up */
getch();
closegraph();
return 0;
grapherrormsg
Syntax
#include <graphics.h>
Description
grapherrormsg returns a pointer to the error message string associated with
errorcode, the value returned by graphresult.
Refer to the entry for errno in the Library Reference, Chapter 4, for a list of error
messages and mnemonics.
Return Value
grapherrormsg returns a pointer to an error message string.
See also
graphresult
Example
/* grapherrormsg example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
errorcode = graphresult();
if (errorcode != grOk) {
getch();
/* draw a line */
line(0, 0, getmaxx(), getmaxy());
/* clean up */
getch();
closegraph();
return 0;
graphresult
Syntax
#include <graphics.h>
int graphresult(void);
Description
graphresult returns the error code for the last graphics operation that reported an
error and resets the error level to grOk.
The following table lists the error codes returned by graphresult. The enumerated
type graph_errors defines the errors in this table. graph_errors is declared in
graphics.h.
Note: The variable maintained by graphresult is reset to 0 after graphresult has been
called. Therefore, you should store the value of graphresult into a temporary
variable and then test it.
Return Value
graphresult returns the current graphics error number, an integer in the range -15 to
0; grapherrormsg returns a pointer to a string associated with the value returned by
graphresult.
See also
detectgraph
drawpoly
fillpoly
floodfill
grapherrormsg
initgraph
pieslice
registerbgidriver
registerbgifont
setallpalette
setcolor
setfillstyle
setgraphmode
setlinestyle
setpalette
settextjustify
settextstyle
setusercharsize
setviewport
setvisualpage
Example
/* graphresult example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
/* draw a line */
/* clean up */
getch();
closegraph();
return 0;
}
imagesize
Syntax
#include <graphics.h>
Description
imagesize determines the size of the memory area required to store a bit image. If
the size required for the selected image is greater than or equal to 64K - 1 bytes,
imagesize returns 0xFFFF (-1).
Return Value
imagesize returns the size of the required memory area in bytes.
See also
getimage
putimage
Example
/* imagesize example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define ARROW_SIZE 10
int main(void)
/* request autodetection */
void *arrow;
int x, y, maxx;
unsigned int size;
errorcode = graphresult();
getch();
maxx = getmaxx();
x = 0;
y = getmaxy() / 2;
draw_arrow(x, y);
arrow = malloc(size);
/* grab the image */
while (!kbhit()) {
x += ARROW_SIZE;
if (x >= maxx)
x = 0;
/* clean up */
free(arrow);
closegraph();
return 0;
moveto(x, y);
linerel(4*ARROW_SIZE, 0);
linerel(-2*ARROW_SIZE, -1*ARROW_SIZE);
linerel(0, 2*ARROW_SIZE);
linerel(2*ARROW_SIZE, -1*ARROW_SIZE);
initgraph
Syntax
#include <graphics.h>
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
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.
After a call to initgraph, *graphdriver is set to the current graphics driver, and
*graphmode is set to the current graphics mode.
Columns
Graphics
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
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request autodetection */
errorcode = graphresult();
getch();
/* draw a line */
/* clean up */
getch();
closegraph();
return 0;
initwindow
Syntax
#include "graphics.h"
Description
The initwindow function is available in the winbgim implementation of BGI
graphics. You do not need to include conio.h; just include graphics.h. The function
initializes the graphics system by opening a graphics window of the specified size.
The first two parameters (width and height) are required, but all other parameters
have default values.
The title parameter is the title that will be printed at the top of the window (with a
default of "Windows BGI".) If this is set to the empty string (no characters), then
the window will be opened without a title bar or border (typically used for a popup
message that the user can then close by clicking), and the user will not be able to
move this window. If you want a title bar with no visible title, then set the title to a
string containing one space.
The left and top parameters determine the screen coordinates of the left and top
sides of the window.
If the closeflag parameter is true, then the user can click on the window's close
button to shut down the entire program.
Return Value
The original version of initgraph was a void function (with no flag argument), and
only one graphics window could be created in any program. The new version allows
multiple graphics windows to be created. The return value from the new initwindow
function is a unique int identifier that can be used as an argument to
setcurrentwindow in order to set which of several windows is currently being used.
Immediately after calling initwindow, the current window is always the window that
was just created.
See also
closegraph
getcurrentwindow
getmaxheight
getmaxwidth
initgraph
setcurrentwindow
swapbuffers
Example
/* initwindow example */
#include "graphics.h"
int main(void)
initwindow(400, 300);
/* draw a line */
/* clean up */
getch();
closegraph();
return 0;
#include "graphics.h"
int main(void)
setcurrentwindow(wid1);
setcurrentwindow(wid2);
/* clean up */
getch();
closegraph();
return 0;
installuserdriver
Syntax
#include <graphics.h>
Description
installuserdriver lets you add a vendor-added device driver to the BGI internal table.
The name parameter is the name of the new device-driver file (.BGI), and the detect
parameter is a pointer to an optional autodetect function that can accompany the
new driver. This autodetect function takes no parameters and returns an integer
value.
There are two ways to use this vendor-supplied driver. Suppose you have a new
video card called the Spiffy Graphics Array (SGA) and that the SGA manufacturer
provided you with a BGI device driver (SGA.BGI). The easiest way to use this
driver is to install it by calling installuserdriver and then passing the return value
(the assigned driver number) directly to initgraph.
The other, more general way to use this driver is to link in an autodetect function
that will be called by initgraph as part of its hardware-detection logic (presumably,
the manufacturer of the SGA gave you this autodetect function). When you install
the driver (by calling installuserdriver), you pass the address of this function, along
with the device driver's file name.
After you install the device-driver file name and the SGA autodetect function, call
initgraph and let it go through its normal autodetection process. Before initgraph
calls its built-in autodetection function (detectgraph), it first calls the SGA
autodetect function. If the SGA autodetect function doesn't find the SGA hardware,
it returns a value of -11 (grError), and initgraph proceeds with its normal hardware
detection logic (which can include calling any other vendor-supplied autodetection
functions in the order in which they were "installed"). If, however, the autodetect
function determines that an SGA is present, it returns a nonnegative mode number;
then initgraph locates and loads SGA.BGI, puts the hardware into the default
graphics mode recommended by the autodetect function, and finally returns control
to your program.
Return Value
The value returned by installuserdriver is the driver number parameter you would
pass to initgraph in order to select the newly installed driver manually.
Windows Notes
installuserdriver is not available in the winbgim implementation.
See also
initgraph
registerbgidriver
Example
/* installuserdriver example */
#include
#include
#include
#include
/* function prototypes */
void checkerrors(void);
int main(void)
gdriver = DETECT;
checkerrors();
checkerrors();
/* draw a line */
/* clean up */
getch();
closegraph();
return 0;
else
void checkerrors(void)
int errorcode;
errorcode = graphresult();
if (errorcode != grOk) {
getch();
exit(1);
installuserfont
Syntax
#include <graphics.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
/* function prototype */
void checkerrors(void);
int main(void)
/* request autodetection */
int userfont;
midx = getmaxx() / 2;
midy = getmaxy() / 2;
checkerrors();
userfont = installuserfont("USER.CHR");
checkerrors();
/* clean up */
getch();
closegraph();
return 0;
void checkerrors(void)
int errorcode;
/* read result of last graphics operation */
errorcode = graphresult();
if (errorcode != grOk) {
getch();
exit(1);
ismouseclick
Syntax
#include "graphics.h"
Description
The ismouseclick function is available in the winbgim implementation of BGI
graphics. This function returns true if there is an unprocessed mouse event of the
specified kind. The argument to ismouseclick is one of these constants from the
graphics.h file:
WM_MOUSEMOVE
if you want to detect a mouse movement
WM_LBUTTONDBLCLK
...detect when the left mouse button is double clicked
WM_LBUTTONDOWN
...detect when the left mouse button is clicked down
WM_LBUTTONUP
...detect when the left mouse button is released up
WM_MBUTTONDBLCLK
...detect when the middle mouse button is double clicked
WM_MBUTTONDOWN
...detect when the middle mouse button is clicked down
WM_MBUTTONUP
...detect when the middle mouse button is released up
WM_RBUTTONDBLCLK
...detect when the right mouse button is double clicked
WM_RBUTTONDOWN
...detect when the right mouse button is clicked down
WM_RBUTTONUP
...detect when the right mouse button is released up
The middle mouse button handlers aren't working on my machine. I haven't yet
tracked down the reason--it could be a broken mouse or it could be a bug in my
programming.
Return Value
True if there is an unprocessed mouse event of the specified kind; otherwise false.
See also
getmouseclick
clearmouseclick
Example
/* mouse example */
#include "graphics.h"
void main(void)
// Put the machine into graphics mode and get the maximum
coordinates:
initwindow(450, 300);
maxx = getmaxx( );
maxy = getmaxy( );
setcolor(WHITE);
setcolor(BLUE);
divisor = 2;
while (!ismouseclick(WM_LBUTTONDOWN))
triangle(maxx/divisor, maxy/divisor);
delay(500);
divisor++;
getmouseclick(WM_LBUTTONDOWN, x, y);
closegraph( );
kbhit
Syntax
#include "graphics.h"
int kbhit(void);
Description
The kbhit function is available in the winbgim implementation of BGI graphics.
You do not need to include conio.h; just include graphics.h. The function returns
true (non-zero) if there is a character in the input buffer ready to read. Otherwise it
returns false. In order to work, the user must click in the graphics window (i.e., the
Windows focus must be in the graphics window).
Return Value
True (non-zero) if there is a character in the input buffer, otherwise false.
See also
getch
Example
#include "graphics.h"
int main( )
int i;
initwindow(400, 300);
{
outintxy(20 + (i/10)*40 , (i % 10)*+10, i);
delay(4000);
closegraph( );
char digit_string[20];
outtextxy(x, y, digit_string);
line
Syntax
#include <graphics.h>
Description
line draws a line in the current color, using the current line style and thickness
between the two points specified, (x1,y1) and (x2,y2), without updating the current
position (CP).
Return Value
None.
See also
getlinesettings
linerel
lineto
setcolor
setlinestyle
setwritemode
Example
/* line example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
exit(1);
}
setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy();
/* clean up */
getch();
closegraph();
return 0;
linerel
Syntax
#include <graphics.h>
Description
linerel draws a line from the CP (current position) to a point that is a relative
distance (dx,dy) from the CP. The CP is advanced by (dx,dy).
Return Value
None.
See also
getlinesettings
line
lineto
setcolor
setlinestyle
setwritemode
Example
/* linerel example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
char msg[80];
errorcode = graphresult();
if (errorcode != grOk) {
getch();
exit(1);
moveto(20,30);
outtextxy(20,30, msg);
linerel(100, 100);
outtext(msg);
/* clean up */
getch();
closegraph();
return 0;
lineto
Syntax
#include <graphics.h>
Description
lineto draws a line from the CP (current position) to (x,y), then moves the CP to
(x,y).
Return Value
None.
See also
getlinesettings
line
linerel
setcolor
setlinestyle
setwritemode
Example
/* lineto example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
char msg[80];
errorcode = graphresult();
if (errorcode != grOk) {
getch();
exit(1);
}
/* move the CP to location (20,30) */
moveto(20, 30);
outtextxy(20,30, msg);
lineto(100, 100);
outtext(msg);
/* clean up */
getch();
closegraph();
return 0;
mousex
Syntax
#include "graphics.h"
int mousex(void);
Description
The mousex function is available in the winbgim implementation of BGI graphics.
It returns the most recent x coordinate of the mouse within the graphics window. X-
coordinates start with 0 at the left edge of the window and increase to getmaxx( ) at
the right edge of the window.
Return Value
Most recent x coordinate of the mouse within the graphics window
See also
getmaxx
mousey
registermousehandler
Example
/* mouse example */
#include "graphics.h"
// see if the click was on a red pixel. If so, then the boolean
// work, they should set a variable that will trigger the work
going,
if (getpixel(x,y) == RED)
red_clicked = true;
// height. The triangle will be drawn just above the botton of the
screen.
void main(void)
// Put the machine into graphics mode and get the maximum
coordinates:
initwindow(450, 300);
maxx = getmaxx( );
maxy = getmaxy( );
registermousehandler(WM_LBUTTONDOWN, click_handler);
setfillstyle(SOLID_FILL, RED);
setcolor(WHITE);
setcolor(BLUE);
red_clicked = false;
divisor = 2;
while (!red_clicked)
triangle(maxx/divisor, maxy/divisor);
delay(500);
divisor++;
closegraph( );
mousey
Syntax
#include "graphics.h"
int mousey(void);
Description
The mousey function is available in the winbgim implementation of BGI graphics.
It returns the most recent y coordinate of the mouse within the graphics window. Y-
coordinates start with 0 at the top edge of the window and increase to getmaxy( ) at
the bottom edge of the window.
Return Value
Most recent y coordinate of the mouse within the graphics window
See also
getmaxy
mousey
registermousehandler
Example
/* mouse example */
#include "graphics.h"
// see if the click was on a red pixel. If so, then the boolean
// work, they should set a variable that will trigger the work
going,
if (getpixel(x,y) == RED)
red_clicked = true;
// height. The triangle will be drawn just above the botton of the
screen.
void triangle(int base, int height)
void main(void)
// Put the machine into graphics mode and get the maximum
coordinates:
initwindow(450, 300);
maxx = getmaxx( );
maxy = getmaxy( );
registermousehandler(WM_LBUTTONDOWN, click_handler);
setfillstyle(SOLID_FILL, RED);
setcolor(WHITE);
setcolor(BLUE);
red_clicked = false;
divisor = 2;
while (!red_clicked)
triangle(maxx/divisor, maxy/divisor);
delay(500);
divisor++;
closegraph( );
moverel
Syntax
#include <graphics.h>
Description
moverel moves the current position (CP) dx pixels in the x direction and dy pixels
in the y direction.
Return Value
None.
See also
moveto
Example
/* moverel example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
char msg[80];
errorcode = graphresult();
getch();
moveto(20,30);
outtextxy(20,30, msg);
moverel(100, 100);
outtext(msg);
/* clean up */
getch();
closegraph();
return 0;
}
moveto
Syntax
#include <graphics.h>
Description
moveto moves the current position (CP) to viewport position (x,y).
Return Value
None.
See also
moverel
Example
/* moveto example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
char msg[80];
errorcode = graphresult();
if (errorcode != grOk) { /* an error occurred */
getch();
moveto(20,30);
outtextxy(20,30, msg);
/* move to (100,100) */
moveto(100,100);
outtext(msg);
/* clean up */
getch();
closegraph();
return 0;
outtext
Syntax
#include <graphics.h>
Description
outtext displays a text string in the viewport, using the current font, direction, and
size.
outtext outputs textstring at the current position (CP). If the horizontal text
justification is LEFT_TEXT and the text direction is HORIZ_DIR, the CP's x-
coordinate is advanced by textwidth(textstring). Otherwise, the CP remains
unchanged.
To maintain code compatibility when using several fonts, use textwidth and
textheight to determine the dimensions of the string.
If a string is printed with the default font using outtext, any part of the string that
extends outside the current viewport is truncated.
outtext is for use in graphics mode; it will not work in text mode.
Return Value
None.
See also
gettextsettings
outtextxy
settextjustify
textheight
textwidth
Example
/* outtext example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* move the CP to the center of the screen */
moveto(midx, midy);
outtext("This ");
outtext("is ");
outtext("a ");
outtext("test.");
/* clean up */
getch();
closegraph();
return 0;
outtextxy
Syntax
#include <graphics.h>
Description
outtextxy displays a text string in the viewport at the given position (x, y), using the
current justification settings and the current font, direction, and size.
To maintain code compatibility when using several fonts, use textwidth and
textheight to determine the dimensions of the string.
If a string is printed with the default font using outtext or outtextxy, any part of the
string that extends outside the current viewport is truncated.
outtextxy is for use in graphics mode; it will not work in text mode.
Return Value
None.
See also
gettextsettings
outtext
settextjustify
textheight
textwidth
Example
/* outtextxy example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
exit(1); /* terminate with an error code */
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* clean up */
getch();
closegraph();
return 0;
pieslice
Syntax
#include <graphics.h>
Description
pieslice draws and fills a pie slice centered at (x,y) with a radius given by radius.
The slice travels from stangle to endangle. The slice is outlined in the current
drawing color and then filled using the current fill pattern and fill color.
The angles for pieslice are given in degrees. They are measured counterclockwise,
with 0 degrees at 3 o'clock, 90 degrees at 12 o'clock, and so on.
If you're using a CGA or monochrome adapter, the examples in online Help that
show how to use graphics functions might not produce the expected results. If your
system runs on a CGA or monochrome adapter, use the value 1 (one) instead of the
symbolic color constant, and see the second example under arc whis shows how to
use the pieslice function.
Return Value
None.
See also
fillelipse
graphresult
sector
setfillstyle
Example
/* pieslice example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setfillstyle(EMPTY_FILL, getmaxcolor());
/* clean up */
getch();
closegraph();
return 0;
printimage
Syntax
#include "graphics.h"
void printimage(
Description
The printimage function is available in the winbgim implementation of BGI
graphics. You do not need to include conio.h; just include graphics.h. The function
opens a windows printer dialog to allow the user to print a portion of the active page
from the current window.
See also
getimage
writeimagefile
putimage
Syntax
#include <graphics.h>
Description
putimage puts the bit image previously saved with getimage back onto the screen,
with the upper left corner of the image placed at (left,top). bitmap points to the area
in memory where the source image is stored.
In other words, COPY_PUT copies the source bitmap image onto the screen,
XOR_PUT XORs the source image with the image already onscreen, OR_PUT ORs
the source image with that onscreen, and so on.
Return Value
None.
See also
getimage
imagesize
putpixel
setvisualpage
Example
/* putimage example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define ARROW_SIZE 10
int main()
/* request autodetection */
void *arrow;
int x, y, maxx;
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
getch();
maxx = getmaxx();
x = 0;
y = getmaxy() / 2;
draw_arrow(x, y);
arrow = malloc(size);
while (!kbhit()) {
x += ARROW_SIZE;
if (x >= maxx)
x = 0;
free(arrow);
closegraph();
return 0;
moveto(x, y);
linerel(4*ARROW_SIZE, 0);
linerel(-2*ARROW_SIZE, -1*ARROW_SIZE);
linerel(0, 2*ARROW_SIZE);
linerel(2*ARROW_SIZE, -1*ARROW_SIZE);
putpixel
Syntax
#include <graphics.h>
Description
putpixel plots a point in the color defined by color at (x,y).
Return Value
None.
Windows Notes
The winbgim version allows the color argument to be an ordinary BGI color (from 0
to 15) or an RGB color.
See also
getpixel
putimage
Example
/* putpixel example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
int main()
/* request autodetection */
errorcode = graphresult();
getch();
maxx = getmaxx() + 1;
maxy = getmaxy() + 1;
maxcolor = getmaxcolor() + 1;
while (!kbhit())
seed = random(32767);
srand(seed);
x = random(maxx);
y = random(maxy);
color = random(maxcolor);
putpixel(x, y, color);
delay(DELAY_TIME);
srand(seed);
x = random(maxx);
y = random(maxy);
color = random(maxcolor);
/* clean up */
getch();
closegraph();
return 0;
readimagefile
Syntax
#include "graphics.h"
void readimagefile(
);
Description
The readimagefile function is available in the winbgim implementation of BGI
graphics. You do not need to include conio.h; just include graphics.h. The function
reads a BMP, GIF, JPG, ICO, EMF or WMF image file and displays it in part of the
current active window. The filename may be NULL (in which case a windows file
save dialog box is opened to allow the user to select a file name).
See also
getimage
printimage
writeimagefile
rectangle
Syntax
#include <graphics.h>
Description
rectangle draws a rectangle in the current line style, thickness, and drawing color.
(left,top) is the upper left corner of the rectangle, and (right,bottom) is its lower
right corner.
Return Value
None.
See also
bar
bar3d
setcolor
setlinestyle
Example
/* rectangle example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
getch();
/* draw a rectangle */
rectangle(left,top,right,bottom);
/* clean up */
getch();
closegraph();
return 0;
registerbgidriver
Syntax
#include <graphics.h>
Calling registerbgidriver informs the graphics system that the driver pointed to by
driver was included at link time. This routine checks the linked-in code for the
specified driver; if the code is valid, it registers the code in internal tables.
By using the name of a linked-in driver in a call to registerbgidriver, you also tell
the compiler (and linker) to link in the object file with that public name.
Return Value
registerbgidriver returns a negative graphics error code if the specified driver or font
is invalid. Otherwise, registerbgidriver returns the driver number.
If you register a user-supplied driver, you must pass the result of registerbgidriver to
initgraph as the driver number to be used.
Windows Notes
registerbgidriver is not available in the winbgim implementation.
See also
graphresult
initgraph
installuserdriver
registerbgifont
Example
/* registerbgidriver example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = registerbgidriver(EGAVGA_driver);
if (errorcode < 0) {
getch();
errorcode = graphresult();
getch();
/* draw a line */
getch();
closegraph();
return 0;
registerbgifont
Syntax
#include <graphics.h>
Description
Calling registerbgifont informs the graphics system that the font pointed to by font
was included at link time. This routine checks the linked-in code for the specified
font; if the code is valid, it registers the code in internal tables.
By using the name of a linked-in font in a call to registerbgifont, you also tell the
compiler (and linker) to link in the object file with that public name.
If you register a user-supplied font, you must pass the result of registerbgifont to
settextstyle as the font number to be used.
Return Value
registerbgifont returns a negative graphics error code if the specified font is invalid.
Otherwise, registerbgifont returns the font number of the registered font.
Windows Notes
registerbgifont is not available in the winbgim implementation.
See also
graphresult
initgraph
installuserdriver
registerbgidriver
settextstyle
Example
/* registerbgifont example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = registerbgifont(triplex_font);
if (errorcode < 0) {
getch();
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* clean up */
getch();
closegraph();
return 0;
registermousehandler
Syntax
#include "graphics.h"
The middle mouse button handlers aren't working on my machine. I haven't yet
tracked down the reason--it could be a broken mouse or it could be a bug in my
programming.
Return Value
None.
See also
mousex
mousey
Example
/* mouse example */
#include "graphics.h"
// The click_handler will be called whenever the left mouse button
is
// see if the click was on a red pixel. If so, then the boolean
// work, they should set a variable that will trigger the work
going,
if (getpixel(x,y) == RED)
red_clicked = true;
// height. The triangle will be drawn just above the botton of the
screen.
}
void main(void)
// Put the machine into graphics mode and get the maximum
coordinates:
initwindow(450, 300);
maxx = getmaxx( );
maxy = getmaxy( );
registermousehandler(WM_LBUTTONDOWN, click_handler);
setfillstyle(SOLID_FILL, RED);
setcolor(WHITE);
setcolor(BLUE);
red_clicked = false;
divisor = 2;
while (!red_clicked)
triangle(maxx/divisor, maxy/divisor);
delay(500);
divisor++;
closegraph( );
restorecrtmode
Syntax
#include <graphics.h>
void restorecrtmode(void);
Description
restorecrtmode restores the original video mode detected by initgraph.
This function can be used in conjunction with setgraphmode to switch back and
forth between text and graphics modes. textmode should not be used for this
purpose; use it only when the screen is in text mode, to change to a different text
mode.
Return Value
None.
Windows Notes
restorecrtmode is implemented in winbgim, but it does not do any work. This is
because both the graphics window and the text window are always open during any
Windows program, so there is no need to switch back and forth between the two
modes.
See also
getgraphmode
initgraph
setgraphmode
Example
/* restorecrtmode example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
int x, y;
errorcode = graphresult();
getch();
}
x = getmaxx() / 2;
y = getmaxy() / 2;
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
getch();
restorecrtmode();
getch();
setgraphmode(getgraphmode());
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* clean up */
getch();
closegraph();
return 0;
}
Colors for Windows BGI
The winbgim package supports two types of colors that may be used with any of the
functions that expect colors as arguments:
1. The sixteen ordinary BGI colors. These are the integers 0 through 15 or you may
use the symbolic names:
2.
3. BLACK BLUE GREEN CYAN
4.
5. RED MAGENTA BROWN LIGHTGRAY
6.
7. DARKGRAY LIGHTBLUE LIGHTGREEN LIGHTCYAN
8.
9. LIGHTRED LIGHTMAGENTA YELLOW WHITE
10.
11. A color may be specified from red, green and blue components using a new
function called COLOR(r,g,b). Each of the r,g,b arguments must be a number in the
range 0 to 255. For example, COLOR(255,100,0) is a mostly red color with some
green and no blue. If you create one of these colors, it may be used as an argument
to any of the BGI functions that expect a color. These colors may also be returned
from BGI functions such as getbkcolor and the new function getdisplaycolor (which
tells you what actual color will be displayed on the current monitor).
RGB Examples:
cout << "Current BGI drawing color is: " << current << endl;
if (IS_RGB_COLOR(current)) // Check whether it is an RGB color.
cout << "The usual Windows RGB color int value is:\n"
sector
Syntax
#include <graphics.h>
Description
Draws and fills an elliptical pie slice using (x,y) as the center point, xradius and
yradius as the horizontal and vertical radii, respectively, and drawing from stangle
to endangle. The pie slice is outlined using the current color, and filled using the
pattern and color defined by setfillstyle or setfillpattern.
The angles for sector are given in degrees. They are measured counter-clockwise
with 0 degrees at 3 o'clock, 90 degrees at 12 o'clock, and so on.
If an error occurs while the pie slice is filling, graphresult returns a value of -6
(grNoScanMem).
Return Value
None.
See also
arc
circle
ellipse
getarccoords
getaspectratio
graphresult
pieslice
setfillpattern
setfillstyle
setgraphbufsize
Example
/* sector example */
#include <graphics.h<
#include <stdlib.h<
#include <stdio.h<
#include <conio.h<
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setfillstyle(i, getmaxcolor());
getch();
/* clean up */
closegraph();
return 0;
setactivepage
Syntax
#include <graphics.h>
Description
setactivepage makes page the active graphics page. All subsequent graphics output
will be directed to that graphics page.
The active graphics page might not be the one you see onscreen, depending on how
many graphics pages are available on your system. Only the EGA, VGA, and
Hercules graphics cards support multiple pages.
Return Value
None.
See also
setvisualpage
swapbuffers
Example
/* setactivepage example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
int x, y, ht;
errorcode = graphresult();
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
getch();
x = getmaxx() / 2;
y = getmaxy() / 2;
ht = textheight("W");
setactivepage(1);
settextjustify(CENTER_TEXT, CENTER_TEXT);
setactivepage(0);
setvisualpage(1);
/* clean up */
getch();
closegraph();
return 0;
setallpalette
Syntax
#include <graphics.h>
Description
setallpalette sets the current palette to the values given in the palettetype structure
pointed to by palette.
You can partially (or completely) change the colors in the EGA/VGA palette with
setallpalette.
The MAXCOLORS constant and the palettetype structure used by setallpalette are
defined in graphics.h as follows:
#define MAXCOLORS 15
struct palettetype {
size gives the number of colors in the palette for the current graphics driver in the
current mode.
colors is an array of size bytes containing the actual raw color numbers for each
entry in the palette. If an element of colors is -1, the palette color for that entry is
not changed.
Name Value
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15
EGA_BROWN 20
EGA_DARKGRAY 56
EGA_LIGHTBLUE 57
EGA_LIGHTGREEN 58
EGA_LIGHTCYAN 59
EGA_LIGHTRED 60
EGA_LIGHTMAGENTA 61
EGA_YELLOW 62
EGA_WHITE 63
Changes made to the palette are seen immediately onscreen. Each time a palette
color is changed, all occurrences of that color onscreen change to the new color
value.
Note: Valid colors depend on the current graphics driver and current graphics mode.
Return Value
If invalid input is passed to setallpalette, graphresult returns -11 (grError), and the
current palette remains unchanged.
Windows Notes
The winbgim version of setallpalette expects a palettetype object of up to 16 colors.
Each color is one of the 16 BGI color numbers (0 through 15). If you want to set a
palette color to an RGB color, then use setrgbpalette.
In the windows version, changing the palette effects only future drawing. Currently
drawn pixels do not change their color when the palette changes (no "palette
animation").
See also
getpalette
getpalettesize
graphresult
setbkcolor
setcolor
setpalette
Example
See also
getpalette
getpalettesize
graphresult
setbkcolor
setcolor
setpalette
Example
/* setallpalette example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
int y = 10;
char msg[80];
errorcode = graphresult();
getch();
maxcolor = getmaxcolor();
ht = 2 * textheight("W");
getpalette(&pal);
setcolor(color);
outtextxy(1, y, msg);
y += ht;
getch();
setpalette(color, BLACK);
getch();
setallpalette(&pal);
/* clean up */
getch();
closegraph();
return 0;
setaspectratio
Syntax
#include <graphics.h>
Description
setaspectratio changes the default aspect ratio of the graphics system. The graphics
system uses the aspect ratio to make sure that circles are round onscreen. If circles
appear elliptical, the monitor is not aligned properly. You could correct this in the
hardware by realigning the monitor, but it's easier to change in the software by
using setaspectratio to set the aspect ratio. To obtain the current aspect ratio from
the system, call getaspectratio.
Return Value
None.
See also
circle
getaspectratio
Example
/* setaspectratio example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request autodetection */
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
getaspectratio(&xasp, &yasp);
getch();
cleardevice();
setaspectratio(xasp/2, yasp);
getch();
cleardevice();
setaspectratio(xasp, yasp/2);
/* clean up */
getch();
closegraph();
return 0;
setbkcolor
Syntax
#include <graphics.h>
On CGA and EGA systems, setbkcolor changes the background color by changing
the first entry in the palette.
If you use an EGA or a VGA, and you change the palette colors with setpalette or
setallpalette, the defined symbolic constants might not give you the correct color.
This is because the parameter to setbkcolor indicates the entry number in the current
palette rather than a specific color (unless the parameter passed is 0, which always
sets the background color to black).
Return Value
None.
Windows Notes
The winbgim version allows the color argument to be an ordinary BGI color (from 0
to 15) or an RGB color. Also, only future drawing will use the new background
color (anything currently drawn in the old background color will stay in the old
color). Calling setbkcolor(0) will change the background color to the current color
at index [0] of the palette (rather than always changing the background to black).
See also
getbkcolor
setallpalette
setcolor
setpalette
Example
/* setbkcolor example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
char msg[80];
errorcode = graphresult();
getch();
maxcolor = getmaxcolor();
settextjustify(CENTER_TEXT, CENTER_TEXT);
x = getmaxx() / 2;
y = getmaxy() / 2;
cleardevice();
setbkcolor(bkcol);
/* output a messsage */
if (bkcol == WHITE)
setcolor(EGA_BLUE);
outtextxy(x, y, msg);
getch();
}
/* clean up */
closegraph();
return 0;
setcolor
Syntax
#include <graphics.h>
Description
setcolor sets the current drawing color to color, which can range from 0 to
getmaxcolor. The current drawing color is the value to which pixels are set when
lines, and so on are drawn. The drawing colors shown below are available for the
CGA and EGA, respectively.
Palette Number Three Colors
0 LIGHTGREEN LIGHTRED YELLOW
1 LIGHTCYAN LIGHTMAGENTA WHITE
2 GREEN RED BROWN
3 CYAN MAGENTA LIGHTGRAY
Name Value
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15
You select a drawing color by passing either the color number itself or the
equivalent symbolic name to setcolor. For example, in CGAC0 mode, the palette
contains four colors: the background color, light green, light red, and yellow. In this
mode, either setcolor(3) or setcolor(CGA_YELLOW) selects a drawing color of
yellow.
Return Value
None.
Windows Notes
The winbgim version allows the color argument to be an ordinary BGI color (from 0
to 15) or an RGB color.
See also
getcolor
getmaxcolor
graphresult
setallpalette
setbkcolor
setpalette
Example
/* setcolor example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
char msg[80];
/* initialize graphics and local variables */
errorcode = graphresult();
getch();
maxcolor = getmaxcolor();
settextjustify(CENTER_TEXT, CENTER_TEXT);
x = getmaxx() / 2;
y = getmaxy() / 2;
/* output a messsage */
sprintf(msg, "Color: %d", color);
outtextxy(x, y, msg);
getch();
/* clean up */
closegraph();
return 0;
setcurrentwindow
Syntax
#include "graphics.h"
Description
The setcurrentwindow function is available in the winbgim implementation of BGI
graphics. You do not need to include conio.h; just include graphics.h.
The function changes the current window for all graphics operations to the specified
window. This window number must be a number returned by the initiwindow
functiion. The current window is the window where all other graphics operations
will take place.
Note: Initwindow and initgraph both set the current window to the newly created
window, so there is no need to call setcurrentwindow immediately after opening a
new window. parameters have default values.
See also
getcurrentwindow
initgraph
initwindow
Example
/* setcurrentwindow example */
#include
int main(void)
int big;
width = getwindowwidth( );
height = getwindowheight( );
setcurrentwindow(big);
/* clean up */
getch();
closegraph();
return 0;
setmousequeuestatus
Syntax
#include "graphics.h"
When a window first opens, queuing is turned off for all events.
See also
clearmouseclick
ismouseclick
getmouseclick
Example
/* mouse example */
#include
#include "graphics.h"
int main(void)
// Put the machine into graphics mode and get the maximum
coordinates:
initwindow(450, 300);
setmousequeuestatus(WM_LBUTTONDOWN);
maxx = getmaxx( );
maxy = getmaxy( );
// Draw a white circle with red inside and a radius of 50
pixels:
setfillstyle(SOLID_FILL, RED);
setcolor(WHITE);
setcolor(BLUE);
divisor = 2;
while (!ismouseclick(WM_LBUTTONDOWN))
while (ismouseclick(WM_LBUTTONDOWN))
getmouseclick(WM_LBUTTONDOWN, x, y);
closegraph( );
}
setfillpattern
Syntax
#include <graphics.h>
Description
setfillpattern is like setfillstyle, except that you use it to set a user-defined 8x8
pattern rather than a predefined pattern.
Return Value
None.
Windows Notes
The winbgim version allows the color argument to be an ordinary BGI color (from 0
to 15) or an RGB color.
See also
getfillpattern
getfillsettings
graphresult
sector
setfillstyle
Example
/* setfillpattern example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
errorcode = graphresult();
getch();
maxx = getmaxx();
maxy = getmaxy();
setcolor(getmaxcolor());
setfillpattern(pattern, getmaxcolor());
getch();
closegraph();
return 0;
setfillstyle
Syntax
#include <graphics.h>
Description
setfillstyle sets the current fill pattern and fill color. To set a user-defined fill
pattern, do not give a pattern of 12 (USER_FILL) to setfillstyle; instead, call
setfillpattern.
Note: The EMPTY_FILL style is like a solid fill using the current background color
(which is set by setbkcolor).
Return Value
None.
Windows Notes
The winbgim version allows the color argument to be an ordinary BGI color (from 0
to 15) or an RGB color.
See also
bar
bar3d
fillpoly
floodfill
getfillsettings
graphresult
pieslice
sector
setfillpattern
Example
/* setfillstyle example */
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
char stylestr[40];
errorcode = graphresult();
if (errorcode != grOk) { /* an error occurred */
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setfillstyle(style, getmaxcolor());
strcpy(stylestr, fname[style]);
/* fill a bar */
/* output a message */
getch();
cleardevice();
}
/* clean up */
getch();
closegraph();
return 0;
setgraphbufsize
Syntax
#include <graphics.h>
Description
Some of the graphics routines (such as floodfill) use a memory buffer that is
allocated when initgraph is called and released when closegraph is called. The
default size of this buffer, allocated by _graphgetmem, is 4,096 bytes.
You might want to make this buffer smaller (to save memory space) or bigger (if,
for example, a call to floodfill produces error -7: Out of flood memory).
setgraphbufsize tells initgraph how much memory to allocate for this internal
graphics buffer when it calls _graphgetmem.
You must call setgraphbufsize before calling initgraph. Once initgraph has been
called, all calls to setgraphbufsize are ignored until after the next call to closegraph.
Return Value
setgraphbufsize returns the previous size of the internal buffer.
Windows Notes
setgraphbufsize is not available in the winbgim implementation.
See also
closegraph
initgraph
sector
Example
/* setgraphbufsize example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
int x, y, oldsize;
char msg[80];
oldsize = setgraphbufsize(BUFSIZE);
errorcode = graphresult();
getch();
exit(1); /* terminate with an error code */
x = getmaxx() / 2;
y = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, msg);
/* clean up */
getch();
closegraph();
return 0;
setgraphmode
Syntax
#include <graphics.h>
Description
setgraphmode selects a graphics mode different than the default one set by
initgraph. mode must be a valid mode for the current device driver. setgraphmode
clears the screen and resets all graphics settings to their defaults (current position,
palette, color, viewport, and so on).
You can use setgraphmode in conjunction with restorecrtmode to switch back and
forth between text and graphics modes.
Return Value
If you give setgraphmode an invalid mode for the current device driver, graphresult
returns a value of -10 (grInvalidMode).
Windows Notes
setgraphmode is implemented in winbgim, but it does not do any work.
Windows Notes
setgraphmode is implemented in winbgim, but it does not do any work. This is
because both the graphics window and the text window are always open during any
Windows program, so there is no need to switch back and forth between the two
modes.
See also
getgraphmode
getmoderange
graphresult
initgraph
restorecrtmode
Example
/* setgraphmode example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
int x, y;
errorcode = graphresult();
getch();
x = getmaxx() / 2;
y = getmaxy() / 2;
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
getch();
restorecrtmode();
getch();
setgraphmode(getgraphmode());
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* clean up */
getch();
closegraph();
return 0;
setlinestyle
Syntax
#include <graphics.h>
Description
setlinestyle sets the style for all lines drawn by line, lineto, rectangle, drawpoly, and
so on.
struct linesettingstype {
int linestyle;
unsigned upattern;
int thickness;
};
linestyle specifies in which of several styles subsequent lines will be drawn (such as
solid, dotted, centered, dashed). The enumeration line_styles, which is defined in
graphics.h, gives names to these operators:
Name Value Description
SOLID_LINE 0 Solid line
DOTTED_LINE 1 Dotted line
CENTER_LINE 2 Centered line
DASHED_LINE 3 Dashed line
USERBIT_LINE 4 User-defined line style
thickness specifies whether the width of subsequent lines drawn will be normal or
thick.
Name Value Description
NORM_WIDTH 1 1 pixel wide
THICK_WIDTH 3 3 pixels wide
upattern is a 16-bit pattern that applies only if linestyle is USERBIT_LINE (4). In
that case, whenever a bit in the pattern word is 1, the corresponding pixel in the line
is drawn in the current drawing color. For example, a solid line corresponds to a
upattern of 0xFFFF (all pixels drawn), and a dashed line can correspond to a
upattern of 0x3333 or 0x0F0F. If the linestyle parameter to setlinestyle is not
USERBIT_LINE (in other words, if it is not equal to 4), you must still provide the
upattern parameter, but it will be ignored.
Note: The linestyle parameter does not affect arcs, circles, ellipses, or pie slices.
Only the thickness parameter is used.
Return Value
If invalid input is passed to setlinestyle, graphresult returns -11, and the current line
style remains unchanged.
See also
arc
bar3d
bar
circle
drawpoly
ellipse
getlinesettings
graphresult
line
linerel
lineto
pieslice
rectangle
Example
/* setlinestyle example */
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
char stylestr[40];
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* binary: "0000000000000001" */
userpat = 1;
strcpy(stylestr, lname[style]);
/* draw a line */
/* draw a rectangle */
/* output a message */
cleardevice();
/* clean up */
closegraph();
return 0;
NOTE: Some machines at the University of Colorado have a D: drive that is too small to
compile a project. In this case, you'll get an out of memory error. Move your work to a
temporary directory on the C: Drive for these machines.
setpalette
Syntax
#include <graphics.h>
Description
setpalette changes the colornum entry in the palette to color. For example,
setpalette(0,5) changes the first color in the current palette (the background color) to
actual color number 5. If size is the number of entries in the current palette,
colornum can range between 0 and (size - 1).
You can partially (or completely) change the colors in the EGA/VGA palette with
setpalette. On a CGA, you can only change the first entry in the palette (colornum
equals 0, the background color) with a call to setpalette.
Name Value
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15
setpalette cannot be used with the IBM-8514 driver; use setrgbpalette instead.
Return Value
If invalid input is passed to setpalette, graphresult returns -11, and the current
palette remains unchanged.
Windows Notes
The winbgim version allows the color argument to be an ordinary BGI color (from 0
to 15) or an RGB color. The colornum should be a palette index from 0 to 15.
In the windows version, changing the palette effects only future drawing. Currently
drawn pixels do not change their color when the palette changes (no "palette
animation").
See also
setrgbpalette
Example
/* setpalette example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
int y = 10;
char msg[80];
errorcode = graphresult();
getch();
maxcolor = getmaxcolor();
ht = 2 * textheight("W");
setcolor(color);
outtextxy(1, y, msg);
y += ht;
getch();
setpalette(color, BLACK);
getch();
/* clean up */
closegraph();
return 0;
setrgbpalette
Syntax
#include <graphics.h>
Description
setrgbpalette can be used with the IBM 8514 and VGA drivers.
colornum defines the palette entry to be loaded, while red, green, and blue define
the component colors of the palette entry.
For the IBM 8514 display (and the VGA in 256K color mode), colornum is in the
range 0 to 255. For the remaining modes of the VGA, colornum is in the range 0 to
15. Only the lower byte of red, green, or blue is used, and out of each byte, only the
6 most significant bits are loaded in the palette.
For compatibility with other IBM graphics adapters, the BGI driver defines the first
16 palette entries of the IBM 8514 to the default colors of the EGA/VGA. These
values can be used as is, or they can be changed using setrgbpalette.
Return Value
None.
Windows Notes
The winbgim version allows the the colornum to be a palette index from 0 to 15.
The call
setrgbpalette(colornum, r, g, b);
is similar to
setpalette(colornum, COLOR(r,g,b) );
The difference is that setrgbpalette will use only the six most significant bits of the
least significant byte of r, g and b. However, COLOR(r,g,b) uses the entire least
significant byte of r, g and b. color argument to be an ordinary BGI color (from 0 to
15) or an RGB color.
In the windows version, changing the palette effects only future drawing. Currently
drawn pixels do not change their color when the palette changes (no "palette
animation").
See also
setpalette
Example
/* setrgbpalette example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* select driver and mode that supports use of setrgbpalette */
errorcode = graphresult();
getch();
getpalette(&pal);
ht = getmaxy() / 16;
xmax = getmaxx();
y = 0;
setfillstyle(SOLID_FILL, i);
y += ht;
/* clean up */
getch();
closegraph();
return 0;
settextjustify
Syntax
#include <graphics.h>
Description
Text output after a call to settextjustify is justified around the current position (CP)
horizontally and vertically, as specified. The default justification settings are
LEFT_TEXT (for horizontal) and TOP_TEXT (for vertical). The enumeration
text_just in graphics.h provides names for the horiz and vert settings passed to
settextjustify.
Description Name Value Action
horiz LEFT_TEXT 0 left-justify text
CENTER_TEXT 1 center text
RIGHT_TEXT 2 right-justify text
vertical BOTTOM_TEXT 0 bottom-justify text
CENTER_TEXT 1 center text
TOP_TEXT 2 top-justify text
If horiz is equal to LEFT_TEXT and direction equals HORIZ_DIR, the CP's x
component is advanced after a call to outtext(string) by textwidth(string).
settextjustify affects text written with outtext and cannot be used with text mode and
stream functions.
Return Value
If invalid input is passed to settextjustify, graphresult returns -11, and the current
text justification remains unchanged.
See also
gettextsettings
graphresults
outtext
settextstyle
Example
/* settextjustify example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
/* function prototype */
int main(void)
{
/* request autodetection */
char msg[80];
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
cleardevice();
settextjustify(hj, vj);
/* create a message string */
xat(midx, midy);
getch();
/* clean up */
closegraph();
return 0;
settextstyle
Syntax
#include <graphics.h>
void settextstyle(int font, int direction, int charsize);
Description
settextstyle sets the text font, the direction in which text is displayed, and the size of
the characters. A call to settextstyle affects all text output by outtext and outtextxy.
The parameters font, direction, and charsize passed to settextstyle are described in
the following:
font: One 8x8 bit-mapped font and several "stroked" fonts are available. The 8x8
bit-mapped font is the default. The enumeration font_names, which is defined in
graphics.h, provides names for these different font settings:
The default bit-mapped font is built into the graphics system. Stroked fonts are
stored in *.CHR disk files, and only one at a time is kept in memory. Therefore,
when you select a stroked font (different from the last selected stroked font), the
corresponding *.CHR file must be loaded from disk.
To avoid this loading when several stroked fonts are used, you can link font files
into your program. Do this by converting them into object files with the BGIOBJ
utility, then registering them through registerbgifont.
direction: Font directions supported are horizontal text (left to right) and vertical
text (rotated 90 degrees counterclockwise). The default direction is HORIZ_DIR.
The size of each character can be magnified using the charsize factor. If charsize is
nonzero, it can affect bit-mapped or stroked characters. A charsize value of 0 can be
used only with stroked fonts.
If charsize equals 1, outtext and outtextxy displays characters from the 8x8
bit-mapped font in an 8x8 pixel rectangle onscreen.
If charsize equals 2, these output functions display characters from the 8x8
bit-mapped font in a 16*16 pixel rectangle, and so on (up to a limit of ten
times the normal size).
When charsize equals 0, the output functions outtext and outtextxy magnify
the stroked font text using either the default character magnification factor
(4) or the user-defined character size given by setusercharsize.
Always use textheight and textwidth to determine the actual dimensions of the text.
Return Value
See also
gettextsettings
graphresults
installuserfont
settextjustify
setusercharsize
textheight
textwidth
Example
/* settextstyle example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
"BOLD font"};
int main(void)
{
/* request autodetection */
int size = 1;
errorcode = graphresult();
getch();
midx = getmaxx() / 2;
midy = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
cleardevice();
if (style == TRIPLEX_FONT)
size = 4;
/* output a message */
getch();
/* clean up */
closegraph();
return 0;
setusercharsize
Syntax
#include <graphics.h>
Description
setusercharsize gives you finer control over the size of text from stroked fonts used
with graphics functions. The values set by setusercharsize are active only if charsize
equals 0, as set by a previous call to settextstyle.
With setusercharsize, you specify factors by which the width and height are scaled.
The default width is scaled by multx : divx, and the default height is scaled by
multy : divy. For example, to make text twice as wide and 50% taller than the
default, set
multx = 2; divx = 1;
multy = 3; divy = 2;
Return Value
None.
See also
gettextsettings
graphresult
settextstyle
Example
/* setusercharsize example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void) {
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
Syntax
#include <graphics.h>
void setviewport(int left, int top, int right, int bottom, int
clip);
Description
setviewport establishes a new viewport for graphics output.
The viewport corners are given in absolute screen coordinates by (left,top) and
(right,bottom). The current position (CP) is moved to (0,0) in the new window.
The parameter clip determines whether drawings are clipped (truncated) at the
current viewport boundaries. If clip is nonzero, all drawings will be clipped to the
current viewport.
Return Value
If invalid input is passed to setviewport, graphresult returns -11, and the current
view settings remain unchanged.
See also
clearviewport
getviewsettings
graphresult
Example
/* setviewport example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request autodetection */
errorcode = graphresult();
getch();
setcolor(getmaxcolor());
getch();
closegraph();
return 0;
setvisualpage
Syntax
#include <graphics.h>
Description
setvisualpage makes page the visual graphics page.
Return Value
None.
See also
graphresult
setactivepage
swapbuffers
Example
/* setvisualpage example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* select driver and mode that supports multiple pages */
int x, y, ht;
errorcode = graphresult();
getch();
x = getmaxx() / 2;
y = getmaxy() / 2;
ht = textheight("W");
setactivepage(1);
setactivepage(0);
getch();
setvisualpage(1);
/* clean up */
getch();
closegraph();
return 0;
setwritemode
Syntax
#include <graphics.h>
Description
The following constants are defined:
COPY_PUT = 0 /* MOV */
XOR_PUT = 1 /* XOR */
Each constant corresponds to a binary operation between each byte in the line and
the corresponding bytes onscreen. COPY_PUT uses the assembly language MOV
instruction, overwriting with the line whatever is on the screen. XOR_PUT uses the
XOR command to combine the line with the screen. Two successive XOR
commands will erase the line and restore the screen to its original appearance.
setwritemode currently works only with line, linerel, lineto, rectangle, and
drawpoly.
Return Value
None.
See also
lineto
putimage
Example
/* setwritemode example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
/* request autodetection */
errorcode = graphresult();
getch();
xmax = getmaxx();
ymax = getmaxy();
setwritemode(XOR_PUT);
/* draw a line */
getch();
getch();
setwritemode(COPY_PUT);
/* draw a line */
/* clean up */
getch();
closegraph();
return 0;
showerrorbox
Syntax
#include "graphics.h"
Description
The showerrorbox function is available in the winbgim implementation of BGI
graphics. You do not need to include conio.h; just include graphics.h. The function
opens a windows error message box with the specified message (or a standard
message if the message parameter is NULL). The message box waits for the user to
click on OK button before showerrorbox returns.
See also
bgiout
outtext
outtextxy
Example
/* showerrorbox example */
#include "graphics.h"
int main(void)
/* draw a line */
closegraph();
return 0;
swapbuffers
Syntax
#include "graphics.h"
void swapbuffers( );
Description
The swapbuffers function is available in the winbgim implementation of BGI
graphics. You do not need to include conio.h; just include graphics.h. The function
swaps the roles of the current active and the current visual graphics buffers. It is
equivalent to these statements:
int oldv = getvisualpage( );
setvisualpage(olda);
setactivepage(oldv);
See also
getactivepage
getvisualpage
initwindow
setactivepage
setvisualpage
textheight
Syntax
#include <graphics.h>
Description
The graphics function textheight takes the current font size and multiplication
factor, and determines the height of textstring in pixels. This function is useful for
adjusting the spacing between lines, computing viewport heights, sizing a title to
make it fit on a graph or in a box, and so on.
For example, with the 8x8 bit-mapped font and a multiplication factor of 1 (set by
settextstyle), the string BorlandC++ is 8 pixels high.
Use textheight to compute the height of strings, instead of doing the computations
manually. By using this function, no source code modifications have to be made
when different fonts are selected.
Return Value
textheight returns the text height in pixels.
See also
gettextsettings
outtext
outtextxy
settextstyle
textwidth
Example
/* textheight example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void) {
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
int y = 0;
int i;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) {
/* an error occurred */
textwidth
Syntax
#include <graphics.h>
Description
The graphics function textwidth takes the string length, current font size, and
multiplication factor, and determines the width of textstring in pixels.
This function is useful for computing viewport widths, sizing a title to make it fit on
a graph or in a box, and so on.
Use textwidth to compute the width of strings, instead of doing the computations
manually. When you use this function, no source code modifications have to be
made when different fonts are selected.
Return Value
textwidth returns the text width in pixels.
See also
gettextsettings
outtext
outtextxy
settextstyle
textheight
Example
/* textwidth example */
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
/* request autodetection */
int x = 0, y = 0;
int i;
char msg[80];
errorcode = graphresult();
getch();
y = getmaxy() / 2;
settextjustify(LEFT_TEXT, CENTER_TEXT);
outtextxy(x, y, msg);
x += textwidth(msg);
/* clean up */
getch();
closegraph();
return 0;
}
writeimagefile
Syntax
#include "graphics.h"
void writeimagefile(
);
Description
The writeimagefile function is available in the winbgim implementation of BGI
graphics. You do not need to include conio.h; just include graphics.h. The function
saves a portion of the active page in a BMP file. The filename must end in "BMP"
or ".bmp", or it may be NULL (in which case a windows file save dialog box is
opened to allow the user to select a file name).
See also
getimage
printimage
readimagefile